Admin Model

It is a 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.
public function batch($commands, $pks, $contexts)
{
// Sanitize ids.
$pks = array_unique($pks);
$pks = ArrayHelper::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;

// Initialize re-usable member properties
$this->initBatch();

if ($this->batch_copymove && !empty($commands[$this->batch_copymove]))
{
$cmd = ArrayHelper::getValue($commands, 'move_copy', 'c');

if ($cmd === 'c')
{
$result = $this->batchCopy($commands[$this->batch_copymove], $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[$this->batch_copymove], $pks, $contexts))
{
return false;
}

$done = true;
}

foreach ($this->batch_commands as $identifier => $command)
{
if (!empty($commands[$identifier]))
{
if (!$this->$command($commands[$identifier], $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;
}