Open Journal Systems  2.4.4
 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 
38  function &getGalley($galleyId, $articleId = null) {
39  $params = array((int) $galleyId);
40  if ($articleId !== null) $params[] = (int) $articleId;
41  $result =& $this->retrieve(
42  'SELECT g.*,
43  a.file_name, a.original_file_name, a.file_stage, a.file_type, a.file_size, a.date_uploaded, a.date_modified
44  FROM article_galleys g
45  LEFT JOIN article_files a ON (g.file_id = a.file_id)
46  WHERE g.galley_id = ?' .
47  ($articleId !== null?' AND g.article_id = ?':''),
48  $params
49  );
50 
51  $returner = null;
52  if ($result->RecordCount() != 0) {
53  $returner =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
54  } else {
55  HookRegistry::call('ArticleGalleyDAO::getNewGalley', array(&$galleyId, &$articleId, &$returner));
56  }
57 
58  $result->Close();
59  unset($result);
60 
61  return $returner;
62  }
63 
75  function pubIdExists($pubIdType, $pubId, $galleyId, $journalId) {
76  $result =& $this->retrieve(
77  'SELECT COUNT(*)
78  FROM article_galley_settings ags
79  INNER JOIN article_galleys ag ON ags.galley_id = ag.galley_id
80  INNER JOIN articles a ON ag.article_id = a.article_id
81  WHERE ags.setting_name = ? AND ags.setting_value = ? AND ags.galley_id <> ? AND a.journal_id = ?',
82  array(
83  'pub-id::'.$pubIdType,
84  $pubId,
85  (int) $galleyId,
86  (int) $journalId
87  )
88  );
89  $returner = $result->fields[0] ? true : false;
90  $result->Close();
91  return $returner;
92  }
93 
103  function &getGalleyByPubId($pubIdType, $pubId, $articleId = null) {
104  $galleys =& $this->getGalleysBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
105  if (empty($galleys)) {
106  $galley = null;
107  } else {
108  assert(count($galleys) == 1);
109  $galley =& $galleys[0];
110  }
111 
112  return $galley;
113  }
114 
123  function &getGalleysBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
124  $params = array($settingName);
125 
126  $sql = 'SELECT g.*,
127  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
128  FROM article_galleys g
129  LEFT JOIN article_files af ON (g.file_id = af.file_id)
130  INNER JOIN articles a ON a.article_id = g.article_id
131  LEFT JOIN published_articles pa ON g.article_id = pa.article_id ';
132  if (is_null($settingValue)) {
133  $sql .= 'LEFT JOIN article_galley_settings gs ON g.galley_id = gs.galley_id AND gs.setting_name = ?
134  WHERE (gs.setting_value IS NULL OR gs.setting_value = "")';
135  } else {
136  $params[] = $settingValue;
137  $sql .= 'INNER JOIN article_galley_settings gs ON g.galley_id = gs.galley_id
138  WHERE gs.setting_name = ? AND gs.setting_value = ?';
139  }
140  if ($articleId) {
141  $params[] = (int) $articleId;
142  $sql .= ' AND g.article_id = ?';
143  }
144  if ($journalId) {
145  $params[] = (int) $journalId;
146  $sql .= ' AND a.journal_id = ?';
147  }
148  $sql .= ' ORDER BY a.journal_id, pa.issue_id, g.galley_id';
149  $result =& $this->retrieve($sql, $params);
150 
151  $galleys = array();
152  while (!$result->EOF) {
153  $galleys[] =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
154  $result->moveNext();
155  }
156  $result->Close();
157 
158  return $galleys;
159  }
160 
166  function &getGalleysByArticle($articleId) {
167  $galleys = array();
168 
169  $result =& $this->retrieve(
170  'SELECT g.*,
171  a.file_name, a.original_file_name, a.file_stage, a.file_type, a.file_size, a.date_uploaded, a.date_modified
172  FROM article_galleys g
173  LEFT JOIN article_files a ON (g.file_id = a.file_id)
174  WHERE g.article_id = ? ORDER BY g.seq',
175  (int) $articleId
176  );
177 
178  while (!$result->EOF) {
179  $galleys[] =& $this->_returnGalleyFromRow($result->GetRowAssoc(false));
180  $result->moveNext();
181  }
182 
183  $result->Close();
184  unset($result);
185 
186  HookRegistry::call('ArticleGalleyDAO::getArticleGalleys', array(&$galleys, &$articleId)); // FIXME: XMLGalleyPlugin uses this; should convert to DAO auto call
187 
188  return $galleys;
189  }
190 
196  function &getGalleysByJournalId($journalId) {
197  $result =& $this->retrieve(
198  'SELECT
199  g.*,
200  af.file_name, af.original_file_name, af.file_stage, af.file_type, af.file_size, af.date_uploaded, af.date_modified
201  FROM article_galleys g
202  LEFT JOIN article_files af ON (g.file_id = af.file_id)
203  INNER JOIN articles a ON (g.article_id = a.article_id)
204  WHERE a.journal_id = ?',
205  (int) $journalId
206  );
207 
208  $returner = new DAOResultFactory($result, $this, '_returnGalleyFromRow');
209  return $returner;
210  }
211 
219  function &getGalleyByBestGalleyId($galleyId, $articleId) {
220  if ($galleyId != '') $galley =& $this->getGalleyByPubId('publisher-id', $galleyId, $articleId);
221  if (!isset($galley) && ctype_digit("$galleyId")) $galley =& $this->getGalley((int) $galleyId, $articleId);
222  return $galley;
223  }
224 
229  function getLocaleFieldNames() {
230  return array();
231  }
232 
239  $additionalFields = parent::getAdditionalFieldNames();
240  // FIXME: Move this to a PID plug-in.
241  $additionalFields[] = 'pub-id::publisher-id';
242  return $additionalFields;
243  }
244 
249  function updateLocaleFields(&$galley) {
250  $this->updateDataObjectSettings('article_galley_settings', $galley, array(
251  'galley_id' => $galley->getId()
252  ));
253  }
254 
260  function &_returnGalleyFromRow(&$row) {
261  if ($row['html_galley']) {
262  $galley = new ArticleHTMLGalley();
263 
264  // HTML-specific settings
265  $galley->setStyleFileId($row['style_file_id']);
266  if ($row['style_file_id']) {
267  $galley->setStyleFile($this->articleFileDao->getArticleFile($row['style_file_id']));
268  }
269 
270  // Retrieve images
271  $images =& $this->getGalleyImages($row['galley_id']);
272  $galley->setImageFiles($images);
273 
274  } else {
275  $galley = new ArticleGalley();
276  }
277  $galley->setId($row['galley_id']);
278  $galley->setArticleId($row['article_id']);
279  $galley->setLocale($row['locale']);
280  $galley->setFileId($row['file_id']);
281  $galley->setLabel($row['label']);
282  $galley->setFileStage($row['file_stage']);
283  $galley->setSequence($row['seq']);
284  $galley->setRemoteURL($row['remote_url']);
285 
286  // ArticleFile set methods
287  $galley->setFileName($row['file_name']);
288  $galley->setOriginalFileName($row['original_file_name']);
289  $galley->setFileType($row['file_type']);
290  $galley->setFileSize($row['file_size']);
291  $galley->setDateModified($this->datetimeFromDB($row['date_modified']));
292  $galley->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
293 
294  $this->getDataObjectSettings('article_galley_settings', 'galley_id', $row['galley_id'], $galley);
295 
296  HookRegistry::call('ArticleGalleyDAO::_returnGalleyFromRow', array(&$galley, &$row));
297 
298  return $galley;
299  }
300 
305  function insertGalley(&$galley) {
306  $this->update(
307  'INSERT INTO article_galleys
308  (article_id, file_id, label, locale, html_galley, style_file_id, seq, remote_url)
309  VALUES
310  (?, ?, ?, ?, ?, ?, ?, ?)',
311  array(
312  (int) $galley->getArticleId(),
313  (int) $galley->getFileId(),
314  $galley->getLabel(),
315  $galley->getLocale(),
316  (int) $galley->isHTMLGalley(),
317  $galley->isHTMLGalley() ? (int) $galley->getStyleFileId() : null,
318  $galley->getSequence() == null ? $this->getNextGalleySequence($galley->getArticleId()) : $galley->getSequence(),
319  $galley->getRemoteURL()
320  )
321  );
322  $galley->setId($this->getInsertGalleyId());
323  $this->updateLocaleFields($galley);
324 
325  HookRegistry::call('ArticleGalleyDAO::insertNewGalley', array(&$galley, $galley->getId()));
326 
327  return $galley->getId();
328  }
329 
334  function updateGalley(&$galley) {
335  $this->update(
336  'UPDATE article_galleys
337  SET
338  file_id = ?,
339  label = ?,
340  locale = ?,
341  html_galley = ?,
342  style_file_id = ?,
343  seq = ?,
344  remote_url = ?
345  WHERE galley_id = ?',
346  array(
347  (int) $galley->getFileId(),
348  $galley->getLabel(),
349  $galley->getLocale(),
350  (int) $galley->isHTMLGalley(),
351  $galley->isHTMLGalley() ? (int) $galley->getStyleFileId() : null,
352  $galley->getSequence(),
353  $galley->getRemoteURL(),
354  (int) $galley->getId()
355  )
356  );
357  $this->updateLocaleFields($galley);
358  }
359 
364  function deleteGalley(&$galley) {
365  return $this->deleteGalleyById($galley->getId());
366  }
367 
373  function deleteGalleyById($galleyId, $articleId = null) {
374 
375  HookRegistry::call('ArticleGalleyDAO::deleteGalleyById', array(&$galleyId, &$articleId));
376 
377  if (isset($articleId)) {
378  $this->update(
379  'DELETE FROM article_galleys WHERE galley_id = ? AND article_id = ?',
380  array((int) $galleyId, (int) $articleId)
381  );
382  } else {
383  $this->update(
384  'DELETE FROM article_galleys WHERE galley_id = ?', (int) $galleyId
385  );
386  }
387  if ($this->getAffectedRows()) {
388  $this->update('DELETE FROM article_galley_settings WHERE galley_id = ?', array((int) $galleyId));
389  $this->deleteImagesByGalley($galleyId);
390  }
391  }
392 
398  function deleteGalleysByArticle($articleId) {
399  $galleys =& $this->getGalleysByArticle($articleId);
400  foreach ($galleys as $galley) {
401  $this->deleteGalleyById($galley->getId(), $articleId);
402  }
403  }
404 
411  function galleyExistsByFileId($articleId, $fileId) {
412  $result =& $this->retrieve(
413  'SELECT COUNT(*) FROM article_galleys
414  WHERE article_id = ? AND file_id = ?',
415  array((int) $articleId, (int) $fileId)
416  );
417 
418  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
419 
420  $result->Close();
421  unset($result);
422 
423  return $returner;
424  }
425 
430  function incrementViews($galleyId) {
431  if ( !HookRegistry::call('ArticleGalleyDAO::incrementGalleyViews', array(&$galleyId)) ) {
432  return $this->update(
433  'UPDATE article_galleys SET views = views + 1 WHERE galley_id = ?',
434  (int) $galleyId
435  );
436  } else return false;
437  }
438 
443  function resequenceGalleys($articleId) {
444  $result =& $this->retrieve(
445  'SELECT galley_id FROM article_galleys WHERE article_id = ? ORDER BY seq',
446  (int) $articleId
447  );
448 
449  for ($i=1; !$result->EOF; $i++) {
450  list($galleyId) = $result->fields;
451  $this->update(
452  'UPDATE article_galleys SET seq = ? WHERE galley_id = ?',
453  array($i, $galleyId)
454  );
455  $result->moveNext();
456  }
457 
458  $result->close();
459  unset($result);
460  }
461 
467  function getNextGalleySequence($articleId) {
468  $result =& $this->retrieve(
469  'SELECT MAX(seq) + 1 FROM article_galleys WHERE article_id = ?',
470  (int) $articleId
471  );
472  $returner = floor($result->fields[0]);
473 
474  $result->Close();
475  unset($result);
476 
477  return $returner;
478  }
479 
484  function getInsertGalleyId() {
485  return $this->getInsertId('article_galleys', 'galley_id');
486  }
487 
488 
489  //
490  // Extra routines specific to HTML galleys.
491  //
492 
498  function &getGalleyImages($galleyId) {
499  $images = array();
500 
501  $result =& $this->retrieve(
502  'SELECT a.* FROM article_html_galley_images i, article_files a
503  WHERE i.file_id = a.file_id AND i.galley_id = ?',
504  (int) $galleyId
505  );
506 
507  while (!$result->EOF) {
508  $images[] =& $this->articleFileDao->_returnArticleFileFromRow($result->GetRowAssoc(false));
509  $result->MoveNext();
510  }
511 
512  $result->Close();
513  unset($result);
514 
515  return $images;
516  }
517 
523  function insertGalleyImage($galleyId, $fileId) {
524  return $this->update(
525  'INSERT INTO article_html_galley_images
526  (galley_id, file_id)
527  VALUES
528  (?, ?)',
529  array((int) $galleyId, (int) $fileId)
530  );
531  }
532 
538  function deleteGalleyImage($galleyId, $fileId) {
539  return $this->update(
540  'DELETE FROM article_html_galley_images
541  WHERE galley_id = ? AND file_id = ?',
542  array((int) $galleyId, (int) $fileId)
543  );
544  }
545 
550  function deleteImagesByGalley($galleyId) {
551  return $this->update(
552  'DELETE FROM article_html_galley_images WHERE galley_id = ?',
553  (int) $galleyId
554  );
555  }
556 
565  function changePubId($galleyId, $pubIdType, $pubId) {
566  $idFields = array(
567  'galley_id', 'locale', 'setting_name'
568  );
569  $updateArray = array(
570  'galley_id' => $galleyId,
571  'locale' => '',
572  'setting_name' => 'pub-id::'.$pubIdType,
573  'setting_type' => 'string',
574  'setting_value' => (string)$pubId
575  );
576  $this->replace('article_galley_settings', $updateArray, $idFields);
577  }
578 
586  function deleteAllPubIds($journalId, $pubIdType) {
587  $journalId = (int) $journalId;
588  $settingName = 'pub-id::'.$pubIdType;
589 
590  $galleys =& $this->getGalleysByJournalId($journalId);
591  while ($galley =& $galleys->next()) {
592  $this->update(
593  'DELETE FROM article_galley_settings WHERE setting_name = ? AND galley_id = ?',
594  array(
595  $settingName,
596  (int)$galley->getId()
597  )
598  );
599  unset($galley);
600  }
601  $this->flushCache();
602  }
603 }
604 
605 ?>
getNextGalleySequence($articleId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
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
& getGalley($galleyId, $articleId=null)
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)
flushCache()
Definition: DAO.inc.php:285
An HTML galley may include an optional stylesheet and set of images.
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)