Skip to content

Commit

Permalink
Optimizing batch() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar committed Dec 7, 2014
1 parent 5fd8d9b commit 4696989
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 431 deletions.
102 changes: 13 additions & 89 deletions administrator/components/com_banners/models/banner.php
Expand Up @@ -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.
Expand Down
145 changes: 15 additions & 130 deletions administrator/components/com_contact/models/contact.php
Expand Up @@ -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.
Expand Down
109 changes: 18 additions & 91 deletions administrator/components/com_menus/models/item.php
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down

0 comments on commit 4696989

Please sign in to comment.