Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
ArticleGalleyDAO.inc.php
1 <?php
2 
17 import('classes.article.ArticleGalley');
18 import('lib.pkp.classes.submission.RepresentationDAO');
19 
24  function ArticleGalleyDAO() {
25  parent::RepresentationDAO();
26  }
27 
32  function newDataObject() {
33  return new ArticleGalley();
34  }
35 
39  function getById($galleyId, $submissionId = null, $contextId = null) {
40  $params = array((int) $galleyId);
41  if ($submissionId) $params[] = (int) $submissionId;
42  if ($contextId) $params[] = (int) $contextId;
43 
44  $result = $this->retrieve(
45  'SELECT g.*
46  FROM submission_galleys g
47  ' . ($contextId?' JOIN submissions s ON (s.submission_id = g.submission_id)':'') . '
48  WHERE g.galley_id = ?' .
49  ($submissionId !== null?' AND g.submission_id = ?':'') .
50  ($contextId?' AND s.context_id = ?':''),
51  $params
52  );
53 
54  $returner = null;
55  if ($result->RecordCount() != 0) {
56  $returner = $this->_fromRow($result->GetRowAssoc(false));
57  }
58  $result->Close();
59  HookRegistry::call('ArticleGalleyDAO::getById', array(&$galleyId, &$submissionId, &$returner));
60  return $returner;
61  }
62 
74  function pubIdExists($pubIdType, $pubId, $galleyId, $journalId) {
75  $result = $this->retrieve(
76  'SELECT COUNT(*)
77  FROM submission_galley_settings sgs
78  INNER JOIN submission_galleys sg ON sgs.galley_id = sg.galley_id
79  INNER JOIN submissions s ON sg.submission_id = s.submission_id
80  WHERE sgs.setting_name = ? AND sgs.setting_value = ? AND sgs.galley_id <> ? AND s.context_id = ?',
81  array(
82  'pub-id::'.$pubIdType,
83  $pubId,
84  (int) $galleyId,
85  (int) $journalId
86  )
87  );
88  $returner = $result->fields[0] ? true : false;
89  $result->Close();
90  return $returner;
91  }
92 
102  function getGalleyByPubId($pubIdType, $pubId, $articleId = null) {
103  $galleyFactory = $this->getGalleysBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
104  if ($galleyFactory->wasEmpty()) return null;
105 
106  assert($galleyFactory->getCount() == 1);
107  return $galleyFactory->next();
108  }
109 
118  function getGalleysBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
119  $params = array($settingName);
120 
121  $sql = 'SELECT g.*
122  FROM submission_galleys g
123  INNER JOIN submissions a ON a.submission_id = g.submission_id
124  LEFT JOIN published_submissions pa ON g.submission_id = pa.submission_id ';
125  if (is_null($settingValue)) {
126  $sql .= 'LEFT JOIN submission_galley_settings gs ON g.galley_id = gs.galley_id AND gs.setting_name = ?
127  WHERE (gs.setting_value IS NULL OR gs.setting_value = "")';
128  } else {
129  $params[] = $settingValue;
130  $sql .= 'INNER JOIN submission_galley_settings gs ON g.galley_id = gs.galley_id
131  WHERE gs.setting_name = ? AND gs.setting_value = ?';
132  }
133  if ($articleId) {
134  $params[] = (int) $articleId;
135  $sql .= ' AND g.submission_id = ?';
136  }
137  if ($journalId) {
138  $params[] = (int) $journalId;
139  $sql .= ' AND a.context_id = ?';
140  }
141  $sql .= ' ORDER BY a.context_id, pa.issue_id, g.galley_id';
142  $result = $this->retrieve($sql, $params);
143 
144  return new DAOResultFactory($result, $this, '_fromRow');
145  }
146 
150  function getBySubmissionId($submissionId, $contextId = null) {
151  $params = array((int) $submissionId);
152  if ($contextId) $params[] = (int) $contextId;
153 
154  return new DAOResultFactory(
155  $this->retrieve(
156  'SELECT g.*
157  FROM submission_galleys g ' .
158  ($contextId?'INNER JOIN submissions s ON (g.submission_id = s.submission_id) ':'') .
159  'WHERE g.submission_id = ? ' .
160  ($contextId?' AND s.context_id = ? ':'') .
161  'ORDER BY g.seq',
162  $params
163  ),
164  $this, '_fromRow'
165  );
166  }
167 
173  function getByJournalId($journalId) {
174  $result = $this->retrieve(
175  'SELECT g.*
176  FROM submission_galleys g
177  INNER JOIN submissions a ON (g.submission_id = a.submission_id)
178  WHERE a.context_id = ?',
179  (int) $journalId
180  );
181 
182  return new DAOResultFactory($result, $this, '_fromRow');
183  }
184 
192  function getByBestGalleyId($galleyId, $articleId) {
193  $galley = null;
194  if ($galleyId != '') $galley = $this->getGalleyByPubId('publisher-id', $galleyId, $articleId);
195  if (!isset($galley) && ctype_digit("$galleyId")) $galley = $this->getById((int) $galleyId, $articleId);
196  return $galley;
197  }
198 
203  function getLocaleFieldNames() {
204  return array();
205  }
206 
213  $additionalFields = parent::getAdditionalFieldNames();
214  // FIXME: Move this to a PID plug-in.
215  $additionalFields[] = 'pub-id::publisher-id';
216  return $additionalFields;
217  }
218 
223  function updateLocaleFields($galley) {
224  $this->updateDataObjectSettings('submission_galley_settings', $galley, array(
225  'galley_id' => $galley->getId()
226  ));
227  }
228 
234  function _fromRow($row) {
235  $galley = $this->newDataObject();
236 
237  $galley->setId($row['galley_id']);
238  $galley->setSubmissionId($row['submission_id']);
239  $galley->setLocale($row['locale']);
240  $galley->setLabel($row['label']);
241  $galley->setSequence($row['seq']);
242  $galley->setRemoteURL($row['remote_url']);
243  $galley->setIsApproved($row['is_approved']);
244  $galley->setGalleyType($row['galley_type']);
245 
246  $this->getDataObjectSettings('submission_galley_settings', 'galley_id', $row['galley_id'], $galley);
247 
248  HookRegistry::call('ArticleGalleyDAO::_fromRow', array(&$galley, &$row));
249 
250  return $galley;
251  }
252 
257  function insertObject($galley) {
258  $this->update(
259  'INSERT INTO submission_galleys
260  (submission_id, label, locale, seq, remote_url, is_approved, galley_type)
261  VALUES
262  (?, ?, ?, ?, ?, ?, ?)',
263  array(
264  (int) $galley->getSubmissionId(),
265  $galley->getLabel(),
266  $galley->getLocale(),
267  $galley->getSequence() == null ? $this->getNextGalleySequence($galley->getSubmissionId()) : $galley->getSequence(),
268  $galley->getRemoteURL(),
269  $galley->getIsApproved()?1:0,
270  $galley->getGalleyType(),
271  )
272  );
273  $galley->setId($this->getInsertId());
274  $this->updateLocaleFields($galley);
275 
276  HookRegistry::call('ArticleGalleyDAO::insertNewGalley', array(&$galley, $galley->getId()));
277 
278  return $galley->getId();
279  }
280 
285  function updateObject($galley) {
286  $this->update(
287  'UPDATE submission_galleys
288  SET
289  label = ?,
290  locale = ?,
291  seq = ?,
292  remote_url = ?,
293  is_approved = ?,
294  galley_type = ?
295  WHERE galley_id = ?',
296  array(
297  $galley->getLabel(),
298  $galley->getLocale(),
299  $galley->getSequence(),
300  $galley->getRemoteURL(),
301  (int) $galley->getIsApproved(),
302  $galley->getGalleyType(),
303  (int) $galley->getId(),
304  )
305  );
306  $this->updateLocaleFields($galley);
307  }
308 
313  function deleteObject($galley) {
314  return $this->deleteById($galley->getId());
315  }
316 
322  function deleteById($galleyId, $articleId = null) {
323 
324  HookRegistry::call('ArticleGalleyDAO::deleteById', array(&$galleyId, &$articleId));
325 
326  $params = array((int) $galleyId);
327  if ($articleId) $params[] = (int) $articleId;
328  $this->update(
329  'DELETE FROM submission_galleys
330  WHERE galley_id = ?'
331  . ($articleId?' AND submission_id = ?':''),
332  $params
333  );
334  if ($this->getAffectedRows()) {
335  $this->update('DELETE FROM submission_galley_settings WHERE galley_id = ?', array((int) $galleyId));
336  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
337  import('lib.pkp.classes.submission.SubmissionFile'); // Import constants
338 
339  $galleyFiles = $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galleyId, $articleId, SUBMISSION_FILE_PROOF);
340  foreach ($galleyFiles as $file) {
341  // delete dependent files for each galley file
342  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $file->getFileId(), SUBMISSION_FILE_DEPENDENT);
343  }
344  // delete the galley files.
345  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galleyId, SUBMISSION_FILE_PROOF);
346  }
347  }
348 
354  function deleteByArticleId($articleId) {
355  $galleys = $this->getBySubmissionId($articleId);
356  while ($galley = $galleys->next()) {
357  $this->deleteById($galley->getId(), $articleId);
358  }
359  }
360 
365  function resequenceGalleys($articleId) {
366  $result = $this->retrieve(
367  'SELECT galley_id FROM submission_galleys WHERE submission_id = ? ORDER BY seq',
368  (int) $articleId
369  );
370 
371  for ($i=1; !$result->EOF; $i++) {
372  list($galleyId) = $result->fields;
373  $this->update(
374  'UPDATE submission_galleys SET seq = ? WHERE galley_id = ?',
375  array($i, $galleyId)
376  );
377  $result->MoveNext();
378  }
379  $result->Close();
380  }
381 
387  function getNextGalleySequence($articleId) {
388  $result = $this->retrieve(
389  'SELECT MAX(seq) + 1 FROM submission_galleys WHERE submission_id = ?',
390  (int) $articleId
391  );
392  $returner = floor($result->fields[0]);
393 
394  $result->Close();
395  return $returner;
396  }
397 
402  function getInsertId() {
403  return $this->_getInsertId('submission_galleys', 'galley_id');
404  }
405 
414  function changePubId($galleyId, $pubIdType, $pubId) {
415  $idFields = array(
416  'galley_id', 'locale', 'setting_name'
417  );
418  $updateArray = array(
419  'galley_id' => $galleyId,
420  'locale' => '',
421  'setting_name' => 'pub-id::'.$pubIdType,
422  'setting_type' => 'string',
423  'setting_value' => (string)$pubId
424  );
425  $this->replace('submission_galley_settings', $updateArray, $idFields);
426  }
427 
435  function deleteAllPubIds($journalId, $pubIdType) {
436  $journalId = (int) $journalId;
437  $settingName = 'pub-id::'.$pubIdType;
438 
439  $galleys = $this->getByJournalId($journalId);
440  while ($galley = $galleys->next()) {
441  $this->update(
442  'DELETE FROM submission_galley_settings WHERE setting_name = ? AND galley_id = ?',
443  array(
444  $settingName,
445  (int)$galley->getId()
446  )
447  );
448  }
449  $this->flushCache();
450  }
451 }
452 
453 ?>
static & getDAO($name, $dbconn=null)
getGalleyByPubId($pubIdType, $pubId, $articleId=null)
Abstract DAO for fetching/working with DB storage of Representation objects.
getNextGalleySequence($articleId)
changePubId($galleyId, $pubIdType, $pubId)
deleteAllPubIds($journalId, $pubIdType)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getById($galleyId, $submissionId=null, $contextId=null)
getGalleysBySetting($settingName, $settingValue, $articleId=null, $journalId=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getAffectedRows()
Definition: DAO.inc.php:258
static call($hookName, $args=null)
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:474
deleteById($galleyId, $articleId=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
pubIdExists($pubIdType, $pubId, $galleyId, $journalId)
getByBestGalleyId($galleyId, $articleId)
flushCache()
Definition: DAO.inc.php:282
A galley is a final presentation version of the full-text of an article.
getBySubmissionId($submissionId, $contextId=null)
Operations for retrieving and modifying ArticleGalley objects.
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:568
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208