Scroll.php
00001 <?php
00009 class Dsao_Scroll extends Dsao_Db_Table_DataHandler_Abstract
00010 {
00011 protected $_identifiers = array('id');
00012
00013 protected $_propertiesProtected = array('id');
00014
00015 protected $_table = 'scroll_header';
00016
00017 protected function _checkValidity()
00018 {
00019 return true;
00020 }
00021
00027 protected function _delete()
00028 {
00029
00030 $this->getTable()->delete
00031 (array('id = '. (int) $this->__get('id')));
00032 }
00033
00045 public function delete($idUser = 0, $delete = true)
00046 {
00047 parent::delete();
00048
00049
00050 if (!$delete)
00051 {
00052 $this->__set('deleted', 0);
00053
00054
00055 $this->getTable()->update
00056 (array('deleted' => 0), 'id = '.(int) $this->__get('id'));
00057
00058 return $this;
00059 }
00060
00061
00062 if (0 === $idUser && $delete)
00063 {
00064 $this->_delete();
00065
00066 return $this;
00067 }
00068
00069
00070
00071 $intOther = 0;
00072
00073
00074
00075 $intSelf = 0;
00076
00077
00078
00079 if ($this->__get('idAuthor') == $idUser)
00080 {
00081 $intSelf = 1;
00082 }
00083
00084 else if ($this->__get('idRecipient') == $idUser)
00085 {
00086 $intSelf = 2;
00087 }
00088
00089 else
00090 {
00091 return $this;
00092 }
00093
00094
00095 $intOther = ($intSelf == 1 ? 2 : 1);
00096
00097
00098 if ($intOther == $this->__get('deleted'))
00099 {
00100 $this->_delete();
00101 }
00102 else
00103 {
00104 $this->__set('deleted', $intSelf);
00105
00106
00107 Dsao_Registry::getTable('scroll_header')->update
00108 (array('deleted' => $intSelf), 'id = '.(int) $this->__get('id'));
00109 }
00110
00111 return $this;
00112 }
00113
00120 public function getOtherUser($idUser)
00121 {
00122 $idUser = (int) $idUser;
00123
00124
00125 $userOther = new Dsao_User();
00126
00127
00128 if ($this->isParticipated($idUser))
00129 {
00130
00131 if ($this->__get('idAuthor') == $idUser)
00132 {
00133 $idUserOther = $this->__get('idRecipient');
00134 $usernameOther = $this->__get('nameRecipient');
00135 }
00136 else
00137 {
00138 $idUserOther = $this->__get('idAuthor');
00139 $usernameOther = $this->__get('nameAuthor');
00140 }
00141
00142 $userOther->setId($idUserOther);
00143 $userOther->username = $usernameOther;
00144 $userOther->setValid(true);
00145 }
00146
00147 return $userOther;
00148 }
00149
00150 public function insert()
00151 {
00152
00153 $rowHeader = $this->getTable()
00154 ->createRow($this->_data);
00155 $rowContent = $this->getTable('scroll_content')
00156 ->createRow($this->_data);
00157
00158
00159 $this->getTable()->getAdapter()->beginTransaction();
00160
00161
00162 $rowContent->id = $rowHeader->save();
00163
00164
00165 $rowContent->save();
00166
00167
00168 $this->getTable()->getAdapter()->commit();
00169
00170
00171 return $this->setFromArray($rowHeader->toArray() + $rowContent->toArray())
00172 ->setSynchronous();
00173 }
00174
00181 public function isAuthor($idUser)
00182 {
00183 return ($idUser == $this->__get('idAuthor'));
00184 }
00185
00192 public function isDeleted($idUser = null)
00193 {
00194
00195 if (!$this->__isset('deleted'))
00196 {
00197 return null;
00198 }
00199
00200
00201
00202 if (null === $idUser)
00203 {
00204 return ($this->__get('deleted') > 0);
00205 }
00206
00207
00208 if (!$this->isParticipated($idUser))
00209 {
00210 return false;
00211 }
00212
00213 return ($this->isAuthor($idUser) ?
00214
00215 ($this->__get('deleted') == 1) :
00216
00217 ($this->__get('deleted') == 2));
00218 }
00219
00226 public function isParticipated($idUser)
00227 {
00228 $idUser = (int) $idUser;
00229
00230 return ($this->__get('idAuthor') == $idUser
00231 || $this->__get('idRecipient') == $idUser);
00232 }
00233
00239 public function isRead()
00240 {
00241 return ($this->__isset('read') ? (bool) $this->__get('read') : false);
00242 }
00243
00250 public function isRecipient($idUser)
00251 {
00252 return ($idUser == $this->__get('idRecipient'));
00253 }
00254
00262 public function markRead($idUser = 0, $read = true)
00263 {
00264
00265 $this->isReadOnly(true);
00266 $this->isValid(true);
00267 $this->isSynchronous(true);
00268
00269
00270
00271 $read = (int) (bool) $read;
00272
00273
00274 if (($read != $this->__get('read'))
00275 && (!$idUser || $idUser == $this->__get('idRecipient')))
00276 {
00277 $sqlWhere = 'id = '. (int) $this->__get('id');
00278
00279 $this->getTable()->update(array('read' => $read), $sqlWhere);
00280
00281 $this->__set('read', $read);
00282 }
00283
00284 return $this;
00285 }
00286
00293 public function refresh($content = true)
00294 {
00295 parent::refresh();
00296
00297 $userData = null;
00298
00299
00300 if ($content)
00301 {
00302
00303 $select = $this->getTable()->getAdapter()->select()
00304 ->from('scroll_header')
00305 ->joinUsing('scroll_content', 'id')
00306 ->where('`user_data`.id = ?', $this->__get('id'));
00307
00308 $data = $this->getTable()->getAdapter()->fetchAll($select);
00309
00310 $this->_parseResult(array_pop($data));
00311 }
00312 else
00313 {
00314
00315 $row = $this->getTable()->select()
00316 ->where('id = ?', $this->__get('id'))->fetchRow();
00317
00318 $this->_parseResult($row, false);
00319
00320 $this->setValid();
00321 }
00322
00323 return $this;
00324 }
00325
00331 public function toArrayDecoded()
00332 {
00333
00334 $data = $this->_data;
00335
00336
00337 if ($this->__isset('content'))
00338 {
00339 $filter = new Dsao_Filter_Texts();
00340
00341
00342 $data['content'] = $filter->filter($this->__get('content'));
00343 }
00344
00345
00346 if ($this->__isset('date'))
00347 {
00348 $date = new Dsao_Time($this->__get('date'));
00349
00350
00351 $data['timestamp'] = strtotime($data['date'], 0);
00352
00353
00354 $data['date'] = $date->format();
00355 }
00356
00357 return $data;
00358 }
00359 }