00001 <?php
00002
00016 import('classes.spotlight.Spotlight');
00017
00018 class SpotlightDAO extends DAO {
00022 function SpotlightDAO() {
00023 parent::DAO();
00024 }
00025
00031 function &getById($spotlightId) {
00032 $result =& $this->retrieve(
00033 'SELECT * FROM spotlights WHERE spotlight_id = ?',
00034 (int) $spotlightId
00035 );
00036
00037 $returner = null;
00038 if ($result->RecordCount() != 0) {
00039 $returner =& $this->_fromRow($result->GetRowAssoc(false));
00040 }
00041 $result->Close();
00042 return $returner;
00043 }
00044
00050 function getSpotlightAssocId($spotlightId) {
00051 $result =& $this->retrieve(
00052 'SELECT assoc_id FROM spotlights WHERE spotlight_id = ?',
00053 (int) $spotlightId
00054 );
00055
00056 return isset($result->fields[0]) ? $result->fields[0] : 0;
00057 }
00058
00064 function getSpotlightAssocType($spotlightId) {
00065 $result =& $this->retrieve(
00066 'SELECT assoc_type FROM spotlights WHERE spotlight_id = ?',
00067 (int) $spotlightId
00068 );
00069
00070 return isset($result->fields[0]) ? $result->fields[0] : 0;
00071 }
00072
00077 function getLocaleFieldNames() {
00078 return array('title', 'description');
00079 }
00080
00085 function newDataObject() {
00086 return new Spotlight();
00087 }
00088
00094 function &_fromRow(&$row) {
00095 $spotlight = $this->newDataObject();
00096 $spotlight->setId($row['spotlight_id']);
00097 $spotlight->setAssocType($row['assoc_type']);
00098 $spotlight->setAssocId($row['assoc_id']);
00099 $spotlight->setPressId($row['press_id']);
00100
00101 $this->getDataObjectSettings('spotlight_settings', 'spotlight_id', $row['spotlight_id'], $spotlight);
00102
00103 return $spotlight;
00104 }
00105
00110 function updateLocaleFields(&$spotlight) {
00111 $this->updateDataObjectSettings('spotlight_settings', $spotlight, array(
00112 'spotlight_id' => $spotlight->getId()
00113 ));
00114 }
00115
00121 function insertObject(&$spotlight) {
00122 $this->update(
00123 'INSERT INTO spotlights
00124 (assoc_type, assoc_id, press_id)
00125 VALUES
00126 (?, ?, ?)',
00127 array(
00128 (int) $spotlight->getAssocType(),
00129 (int) $spotlight->getAssocId(),
00130 (int) $spotlight->getPressId(),
00131 )
00132 );
00133 $spotlight->setId($this->getInsertSpotlightId());
00134 $this->updateLocaleFields($spotlight);
00135 return $spotlight->getId();
00136 }
00137
00143 function updateObject(&$spotlight) {
00144 $returner = $this->update(
00145 'UPDATE spotlights
00146 SET
00147 assoc_type = ?,
00148 assoc_id = ?,
00149 press_id = ?
00150 WHERE spotlight_id = ?',
00151 array(
00152 (int) $spotlight->getAssocType(),
00153 (int) $spotlight->getAssocId(),
00154 (int) $spotlight->getPressId(),
00155 (int) $spotlight->getId()
00156 )
00157 );
00158 $this->updateLocaleFields($spotlight);
00159 return $returner;
00160 }
00161
00167 function deleteObject($spotlight) {
00168 return $this->deleteById($spotlight->getId());
00169 }
00170
00176 function deleteById($spotlightId) {
00177 $this->update('DELETE FROM spotlight_settings WHERE spotlight_id = ?', (int) $spotlightId);
00178 return $this->update('DELETE FROM spotlights WHERE spotlight_id = ?', (int) $spotlightId);
00179 }
00180
00186 function deleteByTypeId($typeId) {
00187 $spotlights =& $this->getByTypeId($typeId);
00188 while (($spotlight =& $spotlights->next())) {
00189 $this->deleteObject($spotlight);
00190 unset($spotlight);
00191 }
00192 }
00193
00199 function deleteByAssoc($assocType, $assocId) {
00200 $spotlights =& $this->getByAssocId($assocType, $assocId);
00201 while (($spotlight =& $spotlights->next())) {
00202 $this->deleteById($spotlight->getId());
00203 unset($spotlight);
00204 }
00205 return true;
00206 }
00207
00213 function &getByPressId($pressId, $rangeInfo = null) {
00214 $result =& $this->retrieveRange(
00215 'SELECT *
00216 FROM spotlights
00217 WHERE press_id = ?
00218 ORDER BY spotlight_id DESC',
00219 array((int) $pressId),
00220 $rangeInfo
00221 );
00222
00223 $spotlightFactory = new DAOResultFactory($result, $this, '_fromRow');
00224 $returner = array();
00225
00226
00227 while ($spotlight =& $spotlightFactory->next()) {
00228 $spotlightItem =& $spotlight->getSpotlightItem();
00229 if ($spotlightItem) {
00230 $returner[$spotlight->getId()] =& $spotlight;
00231 }
00232 }
00233
00234 return $returner;
00235 }
00236
00244 function getRandomByPressId($pressId, $quantity = 1) {
00245 $spotlights =& array_values($this->getByPressId($pressId));
00246 $returner = array();
00247 if (count($spotlights) > 0) {
00248 if (count($spotlights) <= $quantity) {
00249
00250 $returner = $spotlights;
00251 } else {
00252
00253 for($quantity; $quantity > 0; $quantity--) {
00254 $randNumber = rand(0, count($spotlights) - 1);
00255 $returner[] = $spotlights[$randNumber];
00256 unset($spotlights[$randNumber]);
00257
00258 $spotlights = array_values($spotlights);
00259 }
00260 }
00261 }
00262
00263 if (count($returner) == 0) {
00264 $returner = null;
00265 }
00266
00267 return $returner;
00268 }
00274 function &getByTypeId($typeId, $rangeInfo = null) {
00275 $result =& $this->retrieveRange(
00276 'SELECT * FROM spotlights WHERE type_id = ? ORDER BY spotlight_id DESC',
00277 (int) $typeId,
00278 $rangeInfo
00279 );
00280
00281 $returner = new DAOResultFactory($result, $this, '_fromRow');
00282 return $returner;
00283 }
00284
00291 function &getByAssoc($assocType, $assocId, $rangeInfo = null) {
00292 $result =& $this->retrieveRange(
00293 'SELECT *
00294 FROM spotlights
00295 WHERE assoc_type = ? AND assoc_id = ?
00296 ORDER BY spotlight_id DESC',
00297 array((int) $assocType, (int) $assocId),
00298 $rangeInfo
00299 );
00300
00301 $returner = new DAOResultFactory($result, $this, '_fromRow');
00302 return $returner;
00303 }
00304
00310 function &getNumSpotlightsByAssoc($assocType, $assocId, $rangeInfo = null) {
00311 $result =& $this->retrieveRange(
00312 'SELECT *
00313 FROM spotlights
00314 WHERE assoc_type = ?
00315 AND assoc_id = ?
00316 ORDER BY spotlight_id DESC',
00317 array((int) $assocType, (int) $assocId),
00318 $rangeInfo
00319 );
00320
00321 $returner = new DAOResultFactory($result, $this, '_fromRow');
00322 return $returner;
00323 }
00324
00330 function &getMostRecentSpotlightByAssoc($assocType, $assocId) {
00331 $result =& $this->retrieve(
00332 'SELECT *
00333 FROM spotlights
00334 WHERE assoc_type = ?
00335 AND assoc_id = ?
00336 ORDER BY spotlight_id DESC LIMIT 1',
00337 array((int) $assocType, (int) $assocId)
00338 );
00339
00340 $returner = null;
00341 if ($result->RecordCount() != 0) {
00342 $returner =& $this->_fromRow($result->GetRowAssoc(false));
00343 }
00344 $result->Close();
00345 return $returner;
00346 }
00347
00352 function getInsertSpotlightId() {
00353 return $this->getInsertId('spotlights', 'spotlight_id');
00354 }
00355 }
00356
00357 ?>