Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
PKPAuthorDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.submission.PKPAuthor');
19 
20 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  function newDataObject() {
186  assert(false); // Should be overridden by child classes
187  }
188 
193  function getLocaleFieldNames() {
194  return array('biography', 'competingInterests', 'affiliation');
195  }
196 
201  function insertObject($author) {
202  // Set author sequence to end of author list
203  if(!$author->getSequence()) {
204  $authorCount = $this->getAuthorCountBySubmissionId($author->getSubmissionId());
205  $author->setSequence($authorCount + 1);
206  }
207  // Reset primary contact for submission to this author if applicable
208  if ($author->getPrimaryContact()) {
209  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
210  }
211 
212  $this->update(
213  'INSERT INTO authors
214  (submission_id, first_name, middle_name, last_name, suffix, country, email, url, user_group_id, primary_contact, seq, include_in_browse)
215  VALUES
216  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
217  array(
218  (int) $author->getSubmissionId(),
219  $author->getFirstName(),
220  $author->getMiddleName() . '', // make non-null
221  $author->getLastName(),
222  $author->getSuffix() . '',
223  $author->getCountry(),
224  $author->getEmail(),
225  $author->getUrl(),
226  (int) $author->getUserGroupId(),
227  (int) $author->getPrimaryContact(),
228  (float) $author->getSequence(),
229  (int) $author->getIncludeInBrowse() ? 1 : 0,
230  )
231  );
232 
233  $author->setId($this->getInsertId());
234  $this->updateLocaleFields($author);
235 
236  return $author->getId();
237  }
238 
243  function updateObject($author) {
244  // Reset primary contact for submission to this author if applicable
245  if ($author->getPrimaryContact()) {
246  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
247  }
248  $returner = $this->update(
249  'UPDATE authors
250  SET first_name = ?,
251  middle_name = ?,
252  last_name = ?,
253  suffix = ?,
254  country = ?,
255  email = ?,
256  url = ?,
257  user_group_id = ?,
258  primary_contact = ?,
259  seq = ?,
260  include_in_browse = ?
261  WHERE author_id = ?',
262  array(
263  $author->getFirstName(),
264  $author->getMiddleName() . '', // make non-null
265  $author->getLastName(),
266  $author->getSuffix() . '',
267  $author->getCountry(),
268  $author->getEmail(),
269  $author->getUrl(),
270  (int) $author->getUserGroupId(),
271  (int) $author->getPrimaryContact(),
272  (float) $author->getSequence(),
273  (int) $author->getIncludeInBrowse() ? 1 : 0,
274  (int) $author->getId()
275  )
276  );
277  $this->updateLocaleFields($author);
278  return $returner;
279  }
280 
285  function deleteObject($author) {
286  return $this->deleteById($author->getId());
287  }
288 
294  function deleteById($authorId, $submissionId = null) {
295  $params = array((int) $authorId);
296  if ($submissionId) $params[] = (int) $submissionId;
297  $returner = $this->update(
298  'DELETE FROM authors WHERE author_id = ?' .
299  ($submissionId?' AND submission_id = ?':''),
300  $params
301  );
302  if ($returner) $this->update('DELETE FROM author_settings WHERE author_id = ?', array((int) $authorId));
303 
304  return $returner;
305  }
306 
311  function resequenceAuthors($submissionId) {
312  $result = $this->retrieve(
313  'SELECT author_id FROM authors WHERE submission_id = ? ORDER BY seq',
314  (int) $submissionId
315  );
316 
317  for ($i=1; !$result->EOF; $i++) {
318  list($authorId) = $result->fields;
319  $this->update(
320  'UPDATE authors SET seq = ? WHERE author_id = ?',
321  array(
322  $i,
323  $authorId
324  )
325  );
326 
327  $result->MoveNext();
328  }
329  $result->Close();
330  }
331 
337  function getPrimaryContact($submissionId) {
338  $result = $this->retrieve(
339  'SELECT a.*, ug.show_title
340  FROM authors a
341  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
342  WHERE submission_id = ? AND primary_contact = 1',
343  (int) $submissionId
344  );
345 
346  $returner = null;
347  if ($result->RecordCount() != 0) {
348  $returner = $this->_fromRow($result->GetRowAssoc(false));
349  }
350  $result->Close();
351  return $returner;
352  }
353 
359  function resetPrimaryContact($authorId, $submissionId) {
360  $this->update(
361  'UPDATE authors SET primary_contact = 0 WHERE primary_contact = 1 AND submission_id = ?',
362  (int) $submissionId
363  );
364  $this->update(
365  'UPDATE authors SET primary_contact = 1 WHERE author_id = ? AND submission_id = ?',
366  array((int) $authorId, (int) $submissionId)
367  );
368  }
369 
374  function getInsertId() {
375  return $this->_getInsertId('authors', 'author_id');
376  }
377 
382  function deleteBySubmissionId($submissionId) {
383  $authors = $this->getBySubmissionId($submissionId);
384  foreach ($authors as $author) {
385  $this->deleteObject($author);
386  }
387  }
388 }
389 
390 ?>
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)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:474
resetPrimaryContact($authorId, $submissionId)
getById($authorId, $submissionId=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
deleteObject($author)
static call($hookName, $args=null)
getPrimaryContact($submissionId)
updateObject($author)
getAuthorCountBySubmissionId($submissionId)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, &$dataObject)
Definition: DAO.inc.php:567
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
updateLocaleFields($author)