/**
* Method to change the published state of one or more records.
*
* @param array &$pks A list of the primary keys to change.
* @param integer $value The value of the published state.
*
* @return boolean True on success.
*
* @since 1.6
*/
public function publish(&$pks, $value = 1)
{
$user = Factory::getUser();
$table = $this->getTable();
$pks = (array) $pks;
$context = $this->option . '.' . $this->name;
// Include the plugins for the change of state event.
PluginHelper::importPlugin($this->events_map['change_state']);
// Access checks.
foreach ($pks as $i => $pk) {
$table->reset();
if ($table->load($pk)) {
if (!$this->canEditState($table)) {
// Prune items that you can't change.
unset($pks[$i]);
Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror');
return false;
}
// If the table is checked out by another user, drop it and report to the user trying to change its state.
if ($table->hasField('checked_out') && $table->checked_out && $table->checked_out != $user->id) {
Log::add(Text::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), Log::WARNING, 'jerror');
// Prune items that you can't change.
unset($pks[$i]);
return false;
}
/**
* Prune items that are already at the given state. Note: Only models whose table correctly
* sets 'published' column alias (if different than published) will benefit from this
*/
$publishedColumnName = $table->getColumnAlias('published');
if (property_exists($table, $publishedColumnName) && $table->get($publishedColumnName, $value) == $value) {
unset($pks[$i]);
}
}
}
// Check if there are items to change
if (!\count($pks)) {
return true;
}
// Trigger the before change state event.
$result = Factory::getApplication()->triggerEvent($this->event_before_change_state, array($context, $pks, $value));
if (\in_array(false, $result, true)) {
$this->setError($table->getError());
return false;
}
// Attempt to change the state of the records.
if (!$table->publish($pks, $value, $user->get('id'))) {
$this->setError($table->getError());
return false;
}
// Trigger the change state event.
$result = Factory::getApplication()->triggerEvent($this->event_change_state, array($context, $pks, $value));
if (\in_array(false, $result, true)) {
$this->setError($table->getError());
return false;
}
// Clear the component's cache
$this->cleanCache();
return true;
}