1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }
/**
@ingroup DC_CORE
@brief User prefs handler
dcPrefs provides user preferences management. This class instance exists as
dcAuth $prefs property. You should create a new prefs instance when
updating another user prefs.
*/
class dcPrefs
{
protected $con; ///< <b>connection</b> Database connection object
protected $table; ///< <b>string</b> Prefs table name
protected $user_id; ///< <b>string</b> User ID
protected $workspaces = array(); ///< <b>array</b> Associative workspaces array
protected $ws; ///< <b>string</b> Current workspace
/**
Object constructor. Retrieves user prefs and puts them in $workspaces
array. Local (user) prefs have a highest priority than global prefs.
@param core <b>dcCore</b> dcCore object
@param user_id <b>string</b> User ID
*/
public function __construct($core,$user_id)
{
$this->con =& $core->con;
$this->table = $core->prefix.'pref';
$this->user_id =& $user_id;
try {$this->loadPrefs();} catch (Exception $e) {
if (version_compare($core->getVersion('core'),'2.3','>')) {
trigger_error(__('Unable to retrieve workspaces:').' '.$this->con->error(), E_USER_ERROR);
}
}
}
/**
Retrieves all workspaces (and their prefs) from database, with one query.
*/
private function loadPrefs()
{
$strReq = 'SELECT user_id, pref_id, pref_value, '.
'pref_type, pref_label, pref_ws '.
'FROM '.$this->table.' '.
"WHERE user_id = '".$this->con->escape($this->user_id)."' ".
'OR user_id IS NULL '.
'ORDER BY pref_ws ASC, pref_id ASC';
try {
$rs = $this->con->select($strReq);
} catch (Exception $e) {
throw $e;
}
/* Prevent empty tables (install phase, for instance) */
if ($rs->isEmpty()) {
return;
}
do {
$ws = trim($rs->f('pref_ws'));
if (!$rs->isStart()) {
// we have to go up 1 step, since workspaces construction performs a fetch()
// at very first time
$rs->movePrev();
}
$this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws,$rs);
} while(!$rs->isStart());
}
/**
Create a new workspace. If the workspace already exists, return it without modification.
@param ws <b>string</b> Workspace name
@return <b>dcWorkspace</b> The workspace created
*/
public function addWorkspace($ws)
{
if (!array_key_exists($ws, $this->workspaces)) {
$this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws);
}
return $this->workspaces[$ws];
}
/**
Rename a workspace.
@param oldWs <b>string</b> Old workspace name
@param newws <b>string</b> New workspace name
@return <b>boolean</b>
*/
public function renWorkspace($oldNs,$newNs)
{
if (!array_key_exists($oldWs, $this->workspaces) || array_key_exists($newWs, $this->workspaces)) {
return false;
}
// Rename the workspace in the workspace array
$this->workspaces[$newWs] = $this->workspaces[$oldWs];
unset($this->workspaces[$oldWs]);
// Rename the workspace in the database
$strReq = 'UPDATE '.$this->table.
" SET pref_ws = '".$this->con->escape($newWs)."' ".
" WHERE pref_ws = '".$this->con->escape($oldWs)."' ";
$this->con->execute($strReq);
return true;
}
/**
Delete a whole workspace with all preferences pertaining to it.
@param ws <b>string</b> Workspace name
@return <b>boolean</b>
*/
public function delWorkspace($ws)
{
if (!array_key_exists($ws, $this->workspaces)) {
return false;
}
// Remove the workspace from the workspace array
unset($this->workspaces[$ws]);
// Delete all preferences from the workspace in the database
$strReq = 'DELETE FROM '.$this->table.
" WHERE pref_ws = '".$this->con->escape($ws)."' ";
$this->con->execute($strReq);
return true;
}
/**
Returns full workspace with all prefs pertaining to it.
@param ws <b>string</b> Workspace name
@return <b>dcWorkspace</b>
*/
public function get($ws)
{
if (array_key_exists($ws, $this->workspaces)) {
return $this->workspaces[$ws];
}
return null;
}
/**
Magic __get method.
@copydoc ::get
*/
public function __get($n)
{
return $this->get($n);
}
/**
Returns $workspaces property content.
@return <b>array</b>
*/
public function dumpWorkspaces()
{
return $this->workspaces;
}
}
|