diff --git a/administrator/components/com_banners/models/banner.php b/administrator/components/com_banners/models/banner.php index bf1e662526716..c53fd9447f9aa 100644 --- a/administrator/components/com_banners/models/banner.php +++ b/administrator/components/com_banners/models/banner.php @@ -95,131 +95,6 @@ protected function batchClient($value, $pks, $contexts) return true; } - /** - * Batch copy items to a new category or current. - * - * @param integer $value The new category. - * @param array $pks An array of row IDs. - * @param array $contexts An array of item contexts. - * - * @return mixed An array of new IDs on success, boolean false on failure. - * - * @since 2.5 - */ - protected function batchCopy($value, $pks, $contexts) - { - $categoryId = (int) $value; - - /** @var BannersTableBanner $table */ - $table = $this->getTable(); - $newIds = array(); - - // 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; - } - - $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 - if (!JFactory::getUser()->authorise('core.create', 'com_banners.category.' . $categoryId)) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); - - return false; - } - - // Parent exists so we let's proceed - while (!empty($pks)) - { - // Pop the first ID off the stack - $pk = array_shift($pks); - - $table->reset(); - - // Check that the row actually exists - if (!$table->load($pk)) - { - if ($error = $table->getError()) - { - // Fatal error - $this->setError($error); - - return false; - } - - // Not fatal error - $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); - continue; - } - - // Alter the title & alias - $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); - $table->name = $data['0']; - $table->alias = $data['1']; - - // Reset the ID because we are making a copy - $table->id = 0; - - // New category ID - $table->catid = $categoryId; - - // Unpublish because we are making a copy - $table->state = 0; - - // TODO: Deal with ordering? - // $table->ordering = 1; - - // Check the row. - if (!$table->check()) - { - $this->setError($table->getError()); - - return false; - } - - // Store the row. - if (!$table->store()) - { - $this->setError($table->getError()); - - return false; - } - - // Get the new item ID - $newId = $table->get('id'); - - // Add the new ID to the array - $newIds[$pk] = $newId; - } - - // Clean the cache - $this->cleanCache(); - - return $newIds; - } - /** * Method to test whether a record can be deleted. * @@ -247,6 +122,25 @@ protected function canDelete($record) } } + /** + * 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 which move or copy is taking place + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function generateTitle($categoryId, $table) + { + // Alter the title & alias + $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); + $table->name = $data['0']; + $table->alias = $data['1']; + } + /** * Method to test whether a record can have its state changed. * diff --git a/administrator/components/com_contact/models/contact.php b/administrator/components/com_contact/models/contact.php index 936591f4979d9..0faa2d034eeb7 100644 --- a/administrator/components/com_contact/models/contact.php +++ b/administrator/components/com_contact/models/contact.php @@ -57,101 +57,6 @@ class ContactModelContact extends JModelAdmin 'user_id' => 'batchUser', ); - /** - * Batch copy items to a new category or current. - * - * @param integer $value The new category. - * @param array $pks An array of row IDs. - * @param array $contexts An array of item contexts. - * - * @return mixed An array of new IDs on success, boolean false on failure. - * - * @since 11.1 - */ - protected function batchCopy($value, $pks, $contexts) - { - $categoryId = (int) $value; - - $newIds = array(); - - if (!parent::checkCategoryId($categoryId)) - { - return false; - } - - // Parent exists so we proceed - while (!empty($pks)) - { - // Pop the first ID off the stack - $pk = array_shift($pks); - - $this->table->reset(); - - // Check that the row actually exists - if (!$this->table->load($pk)) - { - if ($error = $this->table->getError()) - { - // Fatal error - $this->setError($error); - - return false; - } - else - { - // Not fatal error - $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); - continue; - } - } - - // Alter the title & alias - $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 - $this->table->id = 0; - - // New category ID - $this->table->catid = $categoryId; - - // Unpublish because we are making a copy - $this->table->published = 0; - - // TODO: Deal with ordering? - - // Check the row. - if (!$this->table->check()) - { - $this->setError($this->table->getError()); - - return false; - } - - $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); - - // Store the row. - if (!$this->table->store()) - { - $this->setError($this->table->getError()); - - return false; - } - - // Get the new item ID - $newId = $this->table->get('id'); - - // Add the new ID to the array - $newIds[$pk] = $newId; - } - - // Clean the cache - $this->cleanCache(); - - return $newIds; - } - /** * Batch change a linked user. * diff --git a/administrator/components/com_content/models/article.php b/administrator/components/com_content/models/article.php index 868182bd67f2e..4de61547ef1c1 100644 --- a/administrator/components/com_content/models/article.php +++ b/administrator/components/com_content/models/article.php @@ -46,138 +46,49 @@ class ContentModelArticle extends JModelAdmin protected $associationsContext = 'com_content.item'; /** - * Batch copy items to a new category or current. + * Function that can be overriden to do any data cleanup after batch copying data * - * @param integer $value The new category. - * @param array $pks An array of row IDs. - * @param array $contexts An array of item contexts. + * @param \JTableInterface $table The table object containing the newly created item + * @param integer $newId The id of the new item + * @param integer $oldId The original item id * - * @return mixed An array of new IDs on success, boolean false on failure. + * @return void * - * @since 11.1 + * @since __DEPLOY_VERSION__ */ - protected function batchCopy($value, $pks, $contexts) + protected function cleanupPostBatchCopy(\JTableInterface $table, $newId, $oldId) { - $categoryId = (int) $value; - - $newIds = array(); - - if (!$this->checkCategoryId($categoryId)) + // Check if the article was featured and update the #__content_frontpage table + if ($table->featured == 1) { - return false; + $db = $this->getDbo(); + $query = $db->getQuery(true) + ->insert($db->quoteName('#__content_frontpage')) + ->values($newId . ', 0'); + $db->setQuery($query); + $db->execute(); } - JPluginHelper::importPlugin('system'); - $dispatcher = JEventDispatcher::getInstance(); - // Register FieldsHelper JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - // Parent exists so we let's proceed - while (!empty($pks)) - { - // Pop the first ID off the stack - $pk = array_shift($pks); - - $this->table->reset(); + $oldItem = $this->getTable(); + $oldItem->load($oldId); + $fields = FieldsHelper::getFields('com_content.article', $oldItem, true); - // Check that the row actually exists - if (!$this->table->load($pk)) - { - if ($error = $this->table->getError()) - { - // Fatal error - $this->setError($error); - - return false; - } - else - { - // Not fatal error - $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); - continue; - } - } - - $fields = FieldsHelper::getFields('com_content.article', $this->table, true); - $fieldsData = array(); - - if (!empty($fields)) - { - $fieldsData['com_fields'] = array(); + $fieldsData = array(); - foreach ($fields as $field) - { - $fieldsData['com_fields'][$field->name] = $field->rawvalue; - } - } - - // Alter the title & alias - $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 - $this->table->id = 0; - - // Reset hits because we are making a copy - $this->table->hits = 0; - - // Unpublish because we are making a copy - $this->table->state = 0; - - // New category ID - $this->table->catid = $categoryId; - - // TODO: Deal with ordering? - // $table->ordering = 1; - - // Get the featured state - $featured = $this->table->featured; - - // Check the row. - if (!$this->table->check()) - { - $this->setError($this->table->getError()); - - return false; - } - - $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); - - // Store the row. - if (!$this->table->store()) - { - $this->setError($this->table->getError()); - - return false; - } - - // Get the new item ID - $newId = $this->table->get('id'); - - // Add the new ID to the array - $newIds[$pk] = $newId; + if (!empty($fields)) + { + $fieldsData['com_fields'] = array(); - // Check if the article was featured and update the #__content_frontpage table - if ($featured == 1) + foreach ($fields as $field) { - $db = $this->getDbo(); - $query = $db->getQuery(true) - ->insert($db->quoteName('#__content_frontpage')) - ->values($newId . ', 0'); - $db->setQuery($query); - $db->execute(); + $fieldsData['com_fields'][$field->name] = $field->rawvalue; } - - // Run event for copied article - $dispatcher->trigger('onContentAfterSave', array('com_content.article', &$this->table, true, $fieldsData)); } - // Clean the cache - $this->cleanCache(); - - return $newIds; + JEventDispatcher::getInstance()->trigger('onContentAfterSave', array('com_content.article', &$this->table, true, $fieldsData)); } /** diff --git a/administrator/components/com_newsfeeds/models/newsfeed.php b/administrator/components/com_newsfeeds/models/newsfeed.php index 97ee58633890d..19c1c4a88c0a3 100644 --- a/administrator/components/com_newsfeeds/models/newsfeed.php +++ b/administrator/components/com_newsfeeds/models/newsfeed.php @@ -43,102 +43,6 @@ class NewsfeedsModelNewsfeed extends JModelAdmin */ protected $text_prefix = 'COM_NEWSFEEDS'; - /** - * Batch copy items to a new category or current. - * - * @param integer $value The new category. - * @param array $pks An array of row IDs. - * @param array $contexts An array of item contexts. - * - * @return mixed An array of new IDs on success, boolean false on failure. - * - * @since 11.1 - */ - protected function batchCopy($value, $pks, $contexts) - { - $categoryId = (int) $value; - - $newIds = array(); - - if (!parent::checkCategoryId($categoryId)) - { - return false; - } - - // Parent exists so we let's proceed - while (!empty($pks)) - { - // Pop the first ID off the stack - $pk = array_shift($pks); - - $this->table->reset(); - - // Check that the row actually exists - if (!$this->table->load($pk)) - { - if ($error = $this->table->getError()) - { - // Fatal error - $this->setError($error); - - return false; - } - else - { - // Not fatal error - $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); - continue; - } - } - - // Alter the title & alias - $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 - $this->table->id = 0; - - // Unpublish because we are making a copy - $this->table->published = 0; - - // New category ID - $this->table->catid = $categoryId; - - // TODO: Deal with ordering? - // $this->table->ordering = 1; - - // Check the row. - if (!$this->table->check()) - { - $this->setError($this->table->getError()); - - return false; - } - - parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table); - - // Store the row. - if (!$this->table->store()) - { - $this->setError($this->table->getError()); - - return false; - } - - // Get the new item ID - $newId = $this->table->get('id'); - - // Add the new ID to the array - $newIds[$pk] = $newId; - } - - // Clean the cache - $this->cleanCache(); - - return $newIds; - } - /** * Method to test whether a record can be deleted. * diff --git a/libraries/src/MVC/Model/AdminModel.php b/libraries/src/MVC/Model/AdminModel.php index 1055717b59b91..ea21d07dad06c 100644 --- a/libraries/src/MVC/Model/AdminModel.php +++ b/libraries/src/MVC/Model/AdminModel.php @@ -486,6 +486,8 @@ protected function batchCopy($value, $pks, $contexts) // Get the new item ID $newId = $this->table->get('id'); + $this->cleanupPostBatchCopy($this->table, $newId, $pk); + // Add the new ID to the array $newIds[$pk] = $newId; } @@ -496,6 +498,21 @@ protected function batchCopy($value, $pks, $contexts) return $newIds; } + /** + * Function that can be overriden to do any data cleanup after batch copying data + * + * @param \JTableInterface $table The table object containing the newly created item + * @param integer $newId The id of the new item + * @param integer $oldId The original item id + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function cleanupPostBatchCopy(\JTableInterface $table, $newId, $oldId) + { + } + /** * Batch language changes for a group of rows. *