1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Classes for Realm Union.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Default Realm Union class
|
9
|
*/
|
10
|
class VariableRealmUnionController extends VariableRealmDefaultController implements VariableRealmHooks {
|
11
|
protected $union_realms;
|
12
|
/**
|
13
|
* Implementation of VariableRealmControllerInterface::__construct().
|
14
|
*/
|
15
|
public function __construct($realm_name) {
|
16
|
parent::__construct($realm_name);
|
17
|
// Get / create realm controllers for each of the union realms.
|
18
|
foreach ($this->getParentRealms() as $realm_name) {
|
19
|
$this->union_realms[$realm_name] = variable_realm_controller($realm_name);
|
20
|
}
|
21
|
}
|
22
|
/**
|
23
|
* Implementation of VariableRealmControllerInterface::start().
|
24
|
*/
|
25
|
public function start($realm_key = NULL) {
|
26
|
if (!isset($this->current_key)) {
|
27
|
if (isset($realm_key)) {
|
28
|
return $this->current_key = $realm_key;
|
29
|
}
|
30
|
elseif ($request_key = $this->getRequestKey()) {
|
31
|
return $this->current_key = $request_key;
|
32
|
}
|
33
|
}
|
34
|
}
|
35
|
/**
|
36
|
* Implementation of VariableRealmControllerInterface::getAllKeys().
|
37
|
*/
|
38
|
public function getAllKeys() {
|
39
|
$all_realm_keys = $this->invokeUnionRealms('getAllKeys');
|
40
|
// First build all combinations of realm keys.
|
41
|
$combine = array(array('keys' => array(), 'names' => array()));
|
42
|
foreach ($all_realm_keys as $realm => $realm_keys) {
|
43
|
$new_combine = array();
|
44
|
foreach ($combine as $index => $data) {
|
45
|
foreach ($realm_keys as $new_key => $new_name) {
|
46
|
$keys = $data['keys'];
|
47
|
$names = $data['names'];
|
48
|
$keys[$realm] = $new_key;
|
49
|
$names[$realm] = $new_name;
|
50
|
$new_combine[] = array('keys' => $keys, 'names' => $names);
|
51
|
}
|
52
|
}
|
53
|
$combine = $new_combine;
|
54
|
}
|
55
|
// Now build all realm keys for the combinations.
|
56
|
$keys = array();
|
57
|
foreach ($combine as $data) {
|
58
|
$key = $this->buildUnionKey($data['keys']);
|
59
|
$name = $this->buildUnionName($data['names']);
|
60
|
$keys[$key] = $name;
|
61
|
}
|
62
|
return $keys;
|
63
|
}
|
64
|
/**
|
65
|
* Implementation of VariableRealmControllerInterface::getDefaultKey().
|
66
|
*/
|
67
|
public function getDefaultKey() {
|
68
|
$keys = $this->invokeUnionRealms('getDefaultKey');
|
69
|
return $this->buildUnionKey($keys);
|
70
|
}
|
71
|
/**
|
72
|
* Implementation of VariableRealmControllerInterface::getRequestKey().
|
73
|
*/
|
74
|
public function getRequestKey() {
|
75
|
// We'll have a request key if union realms have a current key.
|
76
|
$keys = $this->invokeUnionRealms('getKey');
|
77
|
return $this->buildUnionKey($keys);
|
78
|
}
|
79
|
/**
|
80
|
* Implementation of VariableRealmControllerInterface::getAvailableVariables().
|
81
|
*/
|
82
|
public function getAvailableVariables() {
|
83
|
$variables = $this->invokeUnionRealms('getAvailableVariables');
|
84
|
return call_user_func_array('array_intersect', $variables);
|
85
|
}
|
86
|
/**
|
87
|
* Implementation of VariableRealmControllerInterface::getEnabledVariables().
|
88
|
*/
|
89
|
public function getEnabledVariables() {
|
90
|
$variables = $this->invokeUnionRealms('getEnabledVariables');
|
91
|
return call_user_func_array('array_intersect', $variables);
|
92
|
}
|
93
|
/**
|
94
|
* Implementation of VariableRealmControllerInterface::getParentRealms().
|
95
|
*/
|
96
|
public function getParentRealms() {
|
97
|
return $this->getInfo('union', array());
|
98
|
}
|
99
|
/**
|
100
|
* Get union realms controllers.
|
101
|
*/
|
102
|
protected function getUnionRealms() {
|
103
|
return $this->union_realms;
|
104
|
}
|
105
|
/**
|
106
|
* Implementation of VariableRealmHooks::variableRealmEnable()
|
107
|
*/
|
108
|
public function variableRealmEnable($realm_name, $realm_key) {
|
109
|
// If this realm is enabled but not active, try to find a key.
|
110
|
if ($this->isEnabled() && !$this->isActive() && $this->isUnionRealm($realm_name) && $union_key = $this->getRequestKey()) {
|
111
|
$this->setKey($union_key);
|
112
|
}
|
113
|
}
|
114
|
/**
|
115
|
* Implementation of VariableRealmHooks::variableRealmSwitch()
|
116
|
*/
|
117
|
public function variableRealmSwitch($realm_name, $realm_key) {
|
118
|
// If the this realm is active, try to find new key.
|
119
|
if ($this->isActive() && $this->isUnionRealm($realm_name) && ($union_key = $this->getRequestKey())) {
|
120
|
$this->setKey($union_key);
|
121
|
}
|
122
|
}
|
123
|
/**
|
124
|
* Check whether a realm belongs to the union realms.
|
125
|
*/
|
126
|
protected function isUnionRealm($realm_name) {
|
127
|
return isset($this->union_realms[$realm_name]);
|
128
|
}
|
129
|
/**
|
130
|
* Invoke function on all realms.
|
131
|
*/
|
132
|
protected function invokeUnionRealms($method) {
|
133
|
return _variable_realm_invoke($this->getUnionRealms(), $method);
|
134
|
}
|
135
|
/**
|
136
|
* Build key from union realm keys.
|
137
|
*/
|
138
|
protected static function buildUnionKey($keys) {
|
139
|
if (in_array(FALSE, $keys, TRUE)) {
|
140
|
return FALSE;
|
141
|
}
|
142
|
else {
|
143
|
// Make sure values are in correct order
|
144
|
ksort($keys);
|
145
|
// implode values
|
146
|
return implode(':', $keys);
|
147
|
}
|
148
|
}
|
149
|
/**
|
150
|
* Build key name from union realm key names.
|
151
|
*/
|
152
|
protected static function buildUnionName($names) {
|
153
|
return implode(', ', $names);
|
154
|
}
|
155
|
}
|