Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
PKPUserDAO.inc.php
1 <?php
2 
18 /* These constants are used user-selectable search fields. */
19 define('USER_FIELD_USERID', 'user_id');
20 define('USER_FIELD_FIRSTNAME', 'first_name');
21 define('USER_FIELD_LASTNAME', 'last_name');
22 define('USER_FIELD_USERNAME', 'username');
23 define('USER_FIELD_EMAIL', 'email');
24 define('USER_FIELD_URL', 'url');
25 define('USER_FIELD_INTERESTS', 'interests');
26 define('USER_FIELD_INITIAL', 'initial');
27 define('USER_FIELD_AFFILIATION', 'affiliation');
28 define('USER_FIELD_NONE', null);
29 
30 class PKPUserDAO extends DAO {
34  function PKPUserDAO() {
35  parent::DAO();
36  }
37 
44  function &getById($userId, $allowDisabled = true) {
45  $result =& $this->retrieve(
46  'SELECT * FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
47  array((int) $userId)
48  );
49 
50  $user = null;
51  if ($result->RecordCount() != 0) {
52  $user =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
53  }
54  $result->Close();
55  unset($result);
56  return $user;
57  }
58 
59  function &getUser($userId, $allowDisabled = true) {
60  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
61  $user =& $this->getById($userId, $allowDisabled);
62  return $user;
63  }
64 
71  function &getByUsername($username, $allowDisabled = true) {
72  $result =& $this->retrieve(
73  'SELECT * FROM users WHERE username = ?' . ($allowDisabled?'':' AND disabled = 0'),
74  array($username)
75  );
76 
77  $returner = null;
78  if ($result->RecordCount() != 0) {
79  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
80  }
81  $result->Close();
82  unset($result);
83  return $returner;
84  }
85 
86  function &getUserByUsername($username, $allowDisabled = true) {
87  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
88  $user =& $this->getByUsername($username, $allowDisabled);
89  return $user;
90  }
91 
98  function &getUserByAuthStr($authstr, $allowDisabled = true) {
99  $result =& $this->retrieve(
100  'SELECT * FROM users WHERE auth_str = ?' . ($allowDisabled?'':' AND disabled = 0'),
101  array($authstr)
102  );
103 
104  $returner = null;
105  if ($result->RecordCount() != 0) {
106  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
107  }
108  $result->Close();
109  unset($result);
110  return $returner;
111  }
112 
119  function &getUserByEmail($email, $allowDisabled = true) {
120  $result =& $this->retrieve(
121  'SELECT * FROM users WHERE email = ?' . ($allowDisabled?'':' AND disabled = 0'),
122  array($email)
123  );
124 
125  $returner = null;
126  if ($result->RecordCount() != 0) {
127  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
128  }
129  $result->Close();
130  unset($result);
131  return $returner;
132  }
133 
141  function &getUserByCredentials($username, $password, $allowDisabled = true) {
142  $result =& $this->retrieve(
143  'SELECT * FROM users WHERE username = ? AND password = ?' . ($allowDisabled?'':' AND disabled = 0'),
144  array($username, $password)
145  );
146 
147  $returner = null;
148  if ($result->RecordCount() != 0) {
149  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
150  }
151  $result->Close();
152  unset($result);
153  return $returner;
154  }
155 
156  function &_returnUserFromRowWithData(&$row) {
157  $user =& $this->_returnUserFromRow($row, false);
158  $this->getDataObjectSettings('user_settings', 'user_id', $row['user_id'], $user);
159 
160  HookRegistry::call('UserDAO::_returnUserFromRowWithData', array(&$user, &$row));
161 
162  return $user;
163  }
164 
171  function &_returnUserFromRow(&$row, $callHook = true) {
172  $user = new User();
173  $user->setId($row['user_id']);
174  $user->setUsername($row['username']);
175  $user->setPassword($row['password']);
176  $user->setSalutation($row['salutation']);
177  $user->setFirstName($row['first_name']);
178  $user->setMiddleName($row['middle_name']);
179  $user->setInitials($row['initials']);
180  $user->setLastName($row['last_name']);
181  $user->setSuffix($row['suffix']);
182  $user->setGender($row['gender']);
183  $user->setEmail($row['email']);
184  $user->setUrl($row['url']);
185  $user->setPhone($row['phone']);
186  $user->setFax($row['fax']);
187  $user->setMailingAddress($row['mailing_address']);
188  $user->setBillingAddress($row['billing_address']);
189  $user->setCountry($row['country']);
190  $user->setLocales(isset($row['locales']) && !empty($row['locales']) ? explode(':', $row['locales']) : array());
191  $user->setDateLastEmail($this->datetimeFromDB($row['date_last_email']));
192  $user->setDateRegistered($this->datetimeFromDB($row['date_registered']));
193  $user->setDateValidated($this->datetimeFromDB($row['date_validated']));
194  $user->setDateLastLogin($this->datetimeFromDB($row['date_last_login']));
195  $user->setMustChangePassword($row['must_change_password']);
196  $user->setDisabled($row['disabled']);
197  $user->setDisabledReason($row['disabled_reason']);
198  $user->setAuthId($row['auth_id']);
199  $user->setAuthStr($row['auth_str']);
200  $user->setInlineHelp($row['inline_help']);
201 
202  if ($callHook) HookRegistry::call('UserDAO::_returnUserFromRow', array(&$user, &$row));
203 
204  return $user;
205  }
206 
211  function insertUser(&$user) {
212  if ($user->getDateRegistered() == null) {
213  $user->setDateRegistered(Core::getCurrentDate());
214  }
215  if ($user->getDateLastLogin() == null) {
216  $user->setDateLastLogin(Core::getCurrentDate());
217  }
218  $this->update(
219  sprintf('INSERT INTO users
220  (username, password, salutation, first_name, middle_name, initials, last_name, suffix, gender, email, url, phone, fax, mailing_address, billing_address, country, locales, date_last_email, date_registered, date_validated, date_last_login, must_change_password, disabled, disabled_reason, auth_id, auth_str, inline_help)
221  VALUES
222  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, %s, %s, ?, ?, ?, ?, ?, ?)',
223  $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateRegistered()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
224  array(
225  $user->getUsername(),
226  $user->getPassword(),
227  $user->getSalutation(),
228  $user->getFirstName(),
229  $user->getMiddleName(),
230  $user->getInitials(),
231  $user->getLastName(),
232  $user->getSuffix(),
233  $user->getGender(),
234  $user->getEmail(),
235  $user->getUrl(),
236  $user->getPhone(),
237  $user->getFax(),
238  $user->getMailingAddress(),
239  $user->getBillingAddress(),
240  $user->getCountry(),
241  join(':', $user->getLocales()),
242  $user->getMustChangePassword() ? 1 : 0,
243  $user->getDisabled() ? 1 : 0,
244  $user->getDisabledReason(),
245  $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
246  $user->getAuthStr(),
247  (int) $user->getInlineHelp(),
248  )
249  );
250 
251  $user->setId($this->getInsertUserId());
252  $this->updateLocaleFields($user);
253  return $user->getId();
254  }
255 
256  function getLocaleFieldNames() {
257  return array('biography', 'signature', 'gossip', 'affiliation');
258  }
259 
260  function updateLocaleFields(&$user) {
261  $this->updateDataObjectSettings('user_settings', $user, array(
262  'user_id' => (int) $user->getId()
263  ));
264  }
265 
270  function updateObject(&$user) {
271  if ($user->getDateLastLogin() == null) {
272  $user->setDateLastLogin(Core::getCurrentDate());
273  }
274 
275  $this->updateLocaleFields($user);
276 
277  return $this->update(
278  sprintf('UPDATE users
279  SET username = ?,
280  password = ?,
281  salutation = ?,
282  first_name = ?,
283  middle_name = ?,
284  initials = ?,
285  last_name = ?,
286  suffix = ?,
287  gender = ?,
288  email = ?,
289  url = ?,
290  phone = ?,
291  fax = ?,
292  mailing_address = ?,
293  billing_address = ?,
294  country = ?,
295  locales = ?,
296  date_last_email = %s,
297  date_validated = %s,
298  date_last_login = %s,
299  must_change_password = ?,
300  disabled = ?,
301  disabled_reason = ?,
302  auth_id = ?,
303  auth_str = ?,
304  inline_help = ?
305  WHERE user_id = ?',
306  $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
307  array(
308  $user->getUsername(),
309  $user->getPassword(),
310  $user->getSalutation(),
311  $user->getFirstName(),
312  $user->getMiddleName(),
313  $user->getInitials(),
314  $user->getLastName(),
315  $user->getSuffix(),
316  $user->getGender(),
317  $user->getEmail(),
318  $user->getUrl(),
319  $user->getPhone(),
320  $user->getFax(),
321  $user->getMailingAddress(),
322  $user->getBillingAddress(),
323  $user->getCountry(),
324  join(':', $user->getLocales()),
325  $user->getMustChangePassword() ? 1 : 0,
326  $user->getDisabled() ? 1 : 0,
327  $user->getDisabledReason(),
328  $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
329  $user->getAuthStr(),
330  (int) $user->getInlineHelp(),
331  (int) $user->getId(),
332  )
333  );
334  }
335 
336  function updateUser(&$user) {
337  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
338  return $this->updateObject($user);
339  }
340 
345  function deleteObject(&$user) {
346  return $this->deleteUserById($user->getId());
347  }
348 
349  function deleteUser(&$user) {
350  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
351  return $this->deleteObject($user);
352  }
353 
358  function deleteUserById($userId) {
359  $this->update('DELETE FROM user_settings WHERE user_id = ?', array((int) $userId));
360  return $this->update('DELETE FROM users WHERE user_id = ?', array((int) $userId));
361  }
362 
369  function getUserFullName($userId, $allowDisabled = true) {
370  $result =& $this->retrieve(
371  'SELECT first_name, middle_name, last_name, suffix FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
372  array((int) $userId)
373  );
374 
375  if($result->RecordCount() == 0) {
376  $returner = false;
377  } else {
378  $returner = $result->fields[0] . ' ' . (empty($result->fields[1]) ? '' : $result->fields[1] . ' ') . $result->fields[2] . (empty($result->fields[3]) ? '' : ', ' . $result->fields[3]);
379  }
380 
381  $result->Close();
382  unset($result);
383 
384  return $returner;
385  }
386 
393  function getUserEmail($userId, $allowDisabled = true) {
394  $result =& $this->retrieve(
395  'SELECT email FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
396  array((int) $userId)
397  );
398 
399  if($result->RecordCount() == 0) {
400  $returner = false;
401  } else {
402  $returner = $result->fields[0];
403  }
404 
405  $result->Close();
406  unset($result);
407 
408  return $returner;
409  }
410 
421  function &getUsersByField($field = USER_FIELD_NONE, $match = null, $value = null, $allowDisabled = true, $dbResultRange = null, $sortBy = null, $sortDirection = SORT_DIRECTION_ASC) {
422  $sql = 'SELECT DISTINCT u.* FROM users u';
423  switch ($field) {
424  case USER_FIELD_USERID:
425  $sql .= ' WHERE u.user_id = ?';
426  $var = (int) $value;
427  break;
428  case USER_FIELD_USERNAME:
429  $sql .= ' WHERE LOWER(u.username) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
430  $var = $match == 'is' ? $value : "%$value%";
431  break;
432  case USER_FIELD_INITIAL:
433  $sql .= ' WHERE LOWER(u.last_name) LIKE LOWER(?)';
434  $var = "$value%";
435  break;
436  case USER_FIELD_INTERESTS:
437  $interestDao =& DAORegistry::getDAO('InterestDAO'); // Loaded to ensure interest constant is in namespace
438  $sql .=', controlled_vocabs cv, controlled_vocab_entries cve, controlled_vocab_entry_settings cves
439  WHERE cv.assoc_type = ' . ASSOC_TYPE_USER . ' AND cv.symbolic = "' . CONTROLLED_VOCAB_INTEREST . '" AND cve.controlled_vocab_id = cv.controlled_vocab_id
440  AND cves.controlled_vocab_entry_id = cve.controlled_vocab_entry_id AND LOWER(cves.setting_value) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
441  $var = $match == 'is' ? $value : "%$value%";
442  break;
443  case USER_FIELD_EMAIL:
444  $sql .= ' WHERE LOWER(u.email) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
445  $var = $match == 'is' ? $value : "%$value%";
446  break;
447  case USER_FIELD_URL:
448  $sql .= ' WHERE LOWER(u.url) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
449  $var = $match == 'is' ? $value : "%$value%";
450  break;
451  case USER_FIELD_FIRSTNAME:
452  $sql .= ' WHERE LOWER(u.first_name) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
453  $var = $match == 'is' ? $value : "%$value%";
454  break;
455  case USER_FIELD_LASTNAME:
456  $sql .= ' WHERE LOWER(u.last_name) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
457  $var = $match == 'is' ? $value : "%$value%";
458  break;
459  }
460 
461  $roleDao =& DAORegistry::getDAO('RoleDAO');
462  $orderSql = ($sortBy?(' ORDER BY ' . $roleDao->getSortMapping($sortBy) . ' ' . $this->getDirectionMapping($sortDirection)) : '');
463  if ($field != USER_FIELD_NONE) $result =& $this->retrieveRange($sql . ($allowDisabled?'':' AND u.disabled = 0') . $orderSql, $var, $dbResultRange);
464  else $result =& $this->retrieveRange($sql . ($allowDisabled?'':' WHERE u.disabled = 0') . $orderSql, false, $dbResultRange);
465 
466  $returner = new DAOResultFactory($result, $this, '_returnUserFromRowWithData');
467  return $returner;
468  }
469 
476  function &getUsersWithNoRole($allowDisabled = true, $dbResultRange = null) {
477  $sql = 'SELECT u.* FROM users u LEFT JOIN roles r ON u.user_id=r.user_id WHERE r.role_id IS NULL';
478 
479  $orderSql = ' ORDER BY u.last_name, u.first_name'; // FIXME Add "sort field" parameter?
480 
481  $result =& $this->retrieveRange($sql . ($allowDisabled?'':' AND u.disabled = 0') . $orderSql, false, $dbResultRange);
482 
483  $returner = new DAOResultFactory($result, $this, '_returnUserFromRowWithData');
484  return $returner;
485  }
486 
493  function userExistsById($userId, $allowDisabled = true) {
494  $result =& $this->retrieve(
495  'SELECT COUNT(*) FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
496  array((int) $userId)
497  );
498  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
499 
500  $result->Close();
501  unset($result);
502 
503  return $returner;
504  }
505 
513  function userExistsByUsername($username, $userId = null, $allowDisabled = true) {
514  $result =& $this->retrieve(
515  'SELECT COUNT(*) FROM users WHERE username = ?' . (isset($userId) ? ' AND user_id != ?' : '') . ($allowDisabled?'':' AND disabled = 0'),
516  isset($userId) ? array($username, (int) $userId) : array($username)
517  );
518  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
519 
520  $result->Close();
521  unset($result);
522 
523  return $returner;
524  }
525 
533  function userExistsByEmail($email, $userId = null, $allowDisabled = true) {
534  $result =& $this->retrieve(
535  'SELECT COUNT(*) FROM users WHERE email = ?' . (isset($userId) ? ' AND user_id != ?' : '') . ($allowDisabled?'':' AND disabled = 0'),
536  isset($userId) ? array($email, (int) $userId) : array($email)
537  );
538  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
539 
540  $result->Close();
541  unset($result);
542 
543  return $returner;
544  }
545 
550  function getInsertUserId() {
551  return $this->getInsertId('users', 'user_id');
552  }
553 
558  function getGenderOptions() {
559  return array(
560  '' => '',
561  'M' => 'user.masculine',
562  'F' => 'user.feminine',
563  'O' => 'user.other',
564  );
565  }
566 }
567 
568 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& getUsersWithNoRole($allowDisabled=true, $dbResultRange=null)
userExistsByEmail($email, $userId=null, $allowDisabled=true)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
& _returnUserFromRow(&$row, $callHook=true)
userExistsById($userId, $allowDisabled=true)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:460
getCurrentDate($ts=null)
Definition: Core.inc.php:94
deleteObject(&$user)
& getById($userId, $allowDisabled=true)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
Basic class describing users existing in the system.
Definition: User.inc.php:19
getUserFullName($userId, $allowDisabled=true)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
& getUserByCredentials($username, $password, $allowDisabled=true)
& getUserByEmail($email, $allowDisabled=true)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
updateObject(&$user)
& getDAO($name, $dbconn=null)
deleteUserById($userId)
& getUsersByField($field=USER_FIELD_NONE, $match=null, $value=null, $allowDisabled=true, $dbResultRange=null, $sortBy=null, $sortDirection=SORT_DIRECTION_ASC)
Operations for retrieving and modifying User objects.
call($hookName, $args=null)
getUserEmail($userId, $allowDisabled=true)
& getUserByAuthStr($authstr, $allowDisabled=true)
userExistsByUsername($username, $userId=null, $allowDisabled=true)
insertUser(&$user)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
getDirectionMapping($direction)
Definition: DAO.inc.php:588
& getByUsername($username, $allowDisabled=true)