context.inc 47 KB

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