CMS  Version 3.9
Settings Class Reference

The Settings class provides components with a common API for specifying application settings and configuration values. More...

Inherits DataItem.

Public Member Functions

 save ()
 
 permitsRole ($role)
 
 formatOptionList ()
 

Static Public Member Functions

static getSetting ($component, $name)
 
static getValue ($component, $name)
 Retrieve the value of the specified Setting. More...
 
static setValue ($component, $name, $value, $field_type, $annotation="", $category="", $options="", $weight=0)
 Sets the value of the given component setting. More...
 
static setDefaultValue ($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
 Sets the default value of the given component setting. More...
 
static deleteValue ($component, $name)
 Delete the specified value from the settings table. More...
 
static setPermission ($component, $name, $annotation, $value, $weight)
 Set a Permission value indicating which SiteRoles have the given permission. More...
 
static setDefaultPermission ($component, $name, $annotation, $value, $weight=0)
 Set a default Permission value indicating which SiteRoles have the given permission. More...
 
static checkPermission ($component, $name, $account=null)
 Check whether a user has a specific permission. More...
 
static assertPermission ($component, $name, $redirect="", $message="")
 Asserts whether the current user has a specific permission, optionally redirecting to a specified location if they do not. More...
 
static checkPermissions ($permissions, $account=null)
 Check all the permissions specified in the given string. More...
 
static deletePermission ($component, $name)
 Deletes the specified permissions. More...
 
static getComponents ()
 
static createPermissionsFieldRenderer ($form, $field, $label="")
 

Public Attributes

 $primary_key = "settings_id"
 
 $table = "settings"
 
 $fields
 
 $relations = array()
 

Static Public Attributes

static $fieldTypes
 

Detailed Description

The Settings class provides components with a common API for specifying application settings and configuration values.

These settings are stored in the application database and can be modified through the site administration screens.

Author
andy

Definition at line 48 of file settings.inc.

Member Function Documentation

◆ assertPermission()

static Settings::assertPermission (   $component,
  $name,
  $redirect = "",
  $message = "" 
)
static

Asserts whether the current user has a specific permission, optionally redirecting to a specified location if they do not.

Parameters
string$componentthe component that owns the permission
string$namethe unique name of the permission
string$redirectURL to redirect to on failure

Definition at line 255 of file settings.inc.

256  {
258 
260  }
$redirect
$component
Definition: settings.inc:53
$name
Definition: upload.inc:54
static assertRole($role, $redirect="", $message="")
Assert that the user has one of the specified roles.
Definition: core.inc:297
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
$message
Definition: mail_to.inc:49

◆ checkPermission()

static Settings::checkPermission (   $component,
  $name,
  $account = null 
)
static

Check whether a user has a specific permission.

Parameters
string$componentthe component that owns the permission
string$namethe unique name of the permission
object$account(optional) user account to test. If not specified the currently logged in user is tested.

Definition at line 241 of file settings.inc.

242  {
244 
245  return checkRole($roles, $account);
246  }

◆ checkPermissions()

static Settings::checkPermissions (   $permissions,
  $account = null 
)
static

Check all the permissions specified in the given string.

If the user belongs to a role with ONE of the specified permissions, return true. If the user does not, return false.

Parameters
string$permissionspermission list in the form "[component]:[permission],[component]:[permission]..."
object$account(optional) user account to test. If not specified the currently logged in user is tested.

Definition at line 270 of file settings.inc.

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  }
static checkPermission($component, $name, $account=null)
Check whether a user has a specific permission.
Definition: settings.inc:241

◆ createPermissionsFieldRenderer()

static Settings::createPermissionsFieldRenderer (   $form,
  $field,
  $label = "" 
)
static

Definition at line 343 of file settings.inc.

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  }
$form
Definition: settings.inc:55
The Settings class provides components with a common API for specifying application settings and conf...
Definition: settings.inc:49
if(!checkRole("admin")) $c
$renderer

◆ deletePermission()

static Settings::deletePermission (   $component,
  $name 
)
static

Deletes the specified permissions.

Parameters
string$componentthe name of the component to which the permission belongs
string$namethe name of the permission to be deleted

Definition at line 290 of file settings.inc.

291  {
293  ComponentManager::fireEvent('PermissionDeleted', "{$component}:{$name}");
294  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static deleteValue($component, $name)
Delete the specified value from the settings table.
Definition: settings.inc:195

◆ deleteValue()

static Settings::deleteValue (   $component,
  $name 
)
static

Delete the specified value from the settings table.

Parameters
string$componentthe name of the component that owns the setting
string$namethe name of the setting to be deleted

Definition at line 195 of file settings.inc.

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  }
$settings

◆ formatOptionList()

Settings::formatOptionList ( )

Definition at line 312 of file settings.inc.

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  }

◆ getComponents()

static Settings::getComponents ( )
static

Definition at line 338 of file settings.inc.

339  {
340  return Query::create(Component, "WHERE name in (SELECT DISTINCT component FROM settings) ORDER BY name")->execute();
341  }

◆ getSetting()

static Settings::getSetting (   $component,
  $name 
)
static

Definition at line 83 of file settings.inc.

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  }
$setting

◆ getValue()

static Settings::getValue (   $component,
  $name 
)
static

Retrieve the value of the specified Setting.

Parameters
string$componentthe name of the component that owns the setting
string$namethe name of the setting

Definition at line 104 of file settings.inc.

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  }

◆ permitsRole()

Settings::permitsRole (   $role)

Definition at line 296 of file settings.inc.

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  }
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
$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

◆ save()

Settings::save ( )

Definition at line 77 of file settings.inc.

78  {
79  Cache::invalidate("setting_{$this->component}_{$this->name}");
80  parent::save();
81  }

◆ setDefaultPermission()

static Settings::setDefaultPermission (   $component,
  $name,
  $annotation,
  $value,
  $weight = 0 
)
static

Set a default Permission value indicating which SiteRoles have the given permission.

Parameters
string$componentthe name of the component that owns the permission
string$namethe unique name of the permission
string$annotationshort description of the permission
string$valuecomma-separated list of roles that have the permission
int$weightoptional weight parameter to control display order

Definition at line 230 of file settings.inc.

231  {
232  Settings::setDefaultValue($component, $name, $value, "String", $annotation, "Permissions", "Permissions", $weight);
233  }
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

◆ setDefaultValue()

static Settings::setDefaultValue (   $component,
  $name,
  $value,
  $field_type = "String",
  $annotation = "",
  $category = "",
  $options = "",
  $weight = 0 
)
static

Sets the default value of the given component setting.

Creating settings for a component using this method will generate a tab under the Application Settings area of the site administration interface, allowing your users to control your component's behavior.

Parameters
stringcomponent the name of the component that owns the setting
string$namethe name of the setting (this will be automatically prettified when presented to the user)
mixed$valuethe default value for the setting. If the setting has already been defined, the value will not be overwritten
string$field_typethe type of field renderer to use to display the setting in the admin interface
string$annotationany help annotation to display on the form (optional)
string$categorythe sub-category within the form to which the setting belongs. Settings with the same category name are grouped together into fieldsets.
number$weighta weighting factor to control display order on the Application Settings form. Controls are sorted (within categories) first by weight, then alphabetically (optional).

Definition at line 174 of file settings.inc.

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  }
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

◆ setPermission()

static Settings::setPermission (   $component,
  $name,
  $annotation,
  $value,
  $weight 
)
static

Set a Permission value indicating which SiteRoles have the given permission.

Parameters
string$componentthe name of the component that owns the permission
string$namethe unique name of the permission
string$annotationshort description of the permission
string$valuecomma-separated list of roles that have the permission
int$weightoptional weight parameter to control display order

Definition at line 217 of file settings.inc.

218  {
219  Settings::setValue($component, $name, $value, "String", $annotation, "Permissions", "Permissions", $weight = 0);
220  }

◆ setValue()

static Settings::setValue (   $component,
  $name,
  $value,
  $field_type,
  $annotation = "",
  $category = "",
  $options = "",
  $weight = 0 
)
static

Sets the value of the given component setting.

Parameters
stringcomponent the name of the component that owns the setting
string$namethe name of the setting (this will be automatically prettified when presented to the user)
mixed$valuethe default value for the setting. The value will be overwritten, even if previously set
string$field_typethe type of field renderer to use to display the setting in the admin interface
string$annotationany help annotation to display on the form (optional)
string$categorythe sub-category within the form to which the setting belongs. Settings with the same category name are grouped together into fieldsets.
number$weighta weighting factor to control display order on the Application Settings form. Controls are sorted (within categories) first by weight, then alphabetically (optional).

Definition at line 138 of file settings.inc.

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  }

Member Data Documentation

◆ $fields

Settings::$fields
Initial value:
= array("settings_id" => Number,
"component" => String,
"category" => String,
"name" => String,
"annotation" => Text,
"options" => Text,
"value" => String,
"field_type" => String,
"weight" => Number
)

Definition at line 54 of file settings.inc.

◆ $fieldTypes

Settings::$fieldTypes
static
Initial value:
= array(
"String" => "String",
"Number" => "Number",
"Boolean" => "Boolean",
"Date" => "Date",
"Currency" => "Currency",
"Text" => "Text"
)

Definition at line 68 of file settings.inc.

◆ $primary_key

Settings::$primary_key = "settings_id"

Definition at line 50 of file settings.inc.

◆ $relations

Settings::$relations = array()

Definition at line 66 of file settings.inc.

◆ $table

Settings::$table = "settings"

Definition at line 51 of file settings.inc.


The documentation for this class was generated from the following file: