Back to AdminModel class

Method batch

public bool
batch
(mixed $commands, mixed $pks, mixed $contexts)
Method to perform batch operations on an item or a set of items.
Parameters
  • array $commands An array of commands to perform.
  • array $pks An array of item ids.
  • array $contexts An array of item contexts.
Returns
  • bool Returns true on success, false on failure.
Since
  • 1.7
Class: AdminModel
Project: Joomla

Method batch - Source code

/**
 * 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.7
 */
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(Text::_('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(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
        return false;
    }
    // Clear the cache
    $this->cleanCache();
    return true;
}