Révision 7e72b748
Ajouté par Assos Assos il y a plus de 6 ans
drupal7/sites/all/modules/ctools/includes/context.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* |
|
6 | 5 |
* Contains code related to the ctools system of 'context'. |
7 | 6 |
* |
8 | 7 |
* Context, originally from Panels, is a method of packaging objects into |
... | ... | |
28 | 27 |
* of the context is important. |
29 | 28 |
*/ |
30 | 29 |
class ctools_context { |
31 |
var $type = NULL; |
|
32 |
var $data = NULL; |
|
33 |
// The title of this object. |
|
34 |
var $title = ''; |
|
35 |
// The title of the page if this object exists |
|
36 |
var $page_title = ''; |
|
37 |
// The identifier (in the UI) of this object |
|
38 |
var $identifier = ''; |
|
39 |
var $argument = NULL; |
|
40 |
var $keyword = ''; |
|
41 |
var $original_argument = NULL; |
|
42 |
var $restrictions = array(); |
|
43 |
var $empty = FALSE; |
|
44 |
|
|
45 |
function __construct($type = 'none', $data = NULL) { |
|
46 |
$this->type = $type; |
|
47 |
$this->data = $data; |
|
30 |
/** |
|
31 |
* @var string|array |
|
32 |
* A string naming this specific context type. The values 'any' and 'none' |
|
33 |
* are special: |
|
34 |
* - 'any': used in is_type() to match any other type. |
|
35 |
* - 'none': used to signal the type is not defined. |
|
36 |
*/ |
|
37 |
public $type; |
|
38 |
|
|
39 |
/** |
|
40 |
* @var mixed |
|
41 |
* The data payload for this context object. |
|
42 |
*/ |
|
43 |
public $data; |
|
44 |
|
|
45 |
/** |
|
46 |
* @var string |
|
47 |
* The title of this object. |
|
48 |
*/ |
|
49 |
public $title; |
|
50 |
|
|
51 |
/** |
|
52 |
* @var string |
|
53 |
* The title of the page if this object exists |
|
54 |
*/ |
|
55 |
public $page_title; |
|
56 |
|
|
57 |
/** |
|
58 |
* @var string |
|
59 |
* The identifier (in the UI) of this object. |
|
60 |
*/ |
|
61 |
public $identifier; |
|
62 |
|
|
63 |
/** |
|
64 |
* @var |
|
65 |
*/ |
|
66 |
public $argument; |
|
67 |
|
|
68 |
/** |
|
69 |
* @var string |
|
70 |
*/ |
|
71 |
public $keyword; |
|
72 |
|
|
73 |
/** |
|
74 |
* @var |
|
75 |
*/ |
|
76 |
public $original_argument; |
|
77 |
|
|
78 |
/** |
|
79 |
* @var array |
|
80 |
*/ |
|
81 |
public $restrictions; |
|
82 |
|
|
83 |
/** |
|
84 |
* @var bool |
|
85 |
*/ |
|
86 |
public $empty; |
|
87 |
|
|
88 |
/** |
|
89 |
* The ctools_context constructor. |
|
90 |
* |
|
91 |
* @param string $type |
|
92 |
* The type name of this context. Should be unique. Use the machine_name |
|
93 |
* conventions: lowercase, short, underscores and no spaces. |
|
94 |
* @param mixed $data |
|
95 |
* The data payload, if required for this context. |
|
96 |
*/ |
|
97 |
public function __construct($type = 'none', $data = NULL) { |
|
98 |
$this->type = $type; |
|
99 |
$this->data = $data; |
|
48 | 100 |
$this->title = t('Unknown context'); |
101 |
$this->page_title = ''; |
|
102 |
$this->identifier = ''; |
|
103 |
$this->keyword = ''; |
|
104 |
$this->restrictions = array(); |
|
105 |
$this->empty = FALSE; |
|
106 |
// Other vars are NULL. |
|
49 | 107 |
} |
50 | 108 |
|
51 |
function is_type($type) { |
|
52 |
if ($type == 'any' || $this->type == 'any') { |
|
109 |
/** |
|
110 |
* Determine whether this object is of type @var $type . |
|
111 |
* |
|
112 |
* Both the internal value ($this->type) and the supplied value ($type) can |
|
113 |
* be a string or an array of strings, and if one or both are arrays the match |
|
114 |
* succeeds if at least one common element is found. |
|
115 |
* |
|
116 |
* Type names |
|
117 |
* |
|
118 |
* @param string|array $type |
|
119 |
* 'type' can be: |
|
120 |
* - 'any' to match all types (this is true of the internal value too). |
|
121 |
* - an array of type name strings, when more than one type is acceptable. |
|
122 |
* |
|
123 |
* @return bool |
|
124 |
* True if the type matches, False otherwise. |
|
125 |
*/ |
|
126 |
public function is_type($type) { |
|
127 |
if ($type === 'any' || $this->type === 'any') { |
|
53 | 128 |
return TRUE; |
54 | 129 |
} |
55 | 130 |
|
... | ... | |
58 | 133 |
return (bool) array_intersect($a, $b); |
59 | 134 |
} |
60 | 135 |
|
61 |
function get_argument() { |
|
136 |
/** |
|
137 |
* Return the argument. |
|
138 |
* |
|
139 |
* @return mixed |
|
140 |
* The value of $argument. |
|
141 |
*/ |
|
142 |
public function get_argument() { |
|
62 | 143 |
return $this->argument; |
63 | 144 |
} |
64 | 145 |
|
65 |
function get_original_argument() { |
|
146 |
/** |
|
147 |
* Return the value of argument (or arg) variable as it was passed in. |
|
148 |
* |
|
149 |
* For example see ctools_plugin_load_function() and ctools_terms_context(). |
|
150 |
* |
|
151 |
* @return mixed |
|
152 |
* The original arg value. |
|
153 |
*/ |
|
154 |
public function get_original_argument() { |
|
66 | 155 |
if (!is_null($this->original_argument)) { |
67 | 156 |
return $this->original_argument; |
68 | 157 |
} |
69 | 158 |
return $this->argument; |
70 | 159 |
} |
71 | 160 |
|
72 |
function get_keyword() { |
|
161 |
/** |
|
162 |
* Return the keyword. |
|
163 |
* |
|
164 |
* @return mixed |
|
165 |
* The value of $keyword. |
|
166 |
*/ |
|
167 |
public function get_keyword() { |
|
73 | 168 |
return $this->keyword; |
74 | 169 |
} |
75 | 170 |
|
76 |
function get_identifier() { |
|
171 |
/** |
|
172 |
* Return the identifier. |
|
173 |
* |
|
174 |
* @return mixed |
|
175 |
* The value of $identifier. |
|
176 |
*/ |
|
177 |
public function get_identifier() { |
|
77 | 178 |
return $this->identifier; |
78 | 179 |
} |
79 | 180 |
|
80 |
function get_title() { |
|
181 |
/** |
|
182 |
* Return the title. |
|
183 |
* |
|
184 |
* @return mixed |
|
185 |
* The value of $title. |
|
186 |
*/ |
|
187 |
public function get_title() { |
|
81 | 188 |
return $this->title; |
82 | 189 |
} |
83 | 190 |
|
84 |
function get_page_title() { |
|
191 |
/** |
|
192 |
* Return the page title. |
|
193 |
* |
|
194 |
* @return mixed |
|
195 |
* The value of $page_title. |
|
196 |
*/ |
|
197 |
public function get_page_title() { |
|
85 | 198 |
return $this->page_title; |
86 | 199 |
} |
200 |
|
|
87 | 201 |
} |
88 | 202 |
|
89 | 203 |
/** |
... | ... | |
91 | 205 |
* match a required context type. |
92 | 206 |
*/ |
93 | 207 |
class ctools_context_required { |
94 |
var $keywords = ''; |
|
208 |
/** |
|
209 |
* @var array |
|
210 |
* Keyword strings associated with the context. |
|
211 |
*/ |
|
212 |
public $keywords; |
|
95 | 213 |
|
96 | 214 |
/** |
97 | 215 |
* If set, the title will be used in the selector to identify |
98 | 216 |
* the context. This is very useful when multiple contexts |
99 | 217 |
* are required to inform the user will be used for what. |
100 | 218 |
*/ |
101 |
var $title = NULL;
|
|
219 |
public $title;
|
|
102 | 220 |
|
103 | 221 |
/** |
104 | 222 |
* Test to see if this context is required. |
105 | 223 |
*/ |
106 |
var $required = TRUE;
|
|
224 |
public $required = TRUE;
|
|
107 | 225 |
|
108 | 226 |
/** |
109 | 227 |
* If TRUE, skip the check in ctools_context_required::select() |
110 | 228 |
* for contexts whose names may have changed. |
111 | 229 |
*/ |
112 |
var $skip_name_check = FALSE;
|
|
230 |
public $skip_name_check = FALSE;
|
|
113 | 231 |
|
114 | 232 |
/** |
233 |
* The ctools_context_required constructor. |
|
115 | 234 |
* |
116 |
* @param $title |
|
117 |
* The first parameter should be the 'title' of the context for use |
|
118 |
* in UYI selectors when multiple contexts qualify. |
|
119 |
* @param ... |
|
235 |
* Note: Constructor accepts a variable number of arguments, with optional |
|
236 |
* type-dependent args at the end of the list and one required argument, |
|
237 |
* the title. Note in particular that skip_name_check MUST be passed in as |
|
238 |
* a boolean (and not, for example, as an integer). |
|
239 |
* |
|
240 |
* @param string $title |
|
241 |
* The title of the context for use in UI selectors when multiple contexts |
|
242 |
* qualify. |
|
243 |
* @param string $keywords |
|
120 | 244 |
* One or more keywords to use for matching which contexts are allowed. |
245 |
* @param array $restrictions |
|
246 |
* Array of context restrictions. |
|
247 |
* @param bool $skip_name_check |
|
248 |
* If True, skip the check in select() for contexts whose names may have |
|
249 |
* changed. |
|
121 | 250 |
*/ |
122 |
function __construct($title) { |
|
251 |
public function __construct($title) { |
|
252 |
// If it was possible, using variadic syntax this should be: |
|
253 |
// __construct($title, string ...$keywords, array $restrictions = NULL, bool $skip = NULL) |
|
254 |
// but that form isn't allowed. |
|
123 | 255 |
$args = func_get_args(); |
124 | 256 |
$this->title = array_shift($args); |
125 | 257 |
|
126 |
// If we have a boolean value at the end for $skip_name_check, store it |
|
258 |
// If we have a boolean value at the end for $skip_name_check, store it.
|
|
127 | 259 |
if (is_bool(end($args))) { |
128 | 260 |
$this->skip_name_check = array_pop($args); |
129 | 261 |
} |
... | ... | |
133 | 265 |
$this->restrictions = array_pop($args); |
134 | 266 |
} |
135 | 267 |
|
136 |
if (count($args) == 1) { |
|
268 |
if (count($args) === 1) {
|
|
137 | 269 |
$args = array_shift($args); |
138 | 270 |
} |
139 | 271 |
$this->keywords = $args; |
140 | 272 |
} |
141 | 273 |
|
142 |
function filter($contexts) { |
|
274 |
/** |
|
275 |
* Filter the contexts to determine which apply in the current environment. |
|
276 |
* |
|
277 |
* A context passes the filter if: |
|
278 |
* - the context matches 'type' of the required keywords (uses |
|
279 |
* ctools_context::is_type(), so includes 'any' matches, etc). |
|
280 |
* - AND if restrictions are present, there are some common elements between |
|
281 |
* the requirement and the context. |
|
282 |
* |
|
283 |
* @param array $contexts |
|
284 |
* An array of ctools_context objects (or something which will cast to an |
|
285 |
* array of them). The contexts to apply the filter on. |
|
286 |
* |
|
287 |
* @return array |
|
288 |
* An array of context objects, keyed with the same keys used for $contexts, |
|
289 |
* which pass the filter. |
|
290 |
* |
|
291 |
* @see ctools_context::is_type() |
|
292 |
*/ |
|
293 |
public function filter($contexts) { |
|
143 | 294 |
$result = array(); |
144 | 295 |
|
145 |
// See which of these contexts are valid |
|
296 |
/** |
|
297 |
* See which of these contexts are valid. |
|
298 |
* @var ctools_context $context |
|
299 |
*/ |
|
146 | 300 |
foreach ((array) $contexts as $cid => $context) { |
147 | 301 |
if ($context->is_type($this->keywords)) { |
302 |
|
|
148 | 303 |
// Compare to see if our contexts were met. |
149 | 304 |
if (!empty($this->restrictions) && !empty($context->restrictions)) { |
305 |
|
|
150 | 306 |
foreach ($this->restrictions as $key => $values) { |
151 | 307 |
// If we have a restriction, the context must either not have that |
152 | 308 |
// restriction listed, which means we simply don't know what it is, |
... | ... | |
155 | 311 |
if (!is_array($values)) { |
156 | 312 |
$values = array($values); |
157 | 313 |
} |
158 |
if (!empty($context->restrictions[$key]) && !array_intersect($values, $context->restrictions[$key])) { |
|
314 |
|
|
315 |
if (!empty($context->restrictions[$key]) |
|
316 |
&& !array_intersect($values, $context->restrictions[$key]) |
|
317 |
) { |
|
318 |
// Break out to check next context; this one fails the filter. |
|
159 | 319 |
continue 2; |
160 | 320 |
} |
161 | 321 |
} |
162 | 322 |
} |
323 |
// This context passes the filter. |
|
163 | 324 |
$result[$cid] = $context; |
164 | 325 |
} |
165 | 326 |
} |
... | ... | |
167 | 328 |
return $result; |
168 | 329 |
} |
169 | 330 |
|
170 |
function select($contexts, $context) { |
|
331 |
/** |
|
332 |
* Select one context from the list of contexts, accounting for changed IDs. |
|
333 |
* |
|
334 |
* Fundamentally, this returns $contexts[$context] or FALSE if that does not |
|
335 |
* exist. Additional logic accounts for changes in context names and dealing |
|
336 |
* with a $contexts parameter that is not an array. |
|
337 |
* |
|
338 |
* If we had requested a $context but that $context doesn't exist in our |
|
339 |
* context list, there is a good chance that what happened is the context |
|
340 |
* IDs changed. Look for another context that satisfies our requirements, |
|
341 |
* unless $skip_name_check is set. |
|
342 |
* |
|
343 |
* @param ctools_context|array $contexts |
|
344 |
* A context, or an array of ctools_context. |
|
345 |
* @param string $context |
|
346 |
* A context ID. |
|
347 |
* |
|
348 |
* @return bool|ctools_context |
|
349 |
* The matching ctools_context, or False if no such context was found. |
|
350 |
*/ |
|
351 |
public function select($contexts, $context) { |
|
352 |
// Easier to deal with a standalone object as a 1-element array of objects. |
|
171 | 353 |
if (!is_array($contexts)) { |
172 | 354 |
if (is_object($contexts) && $contexts instanceof ctools_context) { |
173 | 355 |
$contexts = array($contexts->id => $contexts); |
... | ... | |
177 | 359 |
} |
178 | 360 |
} |
179 | 361 |
|
180 |
// If we had requested a $context but that $context doesn't exist |
|
181 |
// in our context list, there is a good chance that what happened |
|
182 |
// is our context IDs changed. See if there's another context |
|
183 |
// that satisfies our requirements. |
|
184 |
if (!$this->skip_name_check && !empty($context) && !isset($contexts[$context])) { |
|
362 |
// If we had requested a $context but that $context doesn't exist in our |
|
363 |
// context list, there is a good chance that what happened is the context |
|
364 |
// IDs changed. Check for another context that satisfies our requirements. |
|
365 |
if (!$this->skip_name_check |
|
366 |
&& !empty($context) && !isset($contexts[$context]) |
|
367 |
) { |
|
185 | 368 |
$choices = $this->filter($contexts); |
186 | 369 |
|
187 | 370 |
// If we got a hit, take the first one that matches. |
... | ... | |
196 | 379 |
} |
197 | 380 |
return $contexts[$context]; |
198 | 381 |
} |
382 |
|
|
199 | 383 |
} |
200 | 384 |
|
201 | 385 |
/** |
... | ... | |
203 | 387 |
* can produce empty contexts to use as placeholders. |
204 | 388 |
*/ |
205 | 389 |
class ctools_context_optional extends ctools_context_required { |
206 |
var $required = FALSE; |
|
207 | 390 |
|
208 | 391 |
/** |
209 |
* Add the 'empty' context which is possible for optional |
|
392 |
* {@inheritdoc} |
|
393 |
*/ |
|
394 |
public $required = FALSE; |
|
395 |
|
|
396 |
/** |
|
397 |
* Add the 'empty' context to the existing set. |
|
398 |
* |
|
399 |
* @param array &$contexts |
|
400 |
* An array of ctools_context objects. |
|
210 | 401 |
*/ |
211 |
function add_empty(&$contexts) { |
|
402 |
public function add_empty(&$contexts) {
|
|
212 | 403 |
$context = new ctools_context('any'); |
213 |
$context->title = t('No context');
|
|
404 |
$context->title = t('No context'); |
|
214 | 405 |
$context->identifier = t('No context'); |
215 | 406 |
$contexts['empty'] = $context; |
216 | 407 |
} |
217 | 408 |
|
218 |
function filter($contexts) { |
|
409 |
/** |
|
410 |
* Filter the contexts to determine which apply in the current environment. |
|
411 |
* |
|
412 |
* As for ctools_context_required, but we add the empty context to those |
|
413 |
* passed in so the check is optional (i.e. if nothing else matches, the |
|
414 |
* empty context will, and so there will always be at least one matched). |
|
415 |
* |
|
416 |
* @param array $contexts |
|
417 |
* An array of ctools_context objects (or something which will cast to an |
|
418 |
* array of them). The contexts to apply the filter on. |
|
419 |
* |
|
420 |
* @return array |
|
421 |
* An array of context objects, keyed with the same keys used for $contexts, |
|
422 |
* which pass the filter. |
|
423 |
* |
|
424 |
* @see ctools_context::is_type() |
|
425 |
*/ |
|
426 |
public function filter($contexts) { |
|
427 |
/** |
|
428 |
* @todo We are assuming here that $contexts is actually an array, whereas |
|
429 |
* ctools_context_required::filter only requires $contexts is convertible |
|
430 |
* to an array. |
|
431 |
*/ |
|
219 | 432 |
$this->add_empty($contexts); |
220 | 433 |
return parent::filter($contexts); |
221 | 434 |
} |
222 | 435 |
|
223 |
function select($contexts, $context) { |
|
436 |
/** |
|
437 |
* Select and return one context from the list of applicable contexts. |
|
438 |
* |
|
439 |
* Fundamentally, this returns $contexts[$context] or the empty context if |
|
440 |
* that does not exist. |
|
441 |
* |
|
442 |
* @param array $contexts |
|
443 |
* The applicable contexts to check. |
|
444 |
* @param string $context |
|
445 |
* The context id to check for. |
|
446 |
* |
|
447 |
* @return bool|ctools_context |
|
448 |
* The matching ctools_context, or False if no such context was found. |
|
449 |
* |
|
450 |
* @see ctools_context_required::select() |
|
451 |
*/ |
|
452 |
public function select($contexts, $context) { |
|
453 |
/** |
|
454 |
* @todo We are assuming here that $contexts is actually an array, whereas |
|
455 |
* ctools_context_required::select permits ctools_context objects as well. |
|
456 |
*/ |
|
224 | 457 |
$this->add_empty($contexts); |
225 | 458 |
if (empty($context)) { |
226 | 459 |
return $contexts['empty']; |
... | ... | |
230 | 463 |
|
231 | 464 |
// Don't flip out if it can't find the context; this is optional, put |
232 | 465 |
// in an empty. |
233 |
if ($result == FALSE) { |
|
466 |
if ($result === FALSE) {
|
|
234 | 467 |
$result = $contexts['empty']; |
235 | 468 |
} |
236 | 469 |
return $result; |
237 | 470 |
} |
471 |
|
|
238 | 472 |
} |
239 | 473 |
|
240 | 474 |
/** |
... | ... | |
249 | 483 |
* Since multiple contexts can be required, this function will accept either |
250 | 484 |
* an array of all required contexts, or just a single required context object. |
251 | 485 |
* |
252 |
* @param $contexts |
|
486 |
* @param array $contexts
|
|
253 | 487 |
* A keyed array of all available contexts. |
254 |
* @param $required |
|
255 |
* A ctools_context_required or ctools_context_optional object, or an array
|
|
256 |
* of such objects.
|
|
488 |
* @param array|ctools_context_required|ctools_context_optional $required
|
|
489 |
* A *_required or *_optional object, or an array of such objects, which
|
|
490 |
* define the selection condition.
|
|
257 | 491 |
* |
258 |
* @return |
|
492 |
* @return array
|
|
259 | 493 |
* A keyed array of contexts that match the filter. |
260 | 494 |
*/ |
261 | 495 |
function ctools_context_filter($contexts, $required) { |
262 | 496 |
if (is_array($required)) { |
263 | 497 |
$result = array(); |
264 |
foreach ($required as $r) {
|
|
265 |
$result = array_merge($result, _ctools_context_filter($contexts, $r));
|
|
498 |
foreach ($required as $item) {
|
|
499 |
$result = array_merge($result, _ctools_context_filter($contexts, $item));
|
|
266 | 500 |
} |
267 | 501 |
return $result; |
268 | 502 |
} |
... | ... | |
270 | 504 |
return _ctools_context_filter($contexts, $required); |
271 | 505 |
} |
272 | 506 |
|
507 |
/** |
|
508 |
* Helper function for ctools_context_filter(). |
|
509 |
* |
|
510 |
* Used to transform the required context during the merge into the final array. |
|
511 |
* |
|
512 |
* @internal This function DOES NOT form part of the CTools API. |
|
513 |
* |
|
514 |
* @param array $contexts |
|
515 |
* A keyed array of all available contexts. |
|
516 |
* @param ctools_context_required|ctools_context_optional $required |
|
517 |
* A ctools_context_required or ctools_context_optional object, although if |
|
518 |
* given something else will return an empty array. |
|
519 |
* |
|
520 |
* @return array |
|
521 |
*/ |
|
273 | 522 |
function _ctools_context_filter($contexts, $required) { |
274 | 523 |
$result = array(); |
275 | 524 |
|
... | ... | |
289 | 538 |
* If an array of required contexts is provided, one selector will be |
290 | 539 |
* provided for each context. |
291 | 540 |
* |
292 |
* @param $contexts |
|
541 |
* @param array $contexts
|
|
293 | 542 |
* A keyed array of all available contexts. |
294 |
* @param $required |
|
543 |
* @param array|ctools_context_required|ctools_context_optional $required
|
|
295 | 544 |
* The required context object or array of objects. |
545 |
* @param array|string $default |
|
546 |
* The default value for the select object, suitable for a #default_value |
|
547 |
* render key. Where $required is an array, this is an array keyed by the |
|
548 |
* same key values as $required for all keys where an empty string is not a |
|
549 |
* suitable default. Otherwise it is just the default value. |
|
296 | 550 |
* |
297 |
* @return |
|
551 |
* @return array
|
|
298 | 552 |
* A form element, or NULL if there are no contexts that satisfy the |
299 | 553 |
* requirements. |
300 | 554 |
*/ |
... | ... | |
302 | 556 |
if (is_array($required)) { |
303 | 557 |
$result = array('#tree' => TRUE); |
304 | 558 |
$count = 1; |
305 |
foreach ($required as $id => $r) { |
|
306 |
$result[] = _ctools_context_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++); |
|
559 |
foreach ($required as $id => $item) { |
|
560 |
$result[] = _ctools_context_selector( |
|
561 |
$contexts, $item, isset($default[$id]) ? $default[$id] : '', $count++ |
|
562 |
); |
|
307 | 563 |
} |
308 | 564 |
return $result; |
309 | 565 |
} |
... | ... | |
311 | 567 |
return _ctools_context_selector($contexts, $required, $default); |
312 | 568 |
} |
313 | 569 |
|
570 |
/** |
|
571 |
* Helper function for ctools_context_selector(). |
|
572 |
* |
|
573 |
* @internal This function DOES NOT form part of the CTools API. Use the API |
|
574 |
* function ctools_context_selector() instead. |
|
575 |
* |
|
576 |
* @param array $contexts |
|
577 |
* A keyed array of all available contexts. |
|
578 |
* @param ctools_context_required|ctools_context_optional $required |
|
579 |
* The required context object. |
|
580 |
* @param $default |
|
581 |
* The default value for the select object, suitable for a #default_value |
|
582 |
* render key. |
|
583 |
* @param int $num |
|
584 |
* If supplied and non-zero, the title of the select form element will be |
|
585 |
* "Context $num", otherwise it will be "Context". |
|
586 |
* |
|
587 |
* @return array |
|
588 |
* A form element, or NULL if there are no contexts that satisfy the |
|
589 |
* requirements. |
|
590 |
*/ |
|
314 | 591 |
function _ctools_context_selector($contexts, $required, $default, $num = 0) { |
315 | 592 |
$filtered = ctools_context_filter($contexts, $required); |
316 | 593 |
$count = count($filtered); |
... | ... | |
354 | 631 |
* @param $required |
355 | 632 |
* The required context object or array of objects. |
356 | 633 |
* |
357 |
* @return |
|
358 |
* TRUE if there are enough contexts, FALSE if there are not.
|
|
634 |
* @return bool
|
|
635 |
* True if there are enough contexts, otherwise False.
|
|
359 | 636 |
*/ |
360 | 637 |
function ctools_context_match_requirements($contexts, $required) { |
361 | 638 |
if (!is_array($required)) { |
... | ... | |
388 | 665 |
* A keyed array of all available contexts. |
389 | 666 |
* @param $required |
390 | 667 |
* The required context object or array of objects. |
668 |
* @param array|string $default |
|
669 |
* The default value for the select object, suitable for a #default_value |
|
670 |
* render key. Where $required is an array, this is an array keyed by the |
|
671 |
* same key values as $required for all keys where an empty string is not a |
|
672 |
* suitable default. Otherwise it is just the default value. |
|
391 | 673 |
* |
392 |
* @return |
|
674 |
* @return array
|
|
393 | 675 |
* A form element, or NULL if there are no contexts that satisfy the |
394 | 676 |
* requirements. |
395 | 677 |
*/ |
... | ... | |
397 | 679 |
if (is_array($required)) { |
398 | 680 |
$result = array('#tree' => TRUE); |
399 | 681 |
$count = 1; |
400 |
foreach ($required as $id => $r) { |
|
401 |
$result[] = _ctools_context_converter_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++); |
|
682 |
foreach ($required as $id => $dependency) { |
|
683 |
$default_id = isset($default[$id]) ? $default[$id] : ''; |
|
684 |
$result[] = _ctools_context_converter_selector( |
|
685 |
$contexts, $dependency, $default_id, $count++ |
|
686 |
); |
|
402 | 687 |
} |
403 | 688 |
return $result; |
404 | 689 |
} |
... | ... | |
406 | 691 |
return _ctools_context_converter_selector($contexts, $required, $default); |
407 | 692 |
} |
408 | 693 |
|
694 |
/** |
|
695 |
* Helper function for ctools_context_converter_selector(). |
|
696 |
* |
|
697 |
* @internal This function DOES NOT form part of the CTools API. Use the API |
|
698 |
* function ctools_context_converter_selector() instead. |
|
699 |
* |
|
700 |
* @param array $contexts |
|
701 |
* A keyed array of all available contexts. |
|
702 |
* @param ctools_context $required |
|
703 |
* The required context object. |
|
704 |
* @param $default |
|
705 |
* The default value for the select object, suitable for a #default_value |
|
706 |
* render key. |
|
707 |
* @param int $num |
|
708 |
* If supplied and non-zero, the title of the select form element will be |
|
709 |
* "Context $num", otherwise it will be "Context". |
|
710 |
* |
|
711 |
* @return array|null |
|
712 |
* A form element, or NULL if there are no contexts that satisfy the |
|
713 |
* requirements. |
|
714 |
*/ |
|
409 | 715 |
function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) { |
410 | 716 |
$filtered = ctools_context_filter($contexts, $required); |
411 | 717 |
$count = count($filtered); |
412 | 718 |
|
413 |
$form = array(); |
|
414 |
|
|
415 | 719 |
if ($count > 1) { |
416 | 720 |
// If there's more than one to choose from, create a select widget. |
417 | 721 |
$options = array(); |
418 | 722 |
foreach ($filtered as $cid => $context) { |
419 |
if ($context->type == 'any') { |
|
723 |
if ($context->type === 'any') {
|
|
420 | 724 |
$options[''] = t('No context'); |
421 | 725 |
continue; |
422 | 726 |
} |
... | ... | |
446 | 750 |
'#default_value' => $default, |
447 | 751 |
); |
448 | 752 |
} |
753 |
else { |
|
754 |
// Not enough choices to need a selector, so don't make one. |
|
755 |
return NULL; |
|
756 |
} |
|
449 | 757 |
} |
450 | 758 |
|
451 | 759 |
/** |
452 | 760 |
* Get a list of converters available for a given context. |
761 |
* |
|
762 |
* @param string $cid |
|
763 |
* A context ID. |
|
764 |
* @param ctools_context $context |
|
765 |
* The context for which converters are needed. |
|
766 |
* |
|
767 |
* @return array |
|
768 |
* A list of context converters. |
|
453 | 769 |
*/ |
454 | 770 |
function ctools_context_get_converters($cid, $context) { |
455 | 771 |
if (empty($context->plugin)) { |
... | ... | |
461 | 777 |
|
462 | 778 |
/** |
463 | 779 |
* Get a list of converters available for a given context. |
780 |
* |
|
781 |
* @internal This function DOES NOT form part of the CTools API. Use the API |
|
782 |
* function ctools_context_get_converters() instead. |
|
783 |
* |
|
784 |
* @param string $id |
|
785 |
* A context ID. |
|
786 |
* @param string $plugin_name |
|
787 |
* The name of the context plugin. |
|
788 |
* |
|
789 |
* @return array |
|
790 |
* A list of context converters. |
|
464 | 791 |
*/ |
465 | 792 |
function _ctools_context_get_converters($id, $plugin_name) { |
466 | 793 |
$plugin = ctools_get_context($plugin_name); |
... | ... | |
472 | 799 |
if (is_array($plugin['convert list'])) { |
473 | 800 |
$converters = $plugin['convert list']; |
474 | 801 |
} |
475 |
else if ($function = ctools_plugin_get_function($plugin, 'convert list')) {
|
|
802 |
elseif ($function = ctools_plugin_get_function($plugin, 'convert list')) { |
|
476 | 803 |
$converters = (array) $function($plugin); |
477 | 804 |
} |
478 | 805 |
|
... | ... | |
492 | 819 |
} |
493 | 820 |
|
494 | 821 |
/** |
495 |
* Get a list of all contexts + converters available. |
|
822 |
* Get a list of all contexts converters available. |
|
823 |
* |
|
824 |
* For all contexts returned by ctools_get_contexts(), return the converter |
|
825 |
* for all contexts that have one. |
|
826 |
* |
|
827 |
* @return array |
|
828 |
* A list of context converters, keyed by the title of the converter. |
|
496 | 829 |
*/ |
497 | 830 |
function ctools_context_get_all_converters() { |
498 | 831 |
$contexts = ctools_get_contexts(); |
... | ... | |
512 | 845 |
/** |
513 | 846 |
* Let the context convert an argument based upon the converter that was given. |
514 | 847 |
* |
515 |
* @param $context |
|
516 |
* The context object |
|
517 |
* @param $converter |
|
518 |
* The converter to use, which should be a string provided by the converter list. |
|
519 |
* @param $converter_options |
|
520 |
* A n array of options to pass on to the generation function. For contexts |
|
848 |
* @param ctools_context $context |
|
849 |
* The context object. |
|
850 |
* @param string $converter |
|
851 |
* The type of converter to use, which should be a string provided by the |
|
852 |
* converter list function. |
|
853 |
* @param array $converter_options |
|
854 |
* An array of options to pass on to the generation function. For contexts |
|
521 | 855 |
* that use token module, of particular use is 'sanitize' => FALSE which can |
522 | 856 |
* get raw tokens. This should ONLY be used in values that will later be |
523 | 857 |
* treated as unsafe user input since these values are by themselves unsafe. |
524 | 858 |
* It is particularly useful to get raw values from Field API. |
859 |
* |
|
860 |
* @return string|null |
|
525 | 861 |
*/ |
526 | 862 |
function ctools_context_convert_context($context, $converter, $converter_options = array()) { |
527 | 863 |
// Contexts without plugins might be optional placeholders. |
528 | 864 |
if (empty($context->plugin)) { |
529 |
return; |
|
865 |
return NULL;
|
|
530 | 866 |
} |
531 | 867 |
|
532 | 868 |
$value = $context->argument; |
... | ... | |
547 | 883 |
* Choose a context or contexts based upon the selection made via |
548 | 884 |
* ctools_context_filter. |
549 | 885 |
* |
550 |
* @param $contexts |
|
551 |
* A keyed array of all available contexts |
|
552 |
* @param $required |
|
553 |
* The required context object provided by the plugin
|
|
886 |
* @param array $contexts
|
|
887 |
* A keyed array of all available contexts.
|
|
888 |
* @param array|ctools_context_required $required
|
|
889 |
* The required context object(s) provided by the plugin.
|
|
554 | 890 |
* @param $context |
555 |
* The selection made using ctools_context_selector |
|
891 |
* The selection made using ctools_context_selector(). |
|
892 |
* |
|
893 |
* @return ctools_context|array|false |
|
894 |
* Returns FALSE if $required is not an object, or array of objects, or |
|
895 |
* the value of $required->select() for the context, or an array of those (if |
|
896 |
* passed an array in $required). |
|
556 | 897 |
*/ |
557 | 898 |
function ctools_context_select($contexts, $required, $context) { |
558 | 899 |
if (is_array($required)) { |
900 |
|
|
901 |
/** |
|
902 |
* @var array $required |
|
903 |
* Array of required context objects. |
|
904 |
* @var ctools_context_required $item |
|
905 |
* A required context object. |
|
906 |
*/ |
|
559 | 907 |
$result = array(); |
560 |
foreach ($required as $id => $r) { |
|
908 |
foreach ($required as $id => $item) { |
|
909 |
// @todo What's the difference between the following and "empty($item)" ? |
|
561 | 910 |
if (empty($required[$id])) { |
562 | 911 |
continue; |
563 | 912 |
} |
564 | 913 |
|
565 |
if (($result[] = _ctools_context_select($contexts, $r, $context[$id])) === FALSE) {
|
|
914 |
if (($result[] = _ctools_context_select($contexts, $item, $context[$id])) === FALSE) {
|
|
566 | 915 |
return FALSE; |
567 | 916 |
} |
568 | 917 |
} |
... | ... | |
572 | 921 |
return _ctools_context_select($contexts, $required, $context); |
573 | 922 |
} |
574 | 923 |
|
924 |
/** |
|
925 |
* Helper function for calling the required context object's selection function. |
|
926 |
* |
|
927 |
* This function DOES NOT form part of the CTools API. |
|
928 |
* |
|
929 |
* @param array $contexts |
|
930 |
* A keyed array of all available contexts. |
|
931 |
* @param ctools_context_required $required |
|
932 |
* The required context object provided by the plugin. |
|
933 |
* @param $context |
|
934 |
* The selection made using ctools_context_selector(). |
|
935 |
* |
|
936 |
* @return ctools_context|bool |
|
937 |
* FALSE if the $required is not an object. A ctools_context object if one |
|
938 |
* matched. |
|
939 |
*/ |
|
575 | 940 |
function _ctools_context_select($contexts, $required, $context) { |
576 | 941 |
if (!is_object($required)) { |
577 | 942 |
return FALSE; |
... | ... | |
583 | 948 |
/** |
584 | 949 |
* Create a new context object. |
585 | 950 |
* |
586 |
* @param $type |
|
951 |
* @param string $type
|
|
587 | 952 |
* The type of context to create; this loads a plugin. |
588 |
* @param $data |
|
953 |
* @param mixed $data
|
|
589 | 954 |
* The data to put into the context. |
590 |
* @param $empty |
|
591 |
* Whether or not this context is specifically empty. |
|
592 | 955 |
* @param $conf |
593 | 956 |
* A configuration structure if this context was created via UI. |
594 | 957 |
* |
595 |
* @return |
|
958 |
* @return ctools_context
|
|
596 | 959 |
* A $context or NULL if one could not be created. |
597 | 960 |
*/ |
598 | 961 |
function ctools_context_create($type, $data = NULL, $conf = FALSE) { |
... | ... | |
615 | 978 |
* @param $type |
616 | 979 |
* The type of context to create; this loads a plugin. |
617 | 980 |
* |
618 |
* @return |
|
981 |
* @return ctools_context
|
|
619 | 982 |
* A $context or NULL if one could not be created. |
620 | 983 |
*/ |
621 | 984 |
function ctools_context_create_empty($type) { |
... | ... | |
632 | 995 |
|
633 | 996 |
/** |
634 | 997 |
* Perform keyword and context substitutions. |
998 |
* |
|
999 |
* @param string $string |
|
1000 |
* The string in which to replace keywords. |
|
1001 |
* @param array $keywords |
|
1002 |
* Array of keyword-replacement pairs. |
|
1003 |
* @param array $contexts |
|
1004 |
* |
|
1005 |
* @param array $converter_options |
|
1006 |
* Options to pass on to ctools_context_convert_context(), defaults to an |
|
1007 |
* empty array. |
|
1008 |
* |
|
1009 |
* @return string |
|
1010 |
* The returned string, with substitutions performed. |
|
635 | 1011 |
*/ |
636 |
function ctools_context_keyword_substitute($string, $keywords, $contexts, $converter_options = array()) { |
|
1012 |
function ctools_context_keyword_substitute($string, $keywords, $contexts, array $converter_options = array()) {
|
|
637 | 1013 |
// Ensure a default keyword exists: |
638 | 1014 |
$keywords['%%'] = '%'; |
639 | 1015 |
|
... | ... | |
656 | 1032 |
|
657 | 1033 |
// If the keyword is already set by something passed in, don't try to |
658 | 1034 |
// overwrite it. |
659 |
if (!empty($keywords['%' . $keyword])) {
|
|
1035 |
if (array_key_exists('%' . $keyword, $keywords)) {
|
|
660 | 1036 |
continue; |
661 | 1037 |
} |
662 | 1038 |
|
... | ... | |
683 | 1059 |
if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) { |
684 | 1060 |
$keywords['%' . $keyword] = ''; |
685 | 1061 |
} |
686 |
else if (!empty($converter)) { |
|
687 |
$keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options); |
|
688 |
} |
|
689 | 1062 |
else { |
690 |
$keywords['%' . $keyword] = $context_keywords[$keyword]->title; |
|
1063 |
if (!empty($converter)) { |
|
1064 |
$keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options); |
|
1065 |
} |
|
1066 |
else { |
|
1067 |
$keywords['%' . $keyword] = $context_keywords[$keyword]->title; |
|
1068 |
} |
|
691 | 1069 |
} |
692 | 1070 |
} |
693 | 1071 |
} |
... | ... | |
696 | 1074 |
} |
697 | 1075 |
|
698 | 1076 |
/** |
699 |
* Determine a unique context ID for a context |
|
1077 |
* Determine a unique context ID for a context.
|
|
700 | 1078 |
* |
701 | 1079 |
* Often contexts of many different types will be placed into a list. This |
702 | 1080 |
* ensures that even though contexts of multiple types may share IDs, they |
703 | 1081 |
* are unique in the final list. |
704 | 1082 |
*/ |
705 | 1083 |
function ctools_context_id($context, $type = 'context') { |
706 |
if (!$context['id']) { |
|
1084 |
// If not set, FALSE or empty. |
|
1085 |
if (!isset($context['id']) || !$context['id']) { |
|
707 | 1086 |
$context['id'] = 1; |
708 | 1087 |
} |
709 | 1088 |
|
710 |
return $type . '_' . $context['name'] . '_' . $context['id']; |
|
1089 |
// @todo is '' the appropriate default value? |
|
1090 |
$name = isset($context['name']) ? $context['name'] : ''; |
|
1091 |
|
|
1092 |
return $type . '_' . $name . '_' . $context['id']; |
|
711 | 1093 |
} |
712 | 1094 |
|
713 | 1095 |
/** |
... | ... | |
715 | 1097 |
* |
716 | 1098 |
* This finds the next id available for the named object. |
717 | 1099 |
* |
718 |
* @param $objects |
|
719 |
* A list of context descriptor objects, i.e, arguments, relationships, contexts, etc. |
|
720 |
* @param $name |
|
1100 |
* @param array $objects |
|
1101 |
* A list of context descriptor objects, i.e, arguments, relationships, |
|
1102 |
* contexts, etc. |
|
1103 |
* @param string $name |
|
721 | 1104 |
* The name being used. |
1105 |
* |
|
1106 |
* @return int |
|
1107 |
* The next integer id available. |
|
722 | 1108 |
*/ |
723 | 1109 |
function ctools_context_next_id($objects, $name) { |
724 | 1110 |
$id = 0; |
725 |
// Figure out which instance of this argument we're creating |
|
1111 |
// Figure out which instance of this argument we're creating.
|
|
726 | 1112 |
if (!$objects) { |
727 | 1113 |
return $id + 1; |
728 | 1114 |
} |
729 | 1115 |
|
730 | 1116 |
foreach ($objects as $object) { |
731 |
if (isset($object['name']) && $object['name'] == $name) { |
|
732 |
if ($object['id'] > $id) { |
|
1117 |
if (isset($object['name']) && $object['name'] === $name) {
|
|
1118 |
if (isset($object['id']) && $object['id'] > $id) {
|
|
733 | 1119 |
$id = $object['id']; |
734 | 1120 |
} |
1121 |
// @todo If obj has no 'id', should we increment local id? $id = $id + 1; |
|
735 | 1122 |
} |
736 | 1123 |
} |
737 | 1124 |
|
738 | 1125 |
return $id + 1; |
739 | 1126 |
} |
740 | 1127 |
|
741 |
|
|
742 | 1128 |
// --------------------------------------------------------------------------- |
743 | 1129 |
// Functions related to contexts from arguments. |
744 |
|
|
745 | 1130 |
/** |
746 |
* Fetch metadata on a specific argument plugin.
|
|
1131 |
* Fetch metadata for a specific argument plugin.
|
|
747 | 1132 |
* |
748 | 1133 |
* @param $argument |
749 | 1134 |
* Name of an argument plugin. |
750 | 1135 |
* |
751 |
* @return |
|
1136 |
* @return array
|
|
752 | 1137 |
* An array with information about the requested argument plugin. |
753 | 1138 |
*/ |
1139 |
|
|
754 | 1140 |
function ctools_get_argument($argument) { |
755 | 1141 |
ctools_include('plugins'); |
756 | 1142 |
return ctools_get_plugins('ctools', 'arguments', $argument); |
... | ... | |
759 | 1145 |
/** |
760 | 1146 |
* Fetch metadata for all argument plugins. |
761 | 1147 |
* |
762 |
* @return |
|
1148 |
* @return array
|
|
763 | 1149 |
* An array of arrays with information about all available argument plugins. |
764 | 1150 |
*/ |
765 | 1151 |
function ctools_get_arguments() { |
... | ... | |
779 | 1165 |
* - keyword: The keyword used for this argument for substitutions. |
780 | 1166 |
* |
781 | 1167 |
* @param $arg |
782 |
* The actual argument received. This is expected to be a string from a URL but
|
|
783 |
* this does not have to be the only source of arguments. |
|
1168 |
* The actual argument received. This is expected to be a string from a URL |
|
1169 |
* but this does not have to be the only source of arguments.
|
|
784 | 1170 |
* @param $empty |
785 | 1171 |
* If true, the $arg will not be used to load the context. Instead, an empty |
786 | 1172 |
* placeholder context will be loaded. |
787 | 1173 |
* |
788 |
* @return |
|
1174 |
* @return ctools_context
|
|
789 | 1175 |
* A context object if one can be loaded. |
790 | 1176 |
*/ |
791 | 1177 |
function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) { |
792 | 1178 |
ctools_include('plugins'); |
793 | 1179 |
if (empty($argument['name'])) { |
794 |
return; |
|
1180 |
return NULL;
|
|
795 | 1181 |
} |
796 | 1182 |
|
797 |
if ($function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context')) { |
|
1183 |
$function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context'); |
|
1184 |
if ($function) { |
|
798 | 1185 |
// Backward compatibility: Merge old style settings into new style: |
799 | 1186 |
if (!empty($argument['settings'])) { |
800 | 1187 |
$argument += $argument['settings']; |
... | ... | |
806 | 1193 |
if (is_object($context)) { |
807 | 1194 |
$context->identifier = $argument['identifier']; |
808 | 1195 |
$context->page_title = isset($argument['title']) ? $argument['title'] : ''; |
809 |
$context->keyword = $argument['keyword'];
|
|
810 |
$context->id = ctools_context_id($argument, 'argument');
|
|
1196 |
$context->keyword = $argument['keyword']; |
|
1197 |
$context->id = ctools_context_id($argument, 'argument'); |
|
811 | 1198 |
$context->original_argument = $arg; |
812 | 1199 |
|
813 | 1200 |
if (!empty($context->empty)) { |
... | ... | |
823 | 1210 |
|
824 | 1211 |
/** |
825 | 1212 |
* Retrieve a list of empty contexts for all arguments. |
1213 |
* |
|
1214 |
* @param array $arguments |
|
1215 |
* |
|
1216 |
* @return array |
|
1217 |
* |
|
1218 |
* @see ctools_context_get_context_from_arguments() |
|
826 | 1219 |
*/ |
827 | 1220 |
function ctools_context_get_placeholders_from_argument($arguments) { |
828 | 1221 |
$contexts = array(); |
... | ... | |
838 | 1231 |
/** |
839 | 1232 |
* Load the contexts for a given list of arguments. |
840 | 1233 |
* |
841 |
* @param $arguments |
|
1234 |
* @param array $arguments
|
|
842 | 1235 |
* The array of argument definitions. |
843 |
* @param &$contexts |
|
1236 |
* @param array &$contexts
|
|
844 | 1237 |
* The array of existing contexts. New contexts will be added to this array. |
845 |
* @param $args |
|
1238 |
* @param array $args
|
|
846 | 1239 |
* The arguments to load. |
847 | 1240 |
* |
848 |
* @return |
|
849 |
* FALSE if an argument wants to 404. |
|
1241 |
* @return bool |
|
1242 |
* TRUE if all is well, FALSE if an argument wants to 404. |
|
1243 |
* |
|
1244 |
* @see ctools_context_get_context_from_argument() |
|
850 | 1245 |
*/ |
851 | 1246 |
function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) { |
852 | 1247 |
foreach ($arguments as $argument) { |
853 |
// pull the argument off the list.
|
|
1248 |
// Pull the argument off the list.
|
|
854 | 1249 |
$arg = array_shift($args); |
855 | 1250 |
$id = ctools_context_id($argument, 'argument'); |
856 | 1251 |
|
... | ... | |
865 | 1260 |
$context = $contexts[$id]; |
866 | 1261 |
} |
867 | 1262 |
|
868 |
if ((empty($context) || empty($context->data)) && !empty($argument['default']) && $argument['default'] == '404') { |
|
1263 |
if ((empty($context) || empty($context->data)) |
|
1264 |
&& !empty($argument['default']) |
|
1265 |
&& $argument['default'] === '404' |
|
1266 |
) { |
|
869 | 1267 |
return FALSE; |
870 | 1268 |
} |
871 | 1269 |
} |
... | ... | |
874 | 1272 |
|
875 | 1273 |
// --------------------------------------------------------------------------- |
876 | 1274 |
// Functions related to contexts from relationships. |
877 |
|
|
878 | 1275 |
/** |
879 |
* Fetch metadata on a specific relationship plugin.
|
|
1276 |
* Fetch plugin metadata for a specific relationship plugin.
|
|
880 | 1277 |
* |
881 |
* @param $content type
|
|
1278 |
* @param $relationship
|
|
882 | 1279 |
* Name of a panel content type. |
883 | 1280 |
* |
884 |
* @return |
|
1281 |
* @return array
|
|
885 | 1282 |
* An array with information about the requested relationship. |
1283 |
* |
|
1284 |
* @see ctools_get_relationships() |
|
886 | 1285 |
*/ |
1286 |
|
|
887 | 1287 |
function ctools_get_relationship($relationship) { |
888 | 1288 |
ctools_include('plugins'); |
889 | 1289 |
return ctools_get_plugins('ctools', 'relationships', $relationship); |
... | ... | |
892 | 1292 |
/** |
893 | 1293 |
* Fetch metadata for all relationship plugins. |
894 | 1294 |
* |
895 |
* @return |
|
1295 |
* @return array
|
|
896 | 1296 |
* An array of arrays with information about all available relationships. |
1297 |
* |
|
1298 |
* @see ctools_get_relationship() |
|
897 | 1299 |
*/ |
898 | 1300 |
function ctools_get_relationships() { |
899 | 1301 |
ctools_include('plugins'); |
... | ... | |
901 | 1303 |
} |
902 | 1304 |
|
903 | 1305 |
/** |
1306 |
* Return a context from a relationship. |
|
904 | 1307 |
* |
905 |
* @param $relationship |
|
1308 |
* @param array $relationship
|
|
906 | 1309 |
* The configuration of a relationship. It must contain the following data: |
907 | 1310 |
* - name: The name of the relationship plugin being used. |
908 | 1311 |
* - relationship_settings: The configuration based upon the plugin forms. |
... | ... | |
910 | 1313 |
* defined by the UI. |
911 | 1314 |
* - keyword: The keyword used for this relationship for substitutions. |
912 | 1315 |
* |
913 |
* @param $source_context |
|
1316 |
* @param ctools_context $source_context
|
|
914 | 1317 |
* The context this relationship is based upon. |
915 |
* |
|
916 |
* @param $placeholders |
|
1318 |
* @param bool $placeholders |
|
917 | 1319 |
* If TRUE, placeholders are acceptable. |
918 | 1320 |
* |
919 |
* @return |
|
920 |
* A context object if one can be loaded. |
|
1321 |
* @return ctools_context|null |
|
1322 |
* A context object if one can be loaded, otherwise NULL. |
|
1323 |
* |
|
1324 |
* @see ctools_context_get_relevant_relationships() |
|
1325 |
* @see ctools_context_get_context_from_relationships() |
|
921 | 1326 |
*/ |
922 | 1327 |
function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) { |
923 | 1328 |
ctools_include('plugins'); |
924 |
if ($function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context')) { |
|
1329 |
$function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context'); |
|
1330 |
if ($function) { |
|
925 | 1331 |
// Backward compatibility: Merge old style settings into new style: |
926 | 1332 |
if (!empty($relationship['relationship_settings'])) { |
927 | 1333 |
$relationship += $relationship['relationship_settings']; |
... | ... | |
932 | 1338 |
if ($context) { |
933 | 1339 |
$context->identifier = $relationship['identifier']; |
934 | 1340 |
$context->page_title = isset($relationship['title']) ? $relationship['title'] : ''; |
935 |
$context->keyword = $relationship['keyword'];
|
|
1341 |
$context->keyword = $relationship['keyword']; |
|
936 | 1342 |
if (!empty($context->empty)) { |
937 | 1343 |
$context->placeholder = array( |
938 | 1344 |
'type' => 'relationship', |
... | ... | |
942 | 1348 |
return $context; |
943 | 1349 |
} |
944 | 1350 |
} |
1351 |
return NULL; |
|
945 | 1352 |
} |
946 | 1353 |
|
947 | 1354 |
/** |
... | ... | |
955 | 1362 |
* @param $contexts |
956 | 1363 |
* An array of contexts used to figure out which relationships are relevant. |
957 | 1364 |
* |
958 |
* @return |
|
1365 |
* @return array
|
|
959 | 1366 |
* An array of relationship keys that are relevant for the given set of |
960 | 1367 |
* contexts. |
1368 |
* |
|
1369 |
* @see ctools_context_filter() |
|
1370 |
* @see ctools_context_get_context_from_relationship() |
|
1371 |
* @see ctools_context_get_context_from_relationships() |
|
961 | 1372 |
*/ |
962 | 1373 |
function ctools_context_get_relevant_relationships($contexts) { |
963 | 1374 |
$relevant = array(); |
964 | 1375 |
$relationships = ctools_get_relationships(); |
965 | 1376 |
|
966 |
// Go through each relationship |
|
1377 |
// Go through each relationship.
|
|
967 | 1378 |
foreach ($relationships as $rid => $relationship) { |
968 | 1379 |
// For each relationship, see if there is a context that satisfies it. |
969 |
if (empty($relationship['no ui']) && ctools_context_filter($contexts, $relationship['required context'])) { |
|
1380 |
if (empty($relationship['no ui']) |
|
1381 |
&& ctools_context_filter($contexts, $relationship['required context']) |
|
1382 |
) { |
|
970 | 1383 |
$relevant[$rid] = $relationship['title']; |
971 | 1384 |
} |
972 | 1385 |
} |
... | ... | |
975 | 1388 |
} |
976 | 1389 |
|
977 | 1390 |
/** |
978 |
* Fetch all active relationships |
|
1391 |
* Fetch all active relationships.
|
|
979 | 1392 |
* |
980 | 1393 |
* @param $relationships |
981 | 1394 |
* An keyed array of relationship data including: |
... | ... | |
990 | 1403 |
* |
991 | 1404 |
* @param $placeholders |
992 | 1405 |
* If TRUE, placeholders are acceptable. |
1406 |
* |
|
1407 |
* @see ctools_context_get_context_from_relationship() |
|
1408 |
* @see ctools_context_get_relevant_relationships() |
|
993 | 1409 |
*/ |
994 | 1410 |
function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) { |
995 |
$return = array(); |
|
996 |
|
|
997 | 1411 |
foreach ($relationships as $rdata) { |
998 | 1412 |
if (!isset($rdata['context'])) { |
999 | 1413 |
continue; |
... | ... | |
1024 | 1438 |
|
1025 | 1439 |
// --------------------------------------------------------------------------- |
1026 | 1440 |
// Functions related to loading contexts from simple context definitions. |
1027 |
|
|
1028 | 1441 |
/** |
1029 | 1442 |
* Fetch metadata on a specific context plugin. |
1030 | 1443 |
* |
1031 |
* @param $context |
|
1444 |
* @param string $context
|
|
1032 | 1445 |
* Name of a context. |
1033 | 1446 |
* |
1034 |
* @return |
|
1447 |
* @return array
|
|
1035 | 1448 |
* An array with information about the requested panel context. |
1036 | 1449 |
*/ |
1450 |
|
|
1037 | 1451 |
function ctools_get_context($context) { |
1038 | 1452 |
static $gate = array(); |
1039 | 1453 |
ctools_include('plugins'); |
... | ... | |
1057 | 1471 |
/** |
1058 | 1472 |
* Fetch metadata for all context plugins. |
1059 | 1473 |
* |
1060 |
* @return |
|
1474 |
* @return array
|
|
1061 | 1475 |
* An array of arrays with information about all available panel contexts. |
1062 | 1476 |
*/ |
1063 | 1477 |
function ctools_get_contexts() { |
... | ... | |
1066 | 1480 |
} |
1067 | 1481 |
|
1068 | 1482 |
/** |
1483 |
* Return a context object from a context definition array. |
|
1069 | 1484 |
* |
1070 |
* @param $context |
|
1485 |
* The input $context contains the information needed to identify and invoke |
|
1486 |
* the context plugin and create the plugin context from that. |
|
1487 |
* |
|
1488 |
* @param array $context |
|
1071 | 1489 |
* The configuration of a context. It must contain the following data: |
1072 | 1490 |
* - name: The name of the context plugin being used. |
1073 | 1491 |
* - context_settings: The configuration based upon the plugin forms. |
1074 | 1492 |
* - identifier: The human readable identifier for this context, usually |
1075 | 1493 |
* defined by the UI. |
1076 | 1494 |
* - keyword: The keyword used for this context for substitutions. |
1077 |
* @param $type |
|
1495 |
* @param string $type
|
|
1078 | 1496 |
* This is either 'context' which indicates the context will be loaded |
1079 |
* from data in the settings, or 'required_context' which means the
|
|
1497 |
* from data in the settings, or 'requiredcontext' which means the |
|
1080 | 1498 |
* context must be acquired from an external source. This is the method |
1081 | 1499 |
* used to pass pure contexts from one system to another. |
1500 |
* @param mixed $argument |
|
1501 |
* Optional information passed to the plugin context via the arg defined in |
|
1502 |
* the plugin's "placeholder name" field. |
|
1082 | 1503 |
* |
1083 |
* @return |
|
1504 |
* @return ctools_context|null
|
|
1084 | 1505 |
* A context object if one can be loaded. |
1506 |
* |
|
1507 |
* @see ctools_get_context() |
|
1508 |
* @see ctools_plugin_get_function() |
|
1085 | 1509 |
*/ |
1086 | 1510 |
function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) { |
1087 | 1511 |
ctools_include('plugins'); |
1088 | 1512 |
$plugin = ctools_get_context($context['name']); |
1089 |
if ($function = ctools_plugin_get_function($plugin, 'context')) { |
|
1513 |
$function = ctools_plugin_get_function($plugin, 'context'); |
|
1514 |
if ($function) { |
|
1090 | 1515 |
// Backward compatibility: Merge old style settings into new style: |
1091 | 1516 |
if (!empty($context['context_settings'])) { |
1092 | 1517 |
$context += $context['context_settings']; |
... | ... | |
1101 | 1526 |
if ($return) { |
1102 | 1527 |
$return->identifier = $context['identifier']; |
1103 | 1528 |
$return->page_title = isset($context['title']) ? $context['title'] : ''; |
1104 |
$return->keyword = $context['keyword'];
|
|
1529 |
$return->keyword = $context['keyword']; |
|
1105 | 1530 |
|
1106 | 1531 |
if (!empty($context->empty)) { |
1107 | 1532 |
$context->placeholder = array( |
... | ... | |
1113 | 1538 |
return $return; |
1114 | 1539 |
} |
1115 | 1540 |
} |
1541 |
|
|
1542 |
return NULL; |
|
1116 | 1543 |
} |
1117 | 1544 |
|
1118 | 1545 |
/** |
... | ... | |
1126 | 1553 |
* Either 'context' or 'requiredcontext', which indicates whether the contexts |
1127 | 1554 |
* are loaded from internal data or copied from an external source. |
1128 | 1555 |
* @param $placeholders |
1129 |
* If true, placeholders are acceptable. |
|
1556 |
* If True, placeholders are acceptable. |
|
1557 |
* |
|
1558 |
* @return array |
|
1559 |
* Array of contexts, keyed by context ID. |
|
1130 | 1560 |
*/ |
1131 | 1561 |
function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) { |
1132 | 1562 |
$return = array(); |
... | ... | |
1145 | 1575 |
/** |
1146 | 1576 |
* Match up external contexts to our required contexts. |
1147 | 1577 |
* |
1148 |
* This function is used to create a list of contexts with proper |
|
1149 |
* IDs based upon a list of required contexts.
|
|
1578 |
* This function is used to create a list of contexts with proper IDs based
|
|
1579 |
* upon a list of required contexts. |
|
1150 | 1580 |
* |
1151 |
* These contexts passed in should match the numeric positions of the |
|
1152 |
* required contexts. The caller must ensure this has already happened
|
|
1153 |
* correctly as this function will not detect errors here.
|
|
1581 |
* These contexts passed in should match the numeric positions of the required
|
|
1582 |
* contexts. The caller must ensure this has already happened correctly as this
|
|
1583 |
* function will not detect errors here. |
|
1154 | 1584 |
* |
1155 | 1585 |
* @param $required |
1156 | 1586 |
* A list of required contexts as defined by the UI. |
1157 | 1587 |
* @param $contexts |
1158 | 1588 |
* A list of matching contexts as passed in from the calling system. |
1589 |
* |
|
1590 |
* @return array |
|
1591 |
* Array of contexts, keyed by context ID. |
|
1159 | 1592 |
*/ |
1160 | 1593 |
function ctools_context_match_required_contexts($required, $contexts) { |
1161 | 1594 |
$return = array(); |
... | ... | |
1167 | 1600 |
$context = clone array_shift($contexts); |
1168 | 1601 |
$context->identifier = $r['identifier']; |
1169 | 1602 |
$context->page_title = isset($r['title']) ? $r['title'] : ''; |
1170 |
$context->keyword = $r['keyword'];
|
|
1603 |
$context->keyword = $r['keyword']; |
|
1171 | 1604 |
$return[ctools_context_id($r, 'requiredcontext')] = $context; |
1172 | 1605 |
} |
1173 | 1606 |
|
... | ... | |
1179 | 1612 |
* |
1180 | 1613 |
* Not all of the types need to be supported by this object. |
1181 | 1614 |
* |
1182 |
* This function is not used to load contexts from external data, but may |
|
1183 |
* be used to load internal contexts and relationships. Otherwise it can also
|
|
1184 |
* be used to generate a full set of placeholders for UI purposes.
|
|
1615 |
* This function is not used to load contexts from external data, but may be
|
|
1616 |
* used to load internal contexts and relationships. Otherwise it can also be
|
|
1617 |
* used to generate a full set of placeholders for UI purposes. |
|
1185 | 1618 |
* |
1186 |
* @param $object |
|
1619 |
* @param object $object
|
|
1187 | 1620 |
* An object that contains some or all of the following variables: |
1188 | 1621 |
* |
1189 |
* - requiredcontexts: A list of UI configured contexts that are required |
|
1190 |
* from an external source. Since these require external data, they will |
|
1191 |
* only be added if $placeholders is set to TRUE, and empty contexts will |
|
1192 |
* be created. |
|
1193 |
* - arguments: A list of UI configured arguments that will create contexts. |
|
1194 |
* Since these require external data, they will only be added if $placeholders |
|
1195 |
* is set to TRUE. |
|
1196 |
* - contexts: A list of UI configured contexts that have no external source, |
|
1197 |
* and are essentially hardcoded. For example, these might configure a |
|
1198 |
* particular node or a particular taxonomy term. |
|
1199 |
* - relationships: A list of UI configured contexts to be derived from other |
|
1200 |
* contexts that already exist from other sources. For example, these might |
|
1201 |
* be used to get a user object from a node via the node author relationship. |
|
1202 |
* @param $placeholders |
|
1203 |
* If TRUE, this will generate placeholder objects for types this function |
|
1622 |
* - requiredcontexts: A list of UI configured contexts that are required |
|
1623 |
* from an external source. Since these require external data, they will |
|
1624 |
* only be added if $placeholders is set to TRUE, and empty contexts will |
|
1625 |
* be created. |
|
1626 |
* - arguments: A list of UI configured arguments that will create contexts. |
|
1627 |
* As these require external data, they will only be added if $placeholders |
|
1628 |
* is set to TRUE. |
|
1629 |
* - contexts: A list of UI configured contexts that have no external source, |
|
1630 |
* and are essentially hardcoded. For example, these might configure a |
|
1631 |
* particular node or a particular taxonomy term. |
|
1632 |
* - relationships: A list of UI configured contexts to be derived from other |
|
1633 |
* contexts that already exist from other sources. For example, these might |
|
1634 |
* be used to get a user object from a node via the node author |
|
1635 |
* relationship. |
|
1636 |
* @param bool $placeholders |
|
1637 |
* If True, this will generate placeholder objects for any types this function |
|
1204 | 1638 |
* cannot load. |
1205 |
* @param $contexts |
|
1639 |
* @param array $contexts
|
|
1206 | 1640 |
* An array of pre-existing contexts that will be part of the return value. |
1641 |
* |
|
1642 |
* @return array |
|
1643 |
* Merged output of all results of ctools_context_get_context_from_contexts(). |
|
1207 | 1644 |
*/ |
1208 | 1645 |
function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) { |
1209 | 1646 |
if (!empty($object->base_contexts)) { |
... | ... | |
1227 | 1664 |
$contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders); |
1228 | 1665 |
} |
1229 | 1666 |
|
1230 |
// add contexts from relationships
|
|
1667 |
// Add contexts from relationships.
|
|
1231 | 1668 |
if (!empty($object->relationships) && is_array($object->relationships)) { |
1232 | 1669 |
ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders); |
1233 | 1670 |
} |
... | ... | |
1246 | 1683 |
function ctools_context_get_form($contexts) { |
1247 | 1684 |
if (!empty($contexts)) { |
1248 | 1685 |
foreach ($contexts as $id => $context) { |
1249 |
// if a form shows its id as being a 'required context' that means the
|
|
1686 |
// If a form shows its id as being a 'required context' that means the
|
|
1250 | 1687 |
// the context is external to this display and does not count. |
1251 | 1688 |
if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') { |
1252 | 1689 |
return $context; |
... | ... | |
1265 | 1702 |
* The arguments. These will be acquired from $form_state['values'] and the |
1266 | 1703 |
* keys must match the context IDs. |
1267 | 1704 |
* |
1268 |
* @return |
|
1705 |
* @return array
|
|
1269 | 1706 |
* A new $contexts array containing the replaced contexts. Not all contexts |
1270 | 1707 |
* may be replaced if, for example, an argument was unable to be converted |
1271 | 1708 |
* into a context. |
... | ... | |
1284 | 1721 |
$new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]); |
1285 | 1722 |
} |
1286 | 1723 |
break; |
1724 |
|
|
1287 | 1725 |
case 'argument': |
1288 | 1726 |
if (isset($arguments[$cid]) && $arguments[$cid] !== '') { |
1289 | 1727 |
$argument = $context->placeholder['conf']; |
1290 | 1728 |
$new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]); |
1291 | 1729 |
} |
1292 | 1730 |
break; |
1731 |
|
|
1293 | 1732 |
case 'context': |
1294 | 1733 |
if (!empty($arguments[$cid])) { |
1295 | 1734 |
$context_info = $context->placeholder['conf']; |
Formats disponibles : Unified diff
Weekly update of contrib modules