From cc2c8242c76bdb39bc89976fb3425a5a934bf4b9 Mon Sep 17 00:00:00 2001 From: cweiske Date: Fri, 30 Oct 2009 06:05:29 +0000 Subject: move user class into own file git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@473 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark.php | 42 +++++--- src/SemanticScuttle/Service/User.php | 168 ++++++++++++------------------- 2 files changed, 91 insertions(+), 119 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 287994d..2ac77a4 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -154,29 +154,41 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService - /* Counts bookmarks for a user. $range = {'public', 'shared', 'private', 'all'}*/ - function countBookmarks($uId, $range = 'public') + /** + * Counts bookmarks for a user. + * + * @param integer $uId User ID + * @param string $range Range of bookmarks: + * 'public', 'shared', 'private' + * or 'all' + * + * @return integer Number of bookmarks + */ + public function countBookmarks($uId, $range = 'public') { - $sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks'; - $sql.= ' WHERE uId = '.$uId; + $sql = 'SELECT COUNT(*) FROM '. $this->getTableName(); + $sql.= ' WHERE uId = ' . intval($uId); switch ($range) { - case 'all': + case 'all': //no constraints break; - case 'private': - $sql.= ' AND bStatus = 2'; + case 'private': + $sql .= ' AND bStatus = 2'; break; - case 'shared': - $sql.= ' AND bStatus = 1'; + case 'shared': + $sql .= ' AND bStatus = 1'; break; - case 'public': - default: - $sql.= ' AND bStatus = 0'; + case 'public': + default: + $sql .= ' AND bStatus = 0'; break; } - if (!($dbresult = & $this->db->sql_query($sql))) { - message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); + if (!($dbresult = $this->db->sql_query($sql))) { + message_die( + GENERAL_ERROR, 'Could not get vars', + '', __LINE__, __FILE__, $sql, $this->db + ); } return $this->db->sql_fetchfield(0, 0); } @@ -227,7 +239,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $address = $this->normalize($address); - $crit = array ('bHash' => md5($address)); + $crit = array('bHash' => md5($address)); if (isset ($uid)) { $crit['uId'] = $uid; } diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index 73b71d6..3f07242 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -13,6 +13,8 @@ * @link http://sourceforge.net/projects/semanticscuttle */ +require_once 'SemanticScuttle/Model/User.php'; + /** * SemanticScuttle user management service. * @@ -128,18 +130,36 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService return $users; } - function & getObjectUsers($nb=0) { - $query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; - if($nb>0) { - $query .= ' LIMIT 0, '.$nb; + /** + * Returns an array of user objects. + * Array is in order of uids + * + * @param integer $nb Number of users to fetch. + * + * @return array Array of SemanticScuttle_Model_User objects + */ + public function getObjectUsers($nb = 0) + { + $query = 'SELECT * FROM ' . $this->getTableName() + . ' ORDER BY uId DESC'; + + if ($nb > 0) { + $query .= ' LIMIT 0, ' . intval($nb); } + if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + message_die( + GENERAL_ERROR, 'Could not get user', + '', __LINE__, __FILE__, $query, $this->db + ); return false; } while ($row = & $this->db->sql_fetchrow($dbresult)) { - $users[] = new User($row[$this->getFieldName('primary')], $row[$this->getFieldName('username')]); + $users[] = new SemanticScuttle_Model_User( + $row[$this->getFieldName('primary')], + $row[$this->getFieldName('username')] + ); } $this->db->sql_freeresult($dbresult); return $users; @@ -181,7 +201,9 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService function getObjectUserByUsername($username) { $user = $this->_getuser($this->getFieldName('username'), $username); if($user != false) { - return new User($user[$this->getFieldName('primary')], $username); + return new SemanticScuttle_Model_User( + $user[$this->getFieldName('primary')], $username + ); } else { return NULL; } @@ -201,14 +223,31 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService return NULL; } - function getUser($id) { + /** + * Returns user row from database. + * + * @param integer $id User ID + * + * @return array User array from database + */ + public function getUser($id) + { return $this->_getuser($this->getFieldName('primary'), $id); } - // Momentary useful in order to go to object code - function getObjectUser($id) { + /** + * Returns user object for given user id + * + * @param integer $id User ID + * + * @return SemanticScuttle_Model_User User object + */ + public function getObjectUser($id) + { $user = $this->_getuser($this->getFieldName('primary'), $id); - return new User($id, $user[$this->getFieldName('username')]); + return new SemanticScuttle_Model_User( + $id, $user[$this->getFieldName('username')] + ); } function isLoggedOn() { @@ -240,10 +279,21 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService return $this->currentuser; } - // Momentary useful in order to go to object code - function getCurrentObjectUser($refresh = FALSE, $newval = NULL) { + /** + * Return current user as object + * + * @param boolean $refresh Reload the user from database + * based on current user id + * @param mixed $newval New user value (used internally + * as setter method) + * + * @return SemanticScuttle_Model_User User object + */ + function getCurrentObjectUser($refresh = false, $newval = null) + { static $currentObjectUser; - if (!is_null($newval)) { //internal use only: reset currentuser + if (!is_null($newval)) { + //internal use only: reset currentuser $currentObjectUser = $newval; } else if ($refresh || !isset($currentObjectUser)) { if ($id = $this->getCurrentUserId()) { @@ -729,94 +779,4 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService function setCookieKey($value) { $this->cookiekey = $value; } } - -/* Defines a user. Rare fields are filled if required. */ -class User { - - var $id; - var $username; - var $name; - var $email; - var $homepage; - var $content; - var $datetime; - var $isAdmin; - - function User($id, $username) { - $this->id = $id; - $this->username = $username; - } - - function getId() { - return $this->id; - } - - function getUsername() { - return $this->username; - } - - function getName() { - // Look for value only if not already set - if(!isset($this->name)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->name = $user['name']; - } - return $this->name; - } - - function getEmail() { - // Look for value only if not already set - if(!isset($this->email)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->email = $user['email']; - } - return $this->email; - } - - function getHomepage() { - // Look for value only if not already set - if(!isset($this->homepage)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->homepage = $user['homepage']; - } - return $this->homepage; - } - - function getContent() { - // Look for value only if not already set - if(!isset($this->content)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->content = $user['uContent']; - } - return $this->content; - } - - function getDatetime() { - // Look for value only if not already set - if(!isset($this->content)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->datetime = $user['uDatetime']; - } - return $this->datetime; - } - - function isAdmin() { - // Look for value only if not already set - if(!isset($this->isAdmin)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $this->isAdmin = $userservice->isAdmin($this->id); - } - return $this->isAdmin; - } - - function getNbBookmarks($range = 'public') { - $bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); - return $bookmarkservice->countBookmarks($this->getId(), $range); - } -} ?> -- cgit v1.2.3-54-g00ecf