* @license PHP License * @package WB * @subpackage db */ /** * Store reference to something else * * Manage storage of something for anything related to other tables. Hence, this allows * to comment or ratings blog entries as well as users or uploaded files * * @version 0.2.0 * @package WB * @subpackage db */ class WBDatasource_XReference extends WBStdClass { /** * table object * @var WBDatasource_Table */ protected $table; /** * prepared clause * @var array */ protected $clause = array( array( 'field' => 'namespace', 'value' => '', ), array( 'field' => 'xid', 'value' => '', ) ); /** * config loader * @var array */ static protected $conf = array( 'default' => array(), 'namespaces' => array() ); /** * user id for comments * @var string */ protected $uid = 0; /** * list of user groups the current user is member of * @var array */ protected $uGroups = array(); /** * user table's primary key * @var string */ protected $uPrimary; /** * static constructor helper * * Load configuration into static property and merge it with * default values. * @param array $defaultConfig * @param string $name */ static protected function staticConstructLoader($defaultConfig, $name) { self::$conf = $defaultConfig; $config = WBClass::create('WBConfig'); $config->load($name); // default config $default = $config->get($name . '/default', array()); if (!is_array($default)) { $default = array(); } self::$conf['default'] = array_merge(self::$conf['default'], $default); // merge namespace config with default values $namespaces = $config->get($name . '/namespaces', array()); if (!is_array($namespaces)) { $namespaces = array(); } foreach ($namespaces as $ns => $c) { if (empty($ns)) { continue; } if (!is_array($c)) { $c = array(); } $c = array_merge(self::$conf['default'], $c); self::$conf['namespaces'][$ns] = $c; } } /** * craete table object * * @param array $parameter */ public function __construct($parameter = array()) { $this->table = WBClass::create('WBDatasource_Table'); $this->uPrimary = $this->table->getIdentifier('user'); $this->uid = 0; $this->uGroups = array(); // act as user if (!isset($parameter['user']) || empty($parameter['user'])) { WBClass::load('WBUser'); $user = WBUser::getCurrent(); if ($user->isAuthenticated()) { $this->uid = $user->getId(); $this->uGroups = $user->getGroups(); } return; } $user = WBClass::create('WBUser'); if (!$user->load($parameter['user'])) { return; } $this->uid = $user->getId(); $this->uGroups = $user->getGroups(); } /** * set namespace string * * @param string $ns * @return WBDatasource_Comment */ public function setNamespace($ns) { if ($this->clause[0]['value'] == $ns) { return $this; } $this->xuid = 0; // verify namespace config if (!isset(self::$conf['namespaces'][$ns])) { WBClass::load('WBException_Config'); throw new WBException_Config('Namespace "' . $ns . '" is not configured.', 1, __CLASS__); } // verify table $primary = $this->table->getIdentifier($ns); if (empty($primary)) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Namespace must be a table with primary key.', 1, __CLASS__); } $this->clause[0]['value'] = $ns; return $this; } /** * get current namespace * * Namespace might be set explicitly or using by loading a special * comment by id * * @see getXid() * @see load() * @return string */ public function getNamespace() { return $this->clause[0]['value']; } /** * set reference id * * @param string $id * @return WBDatasource_Comment */ public function setXId($id) { if ($this->clause[1]['value'] == $id) { return $this; } $this->xuid = 0; // verify xid if (empty($this->clause[0]['value'])) { WBClass::load('WBException_Call'); throw new WBException_Call('There is no namespace set, yet. Use setNamespace() first.', 1, __CLASS__); } $source = $this->table->get($this->clause[0]['value'], $id); if (count($source) != 1) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Could not identify record from namespace using given XId (' . $id . ').', 2, __CLASS__); } $this->clause[1]['value'] = $id; $source = array_shift($source); if (isset($source[$this->uPrimary])) { $this->xuid = $source[$this->uPrimary]; } return $this; } /** * get current xid * * Xid might be set explicitly or using by loading a special * comment by id * * @see getNamespace() * @see load() * @return string */ public function getXid() { return $this->clause[1]['value']; } /** * reset namespace and xid * * @return WBDatasource_XReference */ public function reset() { $this->xuid = 0; foreach ($this->clause as $i => $c) { $this->clause[$i]['value'] = ''; } return $this; } }