ocs  2.3.6
About: OCS (Open Conference System) is a Web publishing tool that will create a complete Web presence for your scholarly conference.
  Fossies Dox: ocs-2.3.6.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

PKPUserDAO.inc.php
Go to the documentation of this file.
1 <?php
2 
16 // $Id$
17 
18 
19 /* These constants are used user-selectable search fields. */
20 define('USER_FIELD_USERID', 'user_id');
21 define('USER_FIELD_FIRSTNAME', 'first_name');
22 define('USER_FIELD_LASTNAME', 'last_name');
23 define('USER_FIELD_USERNAME', 'username');
24 define('USER_FIELD_EMAIL', 'email');
25 define('USER_FIELD_URL', 'url');
26 define('USER_FIELD_INTERESTS', 'interests');
27 define('USER_FIELD_INITIAL', 'initial');
28 define('USER_FIELD_NONE', null);
29 
30 class PKPUserDAO extends DAO {
37  function &getUser($userId, $allowDisabled = true) {
38  $result =& $this->retrieve(
39  'SELECT * FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
40  array((int) $userId)
41  );
42 
43  $user = null;
44  if ($result->RecordCount() != 0) {
45  $user =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
46  }
47  $result->Close();
48  unset($result);
49  return $user;
50  }
51 
58  function &getUserByUsername($username, $allowDisabled = true) {
59  $result =& $this->retrieve(
60  'SELECT * FROM users WHERE username = ?' . ($allowDisabled?'':' AND disabled = 0'),
61  array($username)
62  );
63 
64  $returner = null;
65  if ($result->RecordCount() != 0) {
66  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
67  }
68  $result->Close();
69  unset($result);
70  return $returner;
71  }
72 
79  function &getUserByAuthStr($authstr, $allowDisabled = true) {
80  $result =& $this->retrieve(
81  'SELECT * FROM users WHERE auth_str = ?' . ($allowDisabled?'':' AND disabled = 0'),
82  array($authstr)
83  );
84 
85  $returner = null;
86  if ($result->RecordCount() != 0) {
87  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
88  }
89  $result->Close();
90  unset($result);
91  return $returner;
92  }
93 
100  function &getUserByEmail($email, $allowDisabled = true) {
101  $result =& $this->retrieve(
102  'SELECT * FROM users WHERE email = ?' . ($allowDisabled?'':' AND disabled = 0'),
103  array($email)
104  );
105 
106  $returner = null;
107  if ($result->RecordCount() != 0) {
108  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
109  }
110  $result->Close();
111  unset($result);
112  return $returner;
113  }
114 
122  function &getUserByCredentials($username, $password, $allowDisabled = true) {
123  $result =& $this->retrieve(
124  'SELECT * FROM users WHERE username = ? AND password = ?' . ($allowDisabled?'':' AND disabled = 0'),
125  array($username, $password)
126  );
127 
128  $returner = null;
129  if ($result->RecordCount() != 0) {
130  $returner =& $this->_returnUserFromRowWithData($result->GetRowAssoc(false));
131  }
132  $result->Close();
133  unset($result);
134  return $returner;
135  }
136 
137  function &_returnUserFromRowWithData(&$row) {
138  $user =& $this->_returnUserFromRow($row, false);
139  $this->getDataObjectSettings('user_settings', 'user_id', $row['user_id'], $user);
140 
141  HookRegistry::call('UserDAO::_returnUserFromRowWithData', array(&$user, &$row));
142 
143  return $user;
144  }
145 
152  function &_returnUserFromRow(&$row, $callHook = true) {
153  $user = new User();
154  $user->setId($row['user_id']);
155  $user->setUsername($row['username']);
156  $user->setPassword($row['password']);
157  $user->setSalutation($row['salutation']);
158  $user->setFirstName($row['first_name']);
159  $user->setMiddleName($row['middle_name']);
160  $user->setInitials($row['initials']);
161  $user->setLastName($row['last_name']);
162  $user->setGender($row['gender']);
163  $user->setAffiliation($row['affiliation']);
164  $user->setEmail($row['email']);
165  $user->setUrl($row['url']);
166  $user->setPhone($row['phone']);
167  $user->setFax($row['fax']);
168  $user->setMailingAddress($row['mailing_address']);
169  $user->setCountry($row['country']);
170  $user->setLocales(isset($row['locales']) && !empty($row['locales']) ? explode(':', $row['locales']) : array());
171  $user->setDateLastEmail($this->datetimeFromDB($row['date_last_email']));
172  $user->setDateRegistered($this->datetimeFromDB($row['date_registered']));
173  $user->setDateValidated($this->datetimeFromDB($row['date_validated']));
174  $user->setDateLastLogin($this->datetimeFromDB($row['date_last_login']));
175  $user->setMustChangePassword($row['must_change_password']);
176  $user->setDisabled($row['disabled']);
177  $user->setDisabledReason($row['disabled_reason']);
178  $user->setAuthId($row['auth_id']);
179  $user->setAuthStr($row['auth_str']);
180 
181  if ($callHook) HookRegistry::call('UserDAO::_returnUserFromRow', array(&$user, &$row));
182 
183  return $user;
184  }
185 
190  function insertUser(&$user) {
191  if ($user->getDateRegistered() == null) {
192  $user->setDateRegistered(Core::getCurrentDate());
193  }
194  if ($user->getDateLastLogin() == null) {
195  $user->setDateLastLogin(Core::getCurrentDate());
196  }
197  $this->update(
198  sprintf('INSERT INTO users
199  (username, password, salutation, first_name, middle_name, initials, last_name, gender, affiliation, email, url, phone, fax, mailing_address, country, locales, date_last_email, date_registered, date_validated, date_last_login, must_change_password, disabled, disabled_reason, auth_id, auth_str)
200  VALUES
201  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s, %s, %s, %s, ?, ?, ?, ?, ?)',
202  $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateRegistered()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
203  array(
204  $user->getUsername(),
205  $user->getPassword(),
206  $user->getSalutation(),
207  $user->getFirstName(),
208  $user->getMiddleName(),
209  $user->getInitials(),
210  $user->getLastName(),
211  $user->getGender(),
212  $user->getAffiliation(),
213  $user->getEmail(),
214  $user->getUrl(),
215  $user->getPhone(),
216  $user->getFax(),
217  $user->getMailingAddress(),
218  $user->getCountry(),
219  join(':', $user->getLocales()),
220  $user->getMustChangePassword() ? 1 : 0,
221  $user->getDisabled() ? 1 : 0,
222  $user->getDisabledReason(),
223  $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
224  $user->getAuthStr()
225  )
226  );
227 
228  $user->setId($this->getInsertUserId());
229  $this->updateLocaleFields($user);
230  return $user->getId();
231  }
232 
233  function getLocaleFieldNames() {
234  return array('biography', 'signature', 'interests', 'gossip');
235  }
236 
237  function updateLocaleFields(&$user) {
238  $this->updateDataObjectSettings('user_settings', $user, array(
239  'user_id' => (int) $user->getId()
240  ));
241  }
242 
247  function updateObject(&$user) {
248  if ($user->getDateLastLogin() == null) {
249  $user->setDateLastLogin(Core::getCurrentDate());
250  }
251 
252  $this->updateLocaleFields($user);
253 
254  return $this->update(
255  sprintf('UPDATE users
256  SET username = ?,
257  password = ?,
258  salutation = ?,
259  first_name = ?,
260  middle_name = ?,
261  initials = ?,
262  last_name = ?,
263  gender = ?,
264  affiliation = ?,
265  email = ?,
266  url = ?,
267  phone = ?,
268  fax = ?,
269  mailing_address = ?,
270  country = ?,
271  locales = ?,
272  date_last_email = %s,
273  date_validated = %s,
274  date_last_login = %s,
275  must_change_password = ?,
276  disabled = ?,
277  disabled_reason = ?,
278  auth_id = ?,
279  auth_str = ?
280  WHERE user_id = ?',
281  $this->datetimeToDB($user->getDateLastEmail()), $this->datetimeToDB($user->getDateValidated()), $this->datetimeToDB($user->getDateLastLogin())),
282  array(
283  $user->getUsername(),
284  $user->getPassword(),
285  $user->getSalutation(),
286  $user->getFirstName(),
287  $user->getMiddleName(),
288  $user->getInitials(),
289  $user->getLastName(),
290  $user->getGender(),
291  $user->getAffiliation(),
292  $user->getEmail(),
293  $user->getUrl(),
294  $user->getPhone(),
295  $user->getFax(),
296  $user->getMailingAddress(),
297  $user->getCountry(),
298  join(':', $user->getLocales()),
299  $user->getMustChangePassword() ? 1 : 0,
300  $user->getDisabled() ? 1 : 0,
301  $user->getDisabledReason(),
302  $user->getAuthId()=='' ? null : (int) $user->getAuthId(),
303  $user->getAuthStr(),
304  (int) $user->getId(),
305  )
306  );
307  }
308 
309  function updateUser(&$user) {
310  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
311  return $this->updateObject($user);
312  }
313 
318  function deleteObject(&$user) {
319  return $this->deleteUserById($user->getId());
320  }
321 
322  function deleteUser(&$user) {
323  if (Config::getVar('debug', 'deprecation_warnings')) trigger_error('Deprecated function.');
324  return $this->deleteObject($user);
325  }
326 
331  function deleteUserById($userId) {
332  $this->update('DELETE FROM user_settings WHERE user_id = ?', array((int) $userId));
333  return $this->update('DELETE FROM users WHERE user_id = ?', array((int) $userId));
334  }
335 
342  function getUserFullName($userId, $allowDisabled = true) {
343  $result =& $this->retrieve(
344  'SELECT first_name, middle_name, last_name FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
345  array((int) $userId)
346  );
347 
348  if($result->RecordCount() == 0) {
349  $returner = false;
350  } else {
351  $returner = $result->fields[0] . ' ' . (empty($result->fields[1]) ? '' : $result->fields[1] . ' ') . $result->fields[2];
352  }
353 
354  $result->Close();
355  unset($result);
356 
357  return $returner;
358  }
359 
366  function getUserEmail($userId, $allowDisabled = true) {
367  $result =& $this->retrieve(
368  'SELECT email FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
369  array((int) $userId)
370  );
371 
372  if($result->RecordCount() == 0) {
373  $returner = false;
374  } else {
375  $returner = $result->fields[0];
376  }
377 
378  $result->Close();
379  unset($result);
380 
381  return $returner;
382  }
383 
394  function &getUsersByField($field = USER_FIELD_NONE, $match = null, $value = null, $allowDisabled = true, $dbResultRange = null, $sortBy = null, $sortDirection = SORT_DIRECTION_ASC) {
395  $sql = 'SELECT * FROM users u';
396  switch ($field) {
397  case USER_FIELD_USERID:
398  $sql .= ' WHERE u.user_id = ?';
399  $var = (int) $value;
400  break;
401  case USER_FIELD_USERNAME:
402  $sql .= ' WHERE LOWER(u.username) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
403  $var = $match == 'is' ? $value : "%$value%";
404  break;
405  case USER_FIELD_INITIAL:
406  $sql .= ' WHERE LOWER(u.last_name) LIKE LOWER(?)';
407  $var = "$value%";
408  break;
410  $sql .= ', user_settings us WHERE us.user_id = u.user_id AND us.setting_name = \'interests\' AND LOWER(us.setting_value) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
411  $var = $match == 'is' ? $value : "%$value%";
412  break;
413  case USER_FIELD_EMAIL:
414  $sql .= ' WHERE LOWER(u.email) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
415  $var = $match == 'is' ? $value : "%$value%";
416  break;
417  case USER_FIELD_URL:
418  $sql .= ' WHERE LOWER(u.url) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
419  $var = $match == 'is' ? $value : "%$value%";
420  break;
422  $sql .= ' WHERE LOWER(u.first_name) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
423  $var = $match == 'is' ? $value : "%$value%";
424  break;
425  case USER_FIELD_LASTNAME:
426  $sql .= ' WHERE LOWER(u.last_name) ' . ($match == 'is' ? '=' : 'LIKE') . ' LOWER(?)';
427  $var = $match == 'is' ? $value : "%$value%";
428  break;
429  }
430 
431  $roleDao =& DAORegistry::getDAO('RoleDAO');
432  $orderSql = ($sortBy?(' ORDER BY ' . $roleDao->getSortMapping($sortBy) . ' ' . $this->getDirectionMapping($sortDirection)) : '');
433  if ($field != USER_FIELD_NONE) $result =& $this->retrieveRange($sql . ($allowDisabled?'':' AND u.disabled = 0') . $orderSql, $var, $dbResultRange);
434  else $result =& $this->retrieveRange($sql . ($allowDisabled?'':' WHERE u.disabled = 0') . $orderSql, false, $dbResultRange);
435 
436  $returner = new DAOResultFactory($result, $this, '_returnUserFromRowWithData');
437  return $returner;
438  }
439 
446  function &getUsersWithNoRole($allowDisabled = true, $dbResultRange = null) {
447  $sql = 'SELECT u.* FROM users u LEFT JOIN roles r ON u.user_id=r.user_id WHERE r.role_id IS NULL';
448 
449  $orderSql = ' ORDER BY u.last_name, u.first_name'; // FIXME Add "sort field" parameter?
450 
451  $result =& $this->retrieveRange($sql . ($allowDisabled?'':' AND u.disabled = 0') . $orderSql, false, $dbResultRange);
452 
453  $returner = new DAOResultFactory($result, $this, '_returnUserFromRowWithData');
454  return $returner;
455  }
456 
463  function userExistsById($userId, $allowDisabled = true) {
464  $result =& $this->retrieve(
465  'SELECT COUNT(*) FROM users WHERE user_id = ?' . ($allowDisabled?'':' AND disabled = 0'),
466  array((int) $userId)
467  );
468  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
469 
470  $result->Close();
471  unset($result);
472 
473  return $returner;
474  }
475 
483  function userExistsByUsername($username, $userId = null, $allowDisabled = true) {
484  $result =& $this->retrieve(
485  'SELECT COUNT(*) FROM users WHERE username = ?' . (isset($userId) ? ' AND user_id != ?' : '') . ($allowDisabled?'':' AND disabled = 0'),
486  isset($userId) ? array($username, (int) $userId) : array($username)
487  );
488  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
489 
490  $result->Close();
491  unset($result);
492 
493  return $returner;
494  }
495 
503  function userExistsByEmail($email, $userId = null, $allowDisabled = true) {
504  $result =& $this->retrieve(
505  'SELECT COUNT(*) FROM users WHERE email = ?' . (isset($userId) ? ' AND user_id != ?' : '') . ($allowDisabled?'':' AND disabled = 0'),
506  isset($userId) ? array($email, (int) $userId) : array($email)
507  );
508  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
509 
510  $result->Close();
511  unset($result);
512 
513  return $returner;
514  }
515 
520  function getInsertUserId() {
521  return $this->getInsertId('users', 'user_id');
522  }
523 
528  function getGenderOptions() {
529  return array(
530  '' => '',
531  'M' => 'user.masculine',
532  'F' => 'user.feminine',
533  'O' => 'user.other',
534  );
535  }
536 }
537 
538 ?>
config TEMPLATE inc php
Definition: config.inc.php:4
& _returnUserFromRowWithData(&$row)
const USER_FIELD_INTERESTS
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
const USER_FIELD_FIRSTNAME
& getUsersWithNoRole($allowDisabled=true, $dbResultRange=null)
const USER_FIELD_LASTNAME
userExistsByEmail($email, $userId=null, $allowDisabled=true)
const USER_FIELD_USERNAME
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:62
& _returnUserFromRow(&$row, $callHook=true)
userExistsById($userId, $allowDisabled=true)
& getUser($userId, $allowDisabled=true)
getInsertId($table='', $id='', $callHooks=true)
Definition: DAO.inc.php:225
const USER_FIELD_INITIAL
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:412
getCurrentDate($ts=null)
Definition: Core.inc.php:90
deleteObject(&$user)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:152
Basic class describing users existing in the system.
Definition: User.inc.php:25
deleteUser(&$user)
getUserFullName($userId, $allowDisabled=true)
& getUserByUsername($username, $allowDisabled=true)
datetimeFromDB($dt)
Definition: DAO.inc.php:276
& getUserByCredentials($username, $password, $allowDisabled=true)
const USER_FIELD_USERID
& getUserByEmail($email, $allowDisabled=true)
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
getVar($section, $key, $default=null)
Definition: Config.inc.php:35
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.
const USER_FIELD_URL
call($hookName, $args=null)
getUserEmail($userId, $allowDisabled=true)
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, &$dataObject)
Definition: DAO.inc.php:498
updateLocaleFields(&$user)
& getUserByAuthStr($authstr, $allowDisabled=true)
userExistsByUsername($username, $userId=null, $allowDisabled=true)
insertUser(&$user)
datetimeToDB($dt)
Definition: DAO.inc.php:258
const USER_FIELD_NONE
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:186
const USER_FIELD_EMAIL
getDirectionMapping($direction)
Definition: DAO.inc.php:541
updateUser(&$user)
const SORT_DIRECTION_ASC
Definition: DAO.inc.php:27