User.php
00001 <?php
00012 class Dsao_Hash_User extends Dsao_Hash
00013 {
00017 protected $_row = null;
00018
00022 protected $_uri = null;
00023
00039 protected function __construct($hash = null, $length = 32, array $parameters = array(), $uri = '')
00040 {
00041
00042 if (is_object($uri))
00043 {
00044 $this->_uri = $uri;
00045 $this->_uri->setParameters($parameters, true);
00046 }
00047
00048 else
00049 {
00050 $this->_uri = new Dsao_Uri_Http($uri, $parameters);
00051 }
00052
00053
00054 if (null === $hash)
00055 {
00056 $hash = $this->_uri->hash;
00057 }
00058
00059
00060 parent::__construct($hash, $length);
00061 }
00062
00069 public function __get($parameter)
00070 {
00071 return $this->_uri->__get($parameter);
00072 }
00073
00081 public function __set($parameter, $value)
00082 {
00083
00084 $this->isReadOnly(true);
00085
00086
00087 return $this->_uri->__set($parameter, $value);
00088 }
00089
00096 protected function _findRow($again = false)
00097 {
00098
00099 if (null !== $this->_row && !$again)
00100 {
00101 return;
00102 }
00103
00104
00105 if (!$this->_uri->idHash)
00106 {
00107 $this->_row = Dsao_Registry::getTable('user_hash')->select()
00108 ->where('idUser = ?', $this->_uri->idUser)
00109 ->where('hash = ?', $this->getHash())->fetchRow();
00110
00111
00112 if ($this->_row)
00113 {
00114 $this->_uri->idHash = $this->_row->id;
00115 }
00116 }
00117 else
00118 {
00119 $this->_row = Dsao_Registry::getTable('user_hash')
00120 ->find($this->_uri->idHash)->current();
00121 }
00122 }
00123
00132 public function deleteDuplicates()
00133 {
00134
00135 if (!$this->isValid(null))
00136 {
00137 throw new Dsao_Exception('dsao_hash_user_object_invalid');
00138 }
00139
00140
00141 $sqlWhere = 'idUser = '. (int) $this->_row->idUser.' AND '.
00142 Dsao_Registry::get('db')->quoteInto
00143 ('hashAction = ?', $this->_row->hashAction).' AND '.
00144 'id != '. (int) $this->_row->id;
00145
00146
00147 Dsao_Registry::getTable('user_hash')->delete($sqlWhere);
00148
00149 return $this;
00150 }
00151
00157 public function deleteRow()
00158 {
00159
00160 if ($this->isValid(null))
00161 {
00162 $this->_row->delete();
00163 $this->_row = null;
00164 }
00165
00166 return $this;
00167 }
00168
00178 public static function factory($hash = null, $uri = '', array $parameters = array(), $length = 32)
00179 {
00180
00181 return new self($hash, $length, $parameters, $uri);
00182 }
00183
00193 public static function fromText($text = null, $uri = '', array $parameters = array(), $length = 32)
00194 {
00195 $hash = new self(null, $length, $parameters, $uri);
00196
00197 $hash->_generateHash($text);
00198
00199 return $hash;
00200 }
00201
00207 public function getUri()
00208 {
00209 return $this->_uri;
00210 }
00211
00218 public function hasDuplicates()
00219 {
00220
00221 $this->isReadOnly(true);
00222
00223
00224 return (bool) Dsao_Registry::getTable('user_hash')->select(true)
00225 ->reset(Zend_Db_Select::COLUMNS)
00226 ->columns(new Zend_Db_Expr('COUNT(*) AS duplicates'))
00227 ->where('idUser = ?', (int) $this->_row->idUser)
00228 ->where('hashAction = ?', $this->_row->hashAction)
00229 ->where('id != ?', $this->_row->id)
00230 ->fetchRow()->duplicates;
00231 }
00232
00245 public function isValid($parametersNames = true, $recheck = false)
00246 {
00247
00248 if ($recheck || null === $this->_row)
00249 {
00250 $this->_findRow();
00251 }
00252
00253
00254 if (!$this->_row || null === $parametersNames)
00255 {
00256 return $this->_row;
00257 }
00258
00259
00260 if (!is_array($parametersNames))
00261 {
00262 $parametersNames = array($parametersNames);
00263 }
00264
00265
00266 if (empty($parametersNames))
00267 {
00268 $parametersNames = array(false);
00269 }
00270
00271 else if ($parametersNames[0] === true)
00272 {
00273 $parametersNames = array_merge($parametersNames,
00274 array('hash', 'idHash'));
00275 }
00276
00277
00278 if ($this->_row->idUser != $this->_uri->idUser
00279
00280 || !parent::factory($this->_row->hash)->compareTo($this)
00281
00282 || !parent::factory($this->_row->hashAction)->compareTo
00283 (parent::fromText($this->_uri
00284 ->getUriString($parametersNames))))
00285 {
00286 return false;
00287 }
00288
00289 return true;
00290 }
00291
00297 public function insert()
00298 {
00299
00300 if ($this->isValid(null))
00301 {
00302 throw new Dsao_Exception('dsao_hash_user_hash_inserted');
00303 }
00304
00305
00306 if (!$this->_uri->idUser)
00307 {
00308 throw new Dsao_Exception('dsao_hash_user_id_missing');
00309 }
00310
00311
00312 $this->_uri->hash = $this->getHash();
00313
00314
00315 $hashAction = parent::fromText($this->_uri->
00316 getUriString(array(true, 'hash', 'idHash')), null, $this->_length);
00317
00318
00319 $data = array(
00320 'idUser' => (int) $this->_uri->idUser,
00321 'hash' => $this->getHash(),
00322 'hashAction' => $hashAction->getHash(),
00323 );
00324
00325 $this->_uri->idHash = Dsao_Registry::getTable('user_hash')->insert($data);
00326
00327 return $this;
00328 }
00329 }