diff --git a/administrator/components/com_categories/models/category.php b/administrator/components/com_categories/models/category.php index 3bb2d32d88425..a94635c909238 100644 --- a/administrator/components/com_categories/models/category.php +++ b/administrator/components/com_categories/models/category.php @@ -738,22 +738,23 @@ protected function batchTag($value, $pks, $contexts) */ protected function batchCopy($value, $pks, $contexts) { + $type = new JUcmType; + $this->type = $type->getTypeByAlias($this->typeAlias); + // $value comes as {parent_id}.{extension} $parts = explode('.', $value); $parentId = (int) JArrayHelper::getValue($parts, 0, 1); - $table = $this->getTable(); $db = $this->getDbo(); - $user = JFactory::getUser(); $extension = JFactory::getApplication()->input->get('extension', '', 'word'); $i = 0; // Check that the parent exists if ($parentId) { - if (!$table->load($parentId)) + if (!$this->table->load($parentId)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); @@ -767,11 +768,13 @@ protected function batchCopy($value, $pks, $contexts) } } // Check that user has create permission for parent category - $canCreate = ($parentId == $table->getRootId()) ? $user->authorise('core.create', $extension) : $user->authorise('core.create', $extension . '.category.' . $parentId); + $canCreate = ($parentId == $this->table->getRootId()) ? $this->user->authorise('core.create', $extension) : $this->user->authorise('core.create', $extension . '.category.' . $parentId); + if (!$canCreate) { // Error since user cannot create in parent category $this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_CREATE')); + return false; } } @@ -779,13 +782,13 @@ protected function batchCopy($value, $pks, $contexts) // If the parent is 0, set it to the ID of the root item in the tree if (empty($parentId)) { - if (!$parentId = $table->getRootId()) + if (!$parentId = $this->table->getRootId()) { $this->setError($db->getErrorMsg()); return false; } // Make sure we can create in root - elseif (!$user->authorise('core.create', $extension)) + elseif (!$this->user->authorise('core.create', $extension)) { $this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_CREATE')); return false; @@ -808,21 +811,22 @@ protected function batchCopy($value, $pks, $contexts) catch (RuntimeException $e) { $this->setError($e->getMessage()); + return false; } - // Parent exists so we let's proceed + // Parent exists so let's proceed while (!empty($pks) && $count > 0) { // Pop the first id off the stack $pk = array_shift($pks); - $table->reset(); + $this->table->reset(); // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); @@ -840,8 +844,8 @@ protected function batchCopy($value, $pks, $contexts) $query->clear() ->select('id') ->from($db->quoteName('#__categories')) - ->where('lft > ' . (int) $table->lft) - ->where('rgt < ' . (int) $table->rgt); + ->where('lft > ' . (int) $this->table->lft) + ->where('rgt < ' . (int) $this->table->rgt); $db->setQuery($query); $childIds = $db->loadColumn(); @@ -855,61 +859,63 @@ protected function batchCopy($value, $pks, $contexts) } // Make a copy of the old ID and Parent ID - $oldId = $table->id; - $oldParentId = $table->parent_id; + $oldId = $this->table->id; + $oldParentId = $this->table->parent_id; // Reset the id because we are making a copy. - $table->id = 0; + $this->table->id = 0; // If we a copying children, the Old ID will turn up in the parents list // otherwise it's a new top level item - $table->parent_id = isset($parents[$oldParentId]) ? $parents[$oldParentId] : $parentId; + $this->table->parent_id = isset($parents[$oldParentId]) ? $parents[$oldParentId] : $parentId; // Set the new location in the tree for the node. - $table->setLocation($table->parent_id, 'last-child'); + $this->table->setLocation($this->table->parent_id, 'last-child'); // TODO: Deal with ordering? - // $table->ordering = 1; - $table->level = null; - $table->asset_id = null; - $table->lft = null; - $table->rgt = null; + // $this->table->ordering = 1; + $this->table->level = null; + $this->table->asset_id = null; + $this->table->lft = null; + $this->table->rgt = null; // Alter the title & alias - list($title, $alias) = $this->generateNewTitle($table->parent_id, $table->alias, $table->title); - $table->title = $title; - $table->alias = $alias; + list($title, $alias) = $this->generateNewTitle($this->table->parent_id, $this->table->alias, $this->table->title); + $this->table->title = $title; + $this->table->alias = $alias; + + parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); // Store the row. - if (!$table->store()) + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } // Get the new item ID - $newId = $table->get('id'); + $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; $i++; // Now we log the old 'parent' to the new 'parent' - $parents[$oldId] = $table->id; + $parents[$oldId] = $this->table->id; $count--; } // Rebuild the hierarchy. - if (!$table->rebuild()) + if (!$this->table->rebuild()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } // Rebuild the tree path. - if (!$table->rebuildPath($table->id)) + if (!$this->table->rebuildPath($this->table->id)) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } @@ -930,19 +936,19 @@ protected function batchCopy($value, $pks, $contexts) protected function batchMove($value, $pks, $contexts) { $parentId = (int) $value; + $type = new JUcmType; + $this->type = $type->getTypeByAlias($this->typeAlias); - $table = $this->getTable(); $db = $this->getDbo(); $query = $db->getQuery(true); - $user = JFactory::getUser(); $extension = JFactory::getApplication()->input->get('extension', '', 'word'); // Check that the parent exists. if ($parentId) { - if (!$table->load($parentId)) + if (!$this->table->load($parentId)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); @@ -956,8 +962,10 @@ protected function batchMove($value, $pks, $contexts) $parentId = 0; } } + // Check that user has create permission for parent category - $canCreate = ($parentId == $table->getRootId()) ? $user->authorise('core.create', $extension) : $user->authorise('core.create', $extension . '.category.' . $parentId); + $canCreate = ($parentId == $this->table->getRootId()) ? $this->user->authorise('core.create', $extension) : $this->user->authorise('core.create', $extension . '.category.' . $parentId); + if (!$canCreate) { // Error since user cannot create in parent category @@ -969,7 +977,7 @@ protected function batchMove($value, $pks, $contexts) // Note that the entire batch operation fails if any category lacks edit permission foreach ($pks as $pk) { - if (!$user->authorise('core.edit', $extension . '.category.' . $pk)) + if (!$this->user->authorise('core.edit', $extension . '.category.' . $pk)) { // Error since user cannot edit this category $this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_EDIT')); @@ -981,13 +989,13 @@ protected function batchMove($value, $pks, $contexts) // We are going to store all the children and just move the category $children = array(); - // Parent exists so we let's proceed + // Parent exists so let's proceed foreach ($pks as $pk) { // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); @@ -1002,16 +1010,16 @@ protected function batchMove($value, $pks, $contexts) } // Set the new location in the tree for the node. - $table->setLocation($parentId, 'last-child'); + $this->table->setLocation($parentId, 'last-child'); // Check if we are moving to a different parent - if ($parentId != $table->parent_id) + if ($parentId != $this->table->parent_id) { // Add the child node ids to the children array. $query->clear() ->select('id') ->from($db->quoteName('#__categories')) - ->where($db->quoteName('lft') . ' BETWEEN ' . (int) $table->lft . ' AND ' . (int) $table->rgt); + ->where($db->quoteName('lft') . ' BETWEEN ' . (int) $this->table->lft . ' AND ' . (int) $this->table->rgt); $db->setQuery($query); try @@ -1025,17 +1033,19 @@ protected function batchMove($value, $pks, $contexts) } } + parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } // Rebuild the tree path. - if (!$table->rebuildPath()) + if (!$this->table->rebuildPath()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } } diff --git a/administrator/components/com_contact/models/contact.php b/administrator/components/com_contact/models/contact.php index e3251c5169e7a..4acbb1f65d1e5 100644 --- a/administrator/components/com_contact/models/contact.php +++ b/administrator/components/com_contact/models/contact.php @@ -60,6 +60,27 @@ public function batch($commands, $pks, $contexts) $done = false; + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); + $this->batchSet = true; + + if ($this->type === false) + { + $type = new JUcmType; + $this->type = $type->getTypeByAlias($this->typeAlias); + $typeAlias = $this->type->type_alias; + } + else + { + $typeAlias = $this->type->type_alias; + } + $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags'); + + if (!empty($commands['category_id'])) { $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); @@ -126,6 +147,7 @@ public function batch($commands, $pks, $contexts) if (!$done) { $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); + return false; } @@ -153,52 +175,23 @@ protected function batchCopy($value, $pks, $contexts) $table = $this->getTable(); $i = 0; - // Check that the category exists - if ($categoryId) + if (!parent::checkCategoryId($categoryId)) { - $categoryTable = JTable::getInstance('Category'); - if (!$categoryTable->load($categoryId)) - { - if ($error = $categoryTable->getError()) - { - // Fatal error - $this->setError($error); - return false; - } - else - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - } - } - - if (empty($categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - - // Check that the user has create permission for the component - $user = JFactory::getUser(); - if (!$user->authorise('core.create', 'com_contact.category.' . $categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } - // Parent exists so we let's proceed + // Parent exists so we proceed while (!empty($pks)) { // Pop the first ID off the stack $pk = array_shift($pks); - $table->reset(); + $this->table->reset(); // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); @@ -213,35 +206,37 @@ protected function batchCopy($value, $pks, $contexts) } // Alter the title & alias - $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); - $table->name = $data['0']; - $table->alias = $data['1']; + $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->name); + $this->table->name = $data['0']; + $this->table->alias = $data['1']; // Reset the ID because we are making a copy - $table->id = 0; + $this->table->id = 0; // New category ID - $table->catid = $categoryId; + $this->table->catid = $categoryId; // TODO: Deal with ordering? - //$table->ordering = 1; + //$this->table->ordering = 1; // Check the row. - if (!$table->check()) + if (!$this->table->check()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } + parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } // Get the new item ID - $newId = $table->get('id'); + $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; @@ -267,21 +262,21 @@ protected function batchCopy($value, $pks, $contexts) */ protected function batchUser($value, $pks, $contexts) { - // Set the variables - $user = JFactory::getUser(); - $table = $this->getTable(); foreach ($pks as $pk) { - if ($user->authorise('core.edit', $contexts[$pk])) + if ($this->user->authorise('core.edit', $contexts[$pk])) { - $table->reset(); - $table->load($pk); - $table->user_id = (int) $value; + $this->table->reset(); + $this->table->load($pk); + $this->table->user_id = (int) $value; - if (!$table->store()) + static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->this->setError($table->getError()); + return false; } } diff --git a/administrator/components/com_content/models/article.php b/administrator/components/com_content/models/article.php index a4ee800ab95d6..9acfba5dd4829 100644 --- a/administrator/components/com_content/models/article.php +++ b/administrator/components/com_content/models/article.php @@ -49,41 +49,10 @@ protected function batchCopy($value, $pks, $contexts) { $categoryId = (int) $value; - $table = $this->getTable(); $i = 0; - // Check that the category exists - if ($categoryId) + if (!parent::checkCategoryId($categoryId)) { - $categoryTable = JTable::getInstance('Category'); - if (!$categoryTable->load($categoryId)) - { - if ($error = $categoryTable->getError()) - { - // Fatal error - $this->setError($error); - return false; - } - else - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - } - } - - if (empty($categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - - // Check that the user has create permission for the component - $extension = JFactory::getApplication()->input->get('option', ''); - $user = JFactory::getUser(); - if (!$user->authorise('core.create', $extension . '.category.' . $categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } @@ -93,15 +62,16 @@ protected function batchCopy($value, $pks, $contexts) // Pop the first ID off the stack $pk = array_shift($pks); - $table->reset(); + $this->table->reset(); // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); + return false; } else @@ -113,38 +83,40 @@ protected function batchCopy($value, $pks, $contexts) } // Alter the title & alias - $data = $this->generateNewTitle($categoryId, $table->alias, $table->title); - $table->title = $data['0']; - $table->alias = $data['1']; + $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->title); + $this->table->title = $data['0']; + $this->table->alias = $data['1']; // Reset the ID because we are making a copy - $table->id = 0; + $this->table->id = 0; // New category ID - $table->catid = $categoryId; + $this->table->catid = $categoryId; // TODO: Deal with ordering? //$table->ordering = 1; // Get the featured state - $featured = $table->featured; + $featured = $this->table->featured; // Check the row. - if (!$table->check()) + if (!$this->table->check()) { $this->setError($table->getError()); return false; } + parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { $this->setError($table->getError()); return false; } // Get the new item ID - $newId = $table->get('id'); + $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; diff --git a/administrator/components/com_newsfeeds/models/newsfeed.php b/administrator/components/com_newsfeeds/models/newsfeed.php index c3d1ee250aada..3162b5bed6e68 100644 --- a/administrator/components/com_newsfeeds/models/newsfeed.php +++ b/administrator/components/com_newsfeeds/models/newsfeed.php @@ -50,40 +50,10 @@ protected function batchCopy($value, $pks, $contexts) { $categoryId = (int) $value; - $table = $this->getTable(); $i = 0; - // Check that the category exists - if ($categoryId) + if (!parent::checkCategoryId($categoryId)) { - $categoryTable = JTable::getInstance('Category'); - if (!$categoryTable->load($categoryId)) - { - if ($error = $categoryTable->getError()) - { - // Fatal error - $this->setError($error); - return false; - } - else - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - } - } - - if (empty($categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - - // Check that the user has create permission for the component - $user = JFactory::getUser(); - if (!$user->authorise('core.create', 'com_newsfeeds.category.' . $categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } @@ -93,15 +63,16 @@ protected function batchCopy($value, $pks, $contexts) // Pop the first ID off the stack $pk = array_shift($pks); - $table->reset(); + $this->table->reset(); // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); + return false; } else @@ -113,35 +84,37 @@ protected function batchCopy($value, $pks, $contexts) } // Alter the title & alias - $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); - $table->name = $data['0']; - $table->alias = $data['1']; + $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->name); + $this->table->name = $data['0']; + $this->table->alias = $data['1']; // Reset the ID because we are making a copy - $table->id = 0; + $this->table->id = 0; // New category ID - $table->catid = $categoryId; + $this->table->catid = $categoryId; // TODO: Deal with ordering? - //$table->ordering = 1; + //$this->table->ordering = 1; // Check the row. - if (!$table->check()) + if (!$this->table->check()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } + parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); return false; } // Get the new item ID - $newId = $table->get('id'); + $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; @@ -429,6 +402,7 @@ public function getItem($pk = null) } } } + if (!empty($item->id)) { $item->tags = new JHelperTags; diff --git a/libraries/cms/helper/tags.php b/libraries/cms/helper/tags.php index ebc8ca96a3fbc..bc81cd0ccf747 100644 --- a/libraries/cms/helper/tags.php +++ b/libraries/cms/helper/tags.php @@ -36,7 +36,7 @@ class JHelperTags extends JHelper protected $replaceTags = false; /** - * Alias for quering mapping and content type table. + * Alias for querying mapping and content type table. * * @var string * @since 3.1 @@ -776,9 +776,9 @@ public function postStoreProcess($table, $newTags = array(), $replace = true) $result = true; // Process ucm_content and ucm_base if either tags have changed or we have some tags. - if ($this->tagsChanged || $newTags) + if ($this->tagsChanged || (!empty($newTags) && $newTags[0] != '')) { - if (!$newTags) + if (!$newTags && $replace = true) { // Delete all tags data $key = $table->getKeyName(); @@ -837,10 +837,14 @@ public function preStoreProcess($table, $newTags = array()) } // New items with no tags bypass this step. - if ((!empty($newTags) || (isset($newTags[0]) && $newTags[0] != '')) || isset($this->oldTags)) + if ((!empty($newTags) && is_string($newTags) || (isset($newTags[0]) && $newTags[0] != '')) || isset($this->oldTags)) { + if (is_array($newTags)) + { + $newTags = implode(',', $newTags); + } // We need to process tags if the tags have changed or if we have a new row - $this->tagsChanged = ($this->oldTags != $newTags) || !$table->$key; + $this->tagsChanged = (empty($this->oldTags) && !empty($newTags)) ||(!empty($this->oldTags) && $this->oldTags != $newTags) || !$table->$key; } } @@ -991,7 +995,7 @@ public function tagItem($ucmId, $table, $tags = array(), $replace = true) } } - if (is_array($newTags) && count($newTags) > 0) + if (is_array($newTags) && count($newTags) > 0 && $newTags[0] != '') { $result = $result && $this->addTagMapping($ucmId, $table, $newTags); } diff --git a/libraries/cms/ucm/type.php b/libraries/cms/ucm/type.php index 5fe46729c3881..2662bbe3d58a0 100644 --- a/libraries/cms/ucm/type.php +++ b/libraries/cms/ucm/type.php @@ -146,6 +146,42 @@ public function getTypeByAlias($typeAlias = null) return $type; } + /** + * Get the Content Type from the table class name + * + * @param string $tableName The table for the type + * + * @return mixed The UCM Type data if found, false if no match is found + * + * @since 3.2 + */ + public function getTypeByTable($tableName) + { + $query = $this->db->getQuery(true); + $query->select('ct.*'); + $query->from($this->db->quoteName('#__content_types', 'ct')); + + //$query->where($this->db->quoteName('ct.type_alias') . ' = ' . (int) $typeAlias); + $this->db->setQuery($query); + + $types = $this->db->loadObjectList(); + + foreach ($types as $type) + { + $tableFromType = json_decode($type->table); + $tableNameFromType = $tableFromType->special->prefix . $tableFromType->special->type; + + if ($tableNameFromType == $tableName) + { + return $type; + } + } + + return false; + + } + + /** * Retrieves the UCM type ID * diff --git a/libraries/joomla/table/observer/tags.php b/libraries/joomla/table/observer/tags.php index 72bdf9196f8fe..15929e1df2ade 100644 --- a/libraries/joomla/table/observer/tags.php +++ b/libraries/joomla/table/observer/tags.php @@ -1,6 +1,6 @@ parseTypeAlias(); - $this->tagsHelper->preStoreProcess($this->table); + if (empty($this->table->tagsHelper->tags)) + { + $this->tagsHelper->preStoreProcess($this->table); + } + else + { + $this->tagsHelper->preStoreProcess($this->table, (array) $this->table->tagsHelper->tags); + } } /** @@ -116,8 +127,14 @@ public function onAfterStore(&$result) { if ($result) { - $result = $this->tagsHelper->postStoreProcess($this->table); - + if (empty($this->table->tagsHelper->tags)) + { + $result = $this->tagsHelper->postStoreProcess($this->table); + } + else + { + $result = $this->tagsHelper->postStoreProcess($this->table, $this->table->tagsHelper->tags); + } // Restore default values for the optional params: $this->newTags = array(); $this->replaceTags = true; @@ -141,9 +158,9 @@ public function onBeforeDelete($pk) } /** - * Sets the new tags to be added/replaced to the table row + * Sets the new tags to be added or to replace existing tags * - * @param array $newTags New tags to be added or replaced + * @param array $newTags New tags to be added to or replace current tags for an item * @param boolean $replaceTags Replace tags (true) or add them (false) * * @return boolean diff --git a/libraries/legacy/model/admin.php b/libraries/legacy/model/admin.php index bb2d979c4d683..04e600674fddb 100644 --- a/libraries/legacy/model/admin.php +++ b/libraries/legacy/model/admin.php @@ -160,11 +160,38 @@ public function batch($commands, $pks, $contexts) if (empty($pks)) { $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED')); + return false; } $done = false; + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); + $this->batchSet = true; + + if ($this->type == false) + { + $type = new JUcmType; + $this->type = $type->getTypeByAlias($this->typeAlias); + + } + if ($this->type === false) + { + $type = new JUcmType; + $this->type = $type->getTypeByAlias($this->typeAlias); + $typeAlias = $this->type->type_alias; + } + else + { + $typeAlias = $this->type->type_alias; + } + $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags'); + if (!empty($commands['category_id'])) { $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); @@ -172,6 +199,7 @@ public function batch($commands, $pks, $contexts) if ($cmd == 'c') { $result = $this->batchCopy($commands['category_id'], $pks, $contexts); + if (is_array($result)) { $pks = $result; @@ -185,6 +213,7 @@ public function batch($commands, $pks, $contexts) { return false; } + $done = true; } @@ -243,19 +272,27 @@ public function batch($commands, $pks, $contexts) */ protected function batchAccess($value, $pks, $contexts) { - // Set the variables - $user = JFactory::getUser(); - $table = $this->getTable(); + if (!$this->batchSet) + { + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); + } foreach ($pks as $pk) { - if ($user->authorise('core.edit', $contexts[$pk])) + if ($this->user->authorise('core.edit', $contexts[$pk])) { - $table->reset(); - $table->load($pk); - $table->access = (int) $value; + $this->table->reset(); + $this->table->load($pk); + $this->table->access = (int) $value; - if (!$table->store()) + static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + + if (!$this->table->store()) { $this->setError($table->getError()); @@ -289,61 +326,41 @@ protected function batchAccess($value, $pks, $contexts) */ protected function batchCopy($value, $pks, $contexts) { - $categoryId = (int) $value; - - $table = $this->getTable(); - $i = 0; - - // Check that the category exists - if ($categoryId) + if (!$this->batchSet) { - $categoryTable = JTable::getInstance('Category'); - if (!$categoryTable->load($categoryId)) - { - if ($error = $categoryTable->getError()) - { - // Fatal error - $this->setError($error); - return false; - } - else - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - } + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); } - if (empty($categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } + $i = 0; - // Check that the user has create permission for the component - $extension = JFactory::getApplication()->input->get('option', ''); - $user = JFactory::getUser(); - if (!$user->authorise('core.create', $extension . '.category.' . $categoryId)) + $categoryId = $value; + + if (!static::checkCategoryId($categoryId)) { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } - // Parent exists so we let's proceed + // Parent exists so let's proceed while (!empty($pks)) { // Pop the first ID off the stack $pk = array_shift($pks); - $table->reset(); + $this->table->reset(); // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); + return false; } else @@ -354,36 +371,37 @@ protected function batchCopy($value, $pks, $contexts) } } - // Alter the title & alias - $data = $this->generateNewTitle($categoryId, $table->alias, $table->title); - $table->title = $data['0']; - $table->alias = $data['1']; + static::generateTitle($categoryId, $this->table); // Reset the ID because we are making a copy - $table->id = 0; + $this->table->id = 0; // New category ID - $table->catid = $categoryId; + $this->table->catid = $categoryId; // TODO: Deal with ordering? - // $table->ordering = 1; + // $this->table->ordering = 1; // Check the row. - if (!$table->check()) + if (!$this->table->check()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); + return false; } + static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { $this->setError($table->getError()); + return false; } // Get the new item ID - $newId = $table->get('id'); + $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; @@ -409,27 +427,37 @@ protected function batchCopy($value, $pks, $contexts) */ protected function batchLanguage($value, $pks, $contexts) { - // Set the variables - $user = JFactory::getUser(); - $table = $this->getTable(); + if (!$this->batchSet) + { + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); + } foreach ($pks as $pk) { - if ($user->authorise('core.edit', $contexts[$pk])) + if ($this->user->authorise('core.edit', $contexts[$pk])) { - $table->reset(); - $table->load($pk); - $table->language = $value; + $this->table->reset(); + $this->table->load($pk); + $this->table->language = $value; - if (!$table->store()) + static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); + return false; } } else { $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); + return false; } } @@ -453,61 +481,41 @@ protected function batchLanguage($value, $pks, $contexts) */ protected function batchMove($value, $pks, $contexts) { - $categoryId = (int) $value; - - $table = $this->getTable(); - - // Check that the category exists - if ($categoryId) + if (!$this->batchSet) { - $categoryTable = JTable::getInstance('Category'); - if (!$categoryTable->load($categoryId)) - { - if ($error = $categoryTable->getError()) - { - // Fatal error - $this->setError($error); - return false; - } - else - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } - } + // Set some needed variables. + $this->user = JFactory::getUser(); + $this->table = $this->getTable(); + $this->tableClassName = get_class($this->table); + $this->contentType = new JUcmType(); + $this->type = $this->contentType->getTypeByTable($this->tableClassName); } - if (empty($categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); - return false; - } + $categoryId = (int) $value; - // Check that user has create and edit permission for the component - $extension = JFactory::getApplication()->input->get('option', ''); - $user = JFactory::getUser(); - if (!$user->authorise('core.create', $extension . '.category.' . $categoryId)) + if (!static::checkCategoryId($categoryId)) { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } // Parent exists so we proceed foreach ($pks as $pk) { - if (!$user->authorise('core.edit', $contexts[$pk])) + if (!$this->user->authorise('core.edit', $contexts[$pk])) { $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); + return false; } // Check that the row actually exists - if (!$table->load($pk)) + if (!$this->table->load($pk)) { - if ($error = $table->getError()) + if ($error = $this->table->getError()) { // Fatal error $this->setError($error); + return false; } else @@ -519,19 +527,23 @@ protected function batchMove($value, $pks, $contexts) } // Set the new category ID - $table->catid = $categoryId; + $this->table->catid = $categoryId; // Check the row. - if (!$table->check()) + if (!$this->table->check()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); + return false; } + static::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); + // Store the row. - if (!$table->store()) + if (!$this->table->store()) { - $this->setError($table->getError()); + $this->setError($this->table->getError()); + return false; } } @@ -572,6 +584,7 @@ protected function batchTag($value, $pks, $contexts) */ $tagsObserver = $table->getObserverOfClass('JTableObserverTags'); $result = $tagsObserver->setNewTags($tags, false); + if (!$result) { $this->setError($table->getError()); @@ -605,6 +618,7 @@ protected function batchTag($value, $pks, $contexts) protected function canDelete($record) { $user = JFactory::getUser(); + return $user->authorise('core.delete', $this->option); } @@ -620,6 +634,7 @@ protected function canDelete($record) protected function canEditState($record) { $user = JFactory::getUser(); + return $user->authorise('core.edit.state', $this->option); } @@ -717,6 +732,7 @@ public function delete(&$pks) // Trigger the onContentBeforeDelete event. $result = $dispatcher->trigger($this->event_before_delete, array($context, $table)); + if (in_array(false, $result, true)) { $this->setError($table->getError()); @@ -911,6 +927,7 @@ public function publish(&$pks, $value = 1) // Prune items that you can't change. unset($pks[$i]); JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), JLog::WARNING, 'jerror'); + return false; } } @@ -920,6 +937,7 @@ public function publish(&$pks, $value = 1) if (!$table->publish($pks, $value, $user->get('id'))) { $this->setError($table->getError()); + return false; } @@ -931,6 +949,7 @@ public function publish(&$pks, $value = 1) if (in_array(false, $result, true)) { $this->setError($table->getError()); + return false; } @@ -1051,6 +1070,7 @@ public function save($data) if (!$table->bind($data)) { $this->setError($table->getError()); + return false; } @@ -1066,6 +1086,7 @@ public function save($data) // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, $table, $isNew)); + if (in_array(false, $result, true)) { $this->setError($table->getError()); @@ -1116,6 +1137,11 @@ public function save($data) public function saveorder($pks = null, $order = null) { $table = $this->getTable(); + $tableClassName = get_class($table); + $contentType = new JUcmType(); + $type = $contentType->getTypeByTable($tableClassName); + $typeAlias = $type->type_alias; + $tagsObserver = $table->getObserverOfClass('JTableObserverTags'); $conditions = array(); if (empty($pks)) @@ -1139,6 +1165,8 @@ public function saveorder($pks = null, $order = null) { $table->ordering = $order[$i]; + $this->createTagsHelper($tagsObserver, $type, $pk, $typeAlias, $table); + if (!$table->store()) { $this->setError($table->getError()); @@ -1178,4 +1206,94 @@ public function saveorder($pks = null, $order = null) return true; } + + /** + * Method to creat a tags helper to ensure proper management of tags + * + * @param JTableObserverTags $tagsObserver The tags observer for this table + * @param JUcmType $type The type for the table being processed + * @param integer $pk Primary key of the item bing processed + * @param string $typeAlias The type alias for this table + * + * @return void + * + * @since 3.2 + */ + public function createTagsHelper($tagsObserver, $type, $pk, $typeAlias, $table) + { + if (!empty($tagsObserver) && !empty($type)) + { + $table->tagsHelper = new JHelperTags(); + $table->tagsHelper->typeAlias = $typeAlias; + $table->tagsHelper->tags = explode(',', $table->tagsHelper->getTagIds($pk, $typeAlias)); + } + } + + /** + * Method to check the validity of the category id for batch copy and move + * + * @param integer $categoryId The category id to check + * + * @return boolean + * + * @since 3.2 + */ + protected function checkCategoryId($categoryId) + { + // Check that the category exists + if ($categoryId) + { + $categoryTable = JTable::getInstance('Category'); + + if (!$categoryTable->load($categoryId)) + { + if ($error = $categoryTable->getError()) + { + // Fatal error + $this->setError($error); + return false; + } + else + { + $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); + + return false; + } + } + } + + if (empty($categoryId)) + { + $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); + return false; + } + + // Check that the user has create permission for the component + $extension = JFactory::getApplication()->input->get('option', ''); + + if (!$this->user->authorise('core.create', $extension . '.category.' . $categoryId)) + { + $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); + + return false; + } + + return true; + } + + /** + * A method to preprocess generating a new title in order to allow tables with alternative names + * for alias and title to use the batch move and copy methods + * + * @param integer $categoryId The target category id + * @param JTable $table The JTable within whcih move or copy is taking place + */ + public function generateTitle($categoryId, $table) + { + // Alter the title & alias + $data = $this->generateNewTitle($categoryId, $table->alias, $table->title); + $table->title = $data['0']; + $table->alias = $data['1']; + } + }