move user class into own file

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@473 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
cweiske 2009-10-30 06:05:29 +00:00
parent cb713ccd76
commit cc2c8242c7
3 changed files with 275 additions and 119 deletions

View file

@ -0,0 +1,184 @@
<?php
/**
* SemanticScuttle - your social bookmark manager.
*
* PHP version 5.
*
* @category Bookmarking
* @package SemanticScuttle
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
* @author Christian Weiske <cweiske@cweiske.de>
* @author Eric Dane <ericdane@users.sourceforge.net>
* @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 <mensonge@users.sourceforge.net>
* @author Christian Weiske <cweiske@cweiske.de>
* @author Eric Dane <ericdane@users.sourceforge.net>
* @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);
}
}
?>

View file

@ -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 = 'SELECT COUNT(*) FROM '. $this->getTableName();
$sql.= ' WHERE uId = '.$uId; $sql.= ' WHERE uId = ' . intval($uId);
switch ($range) { switch ($range) {
case 'all': case 'all':
//no constraints //no constraints
break; break;
case 'private': case 'private':
$sql.= ' AND bStatus = 2'; $sql .= ' AND bStatus = 2';
break; break;
case 'shared': case 'shared':
$sql.= ' AND bStatus = 1'; $sql .= ' AND bStatus = 1';
break; break;
case 'public': case 'public':
default: default:
$sql.= ' AND bStatus = 0'; $sql .= ' AND bStatus = 0';
break; break;
} }
if (!($dbresult = & $this->db->sql_query($sql))) { if (!($dbresult = $this->db->sql_query($sql))) {
message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); message_die(
GENERAL_ERROR, 'Could not get vars',
'', __LINE__, __FILE__, $sql, $this->db
);
} }
return $this->db->sql_fetchfield(0, 0); return $this->db->sql_fetchfield(0, 0);
} }
@ -227,7 +239,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
$address = $this->normalize($address); $address = $this->normalize($address);
$crit = array ('bHash' => md5($address)); $crit = array('bHash' => md5($address));
if (isset ($uid)) { if (isset ($uid)) {
$crit['uId'] = $uid; $crit['uId'] = $uid;
} }

View file

@ -13,6 +13,8 @@
* @link http://sourceforge.net/projects/semanticscuttle * @link http://sourceforge.net/projects/semanticscuttle
*/ */
require_once 'SemanticScuttle/Model/User.php';
/** /**
* SemanticScuttle user management service. * SemanticScuttle user management service.
* *
@ -128,18 +130,36 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
return $users; return $users;
} }
function & getObjectUsers($nb=0) { /**
$query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; * Returns an array of user objects.
if($nb>0) { * Array is in order of uids
$query .= ' LIMIT 0, '.$nb; *
* @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)) ) { 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; return false;
} }
while ($row = & $this->db->sql_fetchrow($dbresult)) { 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); $this->db->sql_freeresult($dbresult);
return $users; return $users;
@ -181,7 +201,9 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
function getObjectUserByUsername($username) { function getObjectUserByUsername($username) {
$user = $this->_getuser($this->getFieldName('username'), $username); $user = $this->_getuser($this->getFieldName('username'), $username);
if($user != false) { if($user != false) {
return new User($user[$this->getFieldName('primary')], $username); return new SemanticScuttle_Model_User(
$user[$this->getFieldName('primary')], $username
);
} else { } else {
return NULL; return NULL;
} }
@ -201,14 +223,31 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
return NULL; 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); 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); $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() { function isLoggedOn() {
@ -240,10 +279,21 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
return $this->currentuser; 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; static $currentObjectUser;
if (!is_null($newval)) { //internal use only: reset currentuser if (!is_null($newval)) {
//internal use only: reset currentuser
$currentObjectUser = $newval; $currentObjectUser = $newval;
} else if ($refresh || !isset($currentObjectUser)) { } else if ($refresh || !isset($currentObjectUser)) {
if ($id = $this->getCurrentUserId()) { if ($id = $this->getCurrentUserId()) {
@ -729,94 +779,4 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
function setCookieKey($value) { $this->cookiekey = $value; } 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);
}
}
?> ?>