public function store($updateNulls = false) { if ($this->exists()) { $ret = static::$db->updateObject(static::$tbl, $this, static::$tbl_keys, $updateNulls); } else { $ret = static::$db->insertObject(static::$tbl, $this, static::$tbl_keys); } if (static::$locked) { $this->unlock(); } if (!$ret) { $this->setError(get_class($this) . '::store failed - ' . static::$db->getErrorMsg()); return false; } $this->_exists = true; return true; }
/** * 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 (JDatabaseException $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; }
function publish($cid = null, $publish = 1, $user_id = 0) { JArrayHelper::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; } $enabledName = $this->getColumnAlias('enabled'); $locked_byName = $this->getColumnAlias('locked_by'); $query = $this->_db->getQuery(true)->update($this->_db->qn($this->_tbl))->set($this->_db->qn($enabledName) . ' = ' . (int) $publish); $checkin = in_array($locked_byName, array_keys($this->getProperties())); if ($checkin) { $query->where(' (' . $this->_db->qn($locked_byName) . ' = 0 OR ' . $this->_db->qn($locked_byName) . ' = ' . (int) $user_id . ')', 'AND'); } $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 (JDatabaseException $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->published = $publish; } } } $this->setError(''); return true; }
/** * Method to import a database schema from a file. * * @param JDatabaseDriver $db JDatabase object. * @param string $schema Path to the schema file. * * @return boolean True on success. * * @since 3.1 */ public function populateDatabase($db, $schema) { // Get the application /* @var InstallationApplicationWeb $app */ $app = JFactory::getApplication(); $return = true; // Get the contents of the schema file. if (!($buffer = file_get_contents($schema))) { $app->enqueueMessage($db->getErrorMsg(), 'notice'); return false; } // Get an array of queries from the schema and process them. $queries = $this->_splitQueries($buffer); foreach ($queries as $query) { // Trim any whitespace. $query = trim($query); // If the query isn't empty and is not a MySQL or PostgreSQL comment, execute it. if (!empty($query) && $query[0] != '#' && $query[0] != '-') { /** * If we don't have UTF-8 Multibyte support we'll have to convert queries to plain UTF-8 * * Note: the JDatabaseDriver::convertUtf8mb4QueryToUtf8 performs the conversion ONLY when * necessary, so there's no need to check the conditions in JInstaller. */ $query = $db->convertUtf8mb4QueryToUtf8($query); /** * This is a query which was supposed to convert tables to utf8mb4 charset but the server doesn't * support utf8mb4. Therefore we don't have to run it, it has no effect and it's a mere waste of time. */ if (!$db->hasUTF8mb4Support() && stristr($query, 'CONVERT TO CHARACTER SET utf8 ')) { continue; } // Execute the query. $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $app->enqueueMessage($e->getMessage(), 'notice'); $return = false; } } } return $return; }
/** * Method to import a database schema from a file. * * @param JDatabaseDriver $db JDatabase object. * @param string $schema Path to the schema file. * * @return boolean True on success. * * @since 3.1 */ public function populateDatabase($db, $schema) { // Get the application /* @var InstallationApplicationWeb $app */ $app = JFactory::getApplication(); $return = true; // Get the contents of the schema file. if (!($buffer = file_get_contents($schema))) { $app->enqueueMessage($db->getErrorMsg(), 'notice'); return false; } // Get an array of queries from the schema and process them. $queries = $this->_splitQueries($buffer); foreach ($queries as $query) { // Trim any whitespace. $query = trim($query); // If the query isn't empty and is not a MySQL or PostgreSQL comment, execute it. if (!empty($query) && $query[0] != '#' && $query[0] != '-') { // Execute the query. $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $app->enqueueMessage($e->getMessage(), 'notice'); $return = false; } } } return $return; }
/** * Gets error message * * @deprecated 2.0 (use exceptions instead) * * @return string The error message for the most recent query */ public function getErrorMsg() { return stripslashes($this->_db->getErrorMsg()); }
/** * Method to import a database schema from a file. * * @param JDatabaseDriver $db JDatabase object. * @param string $schema Path to the schema file. * * @return boolean True on success. * * @since 3.0 */ public function populateDatabase($db, $schema) { // Initialise variables. $return = true; // Get the contents of the schema file. if (!($buffer = file_get_contents($schema))) { $this->setError($db->getErrorMsg()); return false; } // Get an array of queries from the schema and process them. $queries = $this->_splitQueries($buffer); foreach ($queries as $query) { // Trim any whitespace. $query = trim($query); // If the query isn't empty and is not a comment, execute it. if (!empty($query) && $query[0] != '#') { // Execute the query. $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $this->setError($e->getMessage()); $return = false; } } } return $return; }