Open Monograph Press  3.3.0
ChapterDAO.inc.php
1 <?php
2 
18 import('classes.monograph.Chapter');
19 import('classes.monograph.ChapterAuthor');
20 
21 class ChapterDAO extends DAO implements PKPPubIdPluginDAO {
28  function getChapter($chapterId, $publicationId = null) {
29  $params = array((int) $chapterId);
30  if ($publicationId !== null) {
31  $params[] = (int) $publicationId;
32  }
33 
34  $result = $this->retrieve(
35  'SELECT * FROM submission_chapters WHERE chapter_id = ?'
36  . ($publicationId !== null?' AND publication_id = ? ':''),
37  $params
38  );
39 
40  $returner = null;
41  if ($result->RecordCount() != 0) {
42  $returner = $this->_fromRow($result->GetRowAssoc(false));
43  }
44  $result->Close();
45  return $returner;
46  }
47 
54  function getByPublicationId($publicationId, $orderBySequence = true) {
55  $result = $this->retrieve(
56  'SELECT spc.*
57  FROM submission_chapters spc
58  INNER JOIN publications p ON (spc.publication_id = p.publication_id)
59  WHERE p.publication_id = ?'
60  . ($orderBySequence ? ' ORDER BY spc.seq ASC' : ''),
61  (int) $publicationId
62  );
63 
64  return new DAOResultFactory($result, $this, '_fromRow');
65  }
66 
72  function getByContextId($pressId) {
73  $result = $this->retrieve(
74  'SELECT spc.*
75  FROM submission_chapters spc
76  INNER JOIN publications p ON (spc.publication_id = p.publication_id)
77  INNER JOIN submissions s ON (p.submission_id = s.submission_id)
78  WHERE s.context_id = ?',
79  (int) $pressId
80  );
81 
82  return new DAOResultFactory($result, $this, '_fromRow');
83  }
84 
89  function getLocaleFieldNames() {
90  return array('title', 'subtitle','abstract');
91  }
92 
99  $additionalFields = parent::getAdditionalFieldNames();
100  // FIXME: Move this to a PID plug-in.
101  $additionalFields[] = 'pub-id::publisher-id';
102  $additionalFields[] = 'datePublished';
103  $additionalFields[] = 'pages';
104  return $additionalFields;
105  }
106 
111  function newDataObject() {
112  return new Chapter();
113  }
114 
120  function _fromRow($row) {
121  $chapter = $this->newDataObject();
122  $chapter->setId((int) $row['chapter_id']);
123  $chapter->setData('publicationId', (int) $row['publication_id']);
124  $chapter->setSequence((int) $row['seq']);
125 
126  $this->getDataObjectSettings('submission_chapter_settings', 'chapter_id', $row['chapter_id'], $chapter);
127 
128  HookRegistry::call('ChapterDAO::_fromRow', array(&$chapter, &$row));
129 
130  return $chapter;
131  }
132 
137  function updateLocaleFields($chapter) {
138  $this->updateDataObjectSettings('submission_chapter_settings', $chapter, array(
139  'chapter_id' => $chapter->getId()
140  ));
141  }
142 
147  function insertChapter($chapter) {
148  $this->update(
149  'INSERT INTO submission_chapters
150  (publication_id, seq)
151  VALUES
152  (?, ?)',
153  array(
154  (int) $chapter->getData('publicationId'),
155  (int) $chapter->getSequence(),
156  )
157  );
158 
159  $chapter->setId($this->getInsertId());
160  $this->updateLocaleFields($chapter);
161  return $chapter->getId();
162  }
163 
168  function updateObject($chapter) {
169  $this->update(
170  'UPDATE submission_chapters
171  SET publication_id = ?,
172  seq = ?
173  WHERE
174  chapter_id = ?',
175  array(
176  (int) $chapter->getData('publicationId'),
177  (int) $chapter->getSequence(),
178  (int) $chapter->getId()
179  )
180  );
181  $this->updateLocaleFields($chapter);
182  }
183 
188  function deleteObject($chapter) {
189  $this->deleteById($chapter->getId());
190  }
191 
196  function deleteById($chapterId) {
197  $this->update('DELETE FROM submission_chapter_authors WHERE chapter_id = ?', (int) $chapterId);
198  $this->update('DELETE FROM submission_chapter_settings WHERE chapter_id = ?', (int) $chapterId);
199  $this->update('DELETE FROM submission_chapters WHERE chapter_id = ?', (int) $chapterId);
200  $this->update('DELETE FROM submission_file_settings WHERE setting_name = ? AND setting_value = ?', ['chapterId', (int) $chapterId]);
201  }
202 
207  function resequenceChapters($publicationId) {
208  $params = array();
209  if ($publicationId !== null) {
210  $params[] = (int) $publicationId;
211  }
212 
213  $result = $this->retrieve(
214  'SELECT chapter_id FROM submission_chapters
215  WHERE 1=1'
216  . ($publicationId !== null ? ' AND publication_id = ?':'')
217  . ' ORDER BY seq',
218  $params
219  );
220 
221  for ($i=1; !$result->EOF; $i++) {
222  list($chapterId) = $result->fields;
223  $this->update(
224  'UPDATE submission_chapters SET seq = ? WHERE chapter_id = ?',
225  array(
226  (int) $i,
227  (int) $chapterId
228  )
229  );
230 
231  $result->MoveNext();
232  }
233 
234  $result->Close();
235  }
236 
241  function getInsertId() {
242  return $this->_getInsertId('submission_chapters', 'chapter_id');
243  }
244 
248  function pubIdExists($pubIdType, $pubId, $excludePubObjectId, $contextId) {
249  $result = $this->retrieve(
250  'SELECT COUNT(*)
251  FROM submission_chapter_settings scs
252  INNER JOIN submission_chapters sc ON scs.chapter_id = sc.chapter_id
253  INNER JOIN publications p ON sc.publication_id = p.publication_id
254  INNER JOIN submissions s ON p.submission_id = s.submission_id
255  WHERE scs.setting_name = ?
256  AND scs.setting_value = ?
257  AND sc.chapter_id <> ?
258  AND s.context_id = ?',
259  array(
260  'pub-id::'.$pubIdType,
261  $pubId,
262  (int) $excludePubObjectId,
263  (int) $contextId
264  )
265  );
266  $returner = $result->fields[0] ? true : false;
267  $result->Close();
268  return $returner;
269  }
270 
274  function changePubId($pubObjectId, $pubIdType, $pubId) {
275  $idFields = array(
276  'chapter_id', 'locale', 'setting_name'
277  );
278  $updateArray = array(
279  'chapter_id' => (int) $pubObjectId,
280  'locale' => '',
281  'setting_name' => 'pub-id::'.$pubIdType,
282  'setting_type' => 'string',
283  'setting_value' => (string)$pubId
284  );
285  $this->replace('submission_chapter_settings', $updateArray, $idFields);
286  }
287 
291  function deletePubId($pubObjectId, $pubIdType) {
292  $settingName = 'pub-id::'.$pubIdType;
293  $this->update(
294  'DELETE FROM submission_chapter_settings WHERE setting_name = ? AND chapter_id = ?',
295  array(
296  $settingName,
297  (int)$pubObjectId
298  )
299  );
300  $this->flushCache();
301  }
302 
306  function deleteAllPubIds($contextId, $pubIdType) {
307  $settingName = 'pub-id::'.$pubIdType;
308 
309  $chapters = $this->getByContextId($contextId);
310  while ($chapter = $chapters->next()) {
311  $this->update(
312  'DELETE FROM submission_chapter_settings WHERE setting_name = ? AND chapter_id = ?',
313  array(
314  $settingName,
315  (int)$chapter->getId()
316  )
317  );
318  }
319  $this->flushCache();
320  }
321 }
ChapterDAO\getByPublicationId
getByPublicationId($publicationId, $orderBySequence=true)
Definition: ChapterDAO.inc.php:54
ChapterDAO\getChapter
getChapter($chapterId, $publicationId=null)
Definition: ChapterDAO.inc.php:28
PKPPubIdPluginDAO
Interface that DAOs would need to implement in order for pub ID support to be added.
Definition: PKPPubIdPluginDAO.inc.php:16
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
ChapterDAO
Operations for retrieving and modifying ChapterAuthor objects.
Definition: ChapterDAO.inc.php:21
ChapterDAO\resequenceChapters
resequenceChapters($publicationId)
Definition: ChapterDAO.inc.php:207
ChapterDAO\updateObject
updateObject($chapter)
Definition: ChapterDAO.inc.php:168
ChapterDAO\changePubId
changePubId($pubObjectId, $pubIdType, $pubId)
Definition: ChapterDAO.inc.php:274
ChapterDAO\deleteById
deleteById($chapterId)
Definition: ChapterDAO.inc.php:196
ChapterDAO\_fromRow
_fromRow($row)
Definition: ChapterDAO.inc.php:120
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
ChapterDAO\getLocaleFieldNames
getLocaleFieldNames()
Definition: ChapterDAO.inc.php:89
ChapterDAO\insertChapter
insertChapter($chapter)
Definition: ChapterDAO.inc.php:147
ChapterDAO\getInsertId
getInsertId()
Definition: ChapterDAO.inc.php:241
ChapterDAO\updateLocaleFields
updateLocaleFields($chapter)
Definition: ChapterDAO.inc.php:137
ChapterDAO\deletePubId
deletePubId($pubObjectId, $pubIdType)
Definition: ChapterDAO.inc.php:291
ChapterDAO\deleteAllPubIds
deleteAllPubIds($contextId, $pubIdType)
Definition: ChapterDAO.inc.php:306
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
ChapterDAO\pubIdExists
pubIdExists($pubIdType, $pubId, $excludePubObjectId, $contextId)
Definition: ChapterDAO.inc.php:248
ChapterDAO\deleteObject
deleteObject($chapter)
Definition: ChapterDAO.inc.php:188
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
DAO\getDataObjectSettings
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:582
Chapter
Describes a monograph chapter (or section)
Definition: Chapter.inc.php:17
DAO\replace
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:243
ChapterDAO\getAdditionalFieldNames
getAdditionalFieldNames()
Definition: ChapterDAO.inc.php:98
DAO\flushCache
flushCache()
Definition: DAO.inc.php:288
DAO\updateDataObjectSettings
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:488
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
ChapterDAO\newDataObject
newDataObject()
Definition: ChapterDAO.inc.php:111
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
ChapterDAO\getByContextId
getByContextId($pressId)
Definition: ChapterDAO.inc.php:72