Minor refactoring: introduce user as an object.

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@172 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
mensonge 2008-11-21 18:45:18 +00:00
parent 49dec69230
commit 9aafe7551e
3 changed files with 590 additions and 553 deletions

View file

@ -13,7 +13,6 @@ if(DEBUG_MODE) {
ini_set('display_errors', '1');
ini_set('mysql.trace_mode', '1');
error_reporting(E_ALL);
//error_reporting(E_ALL^E_NOTICE);
} else {
ini_set('display_errors', '0');
ini_set('mysql.trace_mode', '0');

View file

@ -1,6 +1,15 @@
<?php
class UserService {
var $db;
var $fields = array(
'primary' => 'uId',
'username' => 'username',
'password' => 'password');
var $profileurl;
var $tablename;
var $sessionkey;
var $cookiekey;
var $cookietime = 1209600; // 2 weeks
function &getInstance(&$db) {
static $instance;
@ -9,17 +18,6 @@ class UserService {
return $instance;
}
var $fields = array(
'primary' => 'uId',
'username' => 'username',
'password' => 'password'
);
var $profileurl;
var $tablename;
var $sessionkey;
var $cookiekey;
var $cookietime = 1209600; // 2 weeks
function UserService(& $db) {
$this->db =& $db;
$this->tablename = $GLOBALS['tableprefix'] .'users';
@ -115,15 +113,21 @@ class UserService {
return $this->_getuser($this->getFieldName('primary'), $id);
}
// Momentary useful in order to go to object code
function getObjectUser($id) {
$user = $this->_getuser($this->getFieldName('primary'), $id);
return new User($id, $user[$this->getFieldName('username')]);
}
function isLoggedOn() {
return ($this->getCurrentUserId() !== false);
}
function &getCurrentUser($refresh = FALSE, $newval = NULL) {
static $currentuser;
if (!is_null($newval)) //internal use only: reset currentuser
if (!is_null($newval)) { //internal use only: reset currentuser
$currentuser = $newval;
else if ($refresh || !isset($currentuser)) {
} else if ($refresh || !isset($currentuser)) {
if ($id = $this->getCurrentUserId()) {
$currentuser = $this->getUser($id);
} else {
@ -133,6 +137,21 @@ class UserService {
return $currentuser;
}
// Momentary useful in order to go to object code
function getCurrentObjectUser($refresh = FALSE, $newval = NULL) {
static $currentObjectUser;
if (!is_null($newval)) { //internal use only: reset currentuser
$currentObjectUser = $newval;
} else if ($refresh || !isset($currentObjectUser)) {
if ($id = $this->getCurrentUserId()) {
$currentObjectUser = $this->getObjectUser($id);
} else {
$currentObjectUser = null;
}
}
return $currentObjectUser;
}
function isAdmin($userid) {
$user = $this->getUser($userid);
@ -144,13 +163,11 @@ class UserService {
}
}
/* return current user id based on session or cookie */
function getCurrentUserId() {
if (isset($_SESSION[$this->getSessionKey()])) {
//echo "session";die($_SESSION[$this->getSessionKey()]);
return $_SESSION[$this->getSessionKey()];
} else if (isset($_COOKIE[$this->getCookieKey()])) {
//echo "cookie";die();
$cook = split(':', $_COOKIE[$this->getCookieKey()]);
//cookie looks like this: 'id:md5(username+password)'
$query = 'SELECT * FROM '. $this->getTableName() .
@ -428,4 +445,33 @@ class UserService {
function getCookieKey() { return $this->cookiekey; }
function setCookieKey($value) { $this->cookiekey = $value; }
}
class User {
var $id;
var $username;
var $isAdmin;
function User($id, $username) {
$this->id = $id;
$this->username = $username;
}
function getId() {
return $this->id;
}
function getUsername() {
return $this->username;
}
function isAdmin() {
// Look for value if not already set
if(!isset($this->isAdmin)) {
$userservice =& ServiceFactory::getServiceInstance('UserService');
$this->isAdmin = $userservice->isAdmin($this->id);
}
return $this->isAdmin;
}
}
?>

View file

@ -8,9 +8,13 @@ $cdservice =& ServiceFactory::getServiceInstance('CommonDescriptionService');
$logged_on_userid = $userservice->getCurrentUserId();
$currentUser = $userservice->getCurrentUser();
$currentUsername = $currentUser[$userservice->getFieldName('username')];
//$logged_on_userid = $userservice->getCurrentUserId();
//$currentUser = $userservice->getCurrentUser();
//$currentUsername = $currentUser[$userservice->getFieldName('username')];
// Momentary useful to go to object code
$currentObjectUser = $userservice->getCurrentObjectUser();
$pageName = isset($pageName)?$pageName:"";
$this->includeTemplate($GLOBALS['top_include']);
@ -25,10 +29,8 @@ include('search.inc.php');
<?php
if((isset($currenttag) && $GLOBALS['enableCommonTagDescription'])
|| (isset($hash) && $GLOBALS['enableCommonBookmarkDescription'])):?>
<p class="commondescription">
<?php
|| (isset($hash) && $GLOBALS['enableCommonBookmarkDescription'])):?>
<p class="commondescription"><?php
if(isset($currenttag) && $cdservice->getLastTagDescription($currenttag)) {
$description = $cdservice->getLastTagDescription($currenttag);
echo nl2br(filter($description['cdDescription']));
@ -38,7 +40,7 @@ if(isset($currenttag) && $cdservice->getLastTagDescription($currenttag)) {
echo nl2br(filter($description['cdDescription'])). "<br/>";
}
if($logged_on_userid>0) {
if($userservice->isLoggedOn()) {
if(isset($currenttag)) {
echo ' (<a href="'. createURL('tagcommondescriptionedit', $currenttag).'">';
echo T_('edit common description').'</a>)';
@ -47,8 +49,7 @@ if($logged_on_userid>0) {
echo T_('edit common description').'</a>)';
}
}
?>
</p>
?></p>
<?php endif ?>
@ -58,12 +59,10 @@ if(isset($currenttag) && isset($user)) {
$userObject = $userservice->getUserByUsername($user);
if($tagservice->getDescription($currenttag, $userObject['uId'])) { ?>
<p class="commondescription">
<?php
$description = $tagservice->getDescription($currenttag, $userObject['uId']);
echo nl2br(filter($description['tDescription']));
?>
</p>
<p class="commondescription"><?php
$description = $tagservice->getDescription($currenttag, $userObject['uId']);
echo nl2br(filter($description['tDescription']));
?></p>
<?php
}
@ -75,44 +74,38 @@ if(isset($currenttag) && isset($user)) {
window.onload = playerLoad;
</script>
<p id="sort">
<?php echo $total.' '.T_("bookmark(s)"); ?> -
<?php echo T_("Sort by:"); ?>
<p id="sort"><?php echo $total.' '.T_("bookmark(s)"); ?> - <?php echo T_("Sort by:"); ?>
<?php
$dateSort = (getSortOrder()=='date_desc')? 'date_asc':'date_desc';
$titleSort = (getSortOrder()=='title_asc')? 'title_desc':'title_asc';
$urlSort = (getSortOrder()=='url_asc')? 'url_desc':'url_asc';
?> <a href="?sort=<?php echo $dateSort ?>"><?php echo T_("Date"); ?></a><span>
/ </span> <a href="?sort=<?php echo $titleSort ?>"><?php echo T_("Title"); ?></a><span>
/ </span> <?php
if (!isset($hash)) {
?> <a href="?sort=<?php echo $urlSort ?>"><?php echo T_("URL"); ?></a>
<?php
$dateSort = (getSortOrder()=='date_desc')? 'date_asc':'date_desc';
$titleSort = (getSortOrder()=='title_asc')? 'title_desc':'title_asc';
$urlSort = (getSortOrder()=='url_asc')? 'url_desc':'url_asc';
?>
<a href="?sort=<?php echo $dateSort ?>"><?php echo T_("Date"); ?></a><span> / </span>
<a href="?sort=<?php echo $titleSort ?>"><?php echo T_("Title"); ?></a><span> / </span>
<?php
if (!isset($hash)) {
?>
<a href="?sort=<?php echo $urlSort ?>"><?php echo T_("URL"); ?></a>
<?php
}
?>
<?php
if(isset($currenttag)) {
}
?> <?php
if(isset($currenttag)) {
if(isset($user)) {
echo ' - ';
echo '<a href="'. createURL('tags', $currenttag) .'">';
echo T_('Bookmarks from other users for this tag').'</a>';
//echo T_(' for these tags');
} else if($logged_on_userid>0){
} else if($userservice->isLoggedOn()){
echo ' - ';
echo '<a href="'. createURL('bookmarks', $currentUsername.'/'.$currenttag) .'">';
echo '<a href="'. createURL('bookmarks', $currentObjectUser->getUsername().'/'.$currenttag) .'">';
echo T_('Only your bookmarks for this tag').'</a>';
//echo T_(' for these tags');
}
}
?>
</p>
}
?></p>
<ol<?php echo ($start > 0 ? ' start="'. ++$start .'"' : ''); ?> id="bookmarks">
<ol <?php echo ($start > 0 ? ' start="'. ++$start .'"' : ''); ?>
id="bookmarks">
<?php
foreach(array_keys($bookmarks) as $key) {
@ -172,11 +165,10 @@ window.onload = playerLoad;
}
// Copy link
if ($userservice->isLoggedOn() && ($logged_on_userid != $row['uId']) && !$bookmarkservice->bookmarkExists($row['bAddress'], $logged_on_userid)) {
// Get the username of the current user
$currentUser = $userservice->getCurrentUser();
$currentUsername = $currentUser[$userservice->getFieldName('username')];
$copy .= ' - <a href="'. createURL('bookmarks', $currentUsername .'?action=add&amp;address='. urlencode($row['bAddress']) .'&amp;title='. urlencode($row['bTitle'])). '&amp;description='.urlencode($row['bDescription']). '&amp;tags='.$tagsForCopy .'">'. T_('Copy') .'</a>';
if ($userservice->isLoggedOn()
&& ($currentObjectUser->getId() != $row['uId'])
&& !$bookmarkservice->bookmarkExists($row['bAddress'], $currentObjectUser->getId())) {
$copy .= ' - <a href="'. createURL('bookmarks', $currentObjectUser->getUsername() .'?action=add&amp;address='. urlencode($row['bAddress']) .'&amp;title='. urlencode($row['bTitle'])). '&amp;description='.urlencode($row['bDescription']). '&amp;tags='.$tagsForCopy .'">'. T_('Copy') .'</a>';
}
// Nofollow option