Desk.php
00001 <?php
00009 class Dsao_Model_Game_Desk extends Dsao_Model_Abstract
00010 {
00016 protected function _init()
00017 {
00018
00019 $this->_setDefaultTable('game_text');
00020 }
00021
00031 public function deleteEmptyRows()
00032 {
00033
00034
00035 $since = time()-Dsao_Registry::get('config')
00036 ->modules->game->desk->deleteEmpty->since;
00037
00038
00039 $sqlWhere = array
00040 (
00041 'content IS NULL',
00042 Dsao_Registry::get('db')
00043 ->quoteInto('timeLastEdited <= FROM_UNIXTIME(?)', $since),
00044 );
00045
00046
00047 $this->_getTable()->delete($sqlWhere);
00048
00049 return true;
00050 }
00051
00062 public function deleteText($idUser, $idText)
00063 {
00064
00065 if ($row = $this->hasChangePermission($idUser, $idText))
00066 {
00067 $row->delete();
00068
00069 return true;
00070 }
00071
00072 return false;
00073 }
00074
00085 public function editText(Dsao_User $user, $idText, $content = '')
00086 {
00087
00088 if ($row = $this->hasChangePermission($user->id, $idText))
00089 {
00090
00091 if ('' === $content)
00092 {
00093 return $this->deleteText($user->id, $idText);
00094 }
00095
00096
00097 $row->content = $content;
00098 $row->save($user);
00099
00100 return true;
00101 }
00102
00103 return false;
00104 }
00105
00117 public function fetchGroupTexts($idGroup, $since, $deleted = false)
00118 {
00119
00120 $sqlSelect = $this->_getTable()->select()
00121 ->order('id DESC')
00122 ->where('idGroup = ?', $idGroup)
00123 ->where('timeCreated > FROM_UNIXTIME(?) OR timeLastEdited > FROM_UNIXTIME(?)', $since);
00124
00125
00126 if (!$deleted)
00127 {
00128 $sqlSelect->where('content IS NOT NULL');
00129 }
00130
00131
00132 $rowsetTexts = $this->_getTable()->fetchAll($sqlSelect);
00133
00134 return $rowsetTexts;
00135 }
00136
00143 public function findTexts(array $ids)
00144 {
00145 return $this->_getTable()->find($ids);
00146 }
00147
00158 public function hasChangePermission($idUser, $idText)
00159 {
00160
00161 if (!$idText || !is_numeric($idText))
00162 {
00163 return false;
00164 }
00165
00166
00170 if ($row = $this->_getTable()->select()->where('id = ?', $idText)
00171 ->fetchRow())
00172 {
00173 return $row;
00174 }
00175
00176 return false;
00177 }
00178
00189 public function insertText($idGroup, $author, $content)
00190 {
00191
00192 if (!$author || !$content)
00193 {
00194 return false;
00195 }
00196
00197
00198 $dataNewText = array
00199 (
00200 'idGroup'=> $idGroup,
00201 'author' => $author,
00202 'content' => $content,
00203 );
00204
00205
00206 return $this->_getTable()->insert($dataNewText);
00207 }
00208 }