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 
62  function getBySubmissionId($submissionId, $sortByAuthorId = false) {
63  $authors = array();
64 
65  $result = $this->retrieve(
66  'SELECT a.*, ug.show_title
67  FROM authors a
68  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
69  WHERE a.submission_id = ?
70  ORDER BY seq',
71  (int) $submissionId
72  );
73 
74  while (!$result->EOF) {
75  $row = $result->getRowAssoc(false);
76  if ($sortByAuthorId) {
77  $authorId = $row['author_id'];
78  $authors[$authorId] = $this->_fromRow($row);
79  } else {
80  $authors[] = $this->_fromRow($row);
81  }
82  $result->MoveNext();
83  }
84 
85  $result->Close();
86  return $authors;
87  }
88 
94  function getAuthorCountBySubmissionId($submissionId) {
95  $result = $this->retrieve(
96  'SELECT COUNT(*) FROM authors WHERE submission_id = ?',
97  (int) $submissionId
98  );
99 
100  $returner = $result->fields[0];
101 
102  $result->Close();
103  return $returner;
104  }
105 
110  function updateLocaleFields($author) {
112  'author_settings',
113  $author,
114  array(
115  'author_id' => $author->getId()
116  )
117  );
118  }
119 
125  function _fromRow($row) {
126  $author = $this->newDataObject();
127  $author->setId($row['author_id']);
128  $author->setSubmissionId($row['submission_id']);
129  $author->setFirstName($row['first_name']);
130  $author->setMiddleName($row['middle_name']);
131  $author->setLastName($row['last_name']);
132  $author->setSuffix($row['suffix']);
133  $author->setCountry($row['country']);
134  $author->setEmail($row['email']);
135  $author->setUrl($row['url']);
136  $author->setUserGroupId($row['user_group_id']);
137  $author->setPrimaryContact($row['primary_contact']);
138  $author->setSequence($row['seq']);
139  $author->_setShowTitle($row['show_title']); // Dependent
140 
141  $this->getDataObjectSettings('author_settings', 'author_id', $row['author_id'], $author);
142 
143  HookRegistry::call('AuthorDAO::_fromRow', array(&$author, &$row));
144  return $author;
145  }
146 
153  function _returnSimpleAuthorFromRow($row) {
154  $author = $this->newDataObject();
155  $author->setId($row['author_id']);
156  $author->setSubmissionId($row['submission_id']);
157  $author->setFirstName($row['first_name']);
158  $author->setMiddleName($row['middle_name']);
159  $author->setLastName($row['last_name']);
160  $author->setSuffix($row['suffix']);
161  $author->setCountry($row['country']);
162  $author->setEmail($row['email']);
163  $author->setUrl($row['url']);
164  $author->setUserGroupId($row['user_group_id']);
165  $author->setPrimaryContact($row['primary_contact']);
166  $author->setSequence($row['seq']);
167 
168  $author->setAffiliation($row['affiliation_l'], $row['locale']);
169  $author->setAffiliation($row['affiliation_pl'], $row['primary_locale']);
170 
171  HookRegistry::call('AuthorDAO::_returnSimpleAuthorFromRow', array(&$author, &$row));
172  return $author;
173  }
174 
179  function newDataObject() {
180  assert(false); // Should be overridden by child classes
181  }
182 
187  function getLocaleFieldNames() {
188  return array('biography', 'competingInterests', 'affiliation');
189  }
190 
195  function insertObject($author) {
196  // Set author sequence to end of author list
197  if(!$author->getSequence()) {
198  $authorCount = $this->getAuthorCountBySubmissionId($author->getSubmissionId());
199  $author->setSequence($authorCount + 1);
200  }
201  // Reset primary contact for submission to this author if applicable
202  if ($author->getPrimaryContact()) {
203  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
204  }
205 
206  $this->update(
207  'INSERT INTO authors
208  (submission_id, first_name, middle_name, last_name, suffix, country, email, url, user_group_id, primary_contact, seq)
209  VALUES
210  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
211  array(
212  (int) $author->getSubmissionId(),
213  $author->getFirstName(),
214  $author->getMiddleName() . '', // make non-null
215  $author->getLastName(),
216  $author->getSuffix() . '',
217  $author->getCountry(),
218  $author->getEmail(),
219  $author->getUrl(),
220  (int) $author->getUserGroupId(),
221  (int) $author->getPrimaryContact(),
222  (float) $author->getSequence()
223  )
224  );
225 
226  $author->setId($this->getInsertId());
227  $this->updateLocaleFields($author);
228 
229  return $author->getId();
230  }
231 
236  function updateObject($author) {
237  // Reset primary contact for submission to this author if applicable
238  if ($author->getPrimaryContact()) {
239  $this->resetPrimaryContact($author->getId(), $author->getSubmissionId());
240  }
241  $returner = $this->update(
242  'UPDATE authors
243  SET first_name = ?,
244  middle_name = ?,
245  last_name = ?,
246  suffix = ?,
247  country = ?,
248  email = ?,
249  url = ?,
250  user_group_id = ?,
251  primary_contact = ?,
252  seq = ?
253  WHERE author_id = ?',
254  array(
255  $author->getFirstName(),
256  $author->getMiddleName() . '', // make non-null
257  $author->getLastName(),
258  $author->getSuffix() . '',
259  $author->getCountry(),
260  $author->getEmail(),
261  $author->getUrl(),
262  (int) $author->getUserGroupId(),
263  (int) $author->getPrimaryContact(),
264  (float) $author->getSequence(),
265  (int) $author->getId()
266  )
267  );
268  $this->updateLocaleFields($author);
269  return $returner;
270  }
271 
276  function deleteObject($author) {
277  return $this->deleteById($author->getId());
278  }
279 
285  function deleteById($authorId, $submissionId = null) {
286  $params = array((int) $authorId);
287  if ($submissionId) $params[] = (int) $submissionId;
288  $returner = $this->update(
289  'DELETE FROM authors WHERE author_id = ?' .
290  ($submissionId?' AND submission_id = ?':''),
291  $params
292  );
293  if ($returner) $this->update('DELETE FROM author_settings WHERE author_id = ?', array((int) $authorId));
294 
295  return $returner;
296  }
297 
302  function resequenceAuthors($submissionId) {
303  $result = $this->retrieve(
304  'SELECT author_id FROM authors WHERE submission_id = ? ORDER BY seq',
305  (int) $submissionId
306  );
307 
308  for ($i=1; !$result->EOF; $i++) {
309  list($authorId) = $result->fields;
310  $this->update(
311  'UPDATE authors SET seq = ? WHERE author_id = ?',
312  array(
313  $i,
314  $authorId
315  )
316  );
317 
318  $result->MoveNext();
319  }
320  $result->Close();
321  }
322 
328  function getPrimaryContact($submissionId) {
329  $result = $this->retrieve(
330  'SELECT a.*, ug.show_title
331  FROM authors a
332  JOIN user_groups ug ON (a.user_group_id=ug.user_group_id)
333  WHERE submission_id = ? AND primary_contact = 1',
334  (int) $submissionId
335  );
336 
337  $returner = null;
338  if ($result->RecordCount() != 0) {
339  $returner = $this->_fromRow($result->GetRowAssoc(false));
340  }
341  $result->Close();
342  return $returner;
343  }
344 
350  function resetPrimaryContact($authorId, $submissionId) {
351  $this->update(
352  'UPDATE authors SET primary_contact = 0 WHERE primary_contact = 1 AND submission_id = ?',
353  (int) $submissionId
354  );
355  $this->update(
356  'UPDATE authors SET primary_contact = 1 WHERE author_id = ? AND submission_id = ?',
357  array((int) $authorId, (int) $submissionId)
358  );
359  }
360 
365  function getInsertId() {
366  return $this->_getInsertId('authors', 'author_id');
367  }
368 
373  function deleteBySubmissionId($submissionId) {
374  $authors = $this->getBySubmissionId($submissionId);
375  foreach ($authors as $author) {
376  $this->deleteObject($author);
377  }
378  }
379 }
380 
381 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
insertObject($author)
deleteById($authorId, $submissionId=null)
& 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:457
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)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getBySubmissionId($submissionId, $sortByAuthorId=false)
updateLocaleFields($author)