Scrollset.php
00001 <?php
00009 class Dsao_Scrollset extends ArrayObject
00010 {
00014 protected $_sqlSelect = null;
00015
00022 public function __construct(array $scrolls = array())
00023 {
00024
00025 $this->appendArray($scrolls);
00026 }
00027
00034 public function appendScroll(Dsao_Scroll $scroll)
00035 {
00036
00037 if ($scroll->id)
00038 {
00039 $this->offsetSet($scroll->id, $scroll);
00040 }
00041
00042 return $this;
00043 }
00044
00051 public function appendArray(array $scrolls)
00052 {
00053 foreach ($scrolls as $scroll)
00054 {
00055 $this->appendScroll($scroll);
00056 }
00057
00058 return $this;
00059 }
00060
00068 public static function compareThreads($thread1, $thread2)
00069 {
00070
00071 $lastScroll1 = array_pop($thread1);
00072 $lastScroll2 = array_pop($thread2);
00073
00074
00075 if ($lastScroll1['timestamp'] == $lastScroll2['timestamp'])
00076 {
00077 return 0;
00078 }
00079
00080 return ($lastScroll1['timestamp'] > $lastScroll2['timestamp'] ? -1 : 1);
00081 }
00082
00089 public function delete($idUser = 0)
00090 {
00091
00092 foreach ($this->getIterator() as $scroll)
00093 {
00094 $scroll->delete($idUser);
00095 }
00096
00097 return $this;
00098 }
00099
00108 public function fetchScrolls(array $sqlWhere = array(), $loadContents = true, $reset = true)
00109 {
00110
00111 if (null === $this->_sqlSelect)
00112 {
00113 $this->_sqlSelect = Dsao_Registry::get('db')->select()
00114 ->from('scroll_header');
00115
00116 if ($loadContents)
00117 {
00118 $this->_sqlSelect->joinUsing('scroll_content', 'id', 'content');
00119 }
00120 }
00121
00122 else if (!empty($sqlWhere))
00123 {
00124 $this->_sqlSelect->reset('WHERE');
00125 }
00126
00127
00128 foreach ($sqlWhere as $condition => $value)
00129 {
00130
00131 if (is_int($condition))
00132 {
00133 $this->_sqlSelect->where($value);
00134 }
00135 else
00136 {
00137 $this->_sqlSelect->where($condition, $value);
00138 }
00139 }
00140
00141
00142 if ($reset)
00143 {
00144
00145 $this->exchangeArray(array());
00146 }
00147
00148
00149 $scrolls = Dsao_Registry::get('db')->fetchAll($this->_sqlSelect);
00150
00151
00152 foreach ($scrolls as $scroll)
00153 {
00154 $this->offsetSet($scroll['id'], new Dsao_Scroll($scroll, false, true));
00155 }
00156
00157 return $this;
00158 }
00159
00166 public function getScroll($idScroll)
00167 {
00168
00169 if ($this->offsetExists($idScroll))
00170 {
00171 return $this->offsetGet($idScroll);
00172 }
00173
00174
00175 return new Dsao_Scroll(array('id' => $idScroll));
00176 }
00177
00185 public function getThreads($toArrayDecoded = false)
00186 {
00187
00188 $threads = array();
00189
00190 foreach ($this->getIterator() as $idScroll => $scroll)
00191 {
00192
00193 if ($toArrayDecoded)
00194 {
00195 $scrollReturn = $scroll->toArrayDecoded();
00196 }
00197 else
00198 {
00199 $scrollReturn = $scroll;
00200 }
00201
00202
00203 if (0 == $scroll->idThread)
00204 {
00205 $threads[$scroll->id][0] = $scrollReturn;
00206
00207 continue;
00208 }
00209
00210
00211
00212 if (!isset($threads[$scroll->idThread]))
00213 {
00214
00215 $threads[$scroll->idThread][1] = $scrollReturn;
00216 }
00217 else
00218 {
00219
00220 $threads[$scroll->idThread][] = $scrollReturn;
00221 }
00222 }
00223
00224
00225 uasort($threads, 'Dsao_Scrollset::compareThreads');
00226
00227
00228 return $threads;
00229 }
00230
00236 public function isRead()
00237 {
00238 foreach ($this->getIterator() as $scroll)
00239 {
00240
00241 if (!$scroll->isRead())
00242 {
00243 return false;
00244 }
00245 }
00246
00247 return true;
00248 }
00249
00257 public function markRead($idUser = 0, $read = true)
00258 {
00259 foreach ($this->getIterator() as $scroll)
00260 {
00261 $scroll->markRead($idUser, $read);
00262 }
00263 }
00264
00272 public function refresh($reset = true)
00273 {
00274
00275 if (null != $this->_sqlSelect)
00276 {
00277
00278 $this->fetchScrolls(array(), $reset);
00279 }
00280
00281 return $this;
00282 }
00283
00289 public function toArrayDecoded()
00290 {
00291 $scrollsArray = array();
00292
00293 foreach ($this as $scroll)
00294 {
00295 $scrollsArray[] = $scroll->toArrayDecoded();
00296 }
00297
00298 return $scrollsArray;
00299 }
00300 }