Admin Model

It is a method to change the published state of one or more records.

  • array &$pks - A list of the primary keys to change.
  • integer $value - The value of the published state.
  • return boolean - True on success.
public function publish(&$pks, $value = 1)
{
$dispatcher = \JEventDispatcher::getInstance();
$user = \JFactory::getUser();
$table = $this->getTable();
$pks = (array) $pks;

// Include the plugins for the change of state event.
\JPluginHelper::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]);

\JLog::add(\JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), \JLog::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 (property_exists($table, 'checked_out') && $table->checked_out && ($table->checked_out != $user->id))
{
\JLog::add(\JText::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), \JLog::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]);

continue;
}
}
}

// Check if there are items to change
if (!count($pks))
{
return true;
}

// Attempt to change the state of the records.
if (!$table->publish($pks, $value, $user->get('id')))
{
$this->setError($table->getError());

return false;
}

$context = $this->option . '.' . $this->name;

// Trigger the change state event.
$result = $dispatcher->trigger($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;
}