1: <?php
  2: /**
  3:  * Kronolith_Storage:: defines an API for storing free/busy information.
  4:  *
  5:  * @author  Mike Cochrane <mike@graftonhall.co.nz>
  6:  * @author  Michael J Rubinsky <mrubinsk@horde.org>
  7:  * @package Kronolith
  8:  */
  9: class Kronolith_Storage_Sql extends Kronolith_Storage
 10: {
 11:     /**
 12:      * Handle for the current database connection, used for reading.
 13:      *
 14:      * @var Horde_Db_Adapter
 15:      */
 16:     protected $_db;
 17: 
 18:     /**
 19:      * Hash containing connection parameters.
 20:      *
 21:      * @var array
 22:      */
 23:     protected $_params = array();
 24: 
 25:     /**
 26:      * Constructs a new Kronolith_Storage SQL instance.
 27:      *
 28:      * @param string $user   The user the fb info belongs to.
 29:      * @param array $params  A hash containing connection parameters.
 30:      *
 31:      * @return Kronolith_Storage_Sql
 32:      */
 33:     public function __construct($user, array $params = array())
 34:     {
 35:         $this->_user = $user;
 36:         if (empty($params['db'])) {
 37:             throw new InvalidArgumentException('Missing required db parameter');
 38:         }
 39: 
 40:         $this->_db = $params['db'];
 41:         $this->_params = $params;
 42:         $this->_params['table'] = isset($params['table']) ? $params['table'] : 'kronolith_storage';
 43:     }
 44: 
 45:     /**
 46:      * Search for a user's free/busy information.
 47:      *
 48:      * @param string  $email        The email address to lookup
 49:      * @param boolean $private_only (optional) Only return free/busy
 50:      *                              information owned by this used.
 51:      *
 52:      * @return Horde_Icalendar_Vfreebusy
 53:      * @throws Kronolith_Exception
 54:      */
 55:     public function search($email, $private_only = false)
 56:     {
 57:         /* Build the SQL query. */
 58:         $query = sprintf('SELECT vfb_serialized FROM %s WHERE vfb_email = ? AND (vfb_owner = ?',
 59:                          $this->_params['table']);
 60:         $values = array($email, $this->_user);
 61: 
 62:         if ($private_only) {
 63:             $query .= ')';
 64:         } else {
 65:             $query .= " OR vfb_owner = '')";
 66:         }
 67: 
 68:         /* Execute the query. */
 69:         try {
 70:             $result = $this->_db->selectValue($query, $values);
 71:             if (empty($result)) {
 72:                 throw new Horde_Exception_NotFound();
 73:             }
 74:             return Horde_Serialize::unserialize($result, Horde_Serialize::BASIC);
 75:         } catch (Horde_Db_Exception $e) {
 76:             throw new Kronolith_Exception($e);
 77:         }
 78:     }
 79: 
 80:     /**
 81:      * Store the freebusy information for a given email address.
 82:      *
 83:      * @param string                     $email        The email address to store fb info for.
 84:      * @param Horde_Icalendar_Vfreebusy  $vfb          TODO
 85:      * @param boolean                    $private_only (optional) TODO
 86:      *
 87:      * @throws Kronolith_Exception
 88:      */
 89:     public function store($email, $vfb, $public = false)
 90:     {
 91:         /* Build the SQL query. */
 92:         $query = sprintf('INSERT INTO %s (vfb_owner, vfb_email, vfb_serialized) VALUES (?, ?, ?)',
 93:                          $this->_params['table']);
 94:         $values = array($public ? '' : $this->_user, $email, Horde_Serialize::serialize($vfb, Horde_Serialize::BASIC));
 95: 
 96:         /* Execute the query. */
 97:         try {
 98:             $result = $this->_db->insert($query, $values);
 99:         } catch (Horde_Db_Exception $e) {
100:             throw new Kronolith_Exception($e);
101:         }
102:     }
103: 
104: }
105: