API16

JTableNested/publish

From Joomla! Documentation

< API16:JTableNested

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

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 now 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.



Syntax[edit]

publish($pks=null, $state=1, $userId=0)
Parameter Name Default Value Description
$pks null An optional array of primary key values to update. If not set the instance property value is used.
$state 1 The publishing state. eg. [0 = unpublished, 1 = published]
$userId 0 The user id of the user performing the operation.

Returns[edit]

boolean True on success.

Defined in[edit]

libraries/joomla/database/tablenested.php

Importing[edit]

jimport( 'joomla.database.tablenested' );

Source Body[edit]

public function publish($pks = null, $state = 1, $userId = 0)
{
        // Initialise variables.
        $k = $this->_tbl_key;

        // Sanitize input.
        JArrayHelper::toInteger($pks);
        $userId = (int) $userId;
        $state  = (int) $state;

        // If there are no primary keys set check to see if the instance key is set.
        if (empty($pks)) {
                if ($this->$k) {
                        $pks = array($this->$k);
                }
                // Nothing to set publishing state on, return false.
                else {
                        $this->setError(JText::_('JERROR_NO_ROWS_SELECTED'));
                        return false;
                }
        }

        // Determine if there is checkout support for the table.
        if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
                $checkoutSupport = false;
        } else {
                $checkoutSupport = true;
        }

        // 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.
                        $this->_db->setQuery(
                                'SELECT COUNT('.$this->_tbl_key.')' .
                                ' FROM `'.$this->_tbl.'`' .
                                ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt .
                                ' AND (checked_out <> 0 AND checked_out <> '.(int) $userId.')'
                        );

                        // Check for checked out children.
                        if ($this->_db->loadResult()) {
                                $this->setError('Child_Rows_Checked_Out');
                                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.
                        $this->_db->setQuery(
                                'SELECT p.'.$k .
                                ' FROM `'.$this->_tbl.'` AS n, `'.$this->_tbl.'` AS p' .
                                ' WHERE n.lft BETWEEN p.lft AND p.rgt' .
                                ' AND n.'.$k.' = '.(int) $pk .
                                ' AND p.parent_id > 0' .
                                ' AND p.published < '.(int) $state .
                                ' ORDER BY p.lft DESC',
                                1, 0
                        );
                        $rows = $this->_db->loadResultArray();

                        // Check for a database error.
                        if ($this->_db->getErrorNum()) {
                                $this->setError($this->_db->getErrorMsg());
                                return false;
                        }

                        if (!empty($rows)) {
                                $this->setError('Ancestor_Nodes_Lower_Published_State');
                                return false;
                        }
                }

                // Update the publishing state.
                $this->_db->setQuery(
                        'UPDATE `'.$this->_tbl.'`' .
                        ' SET `published` = '.(int) $state .
                        ' WHERE `'.$this->_tbl_key.'` = '.(int) $pk
                );
                $this->_db->query();

                // Check for a database error.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }

                // If checkout support exists for the object, check the row in.
                if ($checkoutSupport) {
                        $this->checkin($pk);
                }
        }

        // If the JTable 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->_errors = array();
        return true;
}



Examples[edit]

Code Examples[edit]