/**
* Method to set the publishing state for a row or list of rows in the database table.
*
* The method respects checked out rows by other users and will attempt to checkin rows that it can after adjustments are made.
*
* @param mixed $pks An optional array of primary key values to update. If not set the instance property value is used.
* @param integer $state The publishing state. eg. [0 = unpublished, 1 = published]
* @param integer $userId The user ID of the user performing the operation.
*
* @return boolean True on success; false if $pks is empty.
*
* @since 1.7.0
*/
public function publish($pks = null, $state = 1, $userId = 0)
{
// Sanitize input
$userId = (int) $userId;
$state = (int) $state;
// Pre-processing by observers
$event = AbstractEvent::create('onTableBeforePublish', ['subject' => $this, 'pks' => $pks, 'state' => $state, 'userId' => $userId]);
$this->getDispatcher()->dispatch('onTableBeforePublish', $event);
if (!\is_null($pks)) {
if (!\is_array($pks)) {
$pks = array($pks);
}
foreach ($pks as $key => $pk) {
if (!\is_array($pk)) {
$pks[$key] = array($this->_tbl_key => $pk);
}
}
}
// If there are no primary keys set check to see if the instance key is set.
if (empty($pks)) {
$pk = array();
foreach ($this->_tbl_keys as $key) {
if ($this->{$key}) {
$pk[$key] = $this->{$key};
} else {
$this->setError(Text::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
return false;
}
}
$pks = array($pk);
}
$publishedField = $this->getColumnAlias('published');
$checkedOutField = $this->getColumnAlias('checked_out');
foreach ($pks as $pk) {
// Update the publishing state for rows with the given primary keys.
$query = $this->_db->getQuery(true)->update($this->_tbl)->set($this->_db->quoteName($publishedField) . ' = ' . (int) $state);
// If publishing, set published date/time if not previously set
if ($state && $this->hasField('publish_up') && (int) $this->publish_up == 0) {
$nowDate = $this->_db->quote(Factory::getDate()->toSql());
$query->set($this->_db->quoteName($this->getColumnAlias('publish_up')) . ' = ' . $nowDate);
}
// Determine if there is checkin support for the table.
if ($this->hasField('checked_out') || $this->hasField('checked_out_time')) {
$query->where('(' . $this->_db->quoteName($checkedOutField) . ' = 0' . ' OR ' . $this->_db->quoteName($checkedOutField) . ' = ' . (int) $userId . ' OR ' . $this->_db->quoteName($checkedOutField) . ' IS NULL' . ')');
$checkin = true;
} else {
$checkin = false;
}
// Build the WHERE clause for the primary keys.
$this->appendPrimaryKeys($query, $pk);
$this->_db->setQuery($query);
try {
$this->_db->execute();
} catch (\RuntimeException $e) {
$this->setError($e->getMessage());
return false;
}
// If checkin is supported and all rows were adjusted, check them in.
if ($checkin && \count($pks) == $this->_db->getAffectedRows()) {
$this->checkIn($pk);
}
// If the Table instance value is in the list of primary keys that were set, set the instance.
$ours = true;
foreach ($this->_tbl_keys as $key) {
if ($this->{$key} != $pk[$key]) {
$ours = false;
}
}
if ($ours) {
$this->{$publishedField} = $state;
}
}
$this->setError('');
// Pre-processing by observers
$event = AbstractEvent::create('onTableAfterPublish', ['subject' => $this, 'pks' => $pks, 'state' => $state, 'userId' => $userId]);
$this->getDispatcher()->dispatch('onTableAfterPublish', $event);
return true;
}