diff --git a/administrator/components/com_banners/models/banner.php b/administrator/components/com_banners/models/banner.php index 1dda6b02c9fde..b8311f2b9c8e6 100644 --- a/administrator/components/com_banners/models/banner.php +++ b/administrator/components/com_banners/models/banner.php @@ -31,98 +31,22 @@ class BannersModelBanner extends JModelAdmin public $typeAlias = 'com_banners.banner'; /** - * Method to perform batch operations on an item or a set of items. + * Batch copy/move command. If set to false, + * the batch copy/move command is not supported * - * @param array $commands An array of commands to perform. - * @param array $pks An array of item ids. - * @param array $contexts An array of item contexts. - * - * @return boolean Returns true on success, false on failure. - * - * @since 2.5 + * @var string */ - public function batch($commands, $pks, $contexts) - { - // Sanitize user ids. - $pks = array_unique($pks); - JArrayHelper::toInteger($pks); - - // Remove any values of zero. - if (array_search(0, $pks, true)) - { - unset($pks[array_search(0, $pks, true)]); - } - - if (empty($pks)) - { - $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED')); - - return false; - } - - $done = false; - - if (!empty($commands['category_id'])) - { - $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); - - if ($cmd == 'c') - { - $result = $this->batchCopy($commands['category_id'], $pks, $contexts); - - if (is_array($result)) - { - foreach ($result as $old => $new) - { - $contexts[$new] = $contexts[$old]; - } - $pks = array_values($result); - } - else - { - return false; - } - } - elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) - { - return false; - } + protected $batch_copymove = 'category_id'; - $done = true; - } - - if (strlen($commands['client_id']) > 0) - { - if (!$this->batchClient($commands['client_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['language_id'])) - { - if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!$done) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); - - return false; - } - - // Clear the cache - $this->cleanCache(); - - return true; - } + /** + * Allowed batch commands + * + * @var array + */ + protected $batch_commands = array( + 'client_id' => 'batchClient', + 'language_id' => 'batchLanguage' + ); /** * Batch client changes for a group of banners. diff --git a/administrator/components/com_contact/models/contact.php b/administrator/components/com_contact/models/contact.php index 131a1308dea8f..79281c4aa8a8a 100644 --- a/administrator/components/com_contact/models/contact.php +++ b/administrator/components/com_contact/models/contact.php @@ -29,139 +29,24 @@ class ContactModelContact extends JModelAdmin public $typeAlias = 'com_contact.contact'; /** - * Method to perform batch operations on an item or a set of items. + * Batch copy/move command. If set to false, + * the batch copy/move command is not supported * - * @param array $commands An array of commands to perform. - * @param array $pks An array of item ids. - * @param array $contexts An array of item contexts. - * - * @return boolean Returns true on success, false on failure. - * - * @since 2.5 + * @var string */ - public function batch($commands, $pks, $contexts) - { - // Sanitize user ids. - $pks = array_unique($pks); - JArrayHelper::toInteger($pks); - - // Remove any values of zero. - if (array_search(0, $pks, true)) - { - unset($pks[array_search(0, $pks, true)]); - } - - 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); - $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'); - - if ($cmd == 'c') - { - $result = $this->batchCopy($commands['category_id'], $pks, $contexts); - - if (is_array($result)) - { - foreach ($result as $old => $new) - { - $contexts[$new] = $contexts[$old]; - } - $pks = array_values($result); - } - else - { - return false; - } - } - elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['assetgroup_id'])) - { - if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) - { - return false; - } + protected $batch_copymove = 'category_id'; - $done = true; - } - - if (!empty($commands['language_id'])) - { - if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['tag'])) - { - if (!$this->batchTag($commands['tag'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (strlen($commands['user_id']) > 0) - { - if (!$this->batchUser($commands['user_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!$done) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); - - return false; - } - - // Clear the cache - $this->cleanCache(); - - return true; - } + /** + * Allowed batch commands + * + * @var array + */ + protected $batch_commands = array( + 'assetgroup_id' => 'batchAccess', + 'language_id' => 'batchLanguage', + 'tag' => 'batchTag', + 'user_id' => 'batchUser' + ); /** * Batch copy items to a new category or current. diff --git a/administrator/components/com_menus/models/item.php b/administrator/components/com_menus/models/item.php index 9a5b1726efb8f..2e1b7725e87ef 100644 --- a/administrator/components/com_menus/models/item.php +++ b/administrator/components/com_menus/models/item.php @@ -45,6 +45,24 @@ class MenusModelItem extends JModelAdmin */ protected $helpLocal = false; + /** + * Batch copy/move command. If set to false, + * the batch copy/move command is not supported + * + * @var string + */ + protected $batch_copymove = 'menu_id'; + + /** + * Allowed batch commands + * + * @var array + */ + protected $batch_commands = array( + 'assetgroup_id' => 'batchAccess', + 'language_id' => 'batchLanguage' + ); + /** * Method to test whether a record can be deleted. * @@ -67,97 +85,6 @@ protected function canDelete($record) } } - /** - * Method to perform batch operations on an item or a set of items. - * - * @param array $commands An array of commands to perform. - * @param array $pks An array of item ids. - * @param array $contexts An array of item contexts. - * - * @return boolean Returns true on success, false on failure. - * - * @since 1.6 - */ - public function batch($commands, $pks, $contexts) - { - // Sanitize user ids. - $pks = array_unique($pks); - JArrayHelper::toInteger($pks); - - // Remove any values of zero. - if (array_search(0, $pks, true)) - { - unset($pks[array_search(0, $pks, true)]); - } - - if (empty($pks)) - { - $this->setError(JText::_('COM_MENUS_NO_ITEM_SELECTED')); - - return false; - } - - $done = false; - - if (!empty($commands['menu_id'])) - { - $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); - - if ($cmd == 'c') - { - $result = $this->batchCopy($commands['menu_id'], $pks, $contexts); - - if (is_array($result)) - { - foreach ($result as $old => $new) - { - $contexts[$new] = $contexts[$old]; - } - $pks = array_values($result); - } - else - { - return false; - } - } - elseif ($cmd == 'm' && !$this->batchMove($commands['menu_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['assetgroup_id'])) - { - if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['language_id'])) - { - if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!$done) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); - - return false; - } - - return true; - } - /** * Batch copy menu items to a new menu or parent. * diff --git a/administrator/components/com_modules/models/module.php b/administrator/components/com_modules/models/module.php index 210e960fc99e4..0e368a63337a9 100644 --- a/administrator/components/com_modules/models/module.php +++ b/administrator/components/com_modules/models/module.php @@ -36,6 +36,24 @@ class ModulesModelModule extends JModelAdmin */ protected $helpURL; + /** + * Batch copy/move command. If set to false, + * the batch copy/move command is not supported + * + * @var string + */ + protected $batch_copymove = 'position_id'; + + /** + * Allowed batch commands + * + * @var array + */ + protected $batch_commands = array( + 'assetgroup_id' => 'batchAccess', + 'language_id' => 'batchLanguage', + ); + /** * Constructor. * @@ -90,100 +108,6 @@ protected function populateState() $this->setState('params', $params); } - /** - * Method to perform batch operations on a set of modules. - * - * @param array $commands An array of commands to perform. - * @param array $pks An array of item ids. - * @param array $contexts An array of item contexts. - * - * @return boolean Returns true on success, false on failure. - * - * @since 1.7 - */ - public function batch($commands, $pks, $contexts) - { - // Sanitize user ids. - $pks = array_unique($pks); - JArrayHelper::toInteger($pks); - - // Remove any values of zero. - if (array_search(0, $pks, true)) - { - unset($pks[array_search(0, $pks, true)]); - } - - if (empty($pks)) - { - $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED')); - - return false; - } - - $done = false; - - if (!empty($commands['position_id'])) - { - $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); - - if ($cmd == 'c') - { - $result = $this->batchCopy($commands['position_id'], $pks, $contexts); - - if (is_array($result)) - { - foreach ($result as $old => $new) - { - $contexts[$new] = $contexts[$old]; - } - $pks = array_values($result); - } - else - { - return false; - } - } - elseif ($cmd == 'm' && !$this->batchMove($commands['position_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['assetgroup_id'])) - { - if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['language_id'])) - { - if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!$done) - { - $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); - - return false; - } - - // Clear the cache - $this->cleanCache(); - - return true; - } - /** * Batch copy modules to a new position or current. * diff --git a/libraries/legacy/model/admin.php b/libraries/legacy/model/admin.php index 0494bb15e6f0c..5a68f9dd24e13 100644 --- a/libraries/legacy/model/admin.php +++ b/libraries/legacy/model/admin.php @@ -73,6 +73,25 @@ abstract class JModelAdmin extends JModelForm */ protected $events_map = null; + /** + * Batch copy/move command. If set to false, + * the batch copy/move command is not supported + * + * @var string + */ + protected $batch_copymove = 'category_id'; + + /** + * Allowed batch commands + * + * @var array + */ + protected $batch_commands = array( + 'assetgroup_id' => 'batchAccess', + 'language_id' => 'batchLanguage', + 'tag' => 'batchTag' + ); + /** * Constructor. * @@ -199,13 +218,13 @@ public function batch($commands, $pks, $contexts) $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags'); - if (!empty($commands['category_id'])) + if ($this->batch_copymove && !empty($commands[$this->batch_copymove])) { $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c'); if ($cmd == 'c') { - $result = $this->batchCopy($commands['category_id'], $pks, $contexts); + $result = $this->batchCopy($commands[$this->batch_copymove], $pks, $contexts); if (is_array($result)) { @@ -220,17 +239,7 @@ public function batch($commands, $pks, $contexts) return false; } } - elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) - { - return false; - } - - $done = true; - } - - if (!empty($commands['assetgroup_id'])) - { - if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) + elseif ($cmd == 'm' && !$this->batchMove($commands[$this->batch_copymove], $pks, $contexts)) { return false; } @@ -238,24 +247,17 @@ public function batch($commands, $pks, $contexts) $done = true; } - if (!empty($commands['language_id'])) + foreach ($this->batch_commands as $identifier => $command) { - if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) + if (strlen($commands[$identifier]) > 0) { - return false; - } - - $done = true; - } + if (!$this->$command($commands[$identifier], $pks, $contexts)) + { + return false; + } - if (!empty($commands['tag'])) - { - if (!$this->batchTag($commands['tag'], $pks, $contexts)) - { - return false; + $done = true; } - - $done = true; } if (!$done)