Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
MonographDAO.inc.php
1 <?php
2 
17 import('classes.monograph.Monograph');
18 import('lib.pkp.classes.submission.SubmissionDAO');
19 
20 class MonographDAO extends SubmissionDAO {
24  function MonographDAO() {
25  parent::SubmissionDAO();
26  }
27 
32  function getLocaleFieldNames() {
33  return parent::getLocaleFieldNames() + array(
34  'copyrightNotice',
35  );
36  }
37 
43  function _fromRow($row) {
44  $monograph = parent::_fromRow($row);
45 
46  $monograph->setSeriesId($row['series_id']);
47  $monograph->setSeriesPosition($row['series_position']);
48  $monograph->setSeriesAbbrev(isset($row['series_abbrev'])?$row['series_abbrev']:null);
49  $monograph->setWorkType($row['edited_volume']);
50 
51  HookRegistry::call('MonographDAO::_fromRow', array(&$monograph, &$row));
52 
53  return $monograph;
54  }
55 
60  function newDataObject() {
61  return new Monograph();
62  }
63 
69  function insertObject($monograph) {
70  $monograph->stampModified();
71  $this->update(
72  sprintf('INSERT INTO submissions
73  (locale, user_id, context_id, series_id, series_position, language, comments_to_ed, date_submitted, date_status_modified, last_modified, status, submission_progress, stage_id, pages, hide_author, comments_status, edited_volume)
74  VALUES
75  (?, ?, ?, ?, ?, ?, ?, %s, %s, %s, ?, ?, ?, ?, ?, ?, ?)',
76  $this->datetimeToDB($monograph->getDateSubmitted()), $this->datetimeToDB($monograph->getDateStatusModified()), $this->datetimeToDB($monograph->getLastModified())),
77  array(
78  $monograph->getLocale(),
79  (int) $monograph->getUserId(),
80  (int) $monograph->getContextId(),
81  (int) $monograph->getSeriesId(),
82  $monograph->getSeriesPosition(),
83  $monograph->getLanguage(),
84  $monograph->getCommentsToEditor(),
85  $monograph->getStatus() === null ? STATUS_QUEUED : (int) $monograph->getStatus(),
86  $monograph->getSubmissionProgress() === null ? 1 : (int) $monograph->getSubmissionProgress(),
87  $monograph->getStageId() === null ? 1 : (int) $monograph->getStageId(),
88  $monograph->getPages(),
89  (int) $monograph->getHideAuthor(),
90  (int) $monograph->getCommentsStatus(),
91  (int) $monograph->getWorkType(),
92  )
93  );
94 
95  $monograph->setId($this->getInsertId());
96  $this->updateLocaleFields($monograph);
97 
98  return $monograph->getId();
99  }
100 
105  function updateObject($monograph) {
106  $this->update(
107  sprintf('UPDATE submissions
108  SET user_id = ?,
109  series_id = ?,
110  series_position = ?,
111  language = ?,
112  comments_to_ed = ?,
113  date_submitted = %s,
114  date_status_modified = %s,
115  last_modified = %s,
116  status = ?,
117  context_id = ?,
118  submission_progress = ?,
119  stage_id = ?,
120  edited_volume = ?,
121  hide_author = ?
122 
123  WHERE submission_id = ?',
124  $this->datetimeToDB($monograph->getDateSubmitted()), $this->datetimeToDB($monograph->getDateStatusModified()), $this->datetimeToDB($monograph->getLastModified())),
125  array(
126  (int) $monograph->getUserId(),
127  (int) $monograph->getSeriesId(),
128  $monograph->getSeriesPosition(),
129  $monograph->getLanguage(),
130  $monograph->getCommentsToEditor(),
131  (int) $monograph->getStatus(),
132  (int) $monograph->getContextId(),
133  (int) $monograph->getSubmissionProgress(),
134  (int) $monograph->getStageId(),
135  (int) $monograph->getWorkType(),
136  (int) $monograph->getHideAuthor(),
137  (int) $monograph->getId()
138  )
139  );
140  $this->updateLocaleFields($monograph);
141  $this->flushCache();
142  }
143 
148  function deleteById($monographId) {
149  parent::deleteById($monographId);
150 
151  // Delete chapters and assigned chapter authors.
152  $chapterDao = DAORegistry::getDAO('ChapterDAO');
153  $chapters = $chapterDao->getChapters($monographId);
154  while ($chapter = $chapters->next()) {
155  // also removes Chapter Author associations
156  $chapterDao->deleteObject($chapter);
157  }
158 
159  // Delete monograph file directory.
160  $monograph = $this->getById($monographId);
161  assert(is_a($monograph, 'Submission'));
162 
163  import('lib.pkp.classes.file.SubmissionFileManager');
164  $monographFileManager = new SubmissionFileManager($monograph->getPressId(), $monograph->getId());
165  $monographFileManager->rmtree($monographFileManager->getBasePath());
166 
167  // Delete references to features or new releases.
168  $featureDao = DAORegistry::getDAO('FeatureDAO');
169  $featureDao->deleteByMonographId($monographId);
170 
171  $newReleaseDao = DAORegistry::getDAO('NewReleaseDAO');
172  $newReleaseDao->deleteByMonographId($monographId);
173 
174  import('classes.search.MonographSearchIndex');
175  MonographSearchIndex::deleteTextIndex($monograph->getId());
176  }
177 
183  function getByPressId($pressId) {
184  return parent::getByContextId($pressId);
185  }
186 
193  $params = $this->_getFetchParameters();
194  $params[] = (int) $pressId;
195 
196  $result = $this->retrieve(
197  'SELECT s.*, ps.date_published,
198  ' . $this->_getFetchColumns() . '
199  FROM submissions s
200  LEFT JOIN published_submissions ps ON (s.submission_id = ps.submission_id)
201  ' . $this->_getFetchJoins() . '
202  WHERE s.context_id = ? AND
203  (ps.submission_id IS NULL OR ps.date_published IS NULL) AND
204  s.submission_progress = 0',
205  $params
206  );
207 
208  return new DAOResultFactory($result, $this, '_fromRow');
209  }
210 
215  function removeMonographsFromSeries($seriesId) {
216  $this->update(
217  'UPDATE submissions SET series_id = null WHERE series_id = ?',
218  (int) $seriesId
219  );
220 
221  $this->flushCache();
222  }
223 
234  function getBySubEditorId($contextId = null, $subEditorId = null, $includeDeclined = true, $includePublished = true, $rangeInfo = null) {
235  $primaryLocale = AppLocale::getPrimaryLocale();
236  $locale = AppLocale::getLocale();
237 
238  $params = $this->_getFetchParameters();
239  $params[] = (int) ROLE_ID_MANAGER;
240  if ($subEditorId) $params[] = (int) $subEditorId;
241  if ($contextId && is_int($contextId)) $params[] = (int) $contextId;
242 
243  $result = $this->retrieveRange(
244  'SELECT s.*, ps.date_published,
245  ' . $this->_getFetchColumns() . '
246  FROM submissions s
247  LEFT JOIN published_submissions ps ON s.submission_id = ps.submission_id
248  ' . $this->_getFetchJoins() . '
249  LEFT JOIN stage_assignments sa ON (s.submission_id = sa.submission_id)
250  LEFT JOIN user_groups g ON (sa.user_group_id = g.user_group_id AND g.role_id = ?)
251  ' . ($subEditorId?' JOIN series_editors se ON (se.press_id = s.context_id AND se.user_id = ? AND se.series_id = s.series_id)':'') . '
252  WHERE s.date_submitted IS NOT NULL'
253  . (!$includeDeclined?' AND s.status <> ' . STATUS_DECLINED : '' )
254  . (!$includePublished?' AND ps.date_published IS NULL' : '' )
255  . ($contextId && !is_array($contextId)?' AND s.context_id = ?':'')
256  . ($contextId && is_array($contextId)?' AND s.context_id IN (' . join(',', array_map(array($this,'_arrayWalkIntCast'), $contextId)) . ')':'') . '
257  GROUP BY s.submission_id, ps.date_published, stl.setting_value, stpl.setting_value, sal.setting_value, sapl.setting_value',
258  // See bug #8557; the above are required to keep PostgreSQL happy (and s.submission_id is required logically).
259  $params,
260  $rangeInfo
261  );
262 
263  return new DAOResultFactory($result, $this, '_fromRow');
264  }
265 
271  function addCategory($monographId, $categoryId) {
272  $this->update(
273  'INSERT INTO submission_categories
274  (submission_id, category_id)
275  VALUES
276  (?, ?)',
277  array(
278  (int) $monographId,
279  (int) $categoryId
280  )
281  );
282  }
283 
289  function removeCategory($monographId, $categoryId) {
290  $this->update(
291  'DELETE FROM submission_categories WHERE submission_id = ? AND category_id = ?',
292  array(
293  (int) $monographId,
294  (int) $categoryId
295  )
296  );
297 
298  // If any new release or feature object is associated
299  // with this category delete them.
300  $newReleaseDao = DAORegistry::getDAO('NewReleaseDAO'); /* @var $newReleaseDao NewReleaseDAO */
301  $newReleaseDao->deleteNewRelease($monographId, ASSOC_TYPE_CATEGORY, $categoryId);
302 
303  $featureDao = DAORegistry::getDAO('FeatureDAO'); /* @var $featureDao FeatureDAO */
304  $featureDao->deleteFeature($monographId, ASSOC_TYPE_CATEGORY, $categoryId);
305  }
306 
311  function removeCategories($monographId) {
312  $this->update(
313  'DELETE FROM submission_categories WHERE submission_id = ?',
314  (int) $monographId
315  );
316  }
317 
323  function getCategories($monographId, $pressId = null) {
324  $params = array((int) $monographId);
325  if ($pressId) $params[] = (int) $pressId;
326 
327  $categoryDao = DAORegistry::getDAO('CategoryDAO');
328  $result = $this->retrieve(
329  'SELECT c.*
330  FROM categories c,
331  submission_categories sc,
332  submissions s
333  WHERE c.category_id = sc.category_id AND
334  s.submission_id = ? AND
335  ' . ($pressId?' c.press_id = s.context_id AND s.context_id = ? AND':'') . '
336  s.submission_id = sc.submission_id',
337  $params
338  );
339 
340  // Delegate category creation to the category DAO.
341  return new DAOResultFactory($result, $categoryDao, '_fromRow');
342  }
343 
349  function getUnassignedCategories($monographId, $pressId = null) {
350  $params = array((int) $monographId);
351  if ($pressId) $params[] = (int) $pressId;
352 
353  $categoryDao = DAORegistry::getDAO('CategoryDAO');
354  // The strange ORDER BY clause is to return subcategories
355  // immediately after their parent category's entry.
356  $result = $this->retrieve(
357  'SELECT c.*
358  FROM submissions s
359  JOIN categories c ON (c.press_id = s.context_id)
360  LEFT JOIN submission_categories sc ON (s.submission_id = sc.submission_id AND sc.category_id = c.category_id)
361  WHERE s.submission_id = ? AND
362  ' . ($pressId?' s.context_id = ? AND':'') . '
363  sc.submission_id IS NULL
364  ORDER BY CASE WHEN c.parent_id = 0 THEN c.category_id * 2 ELSE (c.parent_id * 2) + 1 END ASC',
365  $params
366  );
367 
368  // Delegate category creation to the category DAO.
369  return new DAOResultFactory($result, $categoryDao, '_fromRow');
370  }
371 
378  function categoryAssociationExists($monographId, $categoryId) {
379  $result = $this->retrieve(
380  'SELECT COUNT(*) FROM submission_categories WHERE submission_id = ? AND category_id = ?',
381  array((int) $monographId, (int) $categoryId)
382  );
383  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
384 
385  $result->Close();
386  return $returner;
387  }
388 
393  protected function _getFetchParameters() {
394  $primaryLocale = AppLocale::getPrimaryLocale();
395  $locale = AppLocale::getLocale();
396  return array(
397  'title', $primaryLocale, // Series title
398  'title', $locale, // Series title
399  'abbrev', $primaryLocale, // Series abbreviation
400  'abbrev', $locale, // Series abbreviation
401  );
402  }
403 
408  protected function _getFetchColumns() {
409  return 'COALESCE(stl.setting_value, stpl.setting_value) AS series_title,
410  COALESCE(sal.setting_value, sapl.setting_value) AS series_abbrev';
411  }
412 
417  protected function _getFetchJoins() {
418  return 'LEFT JOIN series se ON se.series_id = s.series_id
419  LEFT JOIN series_settings stpl ON (se.series_id = stpl.series_id AND stpl.setting_name = ? AND stpl.locale = ?)
420  LEFT JOIN series_settings stl ON (se.series_id = stl.series_id AND stl.setting_name = ? AND stl.locale = ?)
421  LEFT JOIN series_settings sapl ON (se.series_id = sapl.series_id AND sapl.setting_name = ? AND sapl.locale = ?)
422  LEFT JOIN series_settings sal ON (se.series_id = sal.series_id AND sal.setting_name = ? AND sal.locale = ?)';
423  }
424 }
425 
426 ?>
static & getDAO($name, $dbconn=null)
updateObject($monograph)
removeCategories($monographId)
deleteTextIndex($monographId, $type=null, $assocId=null)
Class for a Monograph.
static getPrimaryLocale()
insertObject($monograph)
getCategories($monographId, $pressId=null)
Operations for retrieving and modifying Monograph objects.
getUnassignedCategories($monographId, $pressId=null)
static getLocale()
static call($hookName, $args=null)
getUnpublishedMonographsByPressId($pressId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
removeMonographsFromSeries($seriesId)
getBySubEditorId($contextId=null, $subEditorId=null, $includeDeclined=true, $includePublished=true, $rangeInfo=null)
deleteById($monographId)
removeCategory($monographId, $categoryId)
categoryAssociationExists($monographId, $categoryId)
addCategory($monographId, $categoryId)
getByPressId($pressId)