Open Journal Systems  2.4.8
 All Classes Namespaces Functions Variables Groups Pages
ArticleGalleyDAO.inc.php
1 <?php
2 
17 import('classes.article.ArticleGalley');
18 import('classes.article.ArticleHTMLGalley');
19 
20 class ArticleGalleyDAO extends DAO {
23 
27  function ArticleGalleyDAO() {
28  parent::DAO();
29  $this->articleFileDao =& DAORegistry::getDAO('ArticleFileDAO');
30  }
31 
36  function &_getGalleyCache() {
37  if (!isset($this->galleyCache)) {
38  $cacheManager =& CacheManager::getManager();
39  $this->galleyCache =& $cacheManager->getObjectCache('galley', 0, array(&$this, '_galleyCacheMiss'));
40  }
41  return $this->galleyCache;
42  }
43 
50  function &_galleyCacheMiss(&$cache, $id) {
51  $galley =& $this->getGalley($id, null, false);
52  $cache->setCache($id, $galley);
53  return $galley;
54  }
55 
63  function &getGalley($galleyId, $articleId = null, $useCache = false) {
64  if ($useCache) {
65  $cache =& $this->_getGalleyCache();
66  $returner = $cache->get($galleyId);
67  if ($returner && $articleId != null && $articleId != $returner->getArticleId()) $returner = null;
68  return $returner;
69  }
70 
71  $params = array((int) $galleyId);
72  if ($articleId !== null) $params[] = (int) $articleId;
73  $result =& $this->retrieve(
74  'SELECT g.*,
75  a.file_name, a.original_file_name, a.file_stage, a.file_type, a.file_size, a.date_uploaded, a.date_modified
76  FROM article_galleys g
77  LEFT JOIN article_files a ON (g.file_id = a.file_id)
78  WHERE g.galley_id = ?' .
79  ($articleId !== null?' AND g.article_id = ?':''),
80  $params
81  );
82 
83  $returner = null;
84  if ($result->RecordCount() != 0) {
85  $returner =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
86  } else {
87  HookRegistry::call('ArticleGalleyDAO::getNewGalley', array(&$galleyId, &$articleId, &$returner));
88  }
89 
90  $result->Close();
91  unset($result);
92 
93  return $returner;
94  }
95 
107  function pubIdExists($pubIdType, $pubId, $galleyId, $journalId) {
108  $result =& $this->retrieve(
109  'SELECT COUNT(*)
110  FROM article_galley_settings ags
111  INNER JOIN article_galleys ag ON ags.galley_id = ag.galley_id
112  INNER JOIN articles a ON ag.article_id = a.article_id
113  WHERE ags.setting_name = ? AND ags.setting_value = ? AND ags.galley_id <> ? AND a.journal_id = ?',
114  array(
115  'pub-id::'.$pubIdType,
116  $pubId,
117  (int) $galleyId,
118  (int) $journalId
119  )
120  );
121  $returner = $result->fields[0] ? true : false;
122  $result->Close();
123  return $returner;
124  }
125 
135  function &getGalleyByPubId($pubIdType, $pubId, $articleId = null) {
136  $galleys =& $this->getGalleysBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
137  if (empty($galleys)) {
138  $galley = null;
139  } else {
140  assert(count($galleys) == 1);
141  $galley =& $galleys[0];
142  }
143 
144  return $galley;
145  }
146 
155  function &getGalleysBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
156  $params = array($settingName);
157 
158  $sql = 'SELECT g.*,
159  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
160  FROM article_galleys g
161  LEFT JOIN article_files af ON (g.file_id = af.file_id)
162  INNER JOIN articles a ON a.article_id = g.article_id
163  LEFT JOIN published_articles pa ON g.article_id = pa.article_id ';
164  if (is_null($settingValue)) {
165  $sql .= 'LEFT JOIN article_galley_settings gs ON g.galley_id = gs.galley_id AND gs.setting_name = ?
166  WHERE (gs.setting_value IS NULL OR gs.setting_value = \'\')';
167  } else {
168  $params[] = $settingValue;
169  $sql .= 'INNER JOIN article_galley_settings gs ON g.galley_id = gs.galley_id
170  WHERE gs.setting_name = ? AND gs.setting_value = ?';
171  }
172  if ($articleId) {
173  $params[] = (int) $articleId;
174  $sql .= ' AND g.article_id = ?';
175  }
176  if ($journalId) {
177  $params[] = (int) $journalId;
178  $sql .= ' AND a.journal_id = ?';
179  }
180  $sql .= ' ORDER BY a.journal_id, pa.issue_id, g.galley_id';
181  $result =& $this->retrieve($sql, $params);
182 
183  $galleys = array();
184  while (!$result->EOF) {
185  $galleys[] =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
186  $result->moveNext();
187  }
188  $result->Close();
189 
190  return $galleys;
191  }
192 
198  function &getGalleysByArticle($articleId) {
199  $galleys = array();
200 
201  $result =& $this->retrieve(
202  'SELECT g.*,
203  a.file_name, a.original_file_name, a.file_stage, a.file_type, a.file_size, a.date_uploaded, a.date_modified
204  FROM article_galleys g
205  LEFT JOIN article_files a ON (g.file_id = a.file_id)
206  WHERE g.article_id = ? ORDER BY g.seq',
207  (int) $articleId
208  );
209 
210  while (!$result->EOF) {
211  $galleys[] =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
212  $result->moveNext();
213  }
214 
215  $result->Close();
216  unset($result);
217 
218  HookRegistry::call('ArticleGalleyDAO::getArticleGalleys', array(&$galleys, &$articleId)); // FIXME: XMLGalleyPlugin uses this; should convert to DAO auto call
219 
220  return $galleys;
221  }
222 
228  function &getGalleysByJournalId($journalId) {
229  $result =& $this->retrieve(
230  'SELECT
231  g.*,
232  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
233  FROM article_galleys g
234  LEFT JOIN article_files af ON (g.file_id = af.file_id)
235  INNER JOIN articles a ON (g.article_id = a.article_id)
236  WHERE a.journal_id = ?',
237  (int) $journalId
238  );
239 
240  $returner = new DAOResultFactory($result, $this, '_returnGalleyFromRow');
241  return $returner;
242  }
243 
251  function &getGalleyByBestGalleyId($galleyId, $articleId) {
252  if ($galleyId != '') $galley =& $this->getGalleyByPubId('publisher-id', $galleyId, $articleId);
253  if (!isset($galley) && ctype_digit("$galleyId")) $galley =& $this->getGalley((int) $galleyId, $articleId);
254  return $galley;
255  }
256 
261  function getLocaleFieldNames() {
262  return array();
263  }
264 
271  $additionalFields = parent::getAdditionalFieldNames();
272  // FIXME: Move this to a PID plug-in.
273  $additionalFields[] = 'pub-id::publisher-id';
274  return $additionalFields;
275  }
276 
281  function updateLocaleFields(&$galley) {
282  $this->updateDataObjectSettings('article_galley_settings', $galley, array(
283  'galley_id' => $galley->getId()
284  ));
285  }
286 
292  function &_returnGalleyFromRow(&$row) {
293  if ($row['html_galley']) {
294  $galley = new ArticleHTMLGalley();
295 
296  // HTML-specific settings
297  $galley->setStyleFileId($row['style_file_id']);
298  if ($row['style_file_id']) {
299  $galley->setStyleFile($this->articleFileDao->getArticleFile($row['style_file_id']));
300  }
301 
302  // Retrieve images
303  $images =& $this->getGalleyImages($row['galley_id']);
304  $galley->setImageFiles($images);
305 
306  } else {
307  $galley = new ArticleGalley();
308  }
309  $galley->setId($row['galley_id']);
310  $galley->setArticleId($row['article_id']);
311  $galley->setLocale($row['locale']);
312  $galley->setFileId($row['file_id']);
313  $galley->setLabel($row['label']);
314  $galley->setFileStage($row['file_stage']);
315  $galley->setSequence($row['seq']);
316  $galley->setRemoteURL($row['remote_url']);
317 
318  // ArticleFile set methods
319  $galley->setFileName($row['file_name']);
320  $galley->setOriginalFileName($row['original_file_name']);
321  $galley->setFileType($row['file_type']);
322  $galley->setFileSize($row['file_size']);
323  $galley->setDateModified($this->datetimeFromDB($row['date_modified']));
324  $galley->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
325 
326  $this->getDataObjectSettings('article_galley_settings', 'galley_id', $row['galley_id'], $galley);
327 
328  HookRegistry::call('ArticleGalleyDAO::_returnGalleyFromRow', array(&$galley, &$row));
329 
330  return $galley;
331  }
332 
337  function insertGalley(&$galley) {
338  $this->update(
339  'INSERT INTO article_galleys
340  (article_id, file_id, label, locale, html_galley, style_file_id, seq, remote_url)
341  VALUES
342  (?, ?, ?, ?, ?, ?, ?, ?)',
343  array(
344  (int) $galley->getArticleId(),
345  (int) $galley->getFileId(),
346  $galley->getLabel(),
347  $galley->getLocale(),
348  (int) $galley->isHTMLGalley(),
349  $galley->isHTMLGalley() ? (int) $galley->getStyleFileId() : null,
350  $galley->getSequence() == null ? $this->getNextGalleySequence($galley->getArticleId()) : $galley->getSequence(),
351  $galley->getRemoteURL()
352  )
353  );
354  $galley->setId($this->getInsertGalleyId());
355  $this->updateLocaleFields($galley);
356 
357  HookRegistry::call('ArticleGalleyDAO::insertNewGalley', array(&$galley, $galley->getId()));
358 
359  return $galley->getId();
360  }
361 
366  function updateGalley(&$galley) {
367  $this->update(
368  'UPDATE article_galleys
369  SET
370  file_id = ?,
371  label = ?,
372  locale = ?,
373  html_galley = ?,
374  style_file_id = ?,
375  seq = ?,
376  remote_url = ?
377  WHERE galley_id = ?',
378  array(
379  (int) $galley->getFileId(),
380  $galley->getLabel(),
381  $galley->getLocale(),
382  (int) $galley->isHTMLGalley(),
383  $galley->isHTMLGalley() ? (int) $galley->getStyleFileId() : null,
384  $galley->getSequence(),
385  $galley->getRemoteURL(),
386  (int) $galley->getId()
387  )
388  );
389  $this->updateLocaleFields($galley);
390  }
391 
396  function deleteGalley(&$galley) {
397  return $this->deleteGalleyById($galley->getId());
398  }
399 
405  function deleteGalleyById($galleyId, $articleId = null) {
406 
407  HookRegistry::call('ArticleGalleyDAO::deleteGalleyById', array(&$galleyId, &$articleId));
408 
409  if (isset($articleId)) {
410  $this->update(
411  'DELETE FROM article_galleys WHERE galley_id = ? AND article_id = ?',
412  array((int) $galleyId, (int) $articleId)
413  );
414  } else {
415  $this->update(
416  'DELETE FROM article_galleys WHERE galley_id = ?', (int) $galleyId
417  );
418  }
419  if ($this->getAffectedRows()) {
420  $this->update('DELETE FROM article_galley_settings WHERE galley_id = ?', array((int) $galleyId));
421  $this->deleteImagesByGalley($galleyId);
422  }
423  }
424 
430  function deleteGalleysByArticle($articleId) {
431  $galleys =& $this->getGalleysByArticle($articleId);
432  foreach ($galleys as $galley) {
433  $this->deleteGalleyById($galley->getId(), $articleId);
434  }
435  }
436 
443  function galleyExistsByFileId($articleId, $fileId) {
444  $result =& $this->retrieve(
445  'SELECT COUNT(*) FROM article_galleys
446  WHERE article_id = ? AND file_id = ?',
447  array((int) $articleId, (int) $fileId)
448  );
449 
450  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
451 
452  $result->Close();
453  unset($result);
454 
455  return $returner;
456  }
457 
462  function incrementViews($galleyId) {
463  if ( !HookRegistry::call('ArticleGalleyDAO::incrementGalleyViews', array(&$galleyId)) ) {
464  return $this->update(
465  'UPDATE article_galleys SET views = views + 1 WHERE galley_id = ?',
466  (int) $galleyId
467  );
468  } else return false;
469  }
470 
475  function resequenceGalleys($articleId) {
476  $result =& $this->retrieve(
477  'SELECT galley_id FROM article_galleys WHERE article_id = ? ORDER BY seq',
478  (int) $articleId
479  );
480 
481  for ($i=1; !$result->EOF; $i++) {
482  list($galleyId) = $result->fields;
483  $this->update(
484  'UPDATE article_galleys SET seq = ? WHERE galley_id = ?',
485  array($i, $galleyId)
486  );
487  $result->moveNext();
488  }
489 
490  $result->close();
491  unset($result);
492  }
493 
499  function getNextGalleySequence($articleId) {
500  $result =& $this->retrieve(
501  'SELECT MAX(seq) + 1 FROM article_galleys WHERE article_id = ?',
502  (int) $articleId
503  );
504  $returner = floor($result->fields[0]);
505 
506  $result->Close();
507  unset($result);
508 
509  return $returner;
510  }
511 
516  function getInsertGalleyId() {
517  return $this->getInsertId('article_galleys', 'galley_id');
518  }
519 
520 
521  //
522  // Extra routines specific to HTML galleys.
523  //
524 
530  function &getGalleyImages($galleyId) {
531  $images = array();
532 
533  $result =& $this->retrieve(
534  'SELECT a.* FROM article_html_galley_images i, article_files a
535  WHERE i.file_id = a.file_id AND i.galley_id = ?',
536  (int) $galleyId
537  );
538 
539  while (!$result->EOF) {
540  $images[] =& $this->articleFileDao->_returnArticleFileFromRow($result->GetRowAssoc(false));
541  $result->MoveNext();
542  }
543 
544  $result->Close();
545  unset($result);
546 
547  return $images;
548  }
549 
555  function insertGalleyImage($galleyId, $fileId) {
556  return $this->update(
557  'INSERT INTO article_html_galley_images
558  (galley_id, file_id)
559  VALUES
560  (?, ?)',
561  array((int) $galleyId, (int) $fileId)
562  );
563  }
564 
570  function deleteGalleyImage($galleyId, $fileId) {
571  return $this->update(
572  'DELETE FROM article_html_galley_images
573  WHERE galley_id = ? AND file_id = ?',
574  array((int) $galleyId, (int) $fileId)
575  );
576  }
577 
582  function deleteImagesByGalley($galleyId) {
583  return $this->update(
584  'DELETE FROM article_html_galley_images WHERE galley_id = ?',
585  (int) $galleyId
586  );
587  }
588 
597  function changePubId($galleyId, $pubIdType, $pubId) {
598  $idFields = array(
599  'galley_id', 'locale', 'setting_name'
600  );
601  $updateArray = array(
602  'galley_id' => $galleyId,
603  'locale' => '',
604  'setting_name' => 'pub-id::'.$pubIdType,
605  'setting_type' => 'string',
606  'setting_value' => (string)$pubId
607  );
608  $this->replace('article_galley_settings', $updateArray, $idFields);
609  }
610 
618  function deleteAllPubIds($journalId, $pubIdType) {
619  $journalId = (int) $journalId;
620  $settingName = 'pub-id::'.$pubIdType;
621 
622  $galleys =& $this->getGalleysByJournalId($journalId);
623  while ($galley =& $galleys->next()) {
624  $this->update(
625  'DELETE FROM article_galley_settings WHERE setting_name = ? AND galley_id = ?',
626  array(
627  $settingName,
628  (int)$galley->getId()
629  )
630  );
631  unset($galley);
632  }
633  $this->flushCache();
634  }
635 
643  function deletePubId($galleyId, $pubIdType) {
644  $settingName = 'pub-id::'.$pubIdType;
645  $this->update(
646  'DELETE FROM article_galley_settings WHERE setting_name = ? AND galley_id = ?',
647  array(
648  $settingName,
649  (int)$galleyId
650  )
651  );
652  $this->flushCache();
653  }
654 
658  function flushCache() {
659  $cache =& $this->_getGalleyCache();
660  $cache->flush();
661  unset($cache);
662  }
663 }
664 
665 ?>
getNextGalleySequence($articleId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& _galleyCacheMiss(&$cache, $id)
changePubId($galleyId, $pubIdType, $pubId)
deleteAllPubIds($journalId, $pubIdType)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
insertGalleyImage($galleyId, $fileId)
& getGalleysByArticle($articleId)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:460
deletePubId($galleyId, $pubIdType)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
getAffectedRows()
Definition: DAO.inc.php:261
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
pubIdExists($pubIdType, $pubId, $galleyId, $journalId)
deleteGalleyImage($galleyId, $fileId)
deleteGalleyById($galleyId, $articleId=null)
& getDAO($name, $dbconn=null)
deleteGalleysByArticle($articleId)
galleyExistsByFileId($articleId, $fileId)
call($hookName, $args=null)
An HTML galley may include an optional stylesheet and set of images.
& getGalley($galleyId, $articleId=null, $useCache=false)
A galley is a final presentation version of the full-text of an article.
& getGalleyImages($galleyId)
Operations for retrieving and modifying ArticleGalley/ArticleHTMLGalley objects.
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
& getGalleysBySetting($settingName, $settingValue, $articleId=null, $journalId=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getGalleyByPubId($pubIdType, $pubId, $articleId=null)
& getGalleyByBestGalleyId($galleyId, $articleId)
& getGalleysByJournalId($journalId)