/**
* 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 array|boolean An array of new IDs on success, boolean false on failure.
*
* @since 1.7
*/
protected function batchCopy($value, $pks, $contexts)
{
// Initialize re-usable member properties, and re-usable local variables
$this->initBatch();
$categoryId = $value;
if (!$this->checkCategoryId($categoryId)) {
return false;
}
$newIds = array();
$db = $this->getDbo();
// Parent exists so 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(Text::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
continue;
}
}
// Check for asset_id
if ($this->table->hasField($this->table->getColumnAlias('asset_id'))) {
$oldAssetId = $this->table->asset_id;
}
$this->generateTitle($categoryId, $this->table);
// Reset the ID because we are making a copy
$this->table->id = 0;
// Unpublish because we are making a copy
if (isset($this->table->published)) {
$this->table->published = 0;
} elseif (isset($this->table->state)) {
$this->table->state = 0;
}
$hitsAlias = $this->table->getColumnAlias('hits');
if (isset($this->table->{$hitsAlias})) {
$this->table->{$hitsAlias} = 0;
}
// New category ID
$this->table->catid = $categoryId;
$event = new BeforeBatchEvent($this->event_before_batch, ['src' => $this->table, 'type' => 'copy']);
$this->dispatchEvent($event);
// @todo: Deal with ordering?
// $this->table->ordering = 1;
// Check the row.
if (!$this->table->check()) {
$this->setError($this->table->getError());
return false;
}
// Store the row.
if (!$this->table->store()) {
$this->setError($this->table->getError());
return false;
}
// Get the new item ID
$newId = $this->table->get('id');
if (!empty($oldAssetId)) {
$dbType = strtolower($db->getServerType());
// Copy rules
$query = $db->getQuery(true);
$query->clear()->update($db->quoteName('#__assets', 't'));
if ($dbType === 'mysql') {
$query->set($db->quoteName('t.rules') . ' = ' . $db->quoteName('s.rules'));
} else {
$query->set($db->quoteName('rules') . ' = ' . $db->quoteName('s.rules'));
}
$query->join('INNER', $db->quoteName('#__assets', 's'), $db->quoteName('s.id') . ' = :oldassetid')->where($db->quoteName('t.id') . ' = :assetid')->bind(':oldassetid', $oldAssetId, ParameterType::INTEGER)->bind(':assetid', $this->table->asset_id, ParameterType::INTEGER);
$db->setQuery($query)->execute();
}
$this->cleanupPostBatchCopy($this->table, $newId, $pk);
// Add the new ID to the array
$newIds[$pk] = $newId;
}
// Clean the cache
$this->cleanCache();
return $newIds;
}