PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/context.inc

https://bitbucket.org/luksak/ctools
Pascal | 1583 lines | 818 code | 144 blank | 621 comment | 151 complexity | 24a384a5d66550cd9d9bbff08edee054 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. *
  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. * The context object is largely a wrapper around some other object, with
  21. * an interface to finding out what is contained and getting to both
  22. * the object and information about the object.
  23. *
  24. * Each context object has its own information, but some things are very
  25. * common, such as titles, data, keywords, etc. In particulare, the 'type'
  26. * of the context is important.
  27. */
  28. class ctools_context {
  29. var $type = NULL;
  30. var $data = NULL;
  31. // The title of this object.
  32. var $title = '';
  33. // The title of the page if this object exists
  34. var $page_title = '';
  35. // The identifier (in the UI) of this object
  36. var $identifier = '';
  37. var $argument = NULL;
  38. var $keyword = '';
  39. var $original_argument = NULL;
  40. var $restrictions = array();
  41. var $empty = FALSE;
  42. function ctools_context($type = 'none', $data = NULL) {
  43. $this->type = $type;
  44. $this->data = $data;
  45. $this->title = t('Unknown context');
  46. }
  47. function is_type($type) {
  48. if ($type == 'any' || $this->type == 'any') {
  49. return TRUE;
  50. }
  51. $a = is_array($type) ? $type : array($type);
  52. $b = is_array($this->type) ? $this->type : array($this->type);
  53. return (bool) array_intersect($a, $b);
  54. }
  55. function get_argument() {
  56. return $this->argument;
  57. }
  58. function get_original_argument() {
  59. if (!is_null($this->original_argument)) {
  60. return $this->original_argument;
  61. }
  62. return $this->argument;
  63. }
  64. function get_keyword() {
  65. return $this->keyword;
  66. }
  67. function get_identifier() {
  68. return $this->identifier;
  69. }
  70. function get_title() {
  71. return $this->title;
  72. }
  73. function get_page_title() {
  74. return $this->page_title;
  75. }
  76. }
  77. /**
  78. * Used to create a method of comparing if a list of contexts
  79. * match a required context type.
  80. */
  81. class ctools_context_required {
  82. var $keywords = '';
  83. /**
  84. * If set, the title will be used in the selector to identify
  85. * the context. This is very useful when multiple contexts
  86. * are required to inform the user will be used for what.
  87. */
  88. var $title = NULL;
  89. /**
  90. * Test to see if this context is required.
  91. */
  92. var $required = TRUE;
  93. /**
  94. * If TRUE, skip the check in ctools_context_required::select()
  95. * for contexts whose names may have changed.
  96. */
  97. var $skip_name_check = FALSE;
  98. /**
  99. *
  100. * @param $title
  101. * The first parameter should be the 'title' of the context for use
  102. * in UYI selectors when multiple contexts qualify.
  103. * @param ...
  104. * One or more keywords to use for matching which contexts are allowed.
  105. */
  106. function ctools_context_required($title) {
  107. $args = func_get_args();
  108. $this->title = array_shift($args);
  109. // If we have a boolean value at the end for $skip_name_check, store it
  110. if (is_bool(end($args))) {
  111. $this->skip_name_check = array_pop($args);
  112. }
  113. // If we were given restrictions at the end, store them.
  114. if (count($args) > 1 && is_array(end($args))) {
  115. $this->restrictions = array_pop($args);
  116. }
  117. if (count($args) == 1) {
  118. $args = array_shift($args);
  119. }
  120. $this->keywords = $args;
  121. }
  122. function filter($contexts) {
  123. $result = array();
  124. // See which of these contexts are valid
  125. foreach ((array) $contexts as $cid => $context) {
  126. if ($context->is_type($this->keywords)) {
  127. // Compare to see if our contexts were met.
  128. if (!empty($this->restrictions) && !empty($context->restrictions)) {
  129. foreach ($this->restrictions as $key => $values) {
  130. // If we have a restriction, the context must either not have that
  131. // restriction listed, which means we simply don't know what it is,
  132. // or there must be an intersection of the restricted values on
  133. // both sides.
  134. if (!is_array($values)) {
  135. $values = array($values);
  136. }
  137. if (!empty($context->restrictions[$key]) && !array_intersect($values, $context->restrictions[$key])) {
  138. continue 2;
  139. }
  140. }
  141. }
  142. $result[$cid] = $context;
  143. }
  144. }
  145. return $result;
  146. }
  147. function select($contexts, $context) {
  148. if (!is_array($contexts)) {
  149. $contexts = array($contexts);
  150. }
  151. // If we had requested a $context but that $context doesn't exist
  152. // in our context list, there is a good chance that what happened
  153. // is our context IDs changed. See if there's another context
  154. // that satisfies our requirements.
  155. if (!$this->skip_name_check && !empty($context) && !isset($contexts[$context])) {
  156. $choices = $this->filter($contexts);
  157. // If we got a hit, take the first one that matches.
  158. if ($choices) {
  159. $keys = array_keys($choices);
  160. $context = reset($keys);
  161. }
  162. }
  163. if (empty($context) || empty($contexts[$context])) {
  164. return FALSE;
  165. }
  166. return $contexts[$context];
  167. }
  168. }
  169. /**
  170. * Used to compare to see if a list of contexts match an optional context. This
  171. * can produce empty contexts to use as placeholders.
  172. */
  173. class ctools_context_optional extends ctools_context_required {
  174. var $required = FALSE;
  175. function ctools_context_optional() {
  176. $args = func_get_args();
  177. call_user_func_array(array($this, 'ctools_context_required'), $args);
  178. }
  179. /**
  180. * Add the 'empty' context which is possible for optional
  181. */
  182. function add_empty(&$contexts) {
  183. $context = new ctools_context('any');
  184. $context->title = t('No context');
  185. $context->identifier = t('No context');
  186. $contexts = array_merge(array('empty' => $context), $contexts);
  187. }
  188. function filter($contexts) {
  189. $this->add_empty($contexts);
  190. return parent::filter($contexts);
  191. }
  192. function select($contexts, $context) {
  193. $this->add_empty($contexts);
  194. if (empty($context)) {
  195. return $contexts['empty'];
  196. }
  197. $result = parent::select($contexts, $context);
  198. // Don't flip out if it can't find the context; this is optional, put
  199. // in an empty.
  200. if ($result == FALSE) {
  201. $result = $contexts['empty'];
  202. }
  203. return $result;
  204. }
  205. }
  206. /**
  207. * Return a keyed array of context that match the given 'required context'
  208. * filters.
  209. *
  210. * Functions or systems that require contexts of a particular type provide a
  211. * ctools_context_required or ctools_context_optional object. This function
  212. * examines that object and an array of contexts to determine which contexts
  213. * match the filter.
  214. *
  215. * Since multiple contexts can be required, this function will accept either
  216. * an array of all required contexts, or just a single required context object.
  217. *
  218. * @param $contexts
  219. * A keyed array of all available contexts.
  220. * @param $required
  221. * A ctools_context_required or ctools_context_optional object, or an array
  222. * of such objects.
  223. *
  224. * @return
  225. * A keyed array of contexts that match the filter.
  226. */
  227. function ctools_context_filter($contexts, $required) {
  228. if (is_array($required)) {
  229. $result = array();
  230. foreach ($required as $r) {
  231. $result = array_merge($result, _ctools_context_filter($contexts, $r));
  232. }
  233. return $result;
  234. }
  235. return _ctools_context_filter($contexts, $required);
  236. }
  237. function _ctools_context_filter($contexts, $required) {
  238. $result = array();
  239. if (is_object($required)) {
  240. $result = $required->filter($contexts);
  241. }
  242. return $result;
  243. }
  244. /**
  245. * Create a select box to choose possible contexts.
  246. *
  247. * This only creates a selector if there is actually a choice; if there
  248. * is only one possible context, that one is silently assigned.
  249. *
  250. * If an array of required contexts is provided, one selector will be
  251. * provided for each context.
  252. *
  253. * @param $contexts
  254. * A keyed array of all available contexts.
  255. * @param $required
  256. * The required context object or array of objects.
  257. *
  258. * @return
  259. * A form element, or NULL if there are no contexts that satisfy the
  260. * requirements.
  261. */
  262. function ctools_context_selector($contexts, $required, $default) {
  263. if (is_array($required)) {
  264. $result = array('#tree' => TRUE);
  265. $count = 1;
  266. foreach ($required as $id => $r) {
  267. $result[] = _ctools_context_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++);
  268. }
  269. return $result;
  270. }
  271. return _ctools_context_selector($contexts, $required, $default);
  272. }
  273. function _ctools_context_selector($contexts, $required, $default, $num = 0) {
  274. $filtered = ctools_context_filter($contexts, $required);
  275. $count = count($filtered);
  276. $form = array();
  277. if ($count >= 1) {
  278. // If there's more than one to choose from, create a select widget.
  279. foreach ($filtered as $cid => $context) {
  280. $options[$cid] = $context->get_identifier();
  281. }
  282. if (!empty($required->title)) {
  283. $title = $required->title;
  284. }
  285. else {
  286. $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
  287. }
  288. return array(
  289. '#type' => 'select',
  290. '#options' => $options,
  291. '#title' => $title,
  292. '#default_value' => $default,
  293. );
  294. }
  295. }
  296. /**
  297. * Are there enough contexts for a plugin?
  298. *
  299. * Some plugins can have a 'required contexts' item which can either
  300. * be a context requirement object or an array of them. When contexts
  301. * are required, items that do not have enough contexts should not
  302. * appear. This tests an item to see if it has enough contexts
  303. * to actually appear.
  304. *
  305. * @param $contexts
  306. * A keyed array of all available contexts.
  307. * @param $required
  308. * The required context object or array of objects.
  309. *
  310. * @return
  311. * TRUE if there are enough contexts, FALSE if there are not.
  312. */
  313. function ctools_context_match_requirements($contexts, $required) {
  314. if (!is_array($required)) {
  315. $required = array($required);
  316. }
  317. // Get the keys to avoid bugs in PHP 5.0.8 with keys and loops.
  318. // And use it to remove optional contexts.
  319. $keys = array_keys($required);
  320. foreach ($keys as $key) {
  321. if (empty($required[$key]->required)) {
  322. unset($required[$key]);
  323. }
  324. }
  325. $count = count($required);
  326. return (count(ctools_context_filter($contexts, $required)) >= $count);
  327. }
  328. /**
  329. * Create a select box to choose possible contexts.
  330. *
  331. * This only creates a selector if there is actually a choice; if there
  332. * is only one possible context, that one is silently assigned.
  333. *
  334. * If an array of required contexts is provided, one selector will be
  335. * provided for each context.
  336. *
  337. * @param $contexts
  338. * A keyed array of all available contexts.
  339. * @param $required
  340. * The required context object or array of objects.
  341. *
  342. * @return
  343. * A form element, or NULL if there are no contexts that satisfy the
  344. * requirements.
  345. */
  346. function ctools_context_converter_selector($contexts, $required, $default) {
  347. if (is_array($required)) {
  348. $result = array('#tree' => TRUE);
  349. $count = 1;
  350. foreach ($required as $id => $r) {
  351. $result[] = _ctools_context_converter_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++);
  352. }
  353. return $result;
  354. }
  355. return _ctools_context_converter_selector($contexts, $required, $default);
  356. }
  357. function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) {
  358. $filtered = ctools_context_filter($contexts, $required);
  359. $count = count($filtered);
  360. $form = array();
  361. if ($count > 1) {
  362. // If there's more than one to choose from, create a select widget.
  363. $options = array();
  364. foreach ($filtered as $cid => $context) {
  365. if ($context->type == 'any') {
  366. $options[''] = t('No context');
  367. continue;
  368. }
  369. $key = $context->get_identifier();
  370. if ($converters = ctools_context_get_converters($cid . '.', $context)) {
  371. $options[$key] = $converters;
  372. }
  373. }
  374. if (empty($options)) {
  375. return array(
  376. '#type' => 'value',
  377. '#value' => 'any',
  378. );
  379. }
  380. if (!empty($required->title)) {
  381. $title = $required->title;
  382. }
  383. else {
  384. $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
  385. }
  386. return array(
  387. '#type' => 'select',
  388. '#options' => $options,
  389. '#title' => $title,
  390. '#description' => t('Please choose which context and how you would like it converted.'),
  391. '#default_value' => $default,
  392. );
  393. }
  394. }
  395. /**
  396. * Get a list of converters available for a given context.
  397. */
  398. function ctools_context_get_converters($cid, $context) {
  399. if (empty($context->plugin)) {
  400. return array();
  401. }
  402. return _ctools_context_get_converters($cid, $context->plugin);
  403. }
  404. /**
  405. * Get a list of converters available for a given context.
  406. */
  407. function _ctools_context_get_converters($id, $plugin_name) {
  408. $plugin = ctools_get_context($plugin_name);
  409. if (empty($plugin['convert list'])) {
  410. return array();
  411. }
  412. $converters = array();
  413. if (is_array($plugin['convert list'])) {
  414. $converters = $plugin['convert list'];
  415. }
  416. else if ($function = ctools_plugin_get_function($plugin, 'convert list')) {
  417. $converters = (array) $function($plugin);
  418. }
  419. foreach (module_implements('ctools_context_convert_list_alter') as $module) {
  420. $function = $module . '_ctools_context_convert_list_alter';
  421. $function($plugin, $converters);
  422. }
  423. // Now, change them all to include the plugin:
  424. $return = array();
  425. foreach ($converters as $key => $title) {
  426. $return[$id . $key] = $title;
  427. }
  428. natcasesort($return);
  429. return $return;
  430. }
  431. /**
  432. * Get a list of all contexts + converters available.
  433. */
  434. function ctools_context_get_all_converters() {
  435. $contexts = ctools_get_contexts();
  436. $converters = array();
  437. foreach ($contexts as $name => $context) {
  438. if (empty($context['no required context ui'])) {
  439. $context_converters = _ctools_context_get_converters($name . '.', $name);
  440. if ($context_converters) {
  441. $converters[$context['title']] = $context_converters;
  442. }
  443. }
  444. }
  445. return $converters;
  446. }
  447. /**
  448. * Let the context convert an argument based upon the converter that was given.
  449. */
  450. function ctools_context_convert_context($context, $converter, $converter_options = array()) {
  451. // Contexts without plugins might be optional placeholders.
  452. if (empty($context->plugin)) {
  453. return;
  454. }
  455. $value = $context->argument;
  456. $plugin = ctools_get_context($context->plugin);
  457. if ($function = ctools_plugin_get_function($plugin, 'convert')) {
  458. $value = $function($context, $converter, $converter_options);
  459. }
  460. foreach (module_implements('ctools_context_converter_alter') as $module) {
  461. $function = $module . '_ctools_context_converter_alter';
  462. $function($context, $converter, $value, $converter_options);
  463. }
  464. return $value;
  465. }
  466. /**
  467. * Choose a context or contexts based upon the selection made via
  468. * ctools_context_filter.
  469. *
  470. * @param $contexts
  471. * A keyed array of all available contexts
  472. * @param $required
  473. * The required context object provided by the plugin
  474. * @param $context
  475. * The selection made using ctools_context_selector
  476. */
  477. function ctools_context_select($contexts, $required, $context) {
  478. if (is_array($required)) {
  479. $result = array();
  480. foreach ($required as $id => $r) {
  481. if (empty($required[$id])) {
  482. continue;
  483. }
  484. if (($result[] = _ctools_context_select($contexts, $r, $context[$id])) === FALSE) {
  485. return FALSE;
  486. }
  487. }
  488. return $result;
  489. }
  490. return _ctools_context_select($contexts, $required, $context);
  491. }
  492. function _ctools_context_select($contexts, $required, $context) {
  493. if (!is_object($required)) {
  494. return FALSE;
  495. }
  496. return $required->select($contexts, $context);
  497. }
  498. /**
  499. * Create a new context object.
  500. *
  501. * @param $type
  502. * The type of context to create; this loads a plugin.
  503. * @param $data
  504. * The data to put into the context.
  505. * @param $empty
  506. * Whether or not this context is specifically empty.
  507. * @param $conf
  508. * A configuration structure if this context was created via UI.
  509. *
  510. * @return
  511. * A $context or NULL if one could not be created.
  512. */
  513. function ctools_context_create($type, $data = NULL, $conf = FALSE) {
  514. ctools_include('plugins');
  515. $plugin = ctools_get_context($type);
  516. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  517. return $function(FALSE, $data, $conf, $plugin);
  518. }
  519. }
  520. /**
  521. * Create an empty context object.
  522. *
  523. * Empty context objects are primarily used as placeholders in the UI where
  524. * the actual contents of a context object may not be known. It may have
  525. * additional text embedded to give the user clues as to how the context
  526. * is used.
  527. *
  528. * @param $type
  529. * The type of context to create; this loads a plugin.
  530. *
  531. * @return
  532. * A $context or NULL if one could not be created.
  533. */
  534. function ctools_context_create_empty($type) {
  535. $plugin = ctools_get_context($type);
  536. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  537. $context = $function(TRUE, NULL, FALSE, $plugin);
  538. if (is_object($context)) {
  539. $context->empty = TRUE;
  540. }
  541. return $context;
  542. }
  543. }
  544. /**
  545. * Perform keyword and context substitutions.
  546. */
  547. function ctools_context_keyword_substitute($string, $keywords, $contexts, $converter_options = array()) {
  548. // Ensure a default keyword exists:
  549. $keywords['%%'] = '%';
  550. // Match contexts to the base keywords:
  551. $context_keywords = array();
  552. foreach ($contexts as $context) {
  553. if (isset($context->keyword)) {
  554. $context_keywords[$context->keyword] = $context;
  555. }
  556. }
  557. // Look for context matches we we only have to convert known matches.
  558. $matches = array();
  559. if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)?)/us', $string, $matches)) {
  560. foreach ($matches[1] as $keyword) {
  561. // Ignore anything it finds with %%.
  562. if ($keyword[0] == '%') {
  563. continue;
  564. }
  565. // If the keyword is already set by something passed in, don't try to
  566. // overwrite it.
  567. if (!empty($keywords['%' . $keyword])) {
  568. continue;
  569. }
  570. // Figure out our keyword and converter, if specified.
  571. if (strpos($keyword, ':')) {
  572. list($context, $converter) = explode(':', $keyword, 2);
  573. }
  574. else {
  575. $context = $keyword;
  576. if (isset($context_keywords[$keyword])) {
  577. $plugin = ctools_get_context($context_keywords[$context]->plugin);
  578. // Fall back to a default converter, if specified.
  579. if ($plugin && !empty($plugin['convert default'])) {
  580. $converter = $plugin['convert default'];
  581. }
  582. }
  583. }
  584. if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
  585. $keywords['%' . $keyword] = '';
  586. }
  587. else if (!empty($converter)) {
  588. $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options);
  589. }
  590. else {
  591. $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
  592. }
  593. }
  594. }
  595. return strtr($string, $keywords);
  596. }
  597. /**
  598. * Determine a unique context ID for a context
  599. *
  600. * Often contexts of many different types will be placed into a list. This
  601. * ensures that even though contexts of multiple types may share IDs, they
  602. * are unique in the final list.
  603. */
  604. function ctools_context_id($context, $type = 'context') {
  605. if (!$context['id']) {
  606. $context['id'] = 1;
  607. }
  608. return $type . '_' . $context['name'] . '_' . $context['id'];
  609. }
  610. /**
  611. * Get the next id available given a list of already existing objects.
  612. *
  613. * This finds the next id available for the named object.
  614. *
  615. * @param $objects
  616. * A list of context descriptor objects, i.e, arguments, relationships, contexts, etc.
  617. * @param $name
  618. * The name being used.
  619. */
  620. function ctools_context_next_id($objects, $name) {
  621. $id = 0;
  622. // Figure out which instance of this argument we're creating
  623. if (!$objects) {
  624. return $id + 1;
  625. }
  626. foreach ($objects as $object) {
  627. if (isset($object['name']) && $object['name'] == $name) {
  628. if ($object['id'] > $id) {
  629. $id = $object['id'];
  630. }
  631. }
  632. }
  633. return $id + 1;
  634. }
  635. // ---------------------------------------------------------------------------
  636. // Functions related to contexts from arguments.
  637. /**
  638. * Fetch metadata on a specific argument plugin.
  639. *
  640. * @param $argument
  641. * Name of an argument plugin.
  642. *
  643. * @return
  644. * An array with information about the requested argument plugin.
  645. */
  646. function ctools_get_argument($argument) {
  647. ctools_include('plugins');
  648. return ctools_get_plugins('ctools', 'arguments', $argument);
  649. }
  650. /**
  651. * Fetch metadata for all argument plugins.
  652. *
  653. * @return
  654. * An array of arrays with information about all available argument plugins.
  655. */
  656. function ctools_get_arguments() {
  657. ctools_include('plugins');
  658. return ctools_get_plugins('ctools', 'arguments');
  659. }
  660. /**
  661. * Get a context from an argument.
  662. *
  663. * @param $argument
  664. * The configuration of an argument. It must contain the following data:
  665. * - name: The name of the argument plugin being used.
  666. * - argument_settings: The configuration based upon the plugin forms.
  667. * - identifier: The human readable identifier for this argument, usually
  668. * defined by the UI.
  669. * - keyword: The keyword used for this argument for substitutions.
  670. *
  671. * @param $arg
  672. * The actual argument received. This is expected to be a string from a URL but
  673. * this does not have to be the only source of arguments.
  674. * @param $empty
  675. * If true, the $arg will not be used to load the context. Instead, an empty
  676. * placeholder context will be loaded.
  677. *
  678. * @return
  679. * A context object if one can be loaded.
  680. */
  681. function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) {
  682. ctools_include('plugins');
  683. if (empty($argument['name'])) {
  684. return;
  685. }
  686. if ($function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context')) {
  687. // Backward compatibility: Merge old style settings into new style:
  688. if (!empty($argument['settings'])) {
  689. $argument += $argument['settings'];
  690. unset($argument['settings']);
  691. }
  692. $context = $function($arg, $argument, $empty);
  693. if (is_object($context)) {
  694. $context->identifier = $argument['identifier'];
  695. $context->page_title = isset($argument['title']) ? $argument['title'] : '';
  696. $context->keyword = $argument['keyword'];
  697. $context->id = ctools_context_id($argument, 'argument');
  698. $context->original_argument = $arg;
  699. if (!empty($context->empty)) {
  700. $context->placeholder = array(
  701. 'type' => 'argument',
  702. 'conf' => $argument,
  703. );
  704. }
  705. }
  706. return $context;
  707. }
  708. }
  709. /**
  710. * Retrieve a list of empty contexts for all arguments.
  711. */
  712. function ctools_context_get_placeholders_from_argument($arguments) {
  713. $contexts = array();
  714. foreach ($arguments as $argument) {
  715. $context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
  716. if ($context) {
  717. $contexts[ctools_context_id($argument, 'argument')] = $context;
  718. }
  719. }
  720. return $contexts;
  721. }
  722. /**
  723. * Load the contexts for a given list of arguments.
  724. *
  725. * @param $arguments
  726. * The array of argument definitions.
  727. * @param &$contexts
  728. * The array of existing contexts. New contexts will be added to this array.
  729. * @param $args
  730. * The arguments to load.
  731. *
  732. * @return
  733. * FALSE if an argument wants to 404.
  734. */
  735. function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) {
  736. foreach ($arguments as $argument) {
  737. // pull the argument off the list.
  738. $arg = array_shift($args);
  739. $id = ctools_context_id($argument, 'argument');
  740. // For % arguments embedded in the URL, our context is already loaded.
  741. // There is no need to go and load it again.
  742. if (empty($contexts[$id])) {
  743. if ($context = ctools_context_get_context_from_argument($argument, $arg)) {
  744. $contexts[$id] = $context;
  745. }
  746. }
  747. else {
  748. $context = $contexts[$id];
  749. }
  750. if ((empty($context) || empty($context->data)) && !empty($argument['default']) && $argument['default'] == '404') {
  751. return FALSE;
  752. }
  753. }
  754. return TRUE;
  755. }
  756. // ---------------------------------------------------------------------------
  757. // Functions related to contexts from relationships.
  758. /**
  759. * Fetch metadata on a specific relationship plugin.
  760. *
  761. * @param $content type
  762. * Name of a panel content type.
  763. *
  764. * @return
  765. * An array with information about the requested relationship.
  766. */
  767. function ctools_get_relationship($relationship) {
  768. ctools_include('plugins');
  769. return ctools_get_plugins('ctools', 'relationships', $relationship);
  770. }
  771. /**
  772. * Fetch metadata for all relationship plugins.
  773. *
  774. * @return
  775. * An array of arrays with information about all available relationships.
  776. */
  777. function ctools_get_relationships() {
  778. ctools_include('plugins');
  779. return ctools_get_plugins('ctools', 'relationships');
  780. }
  781. /**
  782. *
  783. * @param $relationship
  784. * The configuration of a relationship. It must contain the following data:
  785. * - name: The name of the relationship plugin being used.
  786. * - relationship_settings: The configuration based upon the plugin forms.
  787. * - identifier: The human readable identifier for this relationship, usually
  788. * defined by the UI.
  789. * - keyword: The keyword used for this relationship for substitutions.
  790. *
  791. * @param $source_context
  792. * The context this relationship is based upon.
  793. *
  794. * @param $placeholders
  795. * If TRUE, placeholders are acceptable.
  796. *
  797. * @return
  798. * A context object if one can be loaded.
  799. */
  800. function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) {
  801. ctools_include('plugins');
  802. if ($function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context')) {
  803. // Backward compatibility: Merge old style settings into new style:
  804. if (!empty($relationship['relationship_settings'])) {
  805. $relationship += $relationship['relationship_settings'];
  806. unset($relationship['relationship_settings']);
  807. }
  808. $context = $function($source_context, $relationship, $placeholders);
  809. if ($context) {
  810. $context->identifier = $relationship['identifier'];
  811. $context->page_title = isset($relationship['title']) ? $relationship['title'] : '';
  812. $context->keyword = $relationship['keyword'];
  813. if (!empty($context->empty)) {
  814. $context->placeholder = array(
  815. 'type' => 'relationship',
  816. 'conf' => $relationship,
  817. );
  818. }
  819. return $context;
  820. }
  821. }
  822. }
  823. /**
  824. * Fetch all relevant relationships.
  825. *
  826. * Relevant relationships are any relationship that can be created based upon
  827. * the list of existing contexts. For example, the 'node author' relationship
  828. * is relevant if there is a 'node' context, but makes no sense if there is
  829. * not one.
  830. *
  831. * @param $contexts
  832. * An array of contexts used to figure out which relationships are relevant.
  833. *
  834. * @return
  835. * An array of relationship keys that are relevant for the given set of
  836. * contexts.
  837. */
  838. function ctools_context_get_relevant_relationships($contexts) {
  839. $relevant = array();
  840. $relationships = ctools_get_relationships();
  841. // Go through each relationship
  842. foreach ($relationships as $rid => $relationship) {
  843. // For each relationship, see if there is a context that satisfies it.
  844. if (empty($relationship['no ui']) && ctools_context_filter($contexts, $relationship['required context'])) {
  845. $relevant[$rid] = $relationship['title'];
  846. }
  847. }
  848. return $relevant;
  849. }
  850. /**
  851. * Fetch all active relationships
  852. *
  853. * @param $relationships
  854. * An keyed array of relationship data including:
  855. * - name: name of relationship
  856. * - context: context id relationship belongs to. This will be used to
  857. * identify which context in the $contexts array to use to create the
  858. * relationship context.
  859. *
  860. * @param $contexts
  861. * A keyed array of contexts used to figure out which relationships
  862. * are relevant. New contexts will be added to this.
  863. *
  864. * @param $placeholders
  865. * If TRUE, placeholders are acceptable.
  866. */
  867. function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) {
  868. $return = array();
  869. foreach ($relationships as $rdata) {
  870. if (!isset($rdata['context'])) {
  871. continue;
  872. }
  873. if (is_array($rdata['context'])) {
  874. $rcontexts = array();
  875. foreach ($rdata['context'] as $cid) {
  876. if (empty($contexts[$cid])) {
  877. continue 2;
  878. }
  879. $rcontexts[] = $contexts[$cid];
  880. }
  881. }
  882. else {
  883. if (empty($contexts[$rdata['context']])) {
  884. continue;
  885. }
  886. $rcontexts = $contexts[$rdata['context']];
  887. }
  888. $cid = ctools_context_id($rdata, 'relationship');
  889. if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) {
  890. $contexts[$cid] = $context;
  891. }
  892. }
  893. }
  894. // ---------------------------------------------------------------------------
  895. // Functions related to loading contexts from simple context definitions.
  896. /**
  897. * Fetch metadata on a specific context plugin.
  898. *
  899. * @param $context
  900. * Name of a context.
  901. *
  902. * @return
  903. * An array with information about the requested panel context.
  904. */
  905. function ctools_get_context($context) {
  906. static $gate = array();
  907. ctools_include('plugins');
  908. $plugin = ctools_get_plugins('ctools', 'contexts', $context);
  909. if (empty($gate['context']) && !empty($plugin['superceded by'])) {
  910. // This gate prevents infinite loops.
  911. $gate[$context] = TRUE;
  912. $new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']);
  913. $gate[$context] = FALSE;
  914. // If a new plugin was returned, return it. Otherwise fall through and
  915. // return the original we fetched.
  916. if ($new_plugin) {
  917. return $new_plugin;
  918. }
  919. }
  920. return $plugin;
  921. }
  922. /**
  923. * Fetch metadata for all context plugins.
  924. *
  925. * @return
  926. * An array of arrays with information about all available panel contexts.
  927. */
  928. function ctools_get_contexts() {
  929. ctools_include('plugins');
  930. return ctools_get_plugins('ctools', 'contexts');
  931. }
  932. /**
  933. *
  934. * @param $context
  935. * The configuration of a context. It must contain the following data:
  936. * - name: The name of the context plugin being used.
  937. * - context_settings: The configuration based upon the plugin forms.
  938. * - identifier: The human readable identifier for this context, usually
  939. * defined by the UI.
  940. * - keyword: The keyword used for this context for substitutions.
  941. * @param $type
  942. * This is either 'context' which indicates the context will be loaded
  943. * from data in the settings, or 'required_context' which means the
  944. * context must be acquired from an external source. This is the method
  945. * used to pass pure contexts from one system to another.
  946. *
  947. * @return
  948. * A context object if one can be loaded.
  949. */
  950. function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) {
  951. ctools_include('plugins');
  952. $plugin = ctools_get_context($context['name']);
  953. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  954. // Backward compatibility: Merge old style settings into new style:
  955. if (!empty($context['context_settings'])) {
  956. $context += $context['context_settings'];
  957. unset($context['context_settings']);
  958. }
  959. if (isset($argument) && isset($plugin['placeholder name'])) {
  960. $context[$plugin['placeholder name']] = $argument;
  961. }
  962. $return = $function($type == 'requiredcontext', $context, TRUE, $plugin);
  963. if ($return) {
  964. $return->identifier = $context['identifier'];
  965. $return->page_title = isset($context['title']) ? $context['title'] : '';
  966. $return->keyword = $context['keyword'];
  967. if (!empty($context->empty)) {
  968. $context->placeholder = array(
  969. 'type' => 'context',
  970. 'conf' => $context,
  971. );
  972. }
  973. return $return;
  974. }
  975. }
  976. }
  977. /**
  978. * Retrieve a list of base contexts based upon a simple 'contexts' definition.
  979. *
  980. * For required contexts this will always retrieve placeholders.
  981. *
  982. * @param $contexts
  983. * The list of contexts defined in the UI.
  984. * @param $type
  985. * Either 'context' or 'requiredcontext', which indicates whether the contexts
  986. * are loaded from internal data or copied from an external source.
  987. * @param $placeholders
  988. * If true, placeholders are acceptable.
  989. */
  990. function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) {
  991. $return = array();
  992. foreach ($contexts as $context) {
  993. $ctext = ctools_context_get_context_from_context($context, $type);
  994. if ($ctext) {
  995. if ($placeholders) {
  996. $ctext->placeholder = TRUE;
  997. }
  998. $return[ctools_context_id($context, $type)] = $ctext;
  999. }
  1000. }
  1001. return $return;
  1002. }
  1003. /**
  1004. * Match up external contexts to our required contexts.
  1005. *
  1006. * This function is used to create a list of contexts with proper
  1007. * IDs based upon a list of required contexts.
  1008. *
  1009. * These contexts passed in should match the numeric positions of the
  1010. * required contexts. The caller must ensure this has already happened
  1011. * correctly as this function will not detect errors here.
  1012. *
  1013. * @param $required
  1014. * A list of required contexts as defined by the UI.
  1015. * @param $contexts
  1016. * A list of matching contexts as passed in from the calling system.
  1017. */
  1018. function ctools_context_match_required_contexts($required, $contexts) {
  1019. $return = array();
  1020. if (!is_array($required)) {
  1021. return $return;
  1022. }
  1023. foreach ($required as $r) {
  1024. $context = clone(array_shift($contexts));
  1025. $context->identifier = $r['identifier'];
  1026. $context->page_title = isset($r['title']) ? $r['title'] : '';
  1027. $context->keyword = $r['keyword'];
  1028. $return[ctools_context_id($r, 'requiredcontext')] = $context;
  1029. }
  1030. return $return;
  1031. }
  1032. /**
  1033. * Load a full array of contexts for an object.
  1034. *
  1035. * Not all of the types need to be supported by this object.
  1036. *
  1037. * This function is not used to load contexts from external data, but may
  1038. * be used to load internal contexts and relationships. Otherwise it can also
  1039. * be used to generate a full set of placeholders for UI purposes.
  1040. *
  1041. * @param $object
  1042. * An object that contains some or all of the following variables:
  1043. *
  1044. * - requiredcontexts: A list of UI configured contexts that are required
  1045. * from an external source. Since these require external data, they will
  1046. * only be added if $placeholders is set to TRUE, and empty contexts will
  1047. * be created.
  1048. * - arguments: A list of UI configured arguments that will create contexts.
  1049. * Since these require external data, they will only be added if $placeholders
  1050. * is set to TRUE.
  1051. * - contexts: A list of UI configured contexts that have no external source,
  1052. * and are essentially hardcoded. For example, these might configure a
  1053. * particular node or a particular taxonomy term.
  1054. * - relationships: A list of UI configured contexts to be derived from other
  1055. * contexts that already exist from other sources. For example, these might
  1056. * be used to get a user object from a node via the node author relationship.
  1057. * @param $placeholders
  1058. * If TRUE, this will generate placeholder objects for types this function
  1059. * cannot load.
  1060. * @param $contexts
  1061. * An array of pre-existing contexts that will be part of the return value.
  1062. */
  1063. function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) {
  1064. if (!empty($object->base_contexts)) {
  1065. $contexts += $object->base_contexts;
  1066. }
  1067. if ($placeholders) {
  1068. // This will load empty contexts as placeholders for arguments that come
  1069. // from external sources. If this isn't set, it's assumed these context
  1070. // will already have been matched up and loaded.
  1071. if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) {
  1072. $contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders);
  1073. }
  1074. if (!empty($object->arguments) && is_array($object->arguments)) {
  1075. $contexts += ctools_context_get_placeholders_from_argument($object->arguments);
  1076. }
  1077. }
  1078. if (!empty($object->contexts) && is_array($object->contexts)) {
  1079. $contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders);
  1080. }
  1081. // add contexts from relationships
  1082. if (!empty($object->relationships) && is_array($object->relationships)) {
  1083. ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders);
  1084. }
  1085. return $contexts;
  1086. }
  1087. /**
  1088. * Return the first context with a form id from a list of contexts.
  1089. *
  1090. * This function is used to figure out which contexts represents 'the form'
  1091. * from a list of contexts. Only one contexts can actually be 'the form' for
  1092. * a given page, since the @code{<form>} tag can not be embedded within
  1093. * itself.
  1094. */
  1095. function ctools_context_get_form($contexts) {
  1096. if (!empty($contexts)) {
  1097. foreach ($contexts as $id => $context) {
  1098. // if a form shows its id as being a 'required context' that means the
  1099. // the context is external to this display and does not count.
  1100. if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') {
  1101. return $context;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. /**
  1107. * Replace placeholders with real contexts using data extracted from a form
  1108. * for the purposes of previews.
  1109. *
  1110. * @param $contexts
  1111. * All of the contexts, including the placeholders.
  1112. * @param $arguments
  1113. * The arguments. These will be acquired from $form_state['values'] and the
  1114. * keys must match the context IDs.
  1115. *
  1116. * @return
  1117. * A new $contexts array containing the replaced contexts. Not all contexts
  1118. * may be replaced if, for example, an argument was unable to be converted
  1119. * into a context.
  1120. */
  1121. function ctools_context_replace_placeholders($contexts, $arguments) {
  1122. foreach ($contexts as $cid => $context) {
  1123. if (empty($context->empty)) {
  1124. continue;
  1125. }
  1126. $new_context = NULL;
  1127. switch ($context->placeholder['type']) {
  1128. case 'relationship':
  1129. $relationship = $context->placeholder['conf'];
  1130. if (isset($contexts[$relationship['context']])) {
  1131. $new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]);
  1132. }
  1133. break;
  1134. case 'argument':
  1135. if (isset($arguments[$cid]) && $arguments[$cid] !== '') {
  1136. $argument = $context->placeholder['conf'];
  1137. $new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]);
  1138. }
  1139. break;
  1140. case 'context':
  1141. if (!empty($arguments[$cid])) {
  1142. $context_info = $context->placeholder['conf'];
  1143. $new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]);
  1144. }
  1145. break;
  1146. }
  1147. if ($new_context && empty($new_context->empty)) {
  1148. $contexts[$cid] = $new_context;
  1149. }
  1150. }
  1151. return $contexts;
  1152. }
  1153. /**
  1154. * Provide a form array for getting data to replace placeholder contexts
  1155. * with real data.
  1156. */
  1157. function ctools_context_replace_form(&$form, $contexts) {
  1158. foreach ($contexts as $cid => $context) {
  1159. if (empty($context->empty)) {
  1160. continue;
  1161. }
  1162. // Get plugin info from the context which should have been set when the
  1163. // empty context was created.
  1164. $info = NULL;
  1165. $plugin = NULL;
  1166. $settings = NULL;
  1167. switch ($context->placeholder['type']) {
  1168. case 'argument':
  1169. $info = $context->placeholder['conf'];
  1170. $plugin = ctools_get_argument($info['name']);
  1171. break;
  1172. case 'context':
  1173. $info = $context->placeholder['conf'];
  1174. $plugin = ctools_get_context($info['name']);
  1175. break;
  1176. }
  1177. // Ask the plugin where the form is.
  1178. if ($plugin && isset($plugin['placeholder form'])) {
  1179. if (is_array($plugin['placeholder form'])) {
  1180. $form[$cid] = $plugin['placeholder form'];
  1181. }
  1182. else if (function_exists($plugin['placeholder form'])) {
  1183. $widget = $plugin['placeholder form']($info);
  1184. if ($widget) {
  1185. $form[$cid] = $widget;
  1186. }
  1187. }
  1188. if (!empty($form[$cid])) {
  1189. $form[$cid]['#title'] = t('@identifier (@keyword)', array('@keyword' => '%' . $context->keyword, '@identifier' => $context->identifier));
  1190. }
  1191. }
  1192. }
  1193. }
  1194. // ---------------------------------------------------------------------------
  1195. // Functions related to loading access control plugins
  1196. /**
  1197. * Fetch metadata on a specific access control plugin.
  1198. *
  1199. * @param $name
  1200. * Name of a plugin.
  1201. *
  1202. * @return
  1203. * An array with information about the requested access control plugin.
  1204. */
  1205. function ctools_get_access_plugin($name) {
  1206. ctools_include('plugins');
  1207. return ctools_get_plugins('ctools', 'access', $name);
  1208. }
  1209. /**
  1210. * Fetch metadata for all access control plugins.
  1211. *
  1212. * @return
  1213. * An array of arrays with information about all available access control plugins.
  1214. */
  1215. function ctools_get_access_plugins() {
  1216. ctools_include('plugins');
  1217. return ctools_get_plugins('ctools', 'access');
  1218. }
  1219. /**
  1220. * Fetch a list of access plugins that are available for a given list of
  1221. * contexts.
  1222. *
  1223. * if 'logged-in-user' is not in the list of contexts, it will be added as
  1224. * this is required.
  1225. */
  1226. function ctools_get_relevant_access_plugins($contexts) {
  1227. if (!isset($contexts['logged-in-user'])) {
  1228. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1229. }
  1230. $all_plugins = ctools_get_access_plugins();
  1231. $plugins = array();
  1232. foreach ($all_plugins as $id => $plugin) {
  1233. if (!empty($plugin['required context']) && !ctools_context_match_requirements($contexts, $plugin['required context'])) {
  1234. continue;
  1235. }
  1236. $plugins[$id] = $plugin;
  1237. }
  1238. return $plugins;
  1239. }
  1240. /**
  1241. * Create a context for the logged in user.
  1242. */
  1243. function ctools_access_get_loggedin_context() {
  1244. global $user;
  1245. $context = ctools_context_create('entity:user', $user);
  1246. $context->identifier = t('Logged in user');
  1247. $context->keyword = 'viewer';
  1248. $context->id = 0;
  1249. return $context;
  1250. }
  1251. /**
  1252. * Get a summary of an access plugin's settings.
  1253. */
  1254. function ctools_access_summary($plugin, $contexts, $test) {
  1255. if (!isset($contexts['logged-in-user'])) {
  1256. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1257. }
  1258. $description = '';
  1259. if ($function = ctools_plugin_get_function($plugin, 'summary')) {
  1260. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1261. $context = isset($test['context']) ? $test['context'] : array();
  1262. $description = $function($test['settings'], ctools_context_select($contexts, $required_context, $context), $plugin);
  1263. }
  1264. if (!empty($test['not'])) {
  1265. $description = "NOT ($description)";
  1266. }
  1267. return $description;
  1268. }
  1269. /**
  1270. * Get a summary of a group of access plugin's settings.
  1271. */
  1272. function ctools_access_group_summary($access, $contexts) {
  1273. if (empty($access['plugins'])) {
  1274. return;
  1275. }
  1276. $descriptions = array();
  1277. foreach ($access['plugins'] as $id => $test) {
  1278. $plugin = ctools_get_access_plugin($test['name']);
  1279. $descriptions[] = ctools_access_summary($plugin, $contexts, $test);
  1280. }
  1281. $separator = (isset($access['logic']) && $access['logic'] == 'and') ? t(', and ') : t(', or ');
  1282. return implode($separator, $descriptions);
  1283. }
  1284. /**
  1285. * Determine if the current user has access via plugin.
  1286. *
  1287. * @param $settings
  1288. * An array of settings theoretically set by the user.
  1289. * @param $contexts
  1290. * An array of zero or more contexts that may be used to determine if
  1291. * the user has access.
  1292. *
  1293. * @return
  1294. * TRUE if access is granted, false if otherwise.
  1295. */
  1296. function ctools_access($settings, $contexts = array()) {
  1297. if (empty($settings['plugins'])) {
  1298. return TRUE;
  1299. }
  1300. if (!isset($settings['logic'])) {
  1301. $settings['logic'] = 'and';
  1302. }
  1303. if (!isset($contexts['logged-in-user'])) {
  1304. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1305. }
  1306. foreach ($settings['plugins'] as $test) {
  1307. $pass = FALSE;
  1308. $plugin = ctools_get_access_plugin($test['name']);
  1309. if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) {
  1310. // Do we need just some contexts or all of them?
  1311. if (!empty($plugin['all contexts'])) {
  1312. $test_contexts = $contexts;
  1313. }
  1314. else {
  1315. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1316. $context = isset($test['context']) ? $test['context'] : array();
  1317. $test_contexts = ctools_context_select($contexts, $required_context, $context);
  1318. }
  1319. $pass = $function($test['settings'], $test_contexts, $plugin);
  1320. if (!empty($test['not'])) {
  1321. $pass = !$pass;
  1322. }
  1323. }
  1324. if ($pass && $settings['logic'] == 'or') {
  1325. // Pass if 'or' and this rule passed.
  1326. return TRUE;
  1327. }
  1328. else if (!$pass && $settings['logic'] == 'and') {
  1329. // Fail if 'and' and htis rule failed.
  1330. return FALSE;
  1331. }
  1332. }
  1333. // Return TRUE if logic was and, meaning all rules passed.
  1334. // Return FALSE if logic was or, meaning no rule passed.
  1335. return $settings['logic'] == 'and';
  1336. }
  1337. /**
  1338. * Create default settings for a new access plugin.
  1339. *
  1340. * @param $plugin
  1341. * The access plugin being used.
  1342. *
  1343. * @return
  1344. * A default configured test that should be placed in $access['plugins'];
  1345. */
  1346. function ctools_access_new_test($plugin) {
  1347. $test = array(
  1348. 'name' => $plugin['name'],
  1349. 'settings' => array(),
  1350. );
  1351. // Set up required context defaults.
  1352. if (isset($plugin['required context'])) {
  1353. if (is_object($plugin['required context'])) {
  1354. $test['context'] = '';
  1355. }
  1356. else {
  1357. $test['context'] = array();
  1358. foreach ($plugin['required context'] as $required) {
  1359. $test['context'][] = '';
  1360. }
  1361. }
  1362. }
  1363. $default = NULL;
  1364. if (isset($plugin['default'])) {
  1365. $default = $plugin['default'];
  1366. }
  1367. elseif (isset($plugin['defaults'])) {
  1368. $default = $plugin['defaults'];
  1369. }
  1370. // Setup plugin defaults.
  1371. if (isset($default)) {
  1372. if (is_array($default)) {
  1373. $test['settings'] = $default;
  1374. }
  1375. else if (function_exists($default)) {
  1376. $test['settings'] = $default();
  1377. }
  1378. else {
  1379. $test['settings'] = array();
  1380. }
  1381. }
  1382. return $test;
  1383. }
  1384. /**
  1385. * Apply restrictions to contexts based upon the access control configured.
  1386. *
  1387. * These restrictions allow the UI to not show content that may not
  1388. * be relevant to all types of a particular context.
  1389. */
  1390. function ctools_access_add_restrictions($settings, $contexts) {
  1391. if (empty($settings['plugins'])) {
  1392. return;
  1393. }
  1394. if (!isset($settings['logic'])) {
  1395. $settings['logic'] = 'and';
  1396. }
  1397. // We're not going to try to figure out restrictions on the or.
  1398. if ($settings['logic'] == 'or' && count($settings['plugins']) > 1) {
  1399. return;
  1400. }
  1401. foreach ($settings['plugins'] as $test) {
  1402. $plugin = ctools_get_access_plugin($test['name']);
  1403. if ($plugin && $function = ctools_plugin_get_function($plugin, 'restrictions')) {
  1404. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1405. $context = isset($test['context']) ? $test['context'] : array();
  1406. $contexts = ctools_context_select($contexts, $required_context, $context);
  1407. $function($test['settings'], $contexts);
  1408. }
  1409. }
  1410. }