* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource'); /** * 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 * * Supports mandator abstraction for multi column primary ids * * @version 0.3.3 * @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; /** * Mandator * @var WBMandator */ protected $mandator; /** * 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; /** @var WBConfig */ $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()) { if (empty($parameter['table'])) { $parameter['table'] = WBClass::create('WBDatasource_Table'); } $this->mandator = WBClass::create('WBMandator', $parameter); $this->table = $parameter['table']; $this->uPrimary = $this->table->getIdentifier(WBDatasource::TABLE_USER); $this->mandator->inject2Clause($this->clause); // $this->clause[2]['field'] = $this->table->getIdentifier(WBDatasource::TABLE_MANDATOR); $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) { $xid = explode('-', $id); if (2 > count($xid)) { $xid = array(0, $xid[0]); } if ($this->clause[1]['value'] == $xid[1] ) { if ($this->mandator->isEnabled()) { if ($this->clause[2]['value'] == $xid[0]) { return $this; } } else { 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, null, array(), array('deleted' => 1)); 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'] = $xid[1]; if ($this->mandator->isEnabled()) { $this->clause[2]['value'] = $xid[0]; } $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() { if ($this->mandator->isEnabled()) { $primary = $this->table->getIdentifier($this->clause[0]['value']); if (is_array($primary) && in_array($this->mandator->getIdentifier(), $primary)) { return $this->clause[2]['value'] . '-' . $this->clause[1]['value']; } } 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'] = ''; } if ($this->mandator->isEnabled()) { $this->clause[2]['value'] = 0; } return $this; } }