diff --git a/administrator/components/com_categories/models/fields/categoryedit.php b/administrator/components/com_categories/models/fields/categoryedit.php index 36528f391895f..3864bc910e900 100644 --- a/administrator/components/com_categories/models/fields/categoryedit.php +++ b/administrator/components/com_categories/models/fields/categoryedit.php @@ -45,6 +45,7 @@ protected function getOptions() // Let's get the id for the current item, either category or content item. $jinput = JFactory::getApplication()->input; + // Load the category options for a given extension. // For categories the old category is the category id or 0 for new category. @@ -63,56 +64,57 @@ protected function getOptions() $db = JFactory::getDbo(); $query = $db->getQuery(true) - ->select('a.id AS value, a.title AS text, a.level, a.published') - ->from('#__categories AS a') - ->join('LEFT', $db->quoteName('#__categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt'); + ->select('DISTINCT a.id AS value, a.title AS text, a.level, a.published'); + $subQuery = $db->getQuery(true) + ->select('DISTINCT id,title,level,published,parent_id,extension,lft,rgt') + ->from('#__categories'); // Filter by the extension type if ($this->element['parent'] == true || $jinput->get('option') == 'com_categories') { - $query->where('(a.extension = ' . $db->quote($extension) . ' OR a.parent_id = 0)'); + $subQuery->where('(extension = ' . $db->quote($extension) . ' OR parent_id = 0)'); } else { - $query->where('(a.extension = ' . $db->quote($extension) . ')'); - } - // If parent isn't explicitly stated but we are in com_categories assume we want parents - if ($oldCat != 0 && ($this->element['parent'] == true || $jinput->get('option') == 'com_categories')) - { - // Prevent parenting to children of this item. - // To rearrange parents and children move the children up, not the parents down. - $query->join('LEFT', $db->quoteName('#__categories') . ' AS p ON p.id = ' . (int) $oldCat) - ->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)'); - - $rowQuery = $db->getQuery(true); - $rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id') - ->from('#__categories AS a') - ->where('a.id = ' . (int) $oldCat); - $db->setQuery($rowQuery); - $row = $db->loadObject(); + $subQuery->where('(extension = ' . $db->quote($extension) . ')'); } // Filter language if (!empty($this->element['language'])) { - - $query->where('a.language = ' . $db->quote($this->element['language'])); + $subQuery->where('language = ' . $db->quote($this->element['language'])); } // Filter on the published state - if (is_numeric($published)) { - $query->where('a.published = ' . (int) $published); + $subQuery->where('published = ' . (int) $published); } elseif (is_array($published)) { JArrayHelper::toInteger($published); - $query->where('a.published IN (' . implode(',', $published) . ')'); + $subQuery->where('published IN (' . implode(',', $published) . ')'); } - $query->group('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id, a.published') - ->order('a.lft ASC'); + $subQuery->order('lft ASC'); + $query->from('(' . $subQuery->__toString() . ') AS a') + ->join('LEFT', $db->quoteName('#__categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt'); + + // If parent isn't explicitly stated but we are in com_categories assume we want parents + if ($oldCat != 0 && ($this->element['parent'] == true || $jinput->get('option') == 'com_categories')) + { + // Prevent parenting to children of this item. + // To rearrange parents and children move the children up, not the parents down. + $query->join('LEFT', $db->quoteName('#__categories') . ' AS p ON p.id = ' . (int) $oldCat) + ->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)'); + + $rowQuery = $db->getQuery(true); + $rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id') + ->from('#__categories AS a') + ->where('a.id = ' . (int) $oldCat); + $db->setQuery($rowQuery); + $row = $db->loadObject(); + } // Get the options. $db->setQuery($query); @@ -137,6 +139,7 @@ protected function getOptions() $options[$i]->text = JText::_('JGLOBAL_ROOT_PARENT'); } } + if ($options[$i]->published == 1) { $options[$i]->text = str_repeat('- ', $options[$i]->level) . $options[$i]->text; @@ -179,10 +182,10 @@ protected function getOptions() unset($options[$i]); } } + if ($user->authorise('core.edit.state', $extension . '.category.' . $oldCat) != true && (isset($oldParent)) - && $option->value != $oldParent - ) + && $option->value != $oldParent) { unset($options[$i]); } @@ -190,17 +193,16 @@ protected function getOptions() // However, if you can edit.state you can also move this to another category for which you have // create permission and you should also still be able to save in the current category. if (($user->authorise('core.create', $extension . '.category.' . $option->value) != true) - && ($option->value != $oldCat && !isset($oldParent)) - ) + && ($option->value != $oldCat && !isset($oldParent))) { { unset($options[$i]); } } + if (($user->authorise('core.create', $extension . '.category.' . $option->value) != true) && (isset($oldParent)) - && $option->value != $oldParent - ) + && $option->value != $oldParent) { { unset($options[$i]); @@ -208,10 +210,10 @@ protected function getOptions() } } } + if (($this->element['parent'] == true || $jinput->get('option') == 'com_categories') && (isset($row) && !isset($options[0])) - && isset($this->element['show_root']) - ) + && isset($this->element['show_root'])) { if ($row->parent_id == '1') { @@ -219,6 +221,7 @@ protected function getOptions() $parent->text = JText::_('JGLOBAL_ROOT_PARENT'); array_unshift($options, $parent); } + array_unshift($options, JHtml::_('select.option', '0', JText::_('JGLOBAL_ROOT'))); } diff --git a/libraries/joomla/user/user.php b/libraries/joomla/user/user.php index bea0e420865be..b77ac5e9a6f12 100644 --- a/libraries/joomla/user/user.php +++ b/libraries/joomla/user/user.php @@ -388,12 +388,17 @@ public function getAuthorisedCategories($component, $action) // Brute force method: get all published category rows for the component and check each one // TODO: Modify the way permissions are stored in the db to allow for faster implementation and better scaling $db = JFactory::getDbo(); + + $subQuery = $db->getQuery(true) + ->select('id,asset_id') + ->from('#__categories') + ->where('extension = ' . $db->quote($component)) + ->where('published = 1'); + $query = $db->getQuery(true) ->select('c.id AS id, a.name AS asset_name') - ->from('#__categories AS c') - ->join('INNER', '#__assets AS a ON c.asset_id = a.id') - ->where('c.extension = ' . $db->quote($component)) - ->where('c.published = 1'); + ->from('(' . $subQuery->__toString() . ') AS c') + ->join('INNER', '#__assets AS a ON c.asset_id = a.id'); $db->setQuery($query); $allCategories = $db->loadObjectList('id'); $allowedCategories = array();