1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Variable realm controller
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Realm Controller Interface.
|
9
|
*/
|
10
|
interface VariableRealmControllerInterface {
|
11
|
/**
|
12
|
* Class constructor.
|
13
|
*
|
14
|
* @param $realm_name
|
15
|
* Realm name.
|
16
|
* @param $store_class
|
17
|
* Realm key.
|
18
|
*/
|
19
|
public function __construct($realm_name);
|
20
|
/**
|
21
|
* Check whether the realm is enabled (has a key set).
|
22
|
*/
|
23
|
public function isEnabled();
|
24
|
/**
|
25
|
* Check whether the realm is active (has a valid key set).
|
26
|
*/
|
27
|
public function isActive();
|
28
|
/**
|
29
|
* Start this realm with request key.
|
30
|
*
|
31
|
* This will only change the realm key if the realm is not initialized yet.
|
32
|
*/
|
33
|
public function enable($realm_key = NULL);
|
34
|
/**
|
35
|
* Get title for this realm.
|
36
|
*/
|
37
|
public function getTitle();
|
38
|
/**
|
39
|
* Get name for variables in this realm.
|
40
|
*/
|
41
|
public function getVariableName();
|
42
|
/**
|
43
|
* Add store for realm key.
|
44
|
*
|
45
|
* @param $variables
|
46
|
* Optional array with variable values to initialize the realm key store.
|
47
|
*/
|
48
|
public function addStore($realm_key, $variables = NULL);
|
49
|
/**
|
50
|
* Get store for realm key.
|
51
|
*/
|
52
|
public function getStore($realm_key);
|
53
|
/**
|
54
|
* Set store for realm key.
|
55
|
*/
|
56
|
public function setStore($realm_key, $realm_store);
|
57
|
/**
|
58
|
* Get current realm key.
|
59
|
*/
|
60
|
public function getKey();
|
61
|
/**
|
62
|
* Get default key for this realm.
|
63
|
*/
|
64
|
public function getDefaultKey();
|
65
|
/**
|
66
|
* Get key for this page request.
|
67
|
*/
|
68
|
public function getRequestKey();
|
69
|
/**
|
70
|
* Set current realm key.
|
71
|
*/
|
72
|
public function setKey($realm_key);
|
73
|
/**
|
74
|
* Get all keys for this realm.
|
75
|
*/
|
76
|
public function getAllKeys();
|
77
|
/**
|
78
|
* Get other realms this one may depend upon.
|
79
|
*/
|
80
|
public function getParentRealms();
|
81
|
/**
|
82
|
* Get current realm weight.
|
83
|
*/
|
84
|
public function getWeight();
|
85
|
/**
|
86
|
* Set current realm weight.
|
87
|
*/
|
88
|
public function setWeight($weight);
|
89
|
/**
|
90
|
* Implementation of VariableRealmControllerInterface::getWeight().
|
91
|
*/
|
92
|
public function getDefaultWeight();
|
93
|
/**
|
94
|
* Get current variable store.
|
95
|
*/
|
96
|
public function getCurrentStore();
|
97
|
/**
|
98
|
* List all current variable values.
|
99
|
*/
|
100
|
public function getCurrentVariables();
|
101
|
/**
|
102
|
* Get information for this realm.
|
103
|
*
|
104
|
* Return information provided by hook_variable_realm_info()
|
105
|
*/
|
106
|
public function getInfo($property = NULL, $default = NULL);
|
107
|
/**
|
108
|
* Get list of variables available for this realm.
|
109
|
*/
|
110
|
public function getAvailableVariables();
|
111
|
/**
|
112
|
* Get list of variables enabled for this realm.
|
113
|
*/
|
114
|
public function getEnabledVariables();
|
115
|
/**
|
116
|
* Get system variable for this realm.
|
117
|
*/
|
118
|
public function getRealmVariable($name, $default = NULL);
|
119
|
/**
|
120
|
* Set system variable for this realm.
|
121
|
*/
|
122
|
public function setRealmVariable($name, $value);
|
123
|
/**
|
124
|
* Delete variable for all keys this realm.
|
125
|
*
|
126
|
* @param $variable_name
|
127
|
* Variable name to delete.
|
128
|
*/
|
129
|
public function deleteVariable($variable_name);
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Variable Realm Hooks.
|
134
|
*/
|
135
|
interface VariableRealmHooks {
|
136
|
/**
|
137
|
* Callback function that will be invoked when a realm is enabled.
|
138
|
*/
|
139
|
public function variableRealmEnable($realm_name, $realm_key);
|
140
|
/**
|
141
|
* Callback method that will be invoked when a realm is switched.
|
142
|
*/
|
143
|
public function variableRealmSwitch($realm_name, $realm_key);
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Realm Store Interface.
|
148
|
*/
|
149
|
interface VariableRealmStoreInterface {
|
150
|
/**
|
151
|
* Class constructor.
|
152
|
*
|
153
|
* @param $realm
|
154
|
* Realm name.
|
155
|
* @param $key
|
156
|
* Realm key.
|
157
|
* @param $variables
|
158
|
* Optional array of variables to initialize the realm with.
|
159
|
*/
|
160
|
public function __construct($realm, $key, $variables = NULL);
|
161
|
/**
|
162
|
* Initialize variables.
|
163
|
*/
|
164
|
public function variable_init();
|
165
|
/**
|
166
|
* Get single variable.
|
167
|
*
|
168
|
* @param $name
|
169
|
* Variable name
|
170
|
* @param $default
|
171
|
* Default value
|
172
|
*/
|
173
|
public function variable_get($name, $default = NULL);
|
174
|
/**
|
175
|
* Set single variable.
|
176
|
*
|
177
|
* @param $name
|
178
|
* Variable name
|
179
|
* @param $value
|
180
|
* Variable value
|
181
|
*/
|
182
|
public function variable_set($name, $value);
|
183
|
/**
|
184
|
* Delete single variable.
|
185
|
*
|
186
|
* @param $name
|
187
|
* Variable name
|
188
|
*/
|
189
|
public function variable_del($name);
|
190
|
/**
|
191
|
* Add single variable to the realm for current request.
|
192
|
*
|
193
|
* While variable set actually sets the variable on whatever realm storage
|
194
|
* we are using, this function just sets a runtime value.
|
195
|
*
|
196
|
* @param $name
|
197
|
* Variable name
|
198
|
* @param $value
|
199
|
* Variable value
|
200
|
*/
|
201
|
public function variable_add($name, $value);
|
202
|
/**
|
203
|
* Check whether a variable exists in the realm.
|
204
|
*/
|
205
|
public function variable_exists($name);
|
206
|
/**
|
207
|
* List all current variable values.
|
208
|
*/
|
209
|
public function variable_list();
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Base class, keeps static list of variables.
|
214
|
*/
|
215
|
class VariableRealmDefaultController implements VariableRealmControllerInterface {
|
216
|
// Unique realm name (language, domain..)
|
217
|
public $realm_name;
|
218
|
// Current realm key.
|
219
|
public $current_key;
|
220
|
// Current realm weight.
|
221
|
public $current_weight;
|
222
|
// Array of variable stores indexed by realm key.
|
223
|
protected $store;
|
224
|
/**
|
225
|
* Implementation of VariableRealmControllerInterface::__construct().
|
226
|
*/
|
227
|
public function __construct($realm_name) {
|
228
|
$this->realm_name = $realm_name;
|
229
|
$this->current_weight = $this->getDefaultWeight();
|
230
|
}
|
231
|
/**
|
232
|
* Implementation of VariableRealmControllerInterface::isEnabled()
|
233
|
*/
|
234
|
public function isEnabled() {
|
235
|
return isset($this->current_key);
|
236
|
}
|
237
|
/**
|
238
|
* Implementation of VariableRealmControllerInterface::isActive()
|
239
|
*/
|
240
|
public function isActive() {
|
241
|
return $this->isEnabled() && $this->current_key !== FALSE;
|
242
|
}
|
243
|
/**
|
244
|
* Implementation of VariableRealmControllerInterface::enable().
|
245
|
*/
|
246
|
public function enable($realm_key = NULL) {
|
247
|
if (!isset($this->current_key)) {
|
248
|
return $this->current_key = isset($realm_key) ? $realm_key : $this->getRequestKey();
|
249
|
}
|
250
|
}
|
251
|
/**
|
252
|
* Implementation of VariableRealmControllerInterface::getTitle().
|
253
|
*/
|
254
|
public function getTitle() {
|
255
|
return $this->getInfo('title');
|
256
|
}
|
257
|
/**
|
258
|
* Implementation of VariableRealmControllerInterface::getVariableName().
|
259
|
*/
|
260
|
public function getVariableName() {
|
261
|
return $this->getInfo('variable name');
|
262
|
}
|
263
|
/**
|
264
|
* Implementation of VariableRealmControllerInterface::getStore().
|
265
|
*/
|
266
|
public function getStore($realm_key) {
|
267
|
if (isset($this->store[$realm_key])) {
|
268
|
return $this->store[$realm_key];
|
269
|
}
|
270
|
else {
|
271
|
return $this->addStore($realm_key);
|
272
|
}
|
273
|
}
|
274
|
/**
|
275
|
* Implementation of VariableRealmControllerInterface::addStore().
|
276
|
*/
|
277
|
public function addStore($realm_key, $variables = NULL) {
|
278
|
$store = $this->createStore($realm_key, $variables);
|
279
|
$this->setStore($realm_key, $store);
|
280
|
return $store;
|
281
|
}
|
282
|
/**
|
283
|
* Create Store for key.
|
284
|
*/
|
285
|
protected function createStore($realm_key, $variables = NULL) {
|
286
|
$class = $this->getInfo('store class');
|
287
|
$class = $class && class_exists($class) ? $class : 'VariableRealmDefaultStore';
|
288
|
return new $class($this->realm_name, $realm_key, $variables);
|
289
|
}
|
290
|
/**
|
291
|
* Set store for realm key.
|
292
|
*/
|
293
|
public function setStore($realm_key, $realm_store) {
|
294
|
$this->store[$realm_key] = $realm_store;
|
295
|
}
|
296
|
/**
|
297
|
* Implementation of VariableRealmControllerInterface::setKey().
|
298
|
*/
|
299
|
public function setKey($realm_key) {
|
300
|
$this->current_key = $realm_key;
|
301
|
}
|
302
|
/**
|
303
|
* Implementation of VariableRealmControllerInterface::getKey().
|
304
|
*/
|
305
|
public function getKey() {
|
306
|
return isset($this->current_key) ? $this->current_key : FALSE;
|
307
|
}
|
308
|
/**
|
309
|
* Implementation of VariableRealmControllerInterface::getAllKeys().
|
310
|
*/
|
311
|
public function getAllKeys() {
|
312
|
return $this->getInfo('keys', array());
|
313
|
}
|
314
|
/**
|
315
|
* Implementation of VariableRealmControllerInterface::getDefaultKey().
|
316
|
*/
|
317
|
public function getDefaultKey() {
|
318
|
return $this->getInfo('default key', FALSE);
|
319
|
}
|
320
|
/**
|
321
|
* Implementation of VariableRealmControllerInterface::getRequestKey().
|
322
|
*/
|
323
|
public function getRequestKey() {
|
324
|
return FALSE;
|
325
|
}
|
326
|
/**
|
327
|
* Implementation of VariableRealmControllerInterface::getWeight().
|
328
|
*/
|
329
|
public function getWeight() {
|
330
|
return isset($this->current_weight) ? $this->current_weight : $this->controller_data['weight'];
|
331
|
}
|
332
|
/**
|
333
|
* Implementation of VariableRealmControllerInterface::setWeight().
|
334
|
*/
|
335
|
public function setWeight($weight) {
|
336
|
$this->current_weight = $weight;
|
337
|
}
|
338
|
/**
|
339
|
* Implementation of VariableRealmControllerInterface::getWeight().
|
340
|
*/
|
341
|
public function getDefaultWeight() {
|
342
|
return $this->getRealmVariable('weight', $this->getInfo('weight', 0));
|
343
|
}
|
344
|
/**
|
345
|
* Implementation of VariableRealmControllerInterface::getParentRealms().
|
346
|
*/
|
347
|
public function getParentRealms() {
|
348
|
return array();
|
349
|
}
|
350
|
/**
|
351
|
* Implementation of VariableRealmControllerInterface::getCurrentVariables().
|
352
|
*/
|
353
|
public function getCurrentVariables() {
|
354
|
if ($store = $this->getCurrentStore()) {
|
355
|
return $store->variable_list();
|
356
|
}
|
357
|
else {
|
358
|
return array();
|
359
|
}
|
360
|
}
|
361
|
/**
|
362
|
* Implementation of VariableRealmControllerInterface::getCurrentStore().
|
363
|
*/
|
364
|
public function getCurrentStore() {
|
365
|
if ($this->isActive()) {
|
366
|
return $this->getStore($this->getKey());
|
367
|
}
|
368
|
else {
|
369
|
return NULL;
|
370
|
}
|
371
|
}
|
372
|
/**
|
373
|
* Implementation of VariableRealmControllerInterface::getAvailableVariables().
|
374
|
*/
|
375
|
public function getAvailableVariables() {
|
376
|
if ($options = $this->getInfo('options')) {
|
377
|
return $options;
|
378
|
}
|
379
|
else {
|
380
|
// Defaults to all available variables.
|
381
|
return array_keys(variable_get_info());
|
382
|
}
|
383
|
}
|
384
|
/**
|
385
|
* Implementation of VariableRealmControllerInterface::getEnabledVariables().
|
386
|
*/
|
387
|
public function getEnabledVariables() {
|
388
|
if ($this->getInfo('select')) {
|
389
|
return $this->getRealmVariable('list', array());
|
390
|
}
|
391
|
else {
|
392
|
// If the variable is not set it will default to all variables
|
393
|
return $this->getAvailableVariables();
|
394
|
}
|
395
|
}
|
396
|
/**
|
397
|
* Implementation of VariableRealmControllerInterface::getInfo().
|
398
|
*/
|
399
|
public function getInfo($property = NULL, $default = NULL) {
|
400
|
$info = variable_realm_info($this->realm_name);
|
401
|
if ($property) {
|
402
|
return isset($info[$property]) ? $info[$property] : $default;
|
403
|
}
|
404
|
else {
|
405
|
return $info;
|
406
|
}
|
407
|
}
|
408
|
/**
|
409
|
* Implementation of VariableRealmControllerInterface::getRealmVariable().
|
410
|
*/
|
411
|
public function getRealmVariable($name, $default = NULL) {
|
412
|
return variable_get('variable_realm_' . $name . '_' . $this->realm_name, $default);
|
413
|
}
|
414
|
/**
|
415
|
* Implementation of VariableRealmControllerInterface::setRealmVariable().
|
416
|
*/
|
417
|
public function setRealmVariable($name, $value) {
|
418
|
variable_realm_global_set('variable_realm_' . $name . '_' . $this->realm_name, $value);
|
419
|
}
|
420
|
/**
|
421
|
* Implementation of VariableRealmControllerInterface::deleteVariable().
|
422
|
*/
|
423
|
public function deleteVariable($variable_name) {
|
424
|
foreach ($this->getAllKeys() as $key => $name) {
|
425
|
variable_realm_del($this->realm_name, $key, $variable_name, FALSE);
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* Base class, keeps static list of variables.
|
432
|
*/
|
433
|
class VariableRealmDefaultStore implements VariableRealmStoreInterface {
|
434
|
public $realm;
|
435
|
public $key;
|
436
|
protected $variables;
|
437
|
/**
|
438
|
* Class constructor.
|
439
|
*/
|
440
|
public function __construct($realm, $key, $variables = NULL) {
|
441
|
$this->realm = $realm;
|
442
|
$this->key = $key;
|
443
|
$this->variables = $variables;
|
444
|
}
|
445
|
/**
|
446
|
* Initialize variables.
|
447
|
*/
|
448
|
public function variable_init() {
|
449
|
if (!isset($this->variables)) {
|
450
|
$this->variables = array();
|
451
|
}
|
452
|
}
|
453
|
/**
|
454
|
* Get single variable.
|
455
|
*
|
456
|
* @param $name
|
457
|
* Variable name
|
458
|
* @param $default
|
459
|
* Default value
|
460
|
*/
|
461
|
public function variable_get($name, $default = NULL) {
|
462
|
$this->variable_init();
|
463
|
return isset($this->variables[$name]) ? $this->variables[$name] : $default;
|
464
|
}
|
465
|
/**
|
466
|
* Set single variable.
|
467
|
*
|
468
|
* @param $name
|
469
|
* Variable name
|
470
|
* @param $value
|
471
|
* Variable value
|
472
|
*/
|
473
|
public function variable_set($name, $value) {
|
474
|
$this->variable_init();
|
475
|
$this->variables[$name] = $value;
|
476
|
}
|
477
|
/**
|
478
|
* Delete single variable.
|
479
|
*
|
480
|
* @param $name
|
481
|
* Variable name
|
482
|
*/
|
483
|
public function variable_del($name) {
|
484
|
$this->variable_init();
|
485
|
unset($this->variables[$name]);
|
486
|
}
|
487
|
/**
|
488
|
* Implementation of VariableRealmStoreInterface::variable_add().
|
489
|
*/
|
490
|
public function variable_add($name, $value) {
|
491
|
$this->variable_init();
|
492
|
$this->variables[$name] = $value;
|
493
|
}
|
494
|
/**
|
495
|
* Implementation of VariableRealmStoreInterface::variable_exists().
|
496
|
*/
|
497
|
public function variable_exists($name) {
|
498
|
$this->variable_init();
|
499
|
return isset($this->variables[$name]);
|
500
|
}
|
501
|
/**
|
502
|
* List all current variable values.
|
503
|
*/
|
504
|
public function variable_list() {
|
505
|
$this->variable_init();
|
506
|
return $this->variables;
|
507
|
}
|
508
|
}
|
509
|
|
510
|
/**
|
511
|
* Controller for default system variables.
|
512
|
*/
|
513
|
class VariableRealmGlobalStore extends VariableRealmDefaultStore {
|
514
|
/**
|
515
|
* Initialize variables.
|
516
|
*/
|
517
|
public function variable_init() {
|
518
|
if (!isset($this->variables)) {
|
519
|
$this->variables = $GLOBALS['conf'];
|
520
|
}
|
521
|
}
|
522
|
/**
|
523
|
* Set single variable.
|
524
|
*
|
525
|
* @param $name
|
526
|
* Variable name
|
527
|
* @param $value
|
528
|
* Variable value
|
529
|
*/
|
530
|
public function variable_set($name, $value) {
|
531
|
parent::variable_set($name, $value);
|
532
|
variable_set($name, $value);
|
533
|
}
|
534
|
/**
|
535
|
* Delete single variable.
|
536
|
*
|
537
|
* @param $name
|
538
|
* Variable name
|
539
|
*/
|
540
|
public function variable_del($name) {
|
541
|
parent::variable_del($name);
|
542
|
variable_del($name);
|
543
|
}
|
544
|
}
|