Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
ArticleGalleyDAO.inc.php
1 <?php
2 
17 import('classes.article.ArticleGalley');
18 
19 class ArticleGalleyDAO extends DAO {
23  function ArticleGalleyDAO() {
24  parent::DAO();
25  }
26 
31  function newDataObject() {
32  return new ArticleGalley();
33  }
34 
41  function getById($galleyId, $submissionId = null) {
42  $params = array((int) $galleyId);
43  if ($submissionId !== null) $params[] = (int) $submissionId;
44  $result = $this->retrieve(
45  'SELECT *
46  FROM submission_galleys
47  WHERE galley_id = ?' .
48  ($submissionId !== null?' AND submission_id = ?':''),
49  $params
50  );
51 
52  $returner = null;
53  if ($result->RecordCount() != 0) {
54  $returner = $this->_fromRow($result->GetRowAssoc(false));
55  }
56  $result->Close();
57  HookRegistry::call('ArticleGalleyDAO::getById', array(&$galleyId, &$submissionId, &$returner));
58  return $returner;
59  }
60 
72  function pubIdExists($pubIdType, $pubId, $galleyId, $journalId) {
73  $result = $this->retrieve(
74  'SELECT COUNT(*)
75  FROM submission_galley_settings sgs
76  INNER JOIN submission_galleys sg ON sgs.galley_id = sg.galley_id
77  INNER JOIN submissions s ON sg.submission_id = s.submission_id
78  WHERE sgs.setting_name = ? AND sgs.setting_value = ? AND sgs.galley_id <> ? AND s.context_id = ?',
79  array(
80  'pub-id::'.$pubIdType,
81  $pubId,
82  (int) $galleyId,
83  (int) $journalId
84  )
85  );
86  $returner = $result->fields[0] ? true : false;
87  $result->Close();
88  return $returner;
89  }
90 
100  function &getGalleyByPubId($pubIdType, $pubId, $articleId = null) {
101  $galleyFactory =& $this->getGalleysBySetting('pub-id::'.$pubIdType, $pubId, $articleId);
102  if ($galleyFactory->wasEmpty()) {
103  $galley = null;
104  } else {
105  assert($galleyFactory->getCount() == 1);
106  $galley =& $galleyFactory->next();
107  }
108 
109  return $galley;
110  }
111 
120  function getGalleysBySetting($settingName, $settingValue, $articleId = null, $journalId = null) {
121  $params = array($settingName);
122 
123  $sql = 'SELECT g.*
124  FROM submission_galleys g
125  INNER JOIN submissions a ON a.submission_id = g.submission_id
126  LEFT JOIN published_submissions pa ON g.submission_id = pa.submission_id ';
127  if (is_null($settingValue)) {
128  $sql .= 'LEFT JOIN submission_galley_settings gs ON g.galley_id = gs.galley_id AND gs.setting_name = ?
129  WHERE (gs.setting_value IS NULL OR gs.setting_value = "")';
130  } else {
131  $params[] = $settingValue;
132  $sql .= 'INNER JOIN submission_galley_settings gs ON g.galley_id = gs.galley_id
133  WHERE gs.setting_name = ? AND gs.setting_value = ?';
134  }
135  if ($articleId) {
136  $params[] = (int) $articleId;
137  $sql .= ' AND g.submission_id = ?';
138  }
139  if ($journalId) {
140  $params[] = (int) $journalId;
141  $sql .= ' AND a.context_id = ?';
142  }
143  $sql .= ' ORDER BY a.context_id, pa.issue_id, g.galley_id';
144  $result = $this->retrieve($sql, $params);
145 
146  return new DAOResultFactory($result, $this, '_fromRow');
147  }
148 
154  function getBySubmissionId($articleId) {
155  $result = $this->retrieve(
156  'SELECT *
157  FROM submission_galleys
158  WHERE submission_id = ? ORDER BY seq',
159  (int) $articleId
160  );
161 
162  return new DAOResultFactory($result, $this, '_fromRow');
163  }
164 
170  function getByJournalId($journalId) {
171  $result = $this->retrieve(
172  'SELECT g.*
173  FROM submission_galleys g
174  INNER JOIN submissions a ON (g.submission_id = a.submission_id)
175  WHERE a.context_id = ?',
176  (int) $journalId
177  );
178 
179  return new DAOResultFactory($result, $this, '_fromRow');
180  }
181 
189  function getGalleyByBestGalleyId($galleyId, $articleId) {
190  $galley = null;
191  if ($galleyId != '') $galley = $this->getGalleyByPubId('publisher-id', $galleyId, $articleId);
192  if (!isset($galley) && ctype_digit("$galleyId")) $galley = $this->getById((int) $galleyId, $articleId);
193  return $galley;
194  }
195 
200  function getLocaleFieldNames() {
201  return array();
202  }
203 
210  $additionalFields = parent::getAdditionalFieldNames();
211  // FIXME: Move this to a PID plug-in.
212  $additionalFields[] = 'pub-id::publisher-id';
213  return $additionalFields;
214  }
215 
220  function updateLocaleFields(&$galley) {
221  $this->updateDataObjectSettings('submission_galley_settings', $galley, array(
222  'galley_id' => $galley->getId()
223  ));
224  }
225 
231  function _fromRow($row) {
232  $galley = $this->newDataObject();
233 
234  $galley->setId($row['galley_id']);
235  $galley->setSubmissionId($row['submission_id']);
236  $galley->setLocale($row['locale']);
237  $galley->setLabel($row['label']);
238  $galley->setSeq($row['seq']);
239  $galley->setRemoteURL($row['remote_url']);
240  $galley->setIsAvailable($row['is_available']);
241  $galley->setGalleyType($row['galley_type']);
242 
243  $this->getDataObjectSettings('submission_galley_settings', 'galley_id', $row['galley_id'], $galley);
244 
245  HookRegistry::call('ArticleGalleyDAO::_fromRow', array(&$galley, &$row));
246 
247  return $galley;
248  }
249 
254  function insertObject($galley) {
255  $this->update(
256  'INSERT INTO submission_galleys
257  (submission_id, file_id, label, locale, seq, remote_url, is_available, galley_type)
258  VALUES
259  (?, ?, ?, ?, ?, ?, ?, ?)',
260  array(
261  (int) $galley->getSubmissionId(),
262  0,
263  $galley->getLabel(),
264  $galley->getLocale(),
265  $galley->getSeq() == null ? $this->getNextGalleySequence($galley->getSubmissionId()) : $galley->getSeq(),
266  $galley->getRemoteURL(),
267  $galley->getIsAvailable()?1:0,
268  $galley->getGalleyType(),
269  )
270  );
271  $galley->setId($this->getInsertId());
272  $this->updateLocaleFields($galley);
273 
274  HookRegistry::call('ArticleGalleyDAO::insertNewGalley', array(&$galley, $galley->getId()));
275 
276  return $galley->getId();
277  }
278 
283  function updateObject($galley) {
284  $this->update(
285  'UPDATE submission_galleys
286  SET
287  file_id = ?,
288  label = ?,
289  locale = ?,
290  seq = ?,
291  remote_url = ?,
292  is_available = ?,
293  galley_type = ?
294  WHERE galley_id = ?',
295  array(
296  0,
297  $galley->getLabel(),
298  $galley->getLocale(),
299  $galley->getSeq(),
300  $galley->getRemoteURL(),
301  (int) $galley->getIsAvailable(),
302  $galley->getGalleyType(),
303  (int) $galley->getId(),
304  )
305  );
306  $this->updateLocaleFields($galley);
307  }
308 
313  function deleteGalley(&$galley) {
314  return $this->deleteGalleyById($galley->getId());
315  }
316 
322  function deleteGalleyById($galleyId, $articleId = null) {
323 
324  HookRegistry::call('ArticleGalleyDAO::deleteGalleyById', array(&$galleyId, &$articleId));
325 
326  if (isset($articleId)) {
327  $this->update(
328  'DELETE FROM submission_galleys WHERE galley_id = ? AND submission_id = ?',
329  array((int) $galleyId, (int) $articleId)
330  );
331  } else {
332  $this->update(
333  'DELETE FROM submission_galleys WHERE galley_id = ?', (int) $galleyId
334  );
335  }
336  if ($this->getAffectedRows()) {
337  $this->update('DELETE FROM submission_galley_settings WHERE galley_id = ?', array((int) $galleyId));
338  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
339  // Import constants
340  import('lib.pkp.classes.submission.SubmissionFile');
341 
342  $galleyFiles = $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galleyId, $articleId, SUBMISSION_FILE_PROOF);
343  foreach ($galleyFiles as $file) {
344  // delete dependent files for each galley file
345  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $file->getFileId(), SUBMISSION_FILE_DEPENDENT);
346  }
347  // delete the galley files.
348  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galleyId, SUBMISSION_FILE_PROOF);
349  }
350  }
351 
357  function deleteGalleysByArticle($articleId) {
358  $galleys = $this->getBySubmissionId($articleId);
359  while ($galley = $galleys->next()) {
360  $this->deleteGalleyById($galley->getId(), $articleId);
361  }
362  }
363 
370  function galleyExistsByFileId($articleId, $fileId) {
371  $result = $this->retrieve(
372  'SELECT COUNT(*) FROM submission_galleys
373  WHERE submission_id = ? AND file_id = ?',
374  array((int) $articleId, (int) $fileId)
375  );
376 
377  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
378 
379  $result->Close();
380  return $returner;
381  }
382 
387  function resequenceGalleys($articleId) {
388  $result = $this->retrieve(
389  'SELECT galley_id FROM submission_galleys WHERE submission_id = ? ORDER BY seq',
390  (int) $articleId
391  );
392 
393  for ($i=1; !$result->EOF; $i++) {
394  list($galleyId) = $result->fields;
395  $this->update(
396  'UPDATE submission_galleys SET seq = ? WHERE galley_id = ?',
397  array($i, $galleyId)
398  );
399  $result->MoveNext();
400  }
401  $result->Close();
402  }
403 
409  function getNextGalleySequence($articleId) {
410  $result = $this->retrieve(
411  'SELECT MAX(seq) + 1 FROM submission_galleys WHERE submission_id = ?',
412  (int) $articleId
413  );
414  $returner = floor($result->fields[0]);
415 
416  $result->Close();
417  return $returner;
418  }
419 
424  function getInsertId() {
425  return $this->_getInsertId('submission_galleys', 'galley_id');
426  }
427 
436  function changePubId($galleyId, $pubIdType, $pubId) {
437  $idFields = array(
438  'galley_id', 'locale', 'setting_name'
439  );
440  $updateArray = array(
441  'galley_id' => $galleyId,
442  'locale' => '',
443  'setting_name' => 'pub-id::'.$pubIdType,
444  'setting_type' => 'string',
445  'setting_value' => (string)$pubId
446  );
447  $this->replace('submission_galley_settings', $updateArray, $idFields);
448  }
449 
457  function deleteAllPubIds($journalId, $pubIdType) {
458  $journalId = (int) $journalId;
459  $settingName = 'pub-id::'.$pubIdType;
460 
461  $galleys = $this->getByJournalId($journalId);
462  while ($galley = $galleys->next()) {
463  $this->update(
464  'DELETE FROM submission_galley_settings WHERE setting_name = ? AND galley_id = ?',
465  array(
466  $settingName,
467  (int)$galley->getId()
468  )
469  );
470  }
471  $this->flushCache();
472  }
473 }
474 
475 ?>
static & getDAO($name, $dbconn=null)
getNextGalleySequence($articleId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
changePubId($galleyId, $pubIdType, $pubId)
getById($galleyId, $submissionId=null)
deleteAllPubIds($journalId, $pubIdType)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:474
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)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
pubIdExists($pubIdType, $pubId, $galleyId, $journalId)
deleteGalleyById($galleyId, $articleId=null)
deleteGalleysByArticle($articleId)
galleyExistsByFileId($articleId, $fileId)
flushCache()
Definition: DAO.inc.php:282
A galley is a final presentation version of the full-text of an article.
getGalleyByBestGalleyId($galleyId, $articleId)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, &$dataObject)
Definition: DAO.inc.php:567
Operations for retrieving and modifying ArticleGalley objects.
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
& getGalleyByPubId($pubIdType, $pubId, $articleId=null)