1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains code related to the ctools system of 'context'.
|
6
|
*
|
7
|
* Context, originally from Panels, is a method of packaging objects into
|
8
|
* a more generic bundle and providing a plugin system so that a UI can
|
9
|
* take advantage of them. The idea is that the context objects
|
10
|
* represent 'the context' that a given operation (usually a page view)
|
11
|
* is operating in or on.
|
12
|
*
|
13
|
* For example, when viewing a page, the 'context' is a node object. When
|
14
|
* viewing a user, the 'context' is a user object. Contexts can also
|
15
|
* have related contexts. For example, when viewing a 'node' you may need
|
16
|
* to know something about the node author. Therefore, the node author
|
17
|
* is a related context.
|
18
|
*/
|
19
|
|
20
|
/**
|
21
|
* The context object is largely a wrapper around some other object, with
|
22
|
* an interface to finding out what is contained and getting to both
|
23
|
* the object and information about the object.
|
24
|
*
|
25
|
* Each context object has its own information, but some things are very
|
26
|
* common, such as titles, data, keywords, etc. In particulare, the 'type'
|
27
|
* of the context is important.
|
28
|
*/
|
29
|
class ctools_context {
|
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;
|
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.
|
107
|
}
|
108
|
|
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') {
|
128
|
return TRUE;
|
129
|
}
|
130
|
|
131
|
$a = is_array($type) ? $type : array($type);
|
132
|
$b = is_array($this->type) ? $this->type : array($this->type);
|
133
|
return (bool) array_intersect($a, $b);
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Return the argument.
|
138
|
*
|
139
|
* @return mixed
|
140
|
* The value of $argument.
|
141
|
*/
|
142
|
public function get_argument() {
|
143
|
return $this->argument;
|
144
|
}
|
145
|
|
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() {
|
155
|
if (!is_null($this->original_argument)) {
|
156
|
return $this->original_argument;
|
157
|
}
|
158
|
return $this->argument;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Return the keyword.
|
163
|
*
|
164
|
* @return mixed
|
165
|
* The value of $keyword.
|
166
|
*/
|
167
|
public function get_keyword() {
|
168
|
return $this->keyword;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Return the identifier.
|
173
|
*
|
174
|
* @return mixed
|
175
|
* The value of $identifier.
|
176
|
*/
|
177
|
public function get_identifier() {
|
178
|
return $this->identifier;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Return the title.
|
183
|
*
|
184
|
* @return mixed
|
185
|
* The value of $title.
|
186
|
*/
|
187
|
public function get_title() {
|
188
|
return $this->title;
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Return the page title.
|
193
|
*
|
194
|
* @return mixed
|
195
|
* The value of $page_title.
|
196
|
*/
|
197
|
public function get_page_title() {
|
198
|
return $this->page_title;
|
199
|
}
|
200
|
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* Used to create a method of comparing if a list of contexts
|
205
|
* match a required context type.
|
206
|
*/
|
207
|
class ctools_context_required {
|
208
|
/**
|
209
|
* @var array
|
210
|
* Keyword strings associated with the context.
|
211
|
*/
|
212
|
public $keywords;
|
213
|
|
214
|
/**
|
215
|
* If set, the title will be used in the selector to identify
|
216
|
* the context. This is very useful when multiple contexts
|
217
|
* are required to inform the user will be used for what.
|
218
|
*/
|
219
|
public $title;
|
220
|
|
221
|
/**
|
222
|
* Test to see if this context is required.
|
223
|
*/
|
224
|
public $required = TRUE;
|
225
|
|
226
|
/**
|
227
|
* If TRUE, skip the check in ctools_context_required::select()
|
228
|
* for contexts whose names may have changed.
|
229
|
*/
|
230
|
public $skip_name_check = FALSE;
|
231
|
|
232
|
/**
|
233
|
* The ctools_context_required constructor.
|
234
|
*
|
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
|
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.
|
250
|
*/
|
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.
|
255
|
$args = func_get_args();
|
256
|
$this->title = array_shift($args);
|
257
|
|
258
|
// If we have a boolean value at the end for $skip_name_check, store it.
|
259
|
if (is_bool(end($args))) {
|
260
|
$this->skip_name_check = array_pop($args);
|
261
|
}
|
262
|
|
263
|
// If we were given restrictions at the end, store them.
|
264
|
if (count($args) > 1 && is_array(end($args))) {
|
265
|
$this->restrictions = array_pop($args);
|
266
|
}
|
267
|
|
268
|
if (count($args) === 1) {
|
269
|
$args = array_shift($args);
|
270
|
}
|
271
|
$this->keywords = $args;
|
272
|
}
|
273
|
|
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) {
|
294
|
$result = array();
|
295
|
|
296
|
/**
|
297
|
* See which of these contexts are valid.
|
298
|
* @var ctools_context $context
|
299
|
*/
|
300
|
foreach ((array) $contexts as $cid => $context) {
|
301
|
if ($context->is_type($this->keywords)) {
|
302
|
|
303
|
// Compare to see if our contexts were met.
|
304
|
if (!empty($this->restrictions) && !empty($context->restrictions)) {
|
305
|
|
306
|
foreach ($this->restrictions as $key => $values) {
|
307
|
// If we have a restriction, the context must either not have that
|
308
|
// restriction listed, which means we simply don't know what it is,
|
309
|
// or there must be an intersection of the restricted values on
|
310
|
// both sides.
|
311
|
if (!is_array($values)) {
|
312
|
$values = array($values);
|
313
|
}
|
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.
|
319
|
continue 2;
|
320
|
}
|
321
|
}
|
322
|
}
|
323
|
// This context passes the filter.
|
324
|
$result[$cid] = $context;
|
325
|
}
|
326
|
}
|
327
|
|
328
|
return $result;
|
329
|
}
|
330
|
|
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.
|
353
|
if (!is_array($contexts)) {
|
354
|
if (is_object($contexts) && $contexts instanceof ctools_context) {
|
355
|
$contexts = array($contexts->id => $contexts);
|
356
|
}
|
357
|
else {
|
358
|
$contexts = array($contexts);
|
359
|
}
|
360
|
}
|
361
|
|
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
|
) {
|
368
|
$choices = $this->filter($contexts);
|
369
|
|
370
|
// If we got a hit, take the first one that matches.
|
371
|
if ($choices) {
|
372
|
$keys = array_keys($choices);
|
373
|
$context = reset($keys);
|
374
|
}
|
375
|
}
|
376
|
|
377
|
if (empty($context) || empty($contexts[$context])) {
|
378
|
return FALSE;
|
379
|
}
|
380
|
return $contexts[$context];
|
381
|
}
|
382
|
|
383
|
}
|
384
|
|
385
|
/**
|
386
|
* Used to compare to see if a list of contexts match an optional context. This
|
387
|
* can produce empty contexts to use as placeholders.
|
388
|
*/
|
389
|
class ctools_context_optional extends ctools_context_required {
|
390
|
|
391
|
/**
|
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.
|
401
|
*/
|
402
|
public function add_empty(&$contexts) {
|
403
|
$context = new ctools_context('any');
|
404
|
$context->title = t('No context');
|
405
|
$context->identifier = t('No context');
|
406
|
$contexts['empty'] = $context;
|
407
|
}
|
408
|
|
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
|
*/
|
432
|
$this->add_empty($contexts);
|
433
|
return parent::filter($contexts);
|
434
|
}
|
435
|
|
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
|
*/
|
457
|
$this->add_empty($contexts);
|
458
|
if (empty($context)) {
|
459
|
return $contexts['empty'];
|
460
|
}
|
461
|
|
462
|
$result = parent::select($contexts, $context);
|
463
|
|
464
|
// Don't flip out if it can't find the context; this is optional, put
|
465
|
// in an empty.
|
466
|
if ($result === FALSE) {
|
467
|
$result = $contexts['empty'];
|
468
|
}
|
469
|
return $result;
|
470
|
}
|
471
|
|
472
|
}
|
473
|
|
474
|
/**
|
475
|
* Return a keyed array of context that match the given 'required context'
|
476
|
* filters.
|
477
|
*
|
478
|
* Functions or systems that require contexts of a particular type provide a
|
479
|
* ctools_context_required or ctools_context_optional object. This function
|
480
|
* examines that object and an array of contexts to determine which contexts
|
481
|
* match the filter.
|
482
|
*
|
483
|
* Since multiple contexts can be required, this function will accept either
|
484
|
* an array of all required contexts, or just a single required context object.
|
485
|
*
|
486
|
* @param array $contexts
|
487
|
* A keyed array of all available contexts.
|
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.
|
491
|
*
|
492
|
* @return array
|
493
|
* A keyed array of contexts that match the filter.
|
494
|
*/
|
495
|
function ctools_context_filter($contexts, $required) {
|
496
|
if (is_array($required)) {
|
497
|
$result = array();
|
498
|
foreach ($required as $item) {
|
499
|
$result = array_merge($result, _ctools_context_filter($contexts, $item));
|
500
|
}
|
501
|
return $result;
|
502
|
}
|
503
|
|
504
|
return _ctools_context_filter($contexts, $required);
|
505
|
}
|
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
|
*/
|
522
|
function _ctools_context_filter($contexts, $required) {
|
523
|
$result = array();
|
524
|
|
525
|
if (is_object($required)) {
|
526
|
$result = $required->filter($contexts);
|
527
|
}
|
528
|
|
529
|
return $result;
|
530
|
}
|
531
|
|
532
|
/**
|
533
|
* Create a select box to choose possible contexts.
|
534
|
*
|
535
|
* This only creates a selector if there is actually a choice; if there
|
536
|
* is only one possible context, that one is silently assigned.
|
537
|
*
|
538
|
* If an array of required contexts is provided, one selector will be
|
539
|
* provided for each context.
|
540
|
*
|
541
|
* @param array $contexts
|
542
|
* A keyed array of all available contexts.
|
543
|
* @param array|ctools_context_required|ctools_context_optional $required
|
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.
|
550
|
*
|
551
|
* @return array
|
552
|
* A form element, or NULL if there are no contexts that satisfy the
|
553
|
* requirements.
|
554
|
*/
|
555
|
function ctools_context_selector($contexts, $required, $default) {
|
556
|
if (is_array($required)) {
|
557
|
$result = array('#tree' => TRUE);
|
558
|
$count = 1;
|
559
|
foreach ($required as $id => $item) {
|
560
|
$result[] = _ctools_context_selector(
|
561
|
$contexts, $item, isset($default[$id]) ? $default[$id] : '', $count++
|
562
|
);
|
563
|
}
|
564
|
return $result;
|
565
|
}
|
566
|
|
567
|
return _ctools_context_selector($contexts, $required, $default);
|
568
|
}
|
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
|
*/
|
591
|
function _ctools_context_selector($contexts, $required, $default, $num = 0) {
|
592
|
$filtered = ctools_context_filter($contexts, $required);
|
593
|
$count = count($filtered);
|
594
|
|
595
|
$form = array();
|
596
|
|
597
|
if ($count >= 1) {
|
598
|
// If there's more than one to choose from, create a select widget.
|
599
|
foreach ($filtered as $cid => $context) {
|
600
|
$options[$cid] = $context->get_identifier();
|
601
|
}
|
602
|
if (!empty($required->title)) {
|
603
|
$title = $required->title;
|
604
|
}
|
605
|
else {
|
606
|
$title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
|
607
|
}
|
608
|
|
609
|
$form = array(
|
610
|
'#type' => 'select',
|
611
|
'#options' => $options,
|
612
|
'#title' => $title,
|
613
|
'#default_value' => $default,
|
614
|
);
|
615
|
}
|
616
|
|
617
|
return $form;
|
618
|
}
|
619
|
|
620
|
/**
|
621
|
* Are there enough contexts for a plugin?
|
622
|
*
|
623
|
* Some plugins can have a 'required contexts' item which can either
|
624
|
* be a context requirement object or an array of them. When contexts
|
625
|
* are required, items that do not have enough contexts should not
|
626
|
* appear. This tests an item to see if it has enough contexts
|
627
|
* to actually appear.
|
628
|
*
|
629
|
* @param $contexts
|
630
|
* A keyed array of all available contexts.
|
631
|
* @param $required
|
632
|
* The required context object or array of objects.
|
633
|
*
|
634
|
* @return bool
|
635
|
* True if there are enough contexts, otherwise False.
|
636
|
*/
|
637
|
function ctools_context_match_requirements($contexts, $required) {
|
638
|
if (!is_array($required)) {
|
639
|
$required = array($required);
|
640
|
}
|
641
|
|
642
|
// Get the keys to avoid bugs in PHP 5.0.8 with keys and loops.
|
643
|
// And use it to remove optional contexts.
|
644
|
$keys = array_keys($required);
|
645
|
foreach ($keys as $key) {
|
646
|
if (empty($required[$key]->required)) {
|
647
|
unset($required[$key]);
|
648
|
}
|
649
|
}
|
650
|
|
651
|
$count = count($required);
|
652
|
return (count(ctools_context_filter($contexts, $required)) >= $count);
|
653
|
}
|
654
|
|
655
|
/**
|
656
|
* Create a select box to choose possible contexts.
|
657
|
*
|
658
|
* This only creates a selector if there is actually a choice; if there
|
659
|
* is only one possible context, that one is silently assigned.
|
660
|
*
|
661
|
* If an array of required contexts is provided, one selector will be
|
662
|
* provided for each context.
|
663
|
*
|
664
|
* @param $contexts
|
665
|
* A keyed array of all available contexts.
|
666
|
* @param $required
|
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.
|
673
|
*
|
674
|
* @return array
|
675
|
* A form element, or NULL if there are no contexts that satisfy the
|
676
|
* requirements.
|
677
|
*/
|
678
|
function ctools_context_converter_selector($contexts, $required, $default) {
|
679
|
if (is_array($required)) {
|
680
|
$result = array('#tree' => TRUE);
|
681
|
$count = 1;
|
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
|
);
|
687
|
}
|
688
|
return $result;
|
689
|
}
|
690
|
|
691
|
return _ctools_context_converter_selector($contexts, $required, $default);
|
692
|
}
|
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
|
*/
|
715
|
function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) {
|
716
|
$filtered = ctools_context_filter($contexts, $required);
|
717
|
$count = count($filtered);
|
718
|
|
719
|
if ($count > 1) {
|
720
|
// If there's more than one to choose from, create a select widget.
|
721
|
$options = array();
|
722
|
foreach ($filtered as $cid => $context) {
|
723
|
if ($context->type === 'any') {
|
724
|
$options[''] = t('No context');
|
725
|
continue;
|
726
|
}
|
727
|
$key = $context->get_identifier();
|
728
|
if ($converters = ctools_context_get_converters($cid . '.', $context)) {
|
729
|
$options[$key] = $converters;
|
730
|
}
|
731
|
}
|
732
|
if (empty($options)) {
|
733
|
return array(
|
734
|
'#type' => 'value',
|
735
|
'#value' => 'any',
|
736
|
);
|
737
|
}
|
738
|
if (!empty($required->title)) {
|
739
|
$title = $required->title;
|
740
|
}
|
741
|
else {
|
742
|
$title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
|
743
|
}
|
744
|
|
745
|
return array(
|
746
|
'#type' => 'select',
|
747
|
'#options' => $options,
|
748
|
'#title' => $title,
|
749
|
'#description' => t('Please choose which context and how you would like it converted.'),
|
750
|
'#default_value' => $default,
|
751
|
);
|
752
|
}
|
753
|
else {
|
754
|
// Not enough choices to need a selector, so don't make one.
|
755
|
return NULL;
|
756
|
}
|
757
|
}
|
758
|
|
759
|
/**
|
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.
|
769
|
*/
|
770
|
function ctools_context_get_converters($cid, $context) {
|
771
|
if (empty($context->plugin)) {
|
772
|
return array();
|
773
|
}
|
774
|
|
775
|
return _ctools_context_get_converters($cid, $context->plugin);
|
776
|
}
|
777
|
|
778
|
/**
|
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.
|
791
|
*/
|
792
|
function _ctools_context_get_converters($id, $plugin_name) {
|
793
|
$plugin = ctools_get_context($plugin_name);
|
794
|
if (empty($plugin['convert list'])) {
|
795
|
return array();
|
796
|
}
|
797
|
|
798
|
$converters = array();
|
799
|
if (is_array($plugin['convert list'])) {
|
800
|
$converters = $plugin['convert list'];
|
801
|
}
|
802
|
elseif ($function = ctools_plugin_get_function($plugin, 'convert list')) {
|
803
|
$converters = (array) $function($plugin);
|
804
|
}
|
805
|
|
806
|
foreach (module_implements('ctools_context_convert_list_alter') as $module) {
|
807
|
$function = $module . '_ctools_context_convert_list_alter';
|
808
|
$function($plugin, $converters);
|
809
|
}
|
810
|
|
811
|
// Now, change them all to include the plugin:
|
812
|
$return = array();
|
813
|
foreach ($converters as $key => $title) {
|
814
|
$return[$id . $key] = $title;
|
815
|
}
|
816
|
|
817
|
natcasesort($return);
|
818
|
return $return;
|
819
|
}
|
820
|
|
821
|
/**
|
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.
|
829
|
*/
|
830
|
function ctools_context_get_all_converters() {
|
831
|
$contexts = ctools_get_contexts();
|
832
|
$converters = array();
|
833
|
foreach ($contexts as $name => $context) {
|
834
|
if (empty($context['no required context ui'])) {
|
835
|
$context_converters = _ctools_context_get_converters($name . '.', $name);
|
836
|
if ($context_converters) {
|
837
|
$converters[$context['title']] = $context_converters;
|
838
|
}
|
839
|
}
|
840
|
}
|
841
|
|
842
|
return $converters;
|
843
|
}
|
844
|
|
845
|
/**
|
846
|
* Let the context convert an argument based upon the converter that was given.
|
847
|
*
|
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
|
855
|
* that use token module, of particular use is 'sanitize' => FALSE which can
|
856
|
* get raw tokens. This should ONLY be used in values that will later be
|
857
|
* treated as unsafe user input since these values are by themselves unsafe.
|
858
|
* It is particularly useful to get raw values from Field API.
|
859
|
*
|
860
|
* @return string|null
|
861
|
*/
|
862
|
function ctools_context_convert_context($context, $converter, $converter_options = array()) {
|
863
|
// Contexts without plugins might be optional placeholders.
|
864
|
if (empty($context->plugin)) {
|
865
|
return NULL;
|
866
|
}
|
867
|
|
868
|
$value = $context->argument;
|
869
|
$plugin = ctools_get_context($context->plugin);
|
870
|
if ($function = ctools_plugin_get_function($plugin, 'convert')) {
|
871
|
$value = $function($context, $converter, $converter_options);
|
872
|
}
|
873
|
|
874
|
foreach (module_implements('ctools_context_converter_alter') as $module) {
|
875
|
$function = $module . '_ctools_context_converter_alter';
|
876
|
$function($context, $converter, $value, $converter_options);
|
877
|
}
|
878
|
|
879
|
return $value;
|
880
|
}
|
881
|
|
882
|
/**
|
883
|
* Choose a context or contexts based upon the selection made via
|
884
|
* ctools_context_filter.
|
885
|
*
|
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.
|
890
|
* @param $context
|
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).
|
897
|
*/
|
898
|
function ctools_context_select($contexts, $required, $context) {
|
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
|
*/
|
907
|
$result = array();
|
908
|
foreach ($required as $id => $item) {
|
909
|
// @todo What's the difference between the following and "empty($item)" ?
|
910
|
if (empty($required[$id])) {
|
911
|
continue;
|
912
|
}
|
913
|
|
914
|
if (($result[] = _ctools_context_select($contexts, $item, $context[$id])) === FALSE) {
|
915
|
return FALSE;
|
916
|
}
|
917
|
}
|
918
|
return $result;
|
919
|
}
|
920
|
|
921
|
return _ctools_context_select($contexts, $required, $context);
|
922
|
}
|
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
|
*/
|
940
|
function _ctools_context_select($contexts, $required, $context) {
|
941
|
if (!is_object($required)) {
|
942
|
return FALSE;
|
943
|
}
|
944
|
|
945
|
return $required->select($contexts, $context);
|
946
|
}
|
947
|
|
948
|
/**
|
949
|
* Create a new context object.
|
950
|
*
|
951
|
* @param string $type
|
952
|
* The type of context to create; this loads a plugin.
|
953
|
* @param mixed $data
|
954
|
* The data to put into the context.
|
955
|
* @param $conf
|
956
|
* A configuration structure if this context was created via UI.
|
957
|
*
|
958
|
* @return ctools_context
|
959
|
* A $context or NULL if one could not be created.
|
960
|
*/
|
961
|
function ctools_context_create($type, $data = NULL, $conf = FALSE) {
|
962
|
ctools_include('plugins');
|
963
|
$plugin = ctools_get_context($type);
|
964
|
|
965
|
if ($function = ctools_plugin_get_function($plugin, 'context')) {
|
966
|
return $function(FALSE, $data, $conf, $plugin);
|
967
|
}
|
968
|
}
|
969
|
|
970
|
/**
|
971
|
* Create an empty context object.
|
972
|
*
|
973
|
* Empty context objects are primarily used as placeholders in the UI where
|
974
|
* the actual contents of a context object may not be known. It may have
|
975
|
* additional text embedded to give the user clues as to how the context
|
976
|
* is used.
|
977
|
*
|
978
|
* @param $type
|
979
|
* The type of context to create; this loads a plugin.
|
980
|
*
|
981
|
* @return ctools_context
|
982
|
* A $context or NULL if one could not be created.
|
983
|
*/
|
984
|
function ctools_context_create_empty($type) {
|
985
|
$plugin = ctools_get_context($type);
|
986
|
if ($function = ctools_plugin_get_function($plugin, 'context')) {
|
987
|
$context = $function(TRUE, NULL, FALSE, $plugin);
|
988
|
if (is_object($context)) {
|
989
|
$context->empty = TRUE;
|
990
|
}
|
991
|
|
992
|
return $context;
|
993
|
}
|
994
|
}
|
995
|
|
996
|
/**
|
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.
|
1011
|
*/
|
1012
|
function ctools_context_keyword_substitute($string, $keywords, $contexts, array $converter_options = array()) {
|
1013
|
// Ensure a default keyword exists:
|
1014
|
$keywords['%%'] = '%';
|
1015
|
|
1016
|
// Match contexts to the base keywords:
|
1017
|
$context_keywords = array();
|
1018
|
foreach ($contexts as $context) {
|
1019
|
if (isset($context->keyword)) {
|
1020
|
$context_keywords[$context->keyword] = $context;
|
1021
|
}
|
1022
|
}
|
1023
|
|
1024
|
// Look for context matches we we only have to convert known matches.
|
1025
|
$matches = array();
|
1026
|
if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) {
|
1027
|
foreach ($matches[1] as $keyword) {
|
1028
|
// Ignore anything it finds with %%.
|
1029
|
if ($keyword[0] == '%') {
|
1030
|
continue;
|
1031
|
}
|
1032
|
|
1033
|
// If the keyword is already set by something passed in, don't try to
|
1034
|
// overwrite it.
|
1035
|
if (array_key_exists('%' . $keyword, $keywords)) {
|
1036
|
continue;
|
1037
|
}
|
1038
|
|
1039
|
// Figure out our keyword and converter, if specified.
|
1040
|
if (strpos($keyword, ':')) {
|
1041
|
list($context, $converter) = explode(':', $keyword, 2);
|
1042
|
}
|
1043
|
else {
|
1044
|
$context = $keyword;
|
1045
|
if (isset($context_keywords[$keyword])) {
|
1046
|
$plugin = ctools_get_context($context_keywords[$context]->plugin);
|
1047
|
|
1048
|
// Fall back to a default converter, if specified.
|
1049
|
if ($plugin && !empty($plugin['convert default'])) {
|
1050
|
$converter = $plugin['convert default'];
|
1051
|
}
|
1052
|
}
|
1053
|
}
|
1054
|
|
1055
|
if (!isset($context_keywords[$context])) {
|
1056
|
$keywords['%' . $keyword] = '%' . $keyword;
|
1057
|
}
|
1058
|
else {
|
1059
|
if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
|
1060
|
$keywords['%' . $keyword] = '';
|
1061
|
}
|
1062
|
else {
|
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
|
}
|
1069
|
}
|
1070
|
}
|
1071
|
}
|
1072
|
}
|
1073
|
return strtr($string, $keywords);
|
1074
|
}
|
1075
|
|
1076
|
/**
|
1077
|
* Determine a unique context ID for a context.
|
1078
|
*
|
1079
|
* Often contexts of many different types will be placed into a list. This
|
1080
|
* ensures that even though contexts of multiple types may share IDs, they
|
1081
|
* are unique in the final list.
|
1082
|
*/
|
1083
|
function ctools_context_id($context, $type = 'context') {
|
1084
|
// If not set, FALSE or empty.
|
1085
|
if (!isset($context['id']) || !$context['id']) {
|
1086
|
$context['id'] = 1;
|
1087
|
}
|
1088
|
|
1089
|
// @todo is '' the appropriate default value?
|
1090
|
$name = isset($context['name']) ? $context['name'] : '';
|
1091
|
|
1092
|
return $type . '_' . $name . '_' . $context['id'];
|
1093
|
}
|
1094
|
|
1095
|
/**
|
1096
|
* Get the next id available given a list of already existing objects.
|
1097
|
*
|
1098
|
* This finds the next id available for the named object.
|
1099
|
*
|
1100
|
* @param array $objects
|
1101
|
* A list of context descriptor objects, i.e, arguments, relationships,
|
1102
|
* contexts, etc.
|
1103
|
* @param string $name
|
1104
|
* The name being used.
|
1105
|
*
|
1106
|
* @return int
|
1107
|
* The next integer id available.
|
1108
|
*/
|
1109
|
function ctools_context_next_id($objects, $name) {
|
1110
|
$id = 0;
|
1111
|
// Figure out which instance of this argument we're creating.
|
1112
|
if (!$objects) {
|
1113
|
return $id + 1;
|
1114
|
}
|
1115
|
|
1116
|
foreach ($objects as $object) {
|
1117
|
if (isset($object['name']) && $object['name'] === $name) {
|
1118
|
if (isset($object['id']) && $object['id'] > $id) {
|
1119
|
$id = $object['id'];
|
1120
|
}
|
1121
|
// @todo If obj has no 'id', should we increment local id? $id = $id + 1;
|
1122
|
}
|
1123
|
}
|
1124
|
|
1125
|
return $id + 1;
|
1126
|
}
|
1127
|
|
1128
|
// ---------------------------------------------------------------------------
|
1129
|
// Functions related to contexts from arguments.
|
1130
|
/**
|
1131
|
* Fetch metadata for a specific argument plugin.
|
1132
|
*
|
1133
|
* @param $argument
|
1134
|
* Name of an argument plugin.
|
1135
|
*
|
1136
|
* @return array
|
1137
|
* An array with information about the requested argument plugin.
|
1138
|
*/
|
1139
|
|
1140
|
function ctools_get_argument($argument) {
|
1141
|
ctools_include('plugins');
|
1142
|
return ctools_get_plugins('ctools', 'arguments', $argument);
|
1143
|
}
|
1144
|
|
1145
|
/**
|
1146
|
* Fetch metadata for all argument plugins.
|
1147
|
*
|
1148
|
* @return array
|
1149
|
* An array of arrays with information about all available argument plugins.
|
1150
|
*/
|
1151
|
function ctools_get_arguments() {
|
1152
|
ctools_include('plugins');
|
1153
|
return ctools_get_plugins('ctools', 'arguments');
|
1154
|
}
|
1155
|
|
1156
|
/**
|
1157
|
* Get a context from an argument.
|
1158
|
*
|
1159
|
* @param $argument
|
1160
|
* The configuration of an argument. It must contain the following data:
|
1161
|
* - name: The name of the argument plugin being used.
|
1162
|
* - argument_settings: The configuration based upon the plugin forms.
|
1163
|
* - identifier: The human readable identifier for this argument, usually
|
1164
|
* defined by the UI.
|
1165
|
* - keyword: The keyword used for this argument for substitutions.
|
1166
|
*
|
1167
|
* @param $arg
|
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.
|
1170
|
* @param $empty
|
1171
|
* If true, the $arg will not be used to load the context. Instead, an empty
|
1172
|
* placeholder context will be loaded.
|
1173
|
*
|
1174
|
* @return ctools_context
|
1175
|
* A context object if one can be loaded.
|
1176
|
*/
|
1177
|
function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) {
|
1178
|
ctools_include('plugins');
|
1179
|
if (empty($argument['name'])) {
|
1180
|
return NULL;
|
1181
|
}
|
1182
|
|
1183
|
$function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context');
|
1184
|
if ($function) {
|
1185
|
// Backward compatibility: Merge old style settings into new style:
|
1186
|
if (!empty($argument['settings'])) {
|
1187
|
$argument += $argument['settings'];
|
1188
|
unset($argument['settings']);
|
1189
|
}
|
1190
|
|
1191
|
$context = $function($arg, $argument, $empty);
|
1192
|
|
1193
|
if (is_object($context)) {
|
1194
|
$context->identifier = $argument['identifier'];
|
1195
|
$context->page_title = isset($argument['title']) ? $argument['title'] : '';
|
1196
|
$context->keyword = $argument['keyword'];
|
1197
|
$context->id = ctools_context_id($argument, 'argument');
|
1198
|
$context->original_argument = $arg;
|
1199
|
|
1200
|
if (!empty($context->empty)) {
|
1201
|
$context->placeholder = array(
|
1202
|
'type' => 'argument',
|
1203
|
'conf' => $argument,
|
1204
|
);
|
1205
|
}
|
1206
|
}
|
1207
|
return $context;
|
1208
|
}
|
1209
|
}
|
1210
|
|
1211
|
/**
|
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()
|
1219
|
*/
|
1220
|
function ctools_context_get_placeholders_from_argument($arguments) {
|
1221
|
$contexts = array();
|
1222
|
foreach ($arguments as $argument) {
|
1223
|
$context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
|
1224
|
if ($context) {
|
1225
|
$contexts[ctools_context_id($argument, 'argument')] = $context;
|
1226
|
}
|
1227
|
}
|
1228
|
return $contexts;
|
1229
|
}
|
1230
|
|
1231
|
/**
|
1232
|
* Load the contexts for a given list of arguments.
|
1233
|
*
|
1234
|
* @param array $arguments
|
1235
|
* The array of argument definitions.
|
1236
|
* @param array &$contexts
|
1237
|
* The array of existing contexts. New contexts will be added to this array.
|
1238
|
* @param array $args
|
1239
|
* The arguments to load.
|
1240
|
*
|
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()
|
1245
|
*/
|
1246
|
function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) {
|
1247
|
foreach ($arguments as $argument) {
|
1248
|
// Pull the argument off the list.
|
1249
|
$arg = array_shift($args);
|
1250
|
$id = ctools_context_id($argument, 'argument');
|
1251
|
|
1252
|
// For % arguments embedded in the URL, our context is already loaded.
|
1253
|
// There is no need to go and load it again.
|
1254
|
if (empty($contexts[$id])) {
|
1255
|
if ($context = ctools_context_get_context_from_argument($argument, $arg)) {
|
1256
|
$contexts[$id] = $context;
|
1257
|
}
|
1258
|
}
|
1259
|
else {
|
1260
|
$context = $contexts[$id];
|
1261
|
}
|
1262
|
|
1263
|
if ((empty($context) || empty($context->data))
|
1264
|
&& !empty($argument['default'])
|
1265
|
&& $argument['default'] === '404'
|
1266
|
) {
|
1267
|
return FALSE;
|
1268
|
}
|
1269
|
}
|
1270
|
return TRUE;
|
1271
|
}
|
1272
|
|
1273
|
// ---------------------------------------------------------------------------
|
1274
|
// Functions related to contexts from relationships.
|
1275
|
/**
|
1276
|
* Fetch plugin metadata for a specific relationship plugin.
|
1277
|
*
|
1278
|
* @param $relationship
|
1279
|
* Name of a panel content type.
|
1280
|
*
|
1281
|
* @return array
|
1282
|
* An array with information about the requested relationship.
|
1283
|
*
|
1284
|
* @see ctools_get_relationships()
|
1285
|
*/
|
1286
|
|
1287
|
function ctools_get_relationship($relationship) {
|
1288
|
ctools_include('plugins');
|
1289
|
return ctools_get_plugins('ctools', 'relationships', $relationship);
|
1290
|
}
|
1291
|
|
1292
|
/**
|
1293
|
* Fetch metadata for all relationship plugins.
|
1294
|
*
|
1295
|
* @return array
|
1296
|
* An array of arrays with information about all available relationships.
|
1297
|
*
|
1298
|
* @see ctools_get_relationship()
|
1299
|
*/
|
1300
|
function ctools_get_relationships() {
|
1301
|
ctools_include('plugins');
|
1302
|
return ctools_get_plugins('ctools', 'relationships');
|
1303
|
}
|
1304
|
|
1305
|
/**
|
1306
|
* Return a context from a relationship.
|
1307
|
*
|
1308
|
* @param array $relationship
|
1309
|
* The configuration of a relationship. It must contain the following data:
|
1310
|
* - name: The name of the relationship plugin being used.
|
1311
|
* - relationship_settings: The configuration based upon the plugin forms.
|
1312
|
* - identifier: The human readable identifier for this relationship, usually
|
1313
|
* defined by the UI.
|
1314
|
* - keyword: The keyword used for this relationship for substitutions.
|
1315
|
*
|
1316
|
* @param ctools_context $source_context
|
1317
|
* The context this relationship is based upon.
|
1318
|
* @param bool $placeholders
|
1319
|
* If TRUE, placeholders are acceptable.
|
1320
|
*
|
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()
|
1326
|
*/
|
1327
|
function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) {
|
1328
|
ctools_include('plugins');
|
1329
|
$function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context');
|
1330
|
if ($function) {
|
1331
|
// Backward compatibility: Merge old style settings into new style:
|
1332
|
if (!empty($relationship['relationship_settings'])) {
|
1333
|
$relationship += $relationship['relationship_settings'];
|
1334
|
unset($relationship['relationship_settings']);
|
1335
|
}
|
1336
|
|
1337
|
$context = $function($source_context, $relationship, $placeholders);
|
1338
|
if ($context) {
|
1339
|
$context->identifier = $relationship['identifier'];
|
1340
|
$context->page_title = isset($relationship['title']) ? $relationship['title'] : '';
|
1341
|
$context->keyword = $relationship['keyword'];
|
1342
|
if (!empty($context->empty)) {
|
1343
|
$context->placeholder = array(
|
1344
|
'type' => 'relationship',
|
1345
|
'conf' => $relationship,
|
1346
|
);
|
1347
|
}
|
1348
|
return $context;
|
1349
|
}
|
1350
|
}
|
1351
|
return NULL;
|
1352
|
}
|
1353
|
|
1354
|
/**
|
1355
|
* Fetch all relevant relationships.
|
1356
|
*
|
1357
|
* Relevant relationships are any relationship that can be created based upon
|
1358
|
* the list of existing contexts. For example, the 'node author' relationship
|
1359
|
* is relevant if there is a 'node' context, but makes no sense if there is
|
1360
|
* not one.
|
1361
|
*
|
1362
|
* @param $contexts
|
1363
|
* An array of contexts used to figure out which relationships are relevant.
|
1364
|
*
|
1365
|
* @return array
|
1366
|
* An array of relationship keys that are relevant for the given set of
|
1367
|
* contexts.
|
1368
|
*
|
1369
|
* @see ctools_context_filter()
|
1370
|
* @see ctools_context_get_context_from_relationship()
|
1371
|
* @see ctools_context_get_context_from_relationships()
|
1372
|
*/
|
1373
|
function ctools_context_get_relevant_relationships($contexts) {
|
1374
|
$relevant = array();
|
1375
|
$relationships = ctools_get_relationships();
|
1376
|
|
1377
|
// Go through each relationship.
|
1378
|
foreach ($relationships as $rid => $relationship) {
|
1379
|
// For each relationship, see if there is a context that satisfies it.
|
1380
|
if (empty($relationship['no ui'])
|
1381
|
&& ctools_context_filter($contexts, $relationship['required context'])
|
1382
|
) {
|
1383
|
$relevant[$rid] = $relationship['title'];
|
1384
|
}
|
1385
|
}
|
1386
|
|
1387
|
return $relevant;
|
1388
|
}
|
1389
|
|
1390
|
/**
|
1391
|
* Fetch all active relationships.
|
1392
|
*
|
1393
|
* @param $relationships
|
1394
|
* An keyed array of relationship data including:
|
1395
|
* - name: name of relationship
|
1396
|
* - context: context id relationship belongs to. This will be used to
|
1397
|
* identify which context in the $contexts array to use to create the
|
1398
|
* relationship context.
|
1399
|
*
|
1400
|
* @param $contexts
|
1401
|
* A keyed array of contexts used to figure out which relationships
|
1402
|
* are relevant. New contexts will be added to this.
|
1403
|
*
|
1404
|
* @param $placeholders
|
1405
|
* If TRUE, placeholders are acceptable.
|
1406
|
*
|
1407
|
* @see ctools_context_get_context_from_relationship()
|
1408
|
* @see ctools_context_get_relevant_relationships()
|
1409
|
*/
|
1410
|
function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) {
|
1411
|
foreach ($relationships as $rdata) {
|
1412
|
if (!isset($rdata['context'])) {
|
1413
|
continue;
|
1414
|
}
|
1415
|
|
1416
|
if (is_array($rdata['context'])) {
|
1417
|
$rcontexts = array();
|
1418
|
foreach ($rdata['context'] as $cid) {
|
1419
|
if (!empty($contexts[$cid])) {
|
1420
|
$rcontexts[] = $contexts[$cid];
|
1421
|
}
|
1422
|
}
|
1423
|
}
|
1424
|
elseif (!empty($contexts[$rdata['context']])) {
|
1425
|
$rcontexts = $contexts[$rdata['context']];
|
1426
|
}
|
1427
|
|
1428
|
$cid = ctools_context_id($rdata, 'relationship');
|
1429
|
if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) {
|
1430
|
$contexts[$cid] = $context;
|
1431
|
}
|
1432
|
}
|
1433
|
}
|
1434
|
|
1435
|
// ---------------------------------------------------------------------------
|
1436
|
// Functions related to loading contexts from simple context definitions.
|
1437
|
/**
|
1438
|
* Fetch metadata on a specific context plugin.
|
1439
|
*
|
1440
|
* @param string $context
|
1441
|
* Name of a context.
|
1442
|
*
|
1443
|
* @return array
|
1444
|
* An array with information about the requested panel context.
|
1445
|
*/
|
1446
|
|
1447
|
function ctools_get_context($context) {
|
1448
|
static $gate = array();
|
1449
|
ctools_include('plugins');
|
1450
|
$plugin = ctools_get_plugins('ctools', 'contexts', $context);
|
1451
|
if (empty($gate['context']) && !empty($plugin['superceded by'])) {
|
1452
|
// This gate prevents infinite loops.
|
1453
|
$gate[$context] = TRUE;
|
1454
|
$new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']);
|
1455
|
$gate[$context] = FALSE;
|
1456
|
|
1457
|
// If a new plugin was returned, return it. Otherwise fall through and
|
1458
|
// return the original we fetched.
|
1459
|
if ($new_plugin) {
|
1460
|
return $new_plugin;
|
1461
|
}
|
1462
|
}
|
1463
|
|
1464
|
return $plugin;
|
1465
|
}
|
1466
|
|
1467
|
/**
|
1468
|
* Fetch metadata for all context plugins.
|
1469
|
*
|
1470
|
* @return array
|
1471
|
* An array of arrays with information about all available panel contexts.
|
1472
|
*/
|
1473
|
function ctools_get_contexts() {
|
1474
|
ctools_include('plugins');
|
1475
|
return ctools_get_plugins('ctools', 'contexts');
|
1476
|
}
|
1477
|
|
1478
|
/**
|
1479
|
* Return a context object from a context definition array.
|
1480
|
*
|
1481
|
* The input $context contains the information needed to identify and invoke
|
1482
|
* the context plugin and create the plugin context from that.
|
1483
|
*
|
1484
|
* @param array $context
|
1485
|
* The configuration of a context. It must contain the following data:
|
1486
|
* - name: The name of the context plugin being used.
|
1487
|
* - context_settings: The configuration based upon the plugin forms.
|
1488
|
* - identifier: The human readable identifier for this context, usually
|
1489
|
* defined by the UI.
|
1490
|
* - keyword: The keyword used for this context for substitutions.
|
1491
|
* @param string $type
|
1492
|
* This is either 'context' which indicates the context will be loaded
|
1493
|
* from data in the settings, or 'requiredcontext' which means the
|
1494
|
* context must be acquired from an external source. This is the method
|
1495
|
* used to pass pure contexts from one system to another.
|
1496
|
* @param mixed $argument
|
1497
|
* Optional information passed to the plugin context via the arg defined in
|
1498
|
* the plugin's "placeholder name" field.
|
1499
|
*
|
1500
|
* @return ctools_context|null
|
1501
|
* A context object if one can be loaded.
|
1502
|
*
|
1503
|
* @see ctools_get_context()
|
1504
|
* @see ctools_plugin_get_function()
|
1505
|
*/
|
1506
|
function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) {
|
1507
|
ctools_include('plugins');
|
1508
|
$plugin = ctools_get_context($context['name']);
|
1509
|
$function = ctools_plugin_get_function($plugin, 'context');
|
1510
|
if ($function) {
|
1511
|
// Backward compatibility: Merge old style settings into new style:
|
1512
|
if (!empty($context['context_settings'])) {
|
1513
|
$context += $context['context_settings'];
|
1514
|
unset($context['context_settings']);
|
1515
|
}
|
1516
|
|
1517
|
if (isset($argument) && isset($plugin['placeholder name'])) {
|
1518
|
$context[$plugin['placeholder name']] = $argument;
|
1519
|
}
|
1520
|
|
1521
|
$return = $function($type == 'requiredcontext', $context, TRUE, $plugin);
|
1522
|
if ($return) {
|
1523
|
$return->identifier = $context['identifier'];
|
1524
|
$return->page_title = isset($context['title']) ? $context['title'] : '';
|
1525
|
$return->keyword = $context['keyword'];
|
1526
|
|
1527
|
if (!empty($context->empty)) {
|
1528
|
$context->placeholder = array(
|
1529
|
'type' => 'context',
|
1530
|
'conf' => $context,
|
1531
|
);
|
1532
|
}
|
1533
|
|
1534
|
return $return;
|
1535
|
}
|
1536
|
}
|
1537
|
|
1538
|
return NULL;
|
1539
|
}
|
1540
|
|
1541
|
/**
|
1542
|
* Retrieve a list of base contexts based upon a simple 'contexts' definition.
|
1543
|
*
|
1544
|
* For required contexts this will always retrieve placeholders.
|
1545
|
*
|
1546
|
* @param $contexts
|
1547
|
* The list of contexts defined in the UI.
|
1548
|
* @param $type
|
1549
|
* Either 'context' or 'requiredcontext', which indicates whether the contexts
|
1550
|
* are loaded from internal data or copied from an external source.
|
1551
|
* @param $placeholders
|
1552
|
* If True, placeholders are acceptable.
|
1553
|
*
|
1554
|
* @return array
|
1555
|
* Array of contexts, keyed by context ID.
|
1556
|
*/
|
1557
|
function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) {
|
1558
|
$return = array();
|
1559
|
foreach ($contexts as $context) {
|
1560
|
$ctext = ctools_context_get_context_from_context($context, $type);
|
1561
|
if ($ctext) {
|
1562
|
if ($placeholders) {
|
1563
|
$ctext->placeholder = TRUE;
|
1564
|
}
|
1565
|
$return[ctools_context_id($context, $type)] = $ctext;
|
1566
|
}
|
1567
|
}
|
1568
|
return $return;
|
1569
|
}
|
1570
|
|
1571
|
/**
|
1572
|
* Match up external contexts to our required contexts.
|
1573
|
*
|
1574
|
* This function is used to create a list of contexts with proper IDs based
|
1575
|
* upon a list of required contexts.
|
1576
|
*
|
1577
|
* These contexts passed in should match the numeric positions of the required
|
1578
|
* contexts. The caller must ensure this has already happened correctly as this
|
1579
|
* function will not detect errors here.
|
1580
|
*
|
1581
|
* @param $required
|
1582
|
* A list of required contexts as defined by the UI.
|
1583
|
* @param $contexts
|
1584
|
* A list of matching contexts as passed in from the calling system.
|
1585
|
*
|
1586
|
* @return array
|
1587
|
* Array of contexts, keyed by context ID.
|
1588
|
*/
|
1589
|
function ctools_context_match_required_contexts($required, $contexts) {
|
1590
|
$return = array();
|
1591
|
if (!is_array($required)) {
|
1592
|
return $return;
|
1593
|
}
|
1594
|
|
1595
|
foreach ($required as $r) {
|
1596
|
$context = clone array_shift($contexts);
|
1597
|
$context->identifier = $r['identifier'];
|
1598
|
$context->page_title = isset($r['title']) ? $r['title'] : '';
|
1599
|
$context->keyword = $r['keyword'];
|
1600
|
$return[ctools_context_id($r, 'requiredcontext')] = $context;
|
1601
|
}
|
1602
|
|
1603
|
return $return;
|
1604
|
}
|
1605
|
|
1606
|
/**
|
1607
|
* Load a full array of contexts for an object.
|
1608
|
*
|
1609
|
* Not all of the types need to be supported by this object.
|
1610
|
*
|
1611
|
* This function is not used to load contexts from external data, but may be
|
1612
|
* used to load internal contexts and relationships. Otherwise it can also be
|
1613
|
* used to generate a full set of placeholders for UI purposes.
|
1614
|
*
|
1615
|
* @param object $object
|
1616
|
* An object that contains some or all of the following variables:
|
1617
|
*
|
1618
|
* - requiredcontexts: A list of UI configured contexts that are required
|
1619
|
* from an external source. Since these require external data, they will
|
1620
|
* only be added if $placeholders is set to TRUE, and empty contexts will
|
1621
|
* be created.
|
1622
|
* - arguments: A list of UI configured arguments that will create contexts.
|
1623
|
* As these require external data, they will only be added if $placeholders
|
1624
|
* is set to TRUE.
|
1625
|
* - contexts: A list of UI configured contexts that have no external source,
|
1626
|
* and are essentially hardcoded. For example, these might configure a
|
1627
|
* particular node or a particular taxonomy term.
|
1628
|
* - relationships: A list of UI configured contexts to be derived from other
|
1629
|
* contexts that already exist from other sources. For example, these might
|
1630
|
* be used to get a user object from a node via the node author
|
1631
|
* relationship.
|
1632
|
* @param bool $placeholders
|
1633
|
* If True, this will generate placeholder objects for any types this function
|
1634
|
* cannot load.
|
1635
|
* @param array $contexts
|
1636
|
* An array of pre-existing contexts that will be part of the return value.
|
1637
|
*
|
1638
|
* @return array
|
1639
|
* Merged output of all results of ctools_context_get_context_from_contexts().
|
1640
|
*/
|
1641
|
function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) {
|
1642
|
if (!empty($object->base_contexts)) {
|
1643
|
$contexts += $object->base_contexts;
|
1644
|
}
|
1645
|
|
1646
|
if ($placeholders) {
|
1647
|
// This will load empty contexts as placeholders for arguments that come
|
1648
|
// from external sources. If this isn't set, it's assumed these context
|
1649
|
// will already have been matched up and loaded.
|
1650
|
if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) {
|
1651
|
$contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders);
|
1652
|
}
|
1653
|
|
1654
|
if (!empty($object->arguments) && is_array($object->arguments)) {
|
1655
|
$contexts += ctools_context_get_placeholders_from_argument($object->arguments);
|
1656
|
}
|
1657
|
}
|
1658
|
|
1659
|
if (!empty($object->contexts) && is_array($object->contexts)) {
|
1660
|
$contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders);
|
1661
|
}
|
1662
|
|
1663
|
// Add contexts from relationships.
|
1664
|
if (!empty($object->relationships) && is_array($object->relationships)) {
|
1665
|
ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders);
|
1666
|
}
|
1667
|
|
1668
|
return $contexts;
|
1669
|
}
|
1670
|
|
1671
|
/**
|
1672
|
* Return the first context with a form id from a list of contexts.
|
1673
|
*
|
1674
|
* This function is used to figure out which contexts represents 'the form'
|
1675
|
* from a list of contexts. Only one contexts can actually be 'the form' for
|
1676
|
* a given page, since the @code{<form>} tag can not be embedded within
|
1677
|
* itself.
|
1678
|
*/
|
1679
|
function ctools_context_get_form($contexts) {
|
1680
|
if (!empty($contexts)) {
|
1681
|
foreach ($contexts as $id => $context) {
|
1682
|
// If a form shows its id as being a 'required context' that means the
|
1683
|
// the context is external to this display and does not count.
|
1684
|
if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') {
|
1685
|
return $context;
|
1686
|
}
|
1687
|
}
|
1688
|
}
|
1689
|
}
|
1690
|
|
1691
|
/**
|
1692
|
* Replace placeholders with real contexts using data extracted from a form
|
1693
|
* for the purposes of previews.
|
1694
|
*
|
1695
|
* @param $contexts
|
1696
|
* All of the contexts, including the placeholders.
|
1697
|
* @param $arguments
|
1698
|
* The arguments. These will be acquired from $form_state['values'] and the
|
1699
|
* keys must match the context IDs.
|
1700
|
*
|
1701
|
* @return array
|
1702
|
* A new $contexts array containing the replaced contexts. Not all contexts
|
1703
|
* may be replaced if, for example, an argument was unable to be converted
|
1704
|
* into a context.
|
1705
|
*/
|
1706
|
function ctools_context_replace_placeholders($contexts, $arguments) {
|
1707
|
foreach ($contexts as $cid => $context) {
|
1708
|
if (empty($context->empty)) {
|
1709
|
continue;
|
1710
|
}
|
1711
|
|
1712
|
$new_context = NULL;
|
1713
|
switch ($context->placeholder['type']) {
|
1714
|
case 'relationship':
|
1715
|
$relationship = $context->placeholder['conf'];
|
1716
|
if (isset($contexts[$relationship['context']])) {
|
1717
|
$new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]);
|
1718
|
}
|
1719
|
break;
|
1720
|
|
1721
|
case 'argument':
|
1722
|
if (isset($arguments[$cid]) && $arguments[$cid] !== '') {
|
1723
|
$argument = $context->placeholder['conf'];
|
1724
|
$new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]);
|
1725
|
}
|
1726
|
break;
|
1727
|
|
1728
|
case 'context':
|
1729
|
if (!empty($arguments[$cid])) {
|
1730
|
$context_info = $context->placeholder['conf'];
|
1731
|
$new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]);
|
1732
|
}
|
1733
|
break;
|
1734
|
}
|
1735
|
|
1736
|
if ($new_context && empty($new_context->empty)) {
|
1737
|
$contexts[$cid] = $new_context;
|
1738
|
}
|
1739
|
}
|
1740
|
|
1741
|
return $contexts;
|
1742
|
}
|
1743
|
|
1744
|
/**
|
1745
|
* Provide a form array for getting data to replace placeholder contexts
|
1746
|
* with real data.
|
1747
|
*/
|
1748
|
function ctools_context_replace_form(&$form, $contexts) {
|
1749
|
foreach ($contexts as $cid => $context) {
|
1750
|
if (empty($context->empty)) {
|
1751
|
continue;
|
1752
|
}
|
1753
|
|
1754
|
// Get plugin info from the context which should have been set when the
|
1755
|
// empty context was created.
|
1756
|
$info = NULL;
|
1757
|
$plugin = NULL;
|
1758
|
$settings = NULL;
|
1759
|
switch ($context->placeholder['type']) {
|
1760
|
case 'argument':
|
1761
|
$info = $context->placeholder['conf'];
|
1762
|
$plugin = ctools_get_argument($info['name']);
|
1763
|
break;
|
1764
|
|
1765
|
case 'context':
|
1766
|
$info = $context->placeholder['conf'];
|
1767
|
$plugin = ctools_get_context($info['name']);
|
1768
|
break;
|
1769
|
}
|
1770
|
|
1771
|
// Ask the plugin where the form is.
|
1772
|
if ($plugin && isset($plugin['placeholder form'])) {
|
1773
|
if (is_array($plugin['placeholder form'])) {
|
1774
|
$form[$cid] = $plugin['placeholder form'];
|
1775
|
}
|
1776
|
else {
|
1777
|
if (function_exists($plugin['placeholder form'])) {
|
1778
|
$widget = $plugin['placeholder form']($info);
|
1779
|
if ($widget) {
|
1780
|
$form[$cid] = $widget;
|
1781
|
}
|
1782
|
}
|
1783
|
}
|
1784
|
|
1785
|
if (!empty($form[$cid])) {
|
1786
|
$form[$cid]['#title'] = t('@identifier (@keyword)', array(
|
1787
|
'@keyword' => '%' . $context->keyword,
|
1788
|
'@identifier' => $context->identifier,
|
1789
|
));
|
1790
|
}
|
1791
|
}
|
1792
|
}
|
1793
|
}
|
1794
|
|
1795
|
// ---------------------------------------------------------------------------
|
1796
|
// Functions related to loading access control plugins.
|
1797
|
/**
|
1798
|
* Fetch metadata on a specific access control plugin.
|
1799
|
*
|
1800
|
* @param $name
|
1801
|
* Name of a plugin.
|
1802
|
*
|
1803
|
* @return array
|
1804
|
* An array with information about the requested access control plugin.
|
1805
|
*/
|
1806
|
|
1807
|
function ctools_get_access_plugin($name) {
|
1808
|
ctools_include('plugins');
|
1809
|
return ctools_get_plugins('ctools', 'access', $name);
|
1810
|
}
|
1811
|
|
1812
|
/**
|
1813
|
* Fetch metadata for all access control plugins.
|
1814
|
*
|
1815
|
* @return array
|
1816
|
* An array of arrays with information about all available access control plugins.
|
1817
|
*/
|
1818
|
function ctools_get_access_plugins() {
|
1819
|
ctools_include('plugins');
|
1820
|
return ctools_get_plugins('ctools', 'access');
|
1821
|
}
|
1822
|
|
1823
|
/**
|
1824
|
* Fetch a list of access plugins that are available for a given list of
|
1825
|
* contexts.
|
1826
|
*
|
1827
|
* If 'logged-in-user' is not in the list of contexts, it will be added as
|
1828
|
* this is required.
|
1829
|
*
|
1830
|
* @param array $contexts
|
1831
|
* Array of ctools_context objects with which to select access plugins.
|
1832
|
*
|
1833
|
* @return array
|
1834
|
* Array of applicable access plugins. Can be empty.
|
1835
|
*/
|
1836
|
function ctools_get_relevant_access_plugins($contexts) {
|
1837
|
if (!isset($contexts['logged-in-user'])) {
|
1838
|
$contexts['logged-in-user'] = ctools_access_get_loggedin_context();
|
1839
|
}
|
1840
|
|
1841
|
$all_plugins = ctools_get_access_plugins();
|
1842
|
$plugins = array();
|
1843
|
foreach ($all_plugins as $id => $plugin) {
|
1844
|
if (!empty($plugin['required context'])
|
1845
|
&& !ctools_context_match_requirements($contexts, $plugin['required context'])
|
1846
|
) {
|
1847
|
continue;
|
1848
|
}
|
1849
|
$plugins[$id] = $plugin;
|
1850
|
}
|
1851
|
|
1852
|
return $plugins;
|
1853
|
}
|
1854
|
|
1855
|
/**
|
1856
|
* Create a context for the logged in user.
|
1857
|
*/
|
1858
|
function ctools_access_get_loggedin_context() {
|
1859
|
$context = ctools_context_create('entity:user', array('type' => 'current'), TRUE);
|
1860
|
$context->identifier = t('Logged in user');
|
1861
|
$context->keyword = 'viewer';
|
1862
|
$context->id = 0;
|
1863
|
|
1864
|
return $context;
|
1865
|
}
|
1866
|
|
1867
|
/**
|
1868
|
* Get a summary of an access plugin's settings.
|
1869
|
*
|
1870
|
* @return string
|
1871
|
* The summary text.
|
1872
|
*/
|
1873
|
function ctools_access_summary($plugin, $contexts, $test) {
|
1874
|
if (!isset($contexts['logged-in-user'])) {
|
1875
|
$contexts['logged-in-user'] = ctools_access_get_loggedin_context();
|
1876
|
}
|
1877
|
|
1878
|
$description = '';
|
1879
|
if ($function = ctools_plugin_get_function($plugin, 'summary')) {
|
1880
|
$required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
|
1881
|
$context = isset($test['context']) ? $test['context'] : array();
|
1882
|
$selected_context = ctools_context_select($contexts, $required_context, $context);
|
1883
|
$description = $function($test['settings'], $selected_context, $plugin);
|
1884
|
}
|
1885
|
|
1886
|
if (!empty($test['not'])) {
|
1887
|
$description = "NOT ($description)";
|
1888
|
}
|
1889
|
|
1890
|
return $description;
|
1891
|
}
|
1892
|
|
1893
|
/**
|
1894
|
* Get a summary of a group of access plugin's settings.
|
1895
|
*
|
1896
|
* @param $access
|
1897
|
* An array of settings theoretically set by the user, including the array
|
1898
|
* of plugins to check:
|
1899
|
* - 'plugins': the array of plugin metadata info to check
|
1900
|
* - 'logic': (optional) either 'and' or 'or', indicating how to combine
|
1901
|
* restrictions. Defaults to 'or'.
|
1902
|
* @param array $contexts
|
1903
|
* An array of zero or more contexts that may be used to determine if
|
1904
|
* the user has access.
|
1905
|
*
|
1906
|
* @return string
|
1907
|
* The summary text. Can be NULL if there are no plugins defined.
|
1908
|
*/
|
1909
|
function ctools_access_group_summary($access, $contexts) {
|
1910
|
if (empty($access['plugins']) || !is_array($access['plugins'])) {
|
1911
|
return NULL;
|
1912
|
}
|
1913
|
|
1914
|
$descriptions = array();
|
1915
|
foreach ($access['plugins'] as $id => $test) {
|
1916
|
$plugin = ctools_get_access_plugin($test['name']);
|
1917
|
$descriptions[] = ctools_access_summary($plugin, $contexts, $test);
|
1918
|
}
|
1919
|
|
1920
|
$separator =
|
1921
|
(isset($access['logic']) && $access['logic'] === 'and')
|
1922
|
? t(', and ') : t(', or ');
|
1923
|
return implode($separator, $descriptions);
|
1924
|
}
|
1925
|
|
1926
|
/**
|
1927
|
* Determine if the current user has access via a plugin.
|
1928
|
*
|
1929
|
* @param array $settings
|
1930
|
* An array of settings theoretically set by the user, including the array
|
1931
|
* of plugins to check:
|
1932
|
* - 'plugins': the array of plugin metadata info to check
|
1933
|
* - 'logic': (optional) either 'and' or 'or', indicating how to combine
|
1934
|
* restrictions. The 'or' case is not fully implemented and returns the
|
1935
|
* input contexts unchanged.
|
1936
|
*
|
1937
|
* @param array $contexts
|
1938
|
* An array of zero or more contexts that may be used to determine if
|
1939
|
* the user has access.
|
1940
|
*
|
1941
|
* @return bool
|
1942
|
* TRUE if access is granted, FALSE if otherwise.
|
1943
|
*/
|
1944
|
function ctools_access($settings, $contexts = array()) {
|
1945
|
if (empty($settings['plugins'])) {
|
1946
|
return TRUE;
|
1947
|
}
|
1948
|
|
1949
|
if (!isset($settings['logic'])) {
|
1950
|
$settings['logic'] = 'and';
|
1951
|
}
|
1952
|
|
1953
|
if (!isset($contexts['logged-in-user'])) {
|
1954
|
$contexts['logged-in-user'] = ctools_access_get_loggedin_context();
|
1955
|
}
|
1956
|
|
1957
|
foreach ($settings['plugins'] as $test) {
|
1958
|
$pass = FALSE;
|
1959
|
$plugin = ctools_get_access_plugin($test['name']);
|
1960
|
if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) {
|
1961
|
// Do we need just some contexts or all of them?
|
1962
|
if (!empty($plugin['all contexts'])) {
|
1963
|
$test_contexts = $contexts;
|
1964
|
}
|
1965
|
else {
|
1966
|
$required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
|
1967
|
$context = isset($test['context']) ? $test['context'] : array();
|
1968
|
$test_contexts = ctools_context_select($contexts, $required_context, $context);
|
1969
|
}
|
1970
|
|
1971
|
$pass = $function($test['settings'], $test_contexts, $plugin);
|
1972
|
if (!empty($test['not'])) {
|
1973
|
$pass = !$pass;
|
1974
|
}
|
1975
|
}
|
1976
|
|
1977
|
if ($pass && $settings['logic'] == 'or') {
|
1978
|
// Pass if 'or' and this rule passed.
|
1979
|
return TRUE;
|
1980
|
}
|
1981
|
elseif (!$pass && $settings['logic'] == 'and') {
|
1982
|
// Fail if 'and' and this rule failed.
|
1983
|
return FALSE;
|
1984
|
}
|
1985
|
}
|
1986
|
|
1987
|
// Return TRUE if logic was and, meaning all rules passed.
|
1988
|
// Return FALSE if logic was or, meaning no rule passed.
|
1989
|
return ($settings['logic'] === 'and');
|
1990
|
}
|
1991
|
|
1992
|
/**
|
1993
|
* Create default settings for a new access plugin.
|
1994
|
*
|
1995
|
* @param $plugin
|
1996
|
* The access plugin being used.
|
1997
|
*
|
1998
|
* @return array
|
1999
|
* A default configured test that should be placed in $access['plugins'];
|
2000
|
*/
|
2001
|
function ctools_access_new_test($plugin) {
|
2002
|
$test = array(
|
2003
|
'name' => $plugin['name'],
|
2004
|
'settings' => array(),
|
2005
|
);
|
2006
|
|
2007
|
// Set up required context defaults.
|
2008
|
if (isset($plugin['required context'])) {
|
2009
|
if (is_object($plugin['required context'])) {
|
2010
|
$test['context'] = '';
|
2011
|
}
|
2012
|
else {
|
2013
|
$test['context'] = array();
|
2014
|
foreach ($plugin['required context'] as $required) {
|
2015
|
$test['context'][] = '';
|
2016
|
}
|
2017
|
}
|
2018
|
}
|
2019
|
|
2020
|
$default = NULL;
|
2021
|
if (isset($plugin['default'])) {
|
2022
|
$default = $plugin['default'];
|
2023
|
}
|
2024
|
elseif (isset($plugin['defaults'])) {
|
2025
|
$default = $plugin['defaults'];
|
2026
|
}
|
2027
|
|
2028
|
// Setup plugin defaults.
|
2029
|
if (isset($default)) {
|
2030
|
if (is_array($default)) {
|
2031
|
$test['settings'] = $default;
|
2032
|
}
|
2033
|
elseif (function_exists($default)) {
|
2034
|
$test['settings'] = $default();
|
2035
|
}
|
2036
|
else {
|
2037
|
$test['settings'] = array();
|
2038
|
}
|
2039
|
}
|
2040
|
|
2041
|
return $test;
|
2042
|
}
|
2043
|
|
2044
|
/**
|
2045
|
* Apply restrictions to contexts based upon the access control configured.
|
2046
|
*
|
2047
|
* These restrictions allow the UI to not show content that may not be relevant
|
2048
|
* to all types of a particular context.
|
2049
|
*
|
2050
|
* @param array $settings
|
2051
|
* Array of keys specifying the settings:
|
2052
|
* - 'plugins': the array of plugin metadata info to check. If not set, or
|
2053
|
* not an array, the function returns with no action.
|
2054
|
* - 'logic': (optional) either 'and' or 'or', indicating how to combine
|
2055
|
* restrictions. Defaults to 'and'.
|
2056
|
* The 'or' case is not fully implemented and returns with no action if
|
2057
|
* there is more than one plugin.
|
2058
|
*
|
2059
|
* @param array $contexts
|
2060
|
* Array of available contexts.
|
2061
|
*/
|
2062
|
function ctools_access_add_restrictions($settings, $contexts) {
|
2063
|
if (empty($settings['plugins']) || !is_array($settings['plugins'])) {
|
2064
|
return;
|
2065
|
}
|
2066
|
|
2067
|
if (!isset($settings['logic'])) {
|
2068
|
$settings['logic'] = 'and';
|
2069
|
}
|
2070
|
|
2071
|
// We're not going to try to figure out restrictions on the or.
|
2072
|
if ($settings['logic'] === 'or' && count($settings['plugins']) > 1) {
|
2073
|
return;
|
2074
|
}
|
2075
|
|
2076
|
foreach ($settings['plugins'] as $test) {
|
2077
|
$plugin = ctools_get_access_plugin($test['name']);
|
2078
|
// $plugin is 'array()' on error.
|
2079
|
if ($plugin
|
2080
|
&& $function = ctools_plugin_get_function($plugin, 'restrictions')
|
2081
|
) {
|
2082
|
$required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
|
2083
|
$context = isset($test['context']) ? $test['context'] : array();
|
2084
|
$contexts = ctools_context_select($contexts, $required_context, $context);
|
2085
|
|
2086
|
if ($contexts !== FALSE) {
|
2087
|
$function($test['settings'], $contexts);
|
2088
|
}
|
2089
|
}
|
2090
|
}
|
2091
|
}
|