CMS  Version 3.9
settings.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFeature("cache", "field_renderers");
40 
48 class Settings extends DataItem
49 {
50  var $primary_key = "settings_id";
51  var $table = "settings";
52 
53  // Fields
54  var $fields = array("settings_id" => Number,
55  "component" => String,
56  "category" => String,
57  "name" => String,
58  "annotation" => Text,
59  "options" => Text,
60  "value" => String,
61  "field_type" => String,
62  "weight" => Number
63  );
64 
65  // relationships
66  var $relations = array();
67 
68  static $fieldTypes = array(
69  "String" => "String",
70  "Number" => "Number",
71  "Boolean" => "Boolean",
72  "Date" => "Date",
73  "Currency" => "Currency",
74  "Text" => "Text"
75  );
76 
77  function save()
78  {
79  Cache::invalidate("setting_{$this->component}_{$this->name}");
80  parent::save();
81  }
82 
83  static function getSetting($component, $name)
84  {
85  try
86  {
87  $setting = Query::create(Settings, "WHERE component=:component AND name=:name")
88  ->bind(":component", $component, ":name", $name)
89  ->executeSingle();
90  Cache::put("setting_{$component}_{$name}", $setting->value);
91  return $setting;
92  }
93  catch(DataNotFoundException $e)
94  {
95  return null;
96  }
97  }
98 
104  static function getValue($component, $name)
105  {
106  $value = Cache::get("setting_{$component}_{$name}");
107  if ($value !== null) return $value;
108 
109  try
110  {
111  $setting = Query::create(Settings, "WHERE component=:c and name=:n")
112  ->filter(new InclusionFilter("value"))
113  ->bind(":c", $component, ":n", $name)
114  ->executeSingle();
115  Cache::put("setting_{$component}_{$name}", $setting->value);
116  return $setting->value;
117  }
118  catch (DataNotFoundException $e)
119  {
120  trace("Setting not found for $component $name", 3);
121  return null;
122  }
123  }
124 
138  static function setValue($component, $name, $value, $field_type, $annotation = "", $category = "", $options = "", $weight = 0)
139  {
141  if (!$setting)
142  {
143  $setting = new Settings();
144  $setting->component = $component;
145  $setting->name = $name;
146  $setting->field_type = $field_type;
147  $setting->annotation = $annotation;
148  $setting->options = $options;
149  $setting->category = $category;
150  $setting->weight = $weight;
151  }
152 
153  $setting->value = $value;
154  $setting->save();
155  Cache::put("setting_{$component}_{$name}", $setting->value);
156  }
157 
174  static function setDefaultValue($component, $name, $value, $field_type = "String", $annotation = "", $category = "", $options = "", $weight = 0)
175  {
177  if ($setting)
178  {
179  $setting->annotation = $annotation;
180  $setting->category = $category;
181  $setting->options = $options;
182  $setting->weight = $weight;
183  $setting->save();
184  return;
185  }
186 
187  Settings::setValue($component, $name, $value, $field_type, $annotation, $category, $options, $weight);
188  }
189 
195  static function deleteValue($component, $name)
196  {
197  $settings = Query::create(Settings, "WHERE component=:c and name=:n")
198  ->bind(":c", $component, ":n", $name)
199  ->execute();
200 
201  foreach($settings as $s)
202  {
203  $s->delete();
204  }
205 
206  Cache::invalidate("setting_{$component}_{$name}");
207  }
208 
217  static function setPermission($component, $name, $annotation, $value, $weight)
218  {
219  Settings::setValue($component, $name, $value, "String", $annotation, "Permissions", "Permissions", $weight = 0);
220  }
221 
230  static function setDefaultPermission($component, $name, $annotation, $value, $weight = 0)
231  {
232  Settings::setDefaultValue($component, $name, $value, "String", $annotation, "Permissions", "Permissions", $weight);
233  }
234 
241  static function checkPermission($component, $name, $account = null)
242  {
244 
245  return checkRole($roles, $account);
246  }
247 
255  static function assertPermission($component, $name, $redirect = "", $message = "")
256  {
258 
260  }
261 
270  static function checkPermissions($permissions, $account = null)
271  {
272  if ($permissions == "") return true;
273 
274  $perms = explode(",", $permissions);
275 
276  foreach($perms as $perm)
277  {
278  list($component, $permission) = explode(":", $perm);
279  if (Settings::checkPermission($component, $permission, $account)) return true;
280  }
281 
282  return false;
283  }
284 
291  {
293  ComponentManager::fireEvent('PermissionDeleted', "{$component}:{$name}");
294  }
295 
296  function permitsRole($role)
297  {
298  if (!$role)
299  {
300  throw new FakoliException("Missing role");
301  }
302 
303  if ($this->options != 'Permissions')
304  {
305  throw new FakoliException("Role check on non-permissions settings");
306  }
307 
308  $expr = "/\\b{$role}\\b/";
309  return (preg_match($expr, $this->value) > 0) ? true : false;
310  }
311 
312  function formatOptionList()
313  {
314  if(!$this->options)
315  return null;
316 
317  $options = explode("\n", $this->options);
318 
319  $optionList = array();
320  foreach($options as $option)
321  {
322  if (strpos($option, "=>") !== false)
323  {
324  list($val, $text) = explode("=>", $option);
325  $val = trim($val);
326  $text = trim($text);
327  $optionList[$val] = $text;
328  }
329  else
330  {
331  $optionList[$option] = $option;
332  }
333  }
334 
335  return $optionList;
336  }
337 
338  static function getComponents()
339  {
340  return Query::create(Component, "WHERE name in (SELECT DISTINCT component FROM settings) ORDER BY name")->execute();
341  }
342 
343  static function createPermissionsFieldRenderer($form, $field, $label = "")
344  {
345  $permissions = Query::create(Settings, "WHERE category='Permissions' ORDER BY component, annotation")->execute();
346 
347  if (count($permissions) == 0)
348  {
349  $form->hide($field);
350  return null;
351  }
352 
353  $options = array();
354 
355  foreach($permissions as $p)
356  {
357  $c = prettify($p->component);
358 
359  if (!array_key_exists($c, $options)) $options[$c] = array();
360  $options[$c]["{$p->component}:{$p->name}"] = $p->annotation;
361  }
362 
363  $renderer = new CheckListFieldRenderer($form, $field, $label, $options, true);
364  $renderer->setSize(400, 150);
365  return $renderer;
366  }
367 }
368 
369 
378 class ComponentSettings extends DataItem
379 {
380  var $fields = array();
381 
382  var $primary_key = "settings_id";
383  var $table = "settings";
384 }
385 ?>
$redirect
$form
Definition: settings.inc:55
$component
Definition: settings.inc:53
$name
Definition: upload.inc:54
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
Dummy data model to render a component's settings as a form, using the "type" field of the setting re...
Definition: settings.inc:379
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static assertRole($role, $redirect="", $message="")
Assert that the user has one of the specified roles.
Definition: core.inc:297
The Settings class provides components with a common API for specifying application settings and conf...
Definition: settings.inc:49
static checkPermissions($permissions, $account=null)
Check all the permissions specified in the given string.
Definition: settings.inc:270
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static deletePermission($component, $name)
Deletes the specified permissions.
Definition: settings.inc:290
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
permitsRole($role)
Definition: settings.inc:296
$primary_key
Definition: settings.inc:50
static setDefaultPermission($component, $name, $annotation, $value, $weight=0)
Set a default Permission value indicating which SiteRoles have the given permission.
Definition: settings.inc:230
static getComponents()
Definition: settings.inc:338
static setPermission($component, $name, $annotation, $value, $weight)
Set a Permission value indicating which SiteRoles have the given permission.
Definition: settings.inc:217
static checkPermission($component, $name, $account=null)
Check whether a user has a specific permission.
Definition: settings.inc:241
static $fieldTypes
Definition: settings.inc:68
static createPermissionsFieldRenderer($form, $field, $label="")
Definition: settings.inc:343
static assertPermission($component, $name, $redirect="", $message="")
Asserts whether the current user has a specific permission, optionally redirecting to a specified loc...
Definition: settings.inc:255
formatOptionList()
Definition: settings.inc:312
static deleteValue($component, $name)
Delete the specified value from the settings table.
Definition: settings.inc:195
static setValue($component, $name, $value, $field_type, $annotation="", $category="", $options="", $weight=0)
Sets the value of the given component setting.
Definition: settings.inc:138
static getSetting($component, $name)
Definition: settings.inc:83
if(!checkRole("admin")) $c
$settings
$message
Definition: mail_to.inc:49
$renderer
$table column("Redirect From", "<a href='redirect_form?redirect_id={redirect_id}'>{redirect_from}</a>", true, "width: 30%") -> column("Redirect To", "<a href='{redirect_to}' target='_blank'>{redirect_to}</a>", true, "width: 30%") ->column("Last Modified", "{last_modified}", true, "width: 20%; text-align: center") ->column("Override", "{ override true
Definition: redirects.inc:9
$role
Definition: role_form.inc:41
$setting