Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SeriesDAO.inc.php
1 <?php
2 
17 import ('classes.press.Series');
18 
19 class SeriesDAO extends DAO {
23  function SeriesDAO() {
24  parent::DAO();
25  }
26 
33  function getById($seriesId, $pressId = null) {
34  $params = array((int) $seriesId);
35  if ($pressId) $params[] = (int) $pressId;
36 
37  $result = $this->retrieve(
38  'SELECT *
39  FROM series
40  WHERE series_id = ?
41  ' . ($pressId?' AND press_id = ?':''),
42  $params
43  );
44 
45  $returner = null;
46  if ($result->RecordCount() != 0) {
47  $returner = $this->_fromRow($result->GetRowAssoc(false));
48  }
49 
50  $result->Close();
51  return $returner;
52  }
53 
60  function getByPath($path, $pressId) {
61  $result = $this->retrieve(
62  'SELECT * FROM series WHERE path = ? AND press_id = ?',
63  array((string) $path, (int) $pressId)
64  );
65 
66  $returner = null;
67  if ($result->RecordCount() != 0) {
68  $returner = $this->_fromRow($result->GetRowAssoc(false));
69  }
70 
71  $result->Close();
72  return $returner;
73  }
74 
79  function newDataObject() {
80  return new Series();
81  }
82 
88  function _fromRow($row) {
89  $series = $this->newDataObject();
90 
91  $series->setId($row['series_id']);
92  $series->setPressId($row['press_id']);
93  $series->setFeatured($row['featured']);
94  $series->setPath($row['path']);
95  $series->setImage(unserialize($row['image']));
96  $series->setSequence($row['seq']);
97  $series->setEditorRestricted($row['editor_restricted']);
98 
99  $this->getDataObjectSettings('series_settings', 'series_id', $row['series_id'], $series);
100 
101  HookRegistry::call('SeriesDAO::_fromRow', array(&$series, &$row));
102 
103  return $series;
104  }
105 
110  function getLocaleFieldNames() {
111  return array('title', 'description', 'prefix', 'subtitle');
112  }
113 
118  function updateLocaleFields($series) {
120  'series_settings',
121  $series,
122  array('series_id' => (int) $series->getId())
123  );
124  }
125 
130  function insertObject($series) {
131  $this->update(
132  'INSERT INTO series
133  (press_id, seq, featured, path, image, editor_restricted)
134  VALUES
135  (?, ?, ?, ?, ?, ?)',
136  array(
137  (int) $series->getPressId(),
138  (float) $series->getSequence(),
139  (int) $series->getFeatured(),
140  (string) $series->getPath(),
141  serialize($series->getImage() ? $series->getImage() : array()),
142  (int) $series->getEditorRestricted(),
143  )
144  );
145 
146  $series->setId($this->getInsertId());
147  $this->updateLocaleFields($series);
148  return $series->getId();
149  }
150 
155  function updateObject($series) {
156  $this->update(
157  'UPDATE series
158  SET press_id = ?,
159  seq = ?,
160  featured = ?,
161  path = ?,
162  image = ?,
163  editor_restricted = ?
164  WHERE series_id = ?',
165  array(
166  (int) $series->getPressId(),
167  (float) $series->getSequence(),
168  (int) $series->getFeatured(),
169  (string) $series->getPath(),
170  serialize($series->getImage() ? $series->getImage() : array()),
171  (int) $series->getEditorRestricted(),
172  (int) $series->getId(),
173  )
174  );
175  $this->updateLocaleFields($series);
176  }
177 
182  function deleteObject($series) {
183  return $this->deleteById($series->getId(), $series->getPressId());
184  }
185 
191  function deleteById($seriesId, $pressId = null) {
192  // Validate the $pressId, if supplied.
193  if (!$this->seriesExists($seriesId, $pressId)) return false;
194 
195  $seriesEditorsDao = DAORegistry::getDAO('SeriesEditorsDAO');
196  $seriesEditorsDao->deleteEditorsBySeriesId($seriesId, $pressId);
197 
198  // Remove monographs from this series
199  $monographDao = DAORegistry::getDAO('MonographDAO');
200  $monographDao->removeMonographsFromSeries($seriesId);
201 
202  // Delete the series and settings.
203  $this->update('DELETE FROM series WHERE series_id = ?', (int) $seriesId);
204  $this->update('DELETE FROM series_settings WHERE series_id = ?', (int) $seriesId);
205  }
206 
213  function deleteByPressId($pressId) {
214  $series = $this->getByPressId($pressId);
215  while ($series = $series->next()) {
216  $this->deleteObject($series);
217  }
218  }
219 
225  function getEditorSeries($pressId) {
226  $result = $this->retrieve(
227  'SELECT a.*,
228  ae.user_id AS editor_id
229  FROM series_editors ae,
230  series a
231  WHERE ae.series_id = a.series_id AND
232  a.press_id = ae.press_id AND
233  a.press_id = ?',
234  (int) $pressId
235  );
236 
237  $returner = array();
238  while (!$result->EOF) {
239  $row = $result->GetRowAssoc(false);
240  $series = $this->_fromRow($row);
241  if (!isset($returner[$row['editor_id']])) {
242  $returner[$row['editor_id']] = array($series);
243  } else {
244  $returner[$row['editor_id']][] = $series;
245  }
246  $result->MoveNext();
247  }
248 
249  $result->Close();
250  return $returner;
251  }
252 
257  function getByPressId($pressId, $rangeInfo = null) {
258  $params = array(
259  'title', AppLocale::getPrimaryLocale(),
260  'title', AppLocale::getLocale(),
261  (int) $pressId
262  );
263 
264  $result = $this->retrieveRange(
265  'SELECT s.*, COALESCE(stpl.setting_value, stl.setting_value) AS series_title FROM series s
266  LEFT JOIN series_settings stpl ON (s.series_id = stpl.series_id AND stpl.setting_name = ? AND stpl.locale = ?)
267  LEFT JOIN series_settings stl ON (s.series_id = stl.series_id AND stl.setting_name = ? AND stl.locale = ?)
268  WHERE press_id = ?
269  ORDER BY seq',
270  $params,
271  $rangeInfo
272  );
273 
274  return new DAOResultFactory($result, $this, '_fromRow');
275  }
276 
281  function getTitlesByPressId($pressId, $submittableOnly = false) {
282  $seriesTitles = array();
283 
284  $seriesIterator = $this->getByPressId($pressId, null);
285  while ($series = $seriesIterator->next()) {
286  if ($submittableOnly) {
287  if (!$series->getEditorRestricted()) {
288  $seriesTitles[$series->getId()] = join(' ', array($series->getLocalizedPrefix(), $series->getLocalizedTitle()));
289  }
290  } else {
291  $seriesTitles[$series->getId()] = join(' ', array($series->getLocalizedPrefix(), $series->getLocalizedTitle()));
292  }
293  }
294 
295  return $seriesTitles;
296  }
297 
304  function seriesExists($seriesId, $pressId) {
305  $result = $this->retrieve(
306  'SELECT COUNT(*) FROM series WHERE series_id = ? AND press_id = ?',
307  array((int) $seriesId, (int) $pressId)
308  );
309  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
310 
311  $result->Close();
312  return $returner;
313  }
314 
319  function getInsertId() {
320  return $this->_getInsertId('series', 'series_id');
321  }
322 
328  function addCategory($seriesId, $categoryId) {
329  $this->update(
330  'INSERT INTO series_categories
331  (series_id, category_id)
332  VALUES
333  (?, ?)',
334  array(
335  (int) $seriesId,
336  (int) $categoryId
337  )
338  );
339  }
340 
346  function removeCategory($seriesId, $categoryId) {
347  $this->update(
348  'DELETE FROM series_categories WHERE series_id = ? AND category_id = ?',
349  array(
350  (int) $seriesId,
351  (int) $categoryId
352  )
353  );
354  }
355 
361  function getCategories($seriesId, $pressId = null) {
362  $params = array((int) $seriesId);
363  if ($pressId) $params[] = (int) $pressId;
364 
365  $categoryDao = DAORegistry::getDAO('CategoryDAO');
366  $result = $this->retrieve(
367  'SELECT c.*
368  FROM categories c,
369  series_categories sc,
370  series s
371  WHERE c.category_id = sc.category_id AND
372  s.series_id = ? AND
373  ' . ($pressId?' c.press_id = s.press_id AND s.press_id = ? AND':'') . '
374  s.series_id = sc.series_id',
375  $params
376  );
377 
378  // Delegate category creation to the category DAO.
379  return new DAOResultFactory($result, $categoryDao, '_fromRow');
380  }
381 
387  function getUnassignedCategories($seriesId, $pressId = null) {
388  $params = array((int) $seriesId);
389  if ($pressId) $params[] = (int) $pressId;
390 
391  $categoryDao = DAORegistry::getDAO('CategoryDAO');
392  $result = $this->retrieve(
393  'SELECT c.*
394  FROM series s
395  JOIN categories c ON (c.press_id = s.press_id)
396  LEFT JOIN series_categories sc ON (s.series_id = sc.series_id AND sc.category_id = c.category_id)
397  WHERE s.series_id = ? AND
398  ' . ($pressId?' s.press_id = ? AND':'') . '
399  sc.series_id IS NULL',
400  $params
401  );
402 
403  // Delegate category creation to the category DAO.
404  return new DAOResultFactory($result, $categoryDao, '_fromRow');
405  }
406 
413  function categoryAssociationExists($seriesId, $categoryId) {
414  $result = $this->retrieve(
415  'SELECT COUNT(*) FROM series_categories WHERE series_id = ? AND category_id = ?',
416  array((int) $seriesId, (int) $categoryId)
417  );
418  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
419 
420  $result->Close();
421  return $returner;
422  }
423 }
424 
425 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
addCategory($seriesId, $categoryId)
deleteById($seriesId, $pressId=null)
getByPath($path, $pressId)
seriesExists($seriesId, $pressId)
getEditorSeries($pressId)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getByPressId($pressId, $rangeInfo=null)
categoryAssociationExists($seriesId, $categoryId)
static getPrimaryLocale()
getTitlesByPressId($pressId, $submittableOnly=false)
Describes basic series properties.
Definition: Series.inc.php:19
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
removeCategory($seriesId, $categoryId)
static getLocale()
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
static call($hookName, $args=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
updateObject($series)
deleteByPressId($pressId)
_fromRow($row)
deleteObject($series)
updateLocaleFields($series)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getCategories($seriesId, $pressId=null)
insertObject($series)
Operations for retrieving and modifying Series objects.
getUnassignedCategories($seriesId, $pressId=null)
getById($seriesId, $pressId=null)