Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
PKPAuthorDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.submission.PKPAuthor');
19 
20 abstract class PKPAuthorDAO extends DAO {
24  function PKPAuthorDAO() {
25  parent::DAO();
26  }
27 
34  function getById($authorId, $submissionId = null) {
35  $params = array((int) $authorId);
36  if ($submissionId !== null) $params[] = (int) $submissionId;
37  $result = $this->retrieve(
38  'SELECT a.*,
39  ug.show_title
40  FROM authors a
41  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
42  WHERE a.author_id = ?'
43  . ($submissionId !== null?' AND a.submission_id = ?':''),
44  $params
45  );
46 
47  $returner = null;
48  if ($result->RecordCount() != 0) {
49  $returner = $this->_fromRow($result->GetRowAssoc(false));
50  }
51 
52  $result->Close();
53  return $returner;
54  }
55 
63  function getBySubmissionId($submissionId, $sortByAuthorId = false, $useIncludeInBrowse = false) {
64  $authors = array();
65  $params = array((int) $submissionId);
66  if ($useIncludeInBrowse) $params[] = 1;
67 
68  $result = $this->retrieve(
69  'SELECT a.*, ug.show_title
70  FROM authors a
71  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
72  WHERE a.submission_id = ? ' .
73  ($useIncludeInBrowse ? ' AND a.include_in_browse = ?' : '')
74  . ' ORDER BY seq',
75  $params
76  );
77 
78  while (!$result->EOF) {
79  $row = $result->getRowAssoc(false);
80  if ($sortByAuthorId) {
81  $authorId = $row['author_id'];
82  $authors[$authorId] = $this->_fromRow($row);
83  } else {
84  $authors[] = $this->_fromRow($row);
85  }
86  $result->MoveNext();
87  }
88 
89  $result->Close();
90  return $authors;
91  }
92 
98  function getAuthorCountBySubmissionId($submissionId) {
99  $result = $this->retrieve(
100  'SELECT COUNT(*) FROM authors WHERE submission_id = ?',
101  (int) $submissionId
102  );
103 
104  $returner = $result->fields[0];
105 
106  $result->Close();
107  return $returner;
108  }
109 
114  function updateLocaleFields($author) {
116  'author_settings',
117  $author,
118  array(
119  'author_id' => $author->getId()
120  )
121  );
122  }
123 
129  function _fromRow($row) {
130  $author = $this->newDataObject();
131  $author->setId($row['author_id']);
132  $author->setSubmissionId($row['submission_id']);
133  $author->setFirstName($row['first_name']);
134  $author->setMiddleName($row['middle_name']);
135  $author->setLastName($row['last_name']);
136  $author->setSuffix($row['suffix']);
137  $author->setCountry($row['country']);
138  $author->setEmail($row['email']);
139  $author->setUrl($row['url']);
140  $author->setUserGroupId($row['user_group_id']);
141  $author->setPrimaryContact($row['primary_contact']);
142  $author->setSequence($row['seq']);
143  $author->setIncludeInBrowse($row['include_in_browse']);
144  $author->_setShowTitle($row['show_title']); // Dependent
145 
146  $this->getDataObjectSettings('author_settings', 'author_id', $row['author_id'], $author);
147 
148  HookRegistry::call('AuthorDAO::_fromRow', array(&$author, &$row));
149  return $author;
150  }
151 
158  function _returnSimpleAuthorFromRow($row) {
159  $author = $this->newDataObject();
160  $author->setId($row['author_id']);
161  $author->setSubmissionId($row['submission_id']);
162  $author->setFirstName($row['first_name']);
163  $author->setMiddleName($row['middle_name']);
164  $author->setLastName($row['last_name']);
165  $author->setSuffix($row['suffix']);
166  $author->setCountry($row['country']);
167  $author->setEmail($row['email']);
168  $author->setUrl($row['url']);
169  $author->setUserGroupId($row['user_group_id']);
170  $author->setPrimaryContact($row['primary_contact']);
171  $author->setSequence($row['seq']);
172  $author->setIncludeInBrowse($row['include_in_browse'] == 1 ? true : false);
173 
174  $author->setAffiliation($row['affiliation_l'], $row['locale']);
175  $author->setAffiliation($row['affiliation_pl'], $row['primary_locale']);
176 
177  HookRegistry::call('AuthorDAO::_returnSimpleAuthorFromRow', array(&$author, &$row));
178  return $author;
179  }
180 
185  abstract function newDataObject();
186 
191  function getLocaleFieldNames() {
192  return array('biography', 'competingInterests', 'affiliation');
193  }
194 
199  return array_merge(parent::getAdditionalFieldNames(), array(
200  'orcid',
201  ));
202  }
203 
208  function insertObject($author) {
209  // Set author sequence to end of author list
210  if(!$author->getSequence()) {
211  $authorCount = $this->getAuthorCountBySubmissionId($author->getSubmissionId());
212  $author->setSequence($authorCount + 1);
213  }
214  // Reset primary contact for submission to this author if applicable
215  if ($author->getPrimaryContact()) {
216  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
217  }
218 
219  $this->update(
220  'INSERT INTO authors (
221  submission_id, first_name, middle_name, last_name, suffix, country,
222  email, url, user_group_id, primary_contact, seq, include_in_browse
223  ) VALUES (
224  ?, ?, ?, ?, ?, ?,
225  ?, ?, ?, ?, ?, ?
226  )',
227  array(
228  (int) $author->getSubmissionId(),
229  $author->getFirstName(),
230  $author->getMiddleName() . '', // make non-null
231  $author->getLastName(),
232  $author->getSuffix() . '',
233  $author->getCountry(),
234  $author->getEmail(),
235  $author->getUrl(),
236  (int) $author->getUserGroupId(),
237  (int) $author->getPrimaryContact(),
238  (float) $author->getSequence(),
239  (int) $author->getIncludeInBrowse() ? 1 : 0,
240  )
241  );
242 
243  $author->setId($this->getInsertId());
244  $this->updateLocaleFields($author);
245 
246  return $author->getId();
247  }
248 
253  function updateObject($author) {
254  // Reset primary contact for submission to this author if applicable
255  if ($author->getPrimaryContact()) {
256  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
257  }
258  $returner = $this->update(
259  'UPDATE authors
260  SET first_name = ?,
261  middle_name = ?,
262  last_name = ?,
263  suffix = ?,
264  country = ?,
265  email = ?,
266  url = ?,
267  user_group_id = ?,
268  primary_contact = ?,
269  seq = ?,
270  include_in_browse = ?
271  WHERE author_id = ?',
272  array(
273  $author->getFirstName(),
274  $author->getMiddleName() . '', // make non-null
275  $author->getLastName(),
276  $author->getSuffix() . '',
277  $author->getCountry(),
278  $author->getEmail(),
279  $author->getUrl(),
280  (int) $author->getUserGroupId(),
281  (int) $author->getPrimaryContact(),
282  (float) $author->getSequence(),
283  (int) $author->getIncludeInBrowse() ? 1 : 0,
284  (int) $author->getId()
285  )
286  );
287  $this->updateLocaleFields($author);
288  return $returner;
289  }
290 
295  function deleteObject($author) {
296  return $this->deleteById($author->getId());
297  }
298 
304  function deleteById($authorId, $submissionId = null) {
305  $params = array((int) $authorId);
306  if ($submissionId) $params[] = (int) $submissionId;
307  $this->update(
308  'DELETE FROM authors WHERE author_id = ?' .
309  ($submissionId?' AND submission_id = ?':''),
310  $params
311  );
312  $this->update('DELETE FROM author_settings WHERE author_id = ?', array((int) $authorId));
313  }
314 
319  function resequenceAuthors($submissionId) {
320  $result = $this->retrieve(
321  'SELECT author_id FROM authors WHERE submission_id = ? ORDER BY seq',
322  (int) $submissionId
323  );
324 
325  for ($i=1; !$result->EOF; $i++) {
326  list($authorId) = $result->fields;
327  $this->update(
328  'UPDATE authors SET seq = ? WHERE author_id = ?',
329  array(
330  $i,
331  $authorId
332  )
333  );
334 
335  $result->MoveNext();
336  }
337  $result->Close();
338  }
339 
345  function getPrimaryContact($submissionId) {
346  $result = $this->retrieve(
347  'SELECT a.*, ug.show_title
348  FROM authors a
349  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
350  WHERE submission_id = ? AND primary_contact = 1',
351  (int) $submissionId
352  );
353 
354  $returner = null;
355  if ($result->RecordCount() != 0) {
356  $returner = $this->_fromRow($result->GetRowAssoc(false));
357  }
358  $result->Close();
359  return $returner;
360  }
361 
367  function resetPrimaryContact($authorId, $submissionId) {
368  $this->update(
369  'UPDATE authors SET primary_contact = 0 WHERE primary_contact = 1 AND submission_id = ?',
370  (int) $submissionId
371  );
372  $this->update(
373  'UPDATE authors SET primary_contact = 1 WHERE author_id = ? AND submission_id = ?',
374  array((int) $authorId, (int) $submissionId)
375  );
376  }
377 
382  function getInsertId() {
383  return $this->_getInsertId('authors', 'author_id');
384  }
385 
390  function deleteBySubmissionId($submissionId) {
391  $authors = $this->getBySubmissionId($submissionId);
392  foreach ($authors as $author) {
393  $this->deleteObject($author);
394  }
395  }
396 }
397 
398 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
insertObject($author)
deleteById($authorId, $submissionId=null)
getBySubmissionId($submissionId, $sortByAuthorId=false, $useIncludeInBrowse=false)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
resequenceAuthors($submissionId)
_returnSimpleAuthorFromRow($row)
Operations for retrieving and modifying PKPAuthor objects.
deleteBySubmissionId($submissionId)
resetPrimaryContact($authorId, $submissionId)
getById($authorId, $submissionId=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
deleteObject($author)
static call($hookName, $args=null)
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:474
getPrimaryContact($submissionId)
updateObject($author)
getAuthorCountBySubmissionId($submissionId)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:568
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
updateLocaleFields($author)