_params = $params; } function &singleton() { // array of instances static $instances; // driver configuration $driver = $GLOBALS['conf']['storage']['driver']; $params = Horde::getDriverConfig('storage', $driver); if (!isset($instances)) { $instances = array(); } // set up this driver configuration $signature = serialize(array($driver, $params)); if (!isset($instances[$signature])) { $instances[$signature] = &new PData($params); } return $instances[$signature]; } function getByCategoryAndLimit($category, $limit) { $this->_connect(); // Build up the query $query = sprintf('SELECT * FROM penguin_data WHERE category=%s ' . 'LIMIT %d', $this->_db->quote($category)); // Log and submit query Horde::logMessage(sprintf('PData::getByCategoryAndLimit %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getAll($query, array(), DB_FETCHMODE_ASSOC); } function getByTitle($title) { $this->_connect(); // Build up the query $query = sprintf('SELECT * FROM penguin_data WHERE title=%s', $this->_db->quote($category)); // Log and submit query Horde::logMessage(sprintf('PData::getByTitle %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getAll($query, array(), DB_FETCHMODE_ASSOC); } function getByCategory($category, $order = 'id', $direction = 'ASC') { $this->_connect(); // Build up the query $query = sprintf('SELECT * FROM penguin_data WHERE category=%s ORDER ' . 'BY %s %s', $this->_db->quote($category), $order, $direction); // Log and submit query Horde::logMessage(sprintf('PData::getByCategory %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getAll($query, array(), DB_FETCHMODE_ASSOC); } function getById($id) { $this->_connect(); // Build up the query $query = sprintf('SELECT * FROM penguin_data WHERE id=%d', $id); // Log and submit query Horde::logMessage(sprintf('PData::getById %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getRow($query, array(), DB_FETCHMODE_ASSOC); } function getJournalById($id, $oper, $order) { $this->_connect(); // Build up the query $query = sprintf('SELECT * FROM penguin_data WHERE id%s%d AND ' . 'category="Journal" ORDER BY id %s LIMIT 1', $oper, $id, $order); // Log and submit query Horde::logMessage(sprintf('PData::getJournalById %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getRow($query, array(), DB_FETCHMODE_ASSOC); } function getNewestJournals() { $this->_connect(); $query = 'SELECT * FROM penguin_data WHERE category = "Journal" ORDER ' . 'BY id DESC LIMIT 5'; // db log/query Horde::logMessage(sprintf('PData::getNewestJournal %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->getAll($query, array(), DB_FETCHMODE_ASSOC); } function getMaxJournalId() { $this->_connect(); $query = 'SELECT MAX(id) FROM penguin_data WHERE category = "Journal"'; Horde::logMessage(sprintf('PDATA::getMaxJournalId %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->getRow($query); if(is_a('PEAR_Error', $result)) { $result = -1; } else { $result = $result[0]; } return $result; } function getMinJournalId() { $this->_connect(); $query = 'SELECT MIN(id) FROM penguin_data WHERE category = "Journal"'; Horde::logMessage(sprintf('PDATA::getMinJournalId %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->getRow($query); if(is_a('PEAR_Error', $result)) { $result = -1; } else { $result = $result[0]; } return $result; } function add($values = array()) { // sanity checks if (!isset($values['title'])) { return PEAR::raiseError(_("No title is set!")); } if (!isset($values['info'])) { $values['info'] = ''; } if (!isset($values['attributes']) || !is_array($values['attributes'])) { $values['attributes'] = array(); } // db init $this->_connect(); require_once 'Horde/SQL.php'; $query = sprintf('INSERT INTO penguin_data %s', Horde_SQL::insertValues( $this->_db, $values)); // db log/query Horde::logMessage(sprintf('PData::add %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->query($query); } function update($id, $values = array()) { // sanity checks if (isset($values['id'])) { unset($values['id']); } // db init $this->_connect(); require_once 'Horde/SQL.php'; $query = sprintf('UPDATE penguin_data SET %s WHERE id=%d', Horde_SQL::updateValues($this->_db, $values), $id); // db log/query Horde::logMessage(sprintf('PData::update %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->query($query); } function delete($id) { $this->_connect(); $query = sprintf('DELETE FROM penguin_data WHERE id=%d', $id); Horde::logMessage(sprintf('PData::delete %s', $query), __FILE__, __LINE__, PEAR_LOG_DEBUG); return $this->_db->query($query); } function _connect() { // yay persistent connections if (!$this->_connected) { Horde::assertDriverConfig($this->_params, 'storage', array('phptype', 'hostspec', 'username', 'database', 'charset')); require_once 'DB.php'; $this->_db = &DB::connect($this->_params, array('persistent' => !empty( $this->_params['persistent']))); if (is_a($this->_db, 'PEAR_Error')) { Horde::fatal($this->_db, __FILE__, __LINE__); } $this->_db->setOption('optimize', 'portability'); $this->_connected = true; } return true; } }