Back to Nested class

Method publish

public bool
publish
(mixed $pks = null, mixed $state = 1, mixed $userId = 0)
Method to set the publishing state for a node or list of nodes in the database table. The method respects rows checked out by other users and will attempt to checkin rows that it can after adjustments are made. The method will not allow you to set a publishing state higher than any ancestor node and will not allow you to set a publishing state on a node with a checked out child.
Parameters
  • mixed $pks An optional array of primary key values to update. If not set the instance property value is used.
  • int $state The publishing state. eg. [0 = unpublished, 1 = published]
  • int $userId The user id of the user performing the operation.
Returns
  • bool True on success.
Since
  • 1.7.0
-
  • \UnexpectedValueException
Class: Nested
Project: Joomla

Method publish - Source code

/**
 * Method to set the publishing state for a node or list of nodes in the database
 * table.  The method respects rows checked out by other users and will attempt
 * to checkin rows that it can after adjustments are made. The method will not
 * allow you to set a publishing state higher than any ancestor node and will
 * not allow you to set a publishing state on a node with a checked out child.
 *
 * @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.
 *
 * @since   1.7.0
 * @throws  \UnexpectedValueException
 */
public function publish($pks = null, $state = 1, $userId = 0)
{
    $k = $this->_tbl_key;
    $query = $this->_db->getQuery(true);
    $table = $this->_db->quoteName($this->_tbl);
    $published = $this->_db->quoteName($this->getColumnAlias('published'));
    $key = $this->_db->quoteName($k);
    // Sanitize input.
    $pks = ArrayHelper::toInteger($pks);
    $userId = (int) $userId;
    $state = (int) $state;
    // If $state > 1, then we allow state changes even if an ancestor has lower state
    // (for example, can change a child state to Archived (2) if an ancestor is Published (1)
    $compareState = $state > 1 ? 1 : $state;
    // If there are no primary keys set check to see if the instance key is set.
    if (empty($pks)) {
        if ($this->{$k}) {
            $pks = explode(',', $this->{$k});
        } else {
            $e = new \UnexpectedValueException(sprintf('%s::publish(%s, %d, %d) empty.', \get_class($this), implode(',', $pks), $state, $userId));
            $this->setError($e);
            return false;
        }
    }
    // Determine if there is checkout support for the table.
    $checkoutSupport = $this->hasField('checked_out') || $this->hasField('checked_out_time');
    // Iterate over the primary keys to execute the publish action if possible.
    foreach ($pks as $pk) {
        // Get the node by primary key.
        if (!($node = $this->_getNode($pk))) {
            // Error message set in getNode method.
            return false;
        }
        // If the table has checkout support, verify no children are checked out.
        if ($checkoutSupport) {
            // Ensure that children are not checked out.
            $query->clear()->select('COUNT(' . $k . ')')->from($this->_tbl)->where('lft BETWEEN ' . (int) $node->lft . ' AND ' . (int) $node->rgt)->where('(checked_out <> 0 AND checked_out <> ' . (int) $userId . ')');
            $this->_db->setQuery($query);
            // Check for checked out children.
            if ($this->_db->loadResult()) {
                // @todo Convert to a conflict exception when available.
                $e = new \RuntimeException(sprintf('%s::publish(%s, %d, %d) checked-out conflict.', \get_class($this), $pks[0], $state, $userId));
                $this->setError($e);
                return false;
            }
        }
        // If any parent nodes have lower published state values, we cannot continue.
        if ($node->parent_id) {
            // Get any ancestor nodes that have a lower publishing state.
            $query->clear()->select('1')->from($table)->where('lft < ' . (int) $node->lft)->where('rgt > ' . (int) $node->rgt)->where('parent_id > 0')->where($published . ' < ' . (int) $compareState);
            // Just fetch one row (one is one too many).
            $query->setLimit(1);
            $this->_db->setQuery($query);
            if ($this->_db->loadResult()) {
                $e = new \UnexpectedValueException(sprintf('%s::publish(%s, %d, %d) ancestors have lower state.', \get_class($this), $pks[0], $state, $userId));
                $this->setError($e);
                return false;
            }
        }
        $this->recursiveUpdatePublishedColumn($pk, $state);
        // If checkout support exists for the object, check the row in.
        if ($checkoutSupport) {
            $this->checkIn($pk);
        }
    }
    // If the Table instance value is in the list of primary keys that were set, set the instance.
    if (\in_array($this->{$k}, $pks)) {
        $this->published = $state;
    }
    $this->setError('');
    return true;
}