Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ApplicationParameterManager.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Manage application parameters
8  * Set and get application parameters
9  * @class ApplicationParameterManager
10  *
11  * @see ApplicationParameterManager
12  *
13  */
15 {
16  const CURRENT_APPLICATION = '##CURRENT APPLICATION##';
17  const GLOBAL_PARAMETER = '##GLOBAL PARAMETER##';
18  /**
19  * @var array
20  * @private
21  */
22  private static $cache = array();
23  /**
24  * for internal purpose only
25  * @private
26  */
27  public static function resetCache()
28  {
29  self::$cache = array();
30  }
31  /**
32  * Return the value of a user parameter
33  *
34  * @param string|int|Application $application logical name or id or object of the application, you can use
35  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
36  * @param string $parameterName logical name of the parameter
37  * @param null|int $userId user login or account id, use it if you want the value for another user
38  *
39  * @throws Dcp\ApplicationParameterManager\Exception
40  *
41  * @return string|null the value of a user parameter (USER="Y") or if not exist
42  */
43  public static function getUserParameterValue($application, $parameterName, $userId = null)
44  {
45  try {
46  if ($userId === null) {
47  $userId = getCurrentUser()->id;
48  }
49  $applicationId = self::getApplicationId($application, $parameterName);
50  if (isset(self::$cache[$applicationId . ' ' . $parameterName . ' ' . $userId])) {
51  return self::$cache[$applicationId . ' ' . $parameterName . ' ' . $userId];
52  }
53  $sql = sprintf("select val from paramv where appid=%d and type='U%d' and name='%s';", $applicationId, $userId, pg_escape_string($parameterName));
54  $return = null;
55  simpleQuery("", $sql, $return, true, true, true);
56  if ($return !== false) {
57  self::$cache[$applicationId . ' ' . $parameterName . ' ' . $userId] = $return;
58  return $return;
59  } else {
60  return null;
61  }
62  }
63  catch(Dcp\ApplicationParameterManager\Exception $exception) {
64  return null;
65  }
66  }
67  /**
68  * Return the default value of a user parameter
69  *
70  * @param string|int|Application $application logical name or id or object of the application, you can use
71  * { @link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
72  * @param string $parameterName logical name of the parameter
73  *
74  * @return string the value of a common parameter (USER="Y")
75  */
76  public static function getUserParameterDefaultValue($application, $parameterName)
77  {
78  return self::getCommonParameterValue($application, $parameterName);
79  }
80  /**
81  * Return the value of a non-user (common) parameter
82  *
83  *
84  * @param string|int|Application $application logical name or id or object of the application, you can use
85  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
86  * @param string $parameterName logical name of the parameter
87  *
88  * @return string the value of a common parameter (USER="N")
89  */
90  public static function getCommonParameterValue($application, $parameterName)
91  {
92  try {
93  $applicationId = self::getApplicationId($application, $parameterName);
94  if (isset(self::$cache[$applicationId . ' ' . $parameterName])) {
95  return self::$cache[$applicationId . ' ' . $parameterName];
96  }
97  $sql = sprintf("select val from paramv where appid=%d and type !~ '^U' and name='%s';", $applicationId, $parameterName, pg_escape_string($parameterName));
98  $return = null;
99  simpleQuery("", $sql, $return, true, true, true);
100  if ($return !== false) {
101  self::$cache[$applicationId . ' ' . $parameterName] = $return;
102  return $return;
103  } else {
104  return null;
105  }
106  }
107  catch(Dcp\ApplicationParameterManager\Exception $exception) {
108  return null;
109  }
110  }
111  /**
112  * Set the user parameter value
113  *
114  *
115  * @param string|int|Application $application logical name or id or object of the application, you can use
116  * { @link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
117  * @param string $parameterName logical name of the parameter
118  * @param string $value value of the parameter
119  * @param null|int|string $userId user login or account id, use it if you want to set the value for another user
120  *
121  * @param bool $check
122  * @throws Dcp\ApplicationParameterManager\Exception
123  *
124  * @return void
125  */
126  public static function setUserParameterValue($application, $parameterName, $value, $userId = null, $check = true)
127  {
128  $applicationId = self::getApplicationId($application, $parameterName);
129 
130  $action = self::getAction();
131  if ($action) {
132  $parameter = $action->parent->param;
133  } else {
134  $parameter = new Param(getDbAccess());
135  }
136 
137  if ($userId === null) {
138  $userId = getCurrentUser()->id;
139  }
140 
141  if ($check) {
142  /* if parameter exists and is user type */
143  $type = self::getParameter($applicationId, $parameterName);
144 
145  if (empty($type) || $type["isuser"] === "N") {
146  throw new \Dcp\ApplicationParameterManager\Exception("APM0006", $applicationId, $parameterName);
147  }
148  }
149  /* Test if user really exist*/
150  simpleQuery('', sprintf("select true from users where id=%d and accounttype='U'", $userId) , $uid, true, true, true);
151 
152  if ($uid === false) {
153  throw new \Dcp\ApplicationParameterManager\Exception("APM0007", $applicationId, $parameterName, $userId);
154  }
155 
156  $err = $parameter->set($parameterName, $value, Param::PARAM_USER . $userId, $applicationId);
157  if ($err) {
158  throw new \Dcp\ApplicationParameterManager\Exception("APM0006", $applicationId, $parameterName, $err);
159  }
160  self::$cache[$applicationId . ' ' . $parameterName . ' ' . $userId] = $value;
161  }
162  /**
163  * Set the user parameter default value
164  *
165  * @param string|int|Application $application logical name or id or object of the application, you can use
166  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
167  * @param string $parameterName logical name of the parameter
168  * @param string $value value of the parameter
169  *
170  * @throws Dcp\ApplicationParameterManager\Exception
171  *
172  * @return void
173  */
174  public static function setUserParameterDefaultValue($application, $parameterName, $value)
175  {
176  self::setCommonParameterValue($application, $parameterName, $value);
177  }
178  /**
179  * Set the common parameter default value
180  *
181  * @param string|int|Application $application logical name or id or object of the application, you can use
182  * { @link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
183  * @param string $parameterName logical name of the parameter
184  * @param string $value value of the parameter
185  *
186  * @throws Dcp\ApplicationParameterManager\Exception
187  *
188  * @return void
189  */
190  public static function setCommonParameterValue($application, $parameterName, $value)
191  {
192  $applicationId = self::getApplicationId($application, $parameterName);
193 
194  $isGlobal = false;
195  $sql = sprintf("select type from paramv where (name='%s' and appid = %d);", pg_escape_string($parameterName) , $applicationId);
196  simpleQuery('', $sql, $isGlobal, true, true, true);
197 
198  if ($isGlobal === false) {
199 
200  $sql = sprintf("select isglob from paramdef where (name='%s' and appid = %d);", pg_escape_string($parameterName) , $applicationId);
201  simpleQuery('', $sql, $isGlobal, true, true, true);
202  if ($isGlobal === false) {
203  throw new \Dcp\ApplicationParameterManager\Exception("APM0011", $parameterName);
204  }
205  if ($isGlobal == 'Y') {
206  $isGlobal = 'G';
207  }
208  }
209 
210  $action = self::getAction();
211  if ($action) {
212  $parameter = $action->parent->param;
213  } else {
214  $parameter = new Param(getDbAccess());
215  }
216 
217  $type = ($isGlobal === "G") ? Param::PARAM_GLB : Param::PARAM_APP;
218 
219  $err = $parameter->set($parameterName, $value, $type, $applicationId);
220 
221  if ($err) {
222  throw new \Dcp\ApplicationParameterManager\Exception("APM0009", $parameterName, $applicationId, $err);
223  }
224  self::$cache[$applicationId . ' ' . $parameterName] = $value;
225  }
226  /**
227  * Get a parameter value in the database
228  *
229  * @api Get parameter in the database
230  * @param string|int|Application $application logical name or id or object of the application, you can use
231  * { @link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
232  * @param string $parameterName logical name of the parameter
233  *
234  * @return string value of the parameter
235  */
236  public static function getParameterValue($application, $parameterName)
237  {
238  if (($value = self::_catchDeprecatedGlobalParameter($parameterName)) !== null) {
239  return $value;
240  }
241  try {
242  $applicationId = self::getApplicationId($application, $parameterName);
243  $type = self::getParameter($applicationId, $parameterName);
244  $return = null;
245 
246  if ($type["isuser"] === "Y") {
247  $return = self::getUserParameterValue($applicationId, $parameterName);
248  if ($return === null) {
249  $return = self::getUserParameterDefaultValue($applicationId, $parameterName);
250  }
251  } else {
252  $return = self::getCommonParameterValue($applicationId, $parameterName);
253  }
254 
255  return $return;
256  }
257  catch(Dcp\ApplicationParameterManager\Exception $exception) {
258  return null;
259  }
260  }
261  /**
262  * Set a parameter value
263  *
264  * @api Set a parameter value
265  * @param string|int|Application $application logical name or id or object of the application, you can use
266  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
267  * @param string $parameterName logical name of the parameter
268  * @param string $value value of the parameter
269  *
270  * @throws Dcp\ApplicationParameterManager\Exception
271  *
272  * @return void|string error string or void
273  */
274  public static function setParameterValue($application, $parameterName, $value)
275  {
276 
277  $applicationId = self::getApplicationId($application, $parameterName);
278 
279  $type = self::getParameter($applicationId, $parameterName);
280 
281  $return = null;
282 
283  if ($type["isuser"] === "Y") {
284  self::setUserParameterValue($applicationId, $parameterName, $value, null, false);
285  } else {
286  self::setCommonParameterValue($applicationId, $parameterName, $value);
287  }
288  }
289  /**
290  * Get a parameter value in the scope (use cache, session cache, volatile param)
291  *
292  * @api Get the value of the parameter in the scope of the current action
293  *
294  * @param $parameter
295  *
296  * @return string value of the parameter
297  */
298  public static function getScopedParameterValue($parameter)
299  {
300  return getParam($parameter);
301  }
302  /**
303  * Get a parameter object (object that describe the parameter)
304  *
305  * @param string|int|Application $application logical name or id or object of the application, you can use
306  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
307  * @param string $parameterName logical name of the parameter
308  *
309  * @throws Dcp\ApplicationParameterManager\Exception
310  *
311  * @return object the object parameter
312  */
313  public static function getParameter($application, $parameterName)
314  {
315  $applicationId = self::getApplicationId($application, $parameterName);
316 
317  $result = array();
318  $sql = sprintf("SELECT
319  paramdef.*
320  FROM
321  application AS app LEFT OUTER JOIN application AS parent ON (app.childof = parent.name),
322  paramdef
323  WHERE
324  (paramdef.appid = app.id OR paramdef.appid = parent.id or paramdef.appid = 1)
325  and paramdef.name = '%s'
326  and app.id = %d;", pg_escape_string($parameterName) , $applicationId);
327  simpleQuery('', $sql, $result, false, true, true);
328  if (empty($result)) {
329  throw new \Dcp\ApplicationParameterManager\Exception("APM0008", $parameterName, $applicationId);
330  }
331  return $result;
332  }
333  /**
334  * Get the parameters objects of an application
335  *
336  * @param string|int|Application $application logical name or id or object of the application, you can use
337  * {@link ApplicationParameterManager::CURRENT_APPLICATION} or {@link ApplicationParameterManager::GLOBAL_PARAMETER}
338  *
339  * @throws Dcp\ApplicationParameterManager\Exception
340  *
341  * @return object the object parameter
342  */
343  public static function getParameters($application)
344  {
345  $applicationId = self::getApplicationId($application);
346 
347  $result = array();
348  $sql = sprintf("SELECT
349  paramdef.*,
350  app.name as applicationName
351  FROM
352  application AS app LEFT OUTER JOIN application AS parent ON (app.childof = parent.name),
353  paramdef
354  WHERE
355  (paramdef.appid = app.id OR paramdef.appid = parent.id or paramdef.appid = 1)
356  and app.id = %d;", $applicationId);
357  simpleQuery('', $sql, $result, false, false, true);
358  if (empty($result)) {
359  throw new \Dcp\ApplicationParameterManager\Exception("APM0003", $applicationId);
360  }
361  return $result;
362  }
363  /**
364  * Get the application name
365  *
366  * @param string|int|Application $application Application
367  * @param string $parameter used only in global detection
368  *
369  * @throws Dcp\ApplicationParameterManager\Exception
370  * @return null|string|array null if not find, string if only id, array if id and name
371  */
372  private static function getApplicationId($application, $parameter = "")
373  {
374  $applicationName = "";
375  $applicationId = "";
376 
377  if (empty($parameter) && $application === ApplicationParameterManager::GLOBAL_PARAMETER) {
378  throw new \Dcp\ApplicationParameterManager\Exception("APM0010");
379  } elseif ($application === ApplicationParameterManager::GLOBAL_PARAMETER) {
380  $applicationId = self::getGlobalParameterApplicationName($parameter);
381  if ($applicationId === false) {
382  throw new \Dcp\ApplicationParameterManager\Exception("APM0002", $parameter);
383  }
384  } elseif ($application === ApplicationParameterManager::CURRENT_APPLICATION) {
385  global $action;
386  if ($action instanceof Action) {
387  $applicationName = $action->parent->name;
388  $applicationId = $action->parent->id;
389  } else {
390  throw new \Dcp\ApplicationParameterManager\Exception("APM0004");
391  }
392  } elseif ($application instanceof Application) {
393  $applicationName = $application->name;
394  } elseif ((!is_int($application) ? (ctype_digit($application)) : true)) {
395  $applicationId = $application;
396  } elseif (is_string($application)) {
397  $applicationName = $application;
398  } else {
399  throw new \Dcp\ApplicationParameterManager\Exception("APM0004");
400  }
401 
402  if ($applicationName && empty($applicationId)) {
403  $applicationId = self::convertApplicationNameToId($applicationName);
404  if ($applicationId === false) {
405  throw new \Dcp\ApplicationParameterManager\Exception("APM0003", $application);
406  }
407  }
408 
409  return $applicationId;
410  }
411  /**
412  * Get global parameter application
413  *
414  * @param string $parameterName global parameter name
415  *
416  * @return null|string null if not find, string otherwise
417  */
418  private static function getGlobalParameterApplicationName($parameterName)
419  {
420  if (($value = self::_catchDeprecatedGlobalParameter($parameterName)) !== null) {
421  return $value;
422  }
423  $sql = sprintf("select paramv.appid from paramv, application where paramv.type='G' and application.id=paramv.appid and paramv.name='%s';", pg_escape_string($parameterName));
424  $result = null;
425  simpleQuery("", $sql, $result, true, true, true);
426  return $result;
427  }
428  /**
429  *
430  * Convert application logical name to application id
431  *
432  * @param string $applicationName application name
433  * @return null|int
434  */
435  private static function convertApplicationNameToId($applicationName)
436  {
437  $sql = sprintf("select id from application where name = '%s';", pg_escape_string($applicationName));
438  $applicationId = null;
439  simpleQuery("", $sql, $applicationId, true, true, true);
440  return $applicationId;
441  }
442  /**
443  * Return global action
444  *
445  * @return Action|null
446  */
447  private static function getAction()
448  {
449  global $action;
450  return $action;
451  }
452  /**
453  * Internal function to catch requests for deprecated parameters
454  *
455  * @param $parameterName
456  * @return null|string return null value if the parameter is not deprecated and the caller should follow on
457  * querying the value on its own, or a non-null value containing the value the caller should use instead of
458  * querying the value on its own.
459  */
460  public static function _catchDeprecatedGlobalParameter($parameterName)
461  {
462  $retVal = null;
463  $msg = '';
464  switch ($parameterName) {
465  case 'CORE_DB':
466  case 'FREEDOM_DB':
467  case 'WEBDAV_DB':
468  $msg = sprintf("Application parameter '%s' is deprecated: use \"getDbAccess()\", \"\$action->dbaccess\", \"\$application->dbaccess\", or \"\$doc->dbaccess\" instead.", $parameterName);
469  $retVal = getDbAccess();
470  break;
471 
472  case 'CORE_PUBDIR':
473  $msg = sprintf("Application parameter '%s' is deprecated: use DEFAULT_PUBDIR constant instead.", $parameterName);
474  $retVal = DEFAULT_PUBDIR;
475  break;
476  }
477  if ($msg !== '') {
478  $action = self::getAction();
479  if (isset($action->log)) {
480  $action->log->deprecated($msg);
481  } else {
482  error_log(__METHOD__ . " " . $msg);
483  }
484  }
485  return $retVal;
486  }
487 }
global $action
static setUserParameterDefaultValue($application, $parameterName, $value)
static getParameterValue($application, $parameterName)
static setParameterValue($application, $parameterName, $value)
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
static setCommonParameterValue($application, $parameterName, $value)
const PARAM_USER
Definition: Class.Param.php:34
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
static getUserParameterDefaultValue($application, $parameterName)
const PARAM_APP
Definition: Class.Param.php:32
getCurrentUser()
Definition: Lib.Common.php:250
getDbAccess()
Definition: Lib.Common.php:368
const PARAM_GLB
Definition: Class.Param.php:33
static setUserParameterValue($application, $parameterName, $value, $userId=null, $check=true)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
static getCommonParameterValue($application, $parameterName)
if($file) if($subject==""&&$file) if($subject=="") $err
$value
static getParameter($application, $parameterName)
static getUserParameterValue($application, $parameterName, $userId=null)
← centre documentaire © anakeen