diff --git a/src/SemanticScuttle/Model/User.php b/src/SemanticScuttle/Model/User.php new file mode 100644 index 0000000..03af5c7 --- /dev/null +++ b/src/SemanticScuttle/Model/User.php @@ -0,0 +1,184 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +/** + * SemanticScuttle user object. + * Rare fields are filled if required. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class SemanticScuttle_Model_User +{ + var $id; + var $username; + var $name; + var $email; + var $homepage; + var $content; + var $datetime; + var $isAdmin; + + /** + * Create a new user object + * + * @param integer $id User ID + * @param string $username Username + */ + public function __construct($id, $username) + { + $this->id = $id; + $this->username = $username; + } + + /** + * Returns user ID + * + * @return integer ID + */ + public function getId() + { + return $this->id; + } + + /** + * Returns logon user name + * + * @return string User name + */ + public function getUsername() + { + return $this->username; + } + + /** + * Returns full user name as specified in the profile. + * + * @return string Full name + */ + public function getName() + { + // Look for value only if not already set + if (!isset($this->name)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $user = $us->getUser($this->id); + $this->name = $user['name']; + } + return $this->name; + } + + /** + * Returns user email address + * + * @return string Email address + */ + public function getEmail() + { + // Look for value only if not already set + if (!isset($this->email)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $user = $us->getUser($this->id); + $this->email = $user['email']; + } + return $this->email; + } + + /** + * Returns user homepage as specified in the profile. + * + * @return string Homepage + */ + public function getHomepage() + { + // Look for value only if not already set + if(!isset($this->homepage)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $user = $us->getUser($this->id); + $this->homepage = $user['homepage']; + } + return $this->homepage; + } + + /** + * Returns custom user description as specified in the profile. + * + * @return string User description + */ + public function getContent() + { + // Look for value only if not already set + if(!isset($this->content)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $user = $us->getUser($this->id); + $this->content = $user['uContent']; + } + return $this->content; + } + + /** + * Returns user creation time + * + * @return string Datetime value: "YYYY-MM-DD HH:MM:SS" + */ + public function getDatetime() + { + // Look for value only if not already set + if(!isset($this->content)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $user = $us->getUser($this->id); + $this->datetime = $user['uDatetime']; + } + return $this->datetime; + } + + /** + * Tells you if the user is an administrator + * + * @return boolean True if the user is admin + */ + public function isAdmin() + { + // Look for value only if not already set + if(!isset($this->isAdmin)) { + $us = SemanticScuttle_Service_Factory::get('User'); + $this->isAdmin = $us->isAdmin($this->id); + } + return $this->isAdmin; + } + + /** + * Returns the number of bookmarks the user owns + * + * @param string $range Range of bookmarks: + * 'public', 'shared', 'private' + * or 'all' + * + * @return integer Number of bookmarks + * + * @uses SemanticScuttle_Service_Bookmark::countBookmarks() + */ + public function getNbBookmarks($range = 'public') + { + $bs = SemanticScuttle_Service_Factory::get('Bookmark'); + return $bs->countBookmarks($this->getId(), $range); + } + +} +?> \ No newline at end of file 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); - } -} ?>