コード例 #1
0
 /**
  * Publish or unpublish records
  *
  * @param   integer|array  $cid      The primary key value(s) of the item(s) to publish/unpublish
  * @param   integer        $publish  1 to publish an item, 0 to unpublish
  * @param   integer        $user_id  The user ID of the user (un)publishing the item.
  *
  * @return  boolean  True on success, false on failure (e.g. record is locked)
  */
 public function publish($cid = null, $publish = 1, $user_id = 0)
 {
     $enabledName = $this->getColumnAlias('enabled');
     $locked_byName = $this->getColumnAlias('locked_by');
     // Mhm... you called the publish method on a table without publish support...
     if (!in_array($enabledName, $this->getKnownFields())) {
         return false;
     }
     //We have to cast the id as array, or the helper function will return an empty set
     if ($cid) {
         $cid = (array) $cid;
     }
     FOFUtilsArray::toInteger($cid);
     $user_id = (int) $user_id;
     $publish = (int) $publish;
     $k = $this->_tbl_key;
     if (count($cid) < 1) {
         if ($this->{$k}) {
             $cid = array($this->{$k});
         } else {
             $this->setError("No items selected.");
             return false;
         }
     }
     if (!$this->onBeforePublish($cid, $publish)) {
         return false;
     }
     $query = $this->_db->getQuery(true)->update($this->_db->qn($this->_tbl))->set($this->_db->qn($enabledName) . ' = ' . (int) $publish);
     $checkin = in_array($locked_byName, $this->getKnownFields());
     if ($checkin) {
         $query->where(' (' . $this->_db->qn($locked_byName) . ' = 0 OR ' . $this->_db->qn($locked_byName) . ' = ' . (int) $user_id . ')', 'AND');
     }
     // TODO Rewrite this statment using IN. Check if it work in SQLServer and PostgreSQL
     $cids = $this->_db->qn($k) . ' = ' . implode(' OR ' . $this->_db->qn($k) . ' = ', $cid);
     $query->where('(' . $cids . ')');
     $this->_db->setQuery((string) $query);
     if (version_compare(JVERSION, '3.0', 'ge')) {
         try {
             $this->_db->execute();
         } catch (Exception $e) {
             $this->setError($e->getMessage());
         }
     } else {
         if (!$this->_db->execute()) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
     }
     if (count($cid) == 1 && $checkin) {
         if ($this->_db->getAffectedRows() == 1) {
             $this->checkin($cid[0]);
             if ($this->{$k} == $cid[0]) {
                 $this->{$enabledName} = $publish;
             }
         }
     }
     $this->setError('');
     return true;
 }