Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SpotlightDAO.inc.php
1 <?php
2 
17 import('classes.spotlight.Spotlight');
18 
19 class SpotlightDAO extends DAO {
23  function SpotlightDAO() {
24  parent::DAO();
25  }
26 
32  function getById($spotlightId) {
33  $result = $this->retrieve(
34  'SELECT * FROM spotlights WHERE spotlight_id = ?',
35  (int) $spotlightId
36  );
37 
38  $returner = null;
39  if ($result->RecordCount() != 0) {
40  $returner = $this->_fromRow($result->GetRowAssoc(false));
41  }
42  $result->Close();
43  return $returner;
44  }
45 
51  function getSpotlightAssocId($spotlightId) {
52  $result = $this->retrieve(
53  'SELECT assoc_id FROM spotlights WHERE spotlight_id = ?',
54  (int) $spotlightId
55  );
56 
57  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
58  $result->Close();
59  return $returner;
60  }
61 
67  function getSpotlightAssocType($spotlightId) {
68  $result = $this->retrieve(
69  'SELECT assoc_type FROM spotlights WHERE spotlight_id = ?',
70  (int) $spotlightId
71  );
72 
73  $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
74  $result->Close();
75  return $returner;
76  }
77 
82  function getLocaleFieldNames() {
83  return array('title', 'description');
84  }
85 
90  function newDataObject() {
91  return new Spotlight();
92  }
93 
99  function _fromRow($row) {
100  $spotlight = $this->newDataObject();
101  $spotlight->setId($row['spotlight_id']);
102  $spotlight->setAssocType($row['assoc_type']);
103  $spotlight->setAssocId($row['assoc_id']);
104  $spotlight->setPressId($row['press_id']);
105 
106  $this->getDataObjectSettings('spotlight_settings', 'spotlight_id', $row['spotlight_id'], $spotlight);
107 
108  return $spotlight;
109  }
110 
115  function updateLocaleFields(&$spotlight) {
116  $this->updateDataObjectSettings('spotlight_settings', $spotlight, array(
117  'spotlight_id' => $spotlight->getId()
118  ));
119  }
120 
126  function insertObject($spotlight) {
127  $this->update(
128  'INSERT INTO spotlights
129  (assoc_type, assoc_id, press_id)
130  VALUES
131  (?, ?, ?)',
132  array(
133  (int) $spotlight->getAssocType(),
134  (int) $spotlight->getAssocId(),
135  (int) $spotlight->getPressId(),
136  )
137  );
138  $spotlight->setId($this->getInsertId());
139  $this->updateLocaleFields($spotlight);
140  return $spotlight->getId();
141  }
142 
148  function updateObject($spotlight) {
149  $returner = $this->update(
150  'UPDATE spotlights
151  SET
152  assoc_type = ?,
153  assoc_id = ?,
154  press_id = ?
155  WHERE spotlight_id = ?',
156  array(
157  (int) $spotlight->getAssocType(),
158  (int) $spotlight->getAssocId(),
159  (int) $spotlight->getPressId(),
160  (int) $spotlight->getId()
161  )
162  );
163  $this->updateLocaleFields($spotlight);
164  return $returner;
165  }
166 
172  function deleteObject($spotlight) {
173  return $this->deleteById($spotlight->getId());
174  }
175 
181  function deleteById($spotlightId) {
182  $this->update('DELETE FROM spotlight_settings WHERE spotlight_id = ?', (int) $spotlightId);
183  return $this->update('DELETE FROM spotlights WHERE spotlight_id = ?', (int) $spotlightId);
184  }
185 
191  function deleteByTypeId($typeId) {
192  $spotlights = $this->getByTypeId($typeId);
193  while (($spotlight = $spotlights->next())) {
194  $this->deleteObject($spotlight);
195  }
196  }
197 
203  function deleteByAssoc($assocType, $assocId) {
204  $spotlights = $this->getByAssocId($assocType, $assocId);
205  while ($spotlight = $spotlights->next()) {
206  $this->deleteById($spotlight->getId());
207  }
208  return true;
209  }
210 
216  function getByPressId($pressId, $rangeInfo = null) {
217  $result = $this->retrieveRange(
218  'SELECT *
219  FROM spotlights
220  WHERE press_id = ?
221  ORDER BY spotlight_id DESC',
222  array((int) $pressId),
223  $rangeInfo
224  );
225 
226  $spotlightFactory = new DAOResultFactory($result, $this, '_fromRow');
227  $returner = array();
228 
229  // Avoid spotlights without items.
230  while ($spotlight = $spotlightFactory->next()) {
231  $spotlightItem = $spotlight->getSpotlightItem();
232  if ($spotlightItem) {
233  $returner[$spotlight->getId()] = $spotlight;
234  }
235  }
236 
237  return $returner;
238  }
239 
247  function getRandomByPressId($pressId, $quantity = 1) {
248  $spotlights = array_values($this->getByPressId($pressId));
249  $returner = array();
250  if (count($spotlights) > 0) {
251  if (count($spotlights) <= $quantity) {
252  // Return the ones that we have.
253  $returner = $spotlights;
254  } else {
255  // Get the random spotlights.
256  for($quantity; $quantity > 0; $quantity--) {
257  $randNumber = rand(0, count($spotlights) - 1);
258  $returner[] = $spotlights[$randNumber];
259  unset($spotlights[$randNumber]);
260  // Reset spotlights array index.
261  $spotlights = array_values($spotlights);
262  }
263  }
264  }
265 
266  if (count($returner) == 0) {
267  $returner = null;
268  }
269 
270  return $returner;
271  }
272 
279  function getByAssoc($assocType, $assocId, $rangeInfo = null) {
280  $result = $this->retrieveRange(
281  'SELECT *
282  FROM spotlights
283  WHERE assoc_type = ? AND assoc_id = ?
284  ORDER BY spotlight_id DESC',
285  array((int) $assocType, (int) $assocId),
286  $rangeInfo
287  );
288 
289  return new DAOResultFactory($result, $this, '_fromRow');
290  }
291 
297  function getNumSpotlightsByAssoc($assocType, $assocId, $rangeInfo = null) {
298  $result = $this->retrieveRange(
299  'SELECT *
300  FROM spotlights
301  WHERE assoc_type = ?
302  AND assoc_id = ?
303  ORDER BY spotlight_id DESC',
304  array((int) $assocType, (int) $assocId),
305  $rangeInfo
306  );
307 
308  return new DAOResultFactory($result, $this, '_fromRow');
309  }
310 
316  function getMostRecentSpotlightByAssoc($assocType, $assocId) {
317  $result = $this->retrieve(
318  'SELECT *
319  FROM spotlights
320  WHERE assoc_type = ?
321  AND assoc_id = ?
322  ORDER BY spotlight_id DESC LIMIT 1',
323  array((int) $assocType, (int) $assocId)
324  );
325 
326  $returner = null;
327  if ($result->RecordCount() != 0) {
328  $returner = $this->_fromRow($result->GetRowAssoc(false));
329  }
330  $result->Close();
331  return $returner;
332  }
333 
338  function getInsertId() {
339  return $this->_getInsertId('spotlights', 'spotlight_id');
340  }
341 }
342 
343 ?>
deleteObject($spotlight)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
deleteByAssoc($assocType, $assocId)
getById($spotlightId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getByPressId($pressId, $rangeInfo=null)
getNumSpotlightsByAssoc($assocType, $assocId, $rangeInfo=null)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
getMostRecentSpotlightByAssoc($assocType, $assocId)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getSpotlightAssocType($spotlightId)
deleteByTypeId($typeId)
Operations for retrieving and modifying Spotlight objects.
updateObject($spotlight)
getRandomByPressId($pressId, $quantity=1)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
updateLocaleFields(&$spotlight)
Basic class describing a spotlight.
getSpotlightAssocId($spotlightId)
getByAssoc($assocType, $assocId, $rangeInfo=null)
deleteById($spotlightId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
insertObject($spotlight)