context.inc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. * @param $context
  451. * The context object
  452. * @param $converter
  453. * The converter to use, which should be a string provided by the converter list.
  454. * @param $converter_options
  455. * A n array of options to pass on to the generation function. For contexts
  456. * that use token module, of particular use is 'sanitize' => FALSE which can
  457. * get raw tokens. This should ONLY be used in values that will later be
  458. * treated as unsafe user input since these values are by themselves unsafe.
  459. * It is particularly useful to get raw values from Field API.
  460. */
  461. function ctools_context_convert_context($context, $converter, $converter_options = array()) {
  462. // Contexts without plugins might be optional placeholders.
  463. if (empty($context->plugin)) {
  464. return;
  465. }
  466. $value = $context->argument;
  467. $plugin = ctools_get_context($context->plugin);
  468. if ($function = ctools_plugin_get_function($plugin, 'convert')) {
  469. $value = $function($context, $converter, $converter_options);
  470. }
  471. foreach (module_implements('ctools_context_converter_alter') as $module) {
  472. $function = $module . '_ctools_context_converter_alter';
  473. $function($context, $converter, $value, $converter_options);
  474. }
  475. return $value;
  476. }
  477. /**
  478. * Choose a context or contexts based upon the selection made via
  479. * ctools_context_filter.
  480. *
  481. * @param $contexts
  482. * A keyed array of all available contexts
  483. * @param $required
  484. * The required context object provided by the plugin
  485. * @param $context
  486. * The selection made using ctools_context_selector
  487. */
  488. function ctools_context_select($contexts, $required, $context) {
  489. if (is_array($required)) {
  490. $result = array();
  491. foreach ($required as $id => $r) {
  492. if (empty($required[$id])) {
  493. continue;
  494. }
  495. if (($result[] = _ctools_context_select($contexts, $r, $context[$id])) === FALSE) {
  496. return FALSE;
  497. }
  498. }
  499. return $result;
  500. }
  501. return _ctools_context_select($contexts, $required, $context);
  502. }
  503. function _ctools_context_select($contexts, $required, $context) {
  504. if (!is_object($required)) {
  505. return FALSE;
  506. }
  507. return $required->select($contexts, $context);
  508. }
  509. /**
  510. * Create a new context object.
  511. *
  512. * @param $type
  513. * The type of context to create; this loads a plugin.
  514. * @param $data
  515. * The data to put into the context.
  516. * @param $empty
  517. * Whether or not this context is specifically empty.
  518. * @param $conf
  519. * A configuration structure if this context was created via UI.
  520. *
  521. * @return
  522. * A $context or NULL if one could not be created.
  523. */
  524. function ctools_context_create($type, $data = NULL, $conf = FALSE) {
  525. ctools_include('plugins');
  526. $plugin = ctools_get_context($type);
  527. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  528. return $function(FALSE, $data, $conf, $plugin);
  529. }
  530. }
  531. /**
  532. * Create an empty context object.
  533. *
  534. * Empty context objects are primarily used as placeholders in the UI where
  535. * the actual contents of a context object may not be known. It may have
  536. * additional text embedded to give the user clues as to how the context
  537. * is used.
  538. *
  539. * @param $type
  540. * The type of context to create; this loads a plugin.
  541. *
  542. * @return
  543. * A $context or NULL if one could not be created.
  544. */
  545. function ctools_context_create_empty($type) {
  546. $plugin = ctools_get_context($type);
  547. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  548. $context = $function(TRUE, NULL, FALSE, $plugin);
  549. if (is_object($context)) {
  550. $context->empty = TRUE;
  551. }
  552. return $context;
  553. }
  554. }
  555. /**
  556. * Perform keyword and context substitutions.
  557. */
  558. function ctools_context_keyword_substitute($string, $keywords, $contexts, $converter_options = array()) {
  559. // Ensure a default keyword exists:
  560. $keywords['%%'] = '%';
  561. // Match contexts to the base keywords:
  562. $context_keywords = array();
  563. foreach ($contexts as $context) {
  564. if (isset($context->keyword)) {
  565. $context_keywords[$context->keyword] = $context;
  566. }
  567. }
  568. // Look for context matches we we only have to convert known matches.
  569. $matches = array();
  570. if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) {
  571. foreach ($matches[1] as $keyword) {
  572. // Ignore anything it finds with %%.
  573. if ($keyword[0] == '%') {
  574. continue;
  575. }
  576. // If the keyword is already set by something passed in, don't try to
  577. // overwrite it.
  578. if (!empty($keywords['%' . $keyword])) {
  579. continue;
  580. }
  581. // Figure out our keyword and converter, if specified.
  582. if (strpos($keyword, ':')) {
  583. list($context, $converter) = explode(':', $keyword, 2);
  584. }
  585. else {
  586. $context = $keyword;
  587. if (isset($context_keywords[$keyword])) {
  588. $plugin = ctools_get_context($context_keywords[$context]->plugin);
  589. // Fall back to a default converter, if specified.
  590. if ($plugin && !empty($plugin['convert default'])) {
  591. $converter = $plugin['convert default'];
  592. }
  593. }
  594. }
  595. if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
  596. $keywords['%' . $keyword] = '';
  597. }
  598. else if (!empty($converter)) {
  599. $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options);
  600. }
  601. else {
  602. $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
  603. }
  604. }
  605. }
  606. return strtr($string, $keywords);
  607. }
  608. /**
  609. * Determine a unique context ID for a context
  610. *
  611. * Often contexts of many different types will be placed into a list. This
  612. * ensures that even though contexts of multiple types may share IDs, they
  613. * are unique in the final list.
  614. */
  615. function ctools_context_id($context, $type = 'context') {
  616. if (!$context['id']) {
  617. $context['id'] = 1;
  618. }
  619. return $type . '_' . $context['name'] . '_' . $context['id'];
  620. }
  621. /**
  622. * Get the next id available given a list of already existing objects.
  623. *
  624. * This finds the next id available for the named object.
  625. *
  626. * @param $objects
  627. * A list of context descriptor objects, i.e, arguments, relationships, contexts, etc.
  628. * @param $name
  629. * The name being used.
  630. */
  631. function ctools_context_next_id($objects, $name) {
  632. $id = 0;
  633. // Figure out which instance of this argument we're creating
  634. if (!$objects) {
  635. return $id + 1;
  636. }
  637. foreach ($objects as $object) {
  638. if (isset($object['name']) && $object['name'] == $name) {
  639. if ($object['id'] > $id) {
  640. $id = $object['id'];
  641. }
  642. }
  643. }
  644. return $id + 1;
  645. }
  646. // ---------------------------------------------------------------------------
  647. // Functions related to contexts from arguments.
  648. /**
  649. * Fetch metadata on a specific argument plugin.
  650. *
  651. * @param $argument
  652. * Name of an argument plugin.
  653. *
  654. * @return
  655. * An array with information about the requested argument plugin.
  656. */
  657. function ctools_get_argument($argument) {
  658. ctools_include('plugins');
  659. return ctools_get_plugins('ctools', 'arguments', $argument);
  660. }
  661. /**
  662. * Fetch metadata for all argument plugins.
  663. *
  664. * @return
  665. * An array of arrays with information about all available argument plugins.
  666. */
  667. function ctools_get_arguments() {
  668. ctools_include('plugins');
  669. return ctools_get_plugins('ctools', 'arguments');
  670. }
  671. /**
  672. * Get a context from an argument.
  673. *
  674. * @param $argument
  675. * The configuration of an argument. It must contain the following data:
  676. * - name: The name of the argument plugin being used.
  677. * - argument_settings: The configuration based upon the plugin forms.
  678. * - identifier: The human readable identifier for this argument, usually
  679. * defined by the UI.
  680. * - keyword: The keyword used for this argument for substitutions.
  681. *
  682. * @param $arg
  683. * The actual argument received. This is expected to be a string from a URL but
  684. * this does not have to be the only source of arguments.
  685. * @param $empty
  686. * If true, the $arg will not be used to load the context. Instead, an empty
  687. * placeholder context will be loaded.
  688. *
  689. * @return
  690. * A context object if one can be loaded.
  691. */
  692. function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) {
  693. ctools_include('plugins');
  694. if (empty($argument['name'])) {
  695. return;
  696. }
  697. if ($function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context')) {
  698. // Backward compatibility: Merge old style settings into new style:
  699. if (!empty($argument['settings'])) {
  700. $argument += $argument['settings'];
  701. unset($argument['settings']);
  702. }
  703. $context = $function($arg, $argument, $empty);
  704. if (is_object($context)) {
  705. $context->identifier = $argument['identifier'];
  706. $context->page_title = isset($argument['title']) ? $argument['title'] : '';
  707. $context->keyword = $argument['keyword'];
  708. $context->id = ctools_context_id($argument, 'argument');
  709. $context->original_argument = $arg;
  710. if (!empty($context->empty)) {
  711. $context->placeholder = array(
  712. 'type' => 'argument',
  713. 'conf' => $argument,
  714. );
  715. }
  716. }
  717. return $context;
  718. }
  719. }
  720. /**
  721. * Retrieve a list of empty contexts for all arguments.
  722. */
  723. function ctools_context_get_placeholders_from_argument($arguments) {
  724. $contexts = array();
  725. foreach ($arguments as $argument) {
  726. $context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
  727. if ($context) {
  728. $contexts[ctools_context_id($argument, 'argument')] = $context;
  729. }
  730. }
  731. return $contexts;
  732. }
  733. /**
  734. * Load the contexts for a given list of arguments.
  735. *
  736. * @param $arguments
  737. * The array of argument definitions.
  738. * @param &$contexts
  739. * The array of existing contexts. New contexts will be added to this array.
  740. * @param $args
  741. * The arguments to load.
  742. *
  743. * @return
  744. * FALSE if an argument wants to 404.
  745. */
  746. function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) {
  747. foreach ($arguments as $argument) {
  748. // pull the argument off the list.
  749. $arg = array_shift($args);
  750. $id = ctools_context_id($argument, 'argument');
  751. // For % arguments embedded in the URL, our context is already loaded.
  752. // There is no need to go and load it again.
  753. if (empty($contexts[$id])) {
  754. if ($context = ctools_context_get_context_from_argument($argument, $arg)) {
  755. $contexts[$id] = $context;
  756. }
  757. }
  758. else {
  759. $context = $contexts[$id];
  760. }
  761. if ((empty($context) || empty($context->data)) && !empty($argument['default']) && $argument['default'] == '404') {
  762. return FALSE;
  763. }
  764. }
  765. return TRUE;
  766. }
  767. // ---------------------------------------------------------------------------
  768. // Functions related to contexts from relationships.
  769. /**
  770. * Fetch metadata on a specific relationship plugin.
  771. *
  772. * @param $content type
  773. * Name of a panel content type.
  774. *
  775. * @return
  776. * An array with information about the requested relationship.
  777. */
  778. function ctools_get_relationship($relationship) {
  779. ctools_include('plugins');
  780. return ctools_get_plugins('ctools', 'relationships', $relationship);
  781. }
  782. /**
  783. * Fetch metadata for all relationship plugins.
  784. *
  785. * @return
  786. * An array of arrays with information about all available relationships.
  787. */
  788. function ctools_get_relationships() {
  789. ctools_include('plugins');
  790. return ctools_get_plugins('ctools', 'relationships');
  791. }
  792. /**
  793. *
  794. * @param $relationship
  795. * The configuration of a relationship. It must contain the following data:
  796. * - name: The name of the relationship plugin being used.
  797. * - relationship_settings: The configuration based upon the plugin forms.
  798. * - identifier: The human readable identifier for this relationship, usually
  799. * defined by the UI.
  800. * - keyword: The keyword used for this relationship for substitutions.
  801. *
  802. * @param $source_context
  803. * The context this relationship is based upon.
  804. *
  805. * @param $placeholders
  806. * If TRUE, placeholders are acceptable.
  807. *
  808. * @return
  809. * A context object if one can be loaded.
  810. */
  811. function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) {
  812. ctools_include('plugins');
  813. if ($function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context')) {
  814. // Backward compatibility: Merge old style settings into new style:
  815. if (!empty($relationship['relationship_settings'])) {
  816. $relationship += $relationship['relationship_settings'];
  817. unset($relationship['relationship_settings']);
  818. }
  819. $context = $function($source_context, $relationship, $placeholders);
  820. if ($context) {
  821. $context->identifier = $relationship['identifier'];
  822. $context->page_title = isset($relationship['title']) ? $relationship['title'] : '';
  823. $context->keyword = $relationship['keyword'];
  824. if (!empty($context->empty)) {
  825. $context->placeholder = array(
  826. 'type' => 'relationship',
  827. 'conf' => $relationship,
  828. );
  829. }
  830. return $context;
  831. }
  832. }
  833. }
  834. /**
  835. * Fetch all relevant relationships.
  836. *
  837. * Relevant relationships are any relationship that can be created based upon
  838. * the list of existing contexts. For example, the 'node author' relationship
  839. * is relevant if there is a 'node' context, but makes no sense if there is
  840. * not one.
  841. *
  842. * @param $contexts
  843. * An array of contexts used to figure out which relationships are relevant.
  844. *
  845. * @return
  846. * An array of relationship keys that are relevant for the given set of
  847. * contexts.
  848. */
  849. function ctools_context_get_relevant_relationships($contexts) {
  850. $relevant = array();
  851. $relationships = ctools_get_relationships();
  852. // Go through each relationship
  853. foreach ($relationships as $rid => $relationship) {
  854. // For each relationship, see if there is a context that satisfies it.
  855. if (empty($relationship['no ui']) && ctools_context_filter($contexts, $relationship['required context'])) {
  856. $relevant[$rid] = $relationship['title'];
  857. }
  858. }
  859. return $relevant;
  860. }
  861. /**
  862. * Fetch all active relationships
  863. *
  864. * @param $relationships
  865. * An keyed array of relationship data including:
  866. * - name: name of relationship
  867. * - context: context id relationship belongs to. This will be used to
  868. * identify which context in the $contexts array to use to create the
  869. * relationship context.
  870. *
  871. * @param $contexts
  872. * A keyed array of contexts used to figure out which relationships
  873. * are relevant. New contexts will be added to this.
  874. *
  875. * @param $placeholders
  876. * If TRUE, placeholders are acceptable.
  877. */
  878. function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) {
  879. $return = array();
  880. foreach ($relationships as $rdata) {
  881. if (!isset($rdata['context'])) {
  882. continue;
  883. }
  884. if (is_array($rdata['context'])) {
  885. $rcontexts = array();
  886. foreach ($rdata['context'] as $cid) {
  887. if (empty($contexts[$cid])) {
  888. continue 2;
  889. }
  890. $rcontexts[] = $contexts[$cid];
  891. }
  892. }
  893. else {
  894. if (empty($contexts[$rdata['context']])) {
  895. continue;
  896. }
  897. $rcontexts = $contexts[$rdata['context']];
  898. }
  899. $cid = ctools_context_id($rdata, 'relationship');
  900. if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) {
  901. $contexts[$cid] = $context;
  902. }
  903. }
  904. }
  905. // ---------------------------------------------------------------------------
  906. // Functions related to loading contexts from simple context definitions.
  907. /**
  908. * Fetch metadata on a specific context plugin.
  909. *
  910. * @param $context
  911. * Name of a context.
  912. *
  913. * @return
  914. * An array with information about the requested panel context.
  915. */
  916. function ctools_get_context($context) {
  917. static $gate = array();
  918. ctools_include('plugins');
  919. $plugin = ctools_get_plugins('ctools', 'contexts', $context);
  920. if (empty($gate['context']) && !empty($plugin['superceded by'])) {
  921. // This gate prevents infinite loops.
  922. $gate[$context] = TRUE;
  923. $new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']);
  924. $gate[$context] = FALSE;
  925. // If a new plugin was returned, return it. Otherwise fall through and
  926. // return the original we fetched.
  927. if ($new_plugin) {
  928. return $new_plugin;
  929. }
  930. }
  931. return $plugin;
  932. }
  933. /**
  934. * Fetch metadata for all context plugins.
  935. *
  936. * @return
  937. * An array of arrays with information about all available panel contexts.
  938. */
  939. function ctools_get_contexts() {
  940. ctools_include('plugins');
  941. return ctools_get_plugins('ctools', 'contexts');
  942. }
  943. /**
  944. *
  945. * @param $context
  946. * The configuration of a context. It must contain the following data:
  947. * - name: The name of the context plugin being used.
  948. * - context_settings: The configuration based upon the plugin forms.
  949. * - identifier: The human readable identifier for this context, usually
  950. * defined by the UI.
  951. * - keyword: The keyword used for this context for substitutions.
  952. * @param $type
  953. * This is either 'context' which indicates the context will be loaded
  954. * from data in the settings, or 'required_context' which means the
  955. * context must be acquired from an external source. This is the method
  956. * used to pass pure contexts from one system to another.
  957. *
  958. * @return
  959. * A context object if one can be loaded.
  960. */
  961. function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) {
  962. ctools_include('plugins');
  963. $plugin = ctools_get_context($context['name']);
  964. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  965. // Backward compatibility: Merge old style settings into new style:
  966. if (!empty($context['context_settings'])) {
  967. $context += $context['context_settings'];
  968. unset($context['context_settings']);
  969. }
  970. if (isset($argument) && isset($plugin['placeholder name'])) {
  971. $context[$plugin['placeholder name']] = $argument;
  972. }
  973. $return = $function($type == 'requiredcontext', $context, TRUE, $plugin);
  974. if ($return) {
  975. $return->identifier = $context['identifier'];
  976. $return->page_title = isset($context['title']) ? $context['title'] : '';
  977. $return->keyword = $context['keyword'];
  978. if (!empty($context->empty)) {
  979. $context->placeholder = array(
  980. 'type' => 'context',
  981. 'conf' => $context,
  982. );
  983. }
  984. return $return;
  985. }
  986. }
  987. }
  988. /**
  989. * Retrieve a list of base contexts based upon a simple 'contexts' definition.
  990. *
  991. * For required contexts this will always retrieve placeholders.
  992. *
  993. * @param $contexts
  994. * The list of contexts defined in the UI.
  995. * @param $type
  996. * Either 'context' or 'requiredcontext', which indicates whether the contexts
  997. * are loaded from internal data or copied from an external source.
  998. * @param $placeholders
  999. * If true, placeholders are acceptable.
  1000. */
  1001. function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) {
  1002. $return = array();
  1003. foreach ($contexts as $context) {
  1004. $ctext = ctools_context_get_context_from_context($context, $type);
  1005. if ($ctext) {
  1006. if ($placeholders) {
  1007. $ctext->placeholder = TRUE;
  1008. }
  1009. $return[ctools_context_id($context, $type)] = $ctext;
  1010. }
  1011. }
  1012. return $return;
  1013. }
  1014. /**
  1015. * Match up external contexts to our required contexts.
  1016. *
  1017. * This function is used to create a list of contexts with proper
  1018. * IDs based upon a list of required contexts.
  1019. *
  1020. * These contexts passed in should match the numeric positions of the
  1021. * required contexts. The caller must ensure this has already happened
  1022. * correctly as this function will not detect errors here.
  1023. *
  1024. * @param $required
  1025. * A list of required contexts as defined by the UI.
  1026. * @param $contexts
  1027. * A list of matching contexts as passed in from the calling system.
  1028. */
  1029. function ctools_context_match_required_contexts($required, $contexts) {
  1030. $return = array();
  1031. if (!is_array($required)) {
  1032. return $return;
  1033. }
  1034. foreach ($required as $r) {
  1035. $context = clone(array_shift($contexts));
  1036. $context->identifier = $r['identifier'];
  1037. $context->page_title = isset($r['title']) ? $r['title'] : '';
  1038. $context->keyword = $r['keyword'];
  1039. $return[ctools_context_id($r, 'requiredcontext')] = $context;
  1040. }
  1041. return $return;
  1042. }
  1043. /**
  1044. * Load a full array of contexts for an object.
  1045. *
  1046. * Not all of the types need to be supported by this object.
  1047. *
  1048. * This function is not used to load contexts from external data, but may
  1049. * be used to load internal contexts and relationships. Otherwise it can also
  1050. * be used to generate a full set of placeholders for UI purposes.
  1051. *
  1052. * @param $object
  1053. * An object that contains some or all of the following variables:
  1054. *
  1055. * - requiredcontexts: A list of UI configured contexts that are required
  1056. * from an external source. Since these require external data, they will
  1057. * only be added if $placeholders is set to TRUE, and empty contexts will
  1058. * be created.
  1059. * - arguments: A list of UI configured arguments that will create contexts.
  1060. * Since these require external data, they will only be added if $placeholders
  1061. * is set to TRUE.
  1062. * - contexts: A list of UI configured contexts that have no external source,
  1063. * and are essentially hardcoded. For example, these might configure a
  1064. * particular node or a particular taxonomy term.
  1065. * - relationships: A list of UI configured contexts to be derived from other
  1066. * contexts that already exist from other sources. For example, these might
  1067. * be used to get a user object from a node via the node author relationship.
  1068. * @param $placeholders
  1069. * If TRUE, this will generate placeholder objects for types this function
  1070. * cannot load.
  1071. * @param $contexts
  1072. * An array of pre-existing contexts that will be part of the return value.
  1073. */
  1074. function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) {
  1075. if (!empty($object->base_contexts)) {
  1076. $contexts += $object->base_contexts;
  1077. }
  1078. if ($placeholders) {
  1079. // This will load empty contexts as placeholders for arguments that come
  1080. // from external sources. If this isn't set, it's assumed these context
  1081. // will already have been matched up and loaded.
  1082. if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) {
  1083. $contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders);
  1084. }
  1085. if (!empty($object->arguments) && is_array($object->arguments)) {
  1086. $contexts += ctools_context_get_placeholders_from_argument($object->arguments);
  1087. }
  1088. }
  1089. if (!empty($object->contexts) && is_array($object->contexts)) {
  1090. $contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders);
  1091. }
  1092. // add contexts from relationships
  1093. if (!empty($object->relationships) && is_array($object->relationships)) {
  1094. ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders);
  1095. }
  1096. return $contexts;
  1097. }
  1098. /**
  1099. * Return the first context with a form id from a list of contexts.
  1100. *
  1101. * This function is used to figure out which contexts represents 'the form'
  1102. * from a list of contexts. Only one contexts can actually be 'the form' for
  1103. * a given page, since the @code{<form>} tag can not be embedded within
  1104. * itself.
  1105. */
  1106. function ctools_context_get_form($contexts) {
  1107. if (!empty($contexts)) {
  1108. foreach ($contexts as $id => $context) {
  1109. // if a form shows its id as being a 'required context' that means the
  1110. // the context is external to this display and does not count.
  1111. if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') {
  1112. return $context;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. /**
  1118. * Replace placeholders with real contexts using data extracted from a form
  1119. * for the purposes of previews.
  1120. *
  1121. * @param $contexts
  1122. * All of the contexts, including the placeholders.
  1123. * @param $arguments
  1124. * The arguments. These will be acquired from $form_state['values'] and the
  1125. * keys must match the context IDs.
  1126. *
  1127. * @return
  1128. * A new $contexts array containing the replaced contexts. Not all contexts
  1129. * may be replaced if, for example, an argument was unable to be converted
  1130. * into a context.
  1131. */
  1132. function ctools_context_replace_placeholders($contexts, $arguments) {
  1133. foreach ($contexts as $cid => $context) {
  1134. if (empty($context->empty)) {
  1135. continue;
  1136. }
  1137. $new_context = NULL;
  1138. switch ($context->placeholder['type']) {
  1139. case 'relationship':
  1140. $relationship = $context->placeholder['conf'];
  1141. if (isset($contexts[$relationship['context']])) {
  1142. $new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]);
  1143. }
  1144. break;
  1145. case 'argument':
  1146. if (isset($arguments[$cid]) && $arguments[$cid] !== '') {
  1147. $argument = $context->placeholder['conf'];
  1148. $new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]);
  1149. }
  1150. break;
  1151. case 'context':
  1152. if (!empty($arguments[$cid])) {
  1153. $context_info = $context->placeholder['conf'];
  1154. $new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]);
  1155. }
  1156. break;
  1157. }
  1158. if ($new_context && empty($new_context->empty)) {
  1159. $contexts[$cid] = $new_context;
  1160. }
  1161. }
  1162. return $contexts;
  1163. }
  1164. /**
  1165. * Provide a form array for getting data to replace placeholder contexts
  1166. * with real data.
  1167. */
  1168. function ctools_context_replace_form(&$form, $contexts) {
  1169. foreach ($contexts as $cid => $context) {
  1170. if (empty($context->empty)) {
  1171. continue;
  1172. }
  1173. // Get plugin info from the context which should have been set when the
  1174. // empty context was created.
  1175. $info = NULL;
  1176. $plugin = NULL;
  1177. $settings = NULL;
  1178. switch ($context->placeholder['type']) {
  1179. case 'argument':
  1180. $info = $context->placeholder['conf'];
  1181. $plugin = ctools_get_argument($info['name']);
  1182. break;
  1183. case 'context':
  1184. $info = $context->placeholder['conf'];
  1185. $plugin = ctools_get_context($info['name']);
  1186. break;
  1187. }
  1188. // Ask the plugin where the form is.
  1189. if ($plugin && isset($plugin['placeholder form'])) {
  1190. if (is_array($plugin['placeholder form'])) {
  1191. $form[$cid] = $plugin['placeholder form'];
  1192. }
  1193. else if (function_exists($plugin['placeholder form'])) {
  1194. $widget = $plugin['placeholder form']($info);
  1195. if ($widget) {
  1196. $form[$cid] = $widget;
  1197. }
  1198. }
  1199. if (!empty($form[$cid])) {
  1200. $form[$cid]['#title'] = t('@identifier (@keyword)', array('@keyword' => '%' . $context->keyword, '@identifier' => $context->identifier));
  1201. }
  1202. }
  1203. }
  1204. }
  1205. // ---------------------------------------------------------------------------
  1206. // Functions related to loading access control plugins
  1207. /**
  1208. * Fetch metadata on a specific access control plugin.
  1209. *
  1210. * @param $name
  1211. * Name of a plugin.
  1212. *
  1213. * @return
  1214. * An array with information about the requested access control plugin.
  1215. */
  1216. function ctools_get_access_plugin($name) {
  1217. ctools_include('plugins');
  1218. return ctools_get_plugins('ctools', 'access', $name);
  1219. }
  1220. /**
  1221. * Fetch metadata for all access control plugins.
  1222. *
  1223. * @return
  1224. * An array of arrays with information about all available access control plugins.
  1225. */
  1226. function ctools_get_access_plugins() {
  1227. ctools_include('plugins');
  1228. return ctools_get_plugins('ctools', 'access');
  1229. }
  1230. /**
  1231. * Fetch a list of access plugins that are available for a given list of
  1232. * contexts.
  1233. *
  1234. * if 'logged-in-user' is not in the list of contexts, it will be added as
  1235. * this is required.
  1236. */
  1237. function ctools_get_relevant_access_plugins($contexts) {
  1238. if (!isset($contexts['logged-in-user'])) {
  1239. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1240. }
  1241. $all_plugins = ctools_get_access_plugins();
  1242. $plugins = array();
  1243. foreach ($all_plugins as $id => $plugin) {
  1244. if (!empty($plugin['required context']) && !ctools_context_match_requirements($contexts, $plugin['required context'])) {
  1245. continue;
  1246. }
  1247. $plugins[$id] = $plugin;
  1248. }
  1249. return $plugins;
  1250. }
  1251. /**
  1252. * Create a context for the logged in user.
  1253. */
  1254. function ctools_access_get_loggedin_context() {
  1255. global $user;
  1256. $context = ctools_context_create('entity:user', $user);
  1257. $context->identifier = t('Logged in user');
  1258. $context->keyword = 'viewer';
  1259. $context->id = 0;
  1260. return $context;
  1261. }
  1262. /**
  1263. * Get a summary of an access plugin's settings.
  1264. */
  1265. function ctools_access_summary($plugin, $contexts, $test) {
  1266. if (!isset($contexts['logged-in-user'])) {
  1267. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1268. }
  1269. $description = '';
  1270. if ($function = ctools_plugin_get_function($plugin, 'summary')) {
  1271. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1272. $context = isset($test['context']) ? $test['context'] : array();
  1273. $description = $function($test['settings'], ctools_context_select($contexts, $required_context, $context), $plugin);
  1274. }
  1275. if (!empty($test['not'])) {
  1276. $description = "NOT ($description)";
  1277. }
  1278. return $description;
  1279. }
  1280. /**
  1281. * Get a summary of a group of access plugin's settings.
  1282. */
  1283. function ctools_access_group_summary($access, $contexts) {
  1284. if (empty($access['plugins'])) {
  1285. return;
  1286. }
  1287. $descriptions = array();
  1288. foreach ($access['plugins'] as $id => $test) {
  1289. $plugin = ctools_get_access_plugin($test['name']);
  1290. $descriptions[] = ctools_access_summary($plugin, $contexts, $test);
  1291. }
  1292. $separator = (isset($access['logic']) && $access['logic'] == 'and') ? t(', and ') : t(', or ');
  1293. return implode($separator, $descriptions);
  1294. }
  1295. /**
  1296. * Determine if the current user has access via plugin.
  1297. *
  1298. * @param $settings
  1299. * An array of settings theoretically set by the user.
  1300. * @param $contexts
  1301. * An array of zero or more contexts that may be used to determine if
  1302. * the user has access.
  1303. *
  1304. * @return
  1305. * TRUE if access is granted, false if otherwise.
  1306. */
  1307. function ctools_access($settings, $contexts = array()) {
  1308. if (empty($settings['plugins'])) {
  1309. return TRUE;
  1310. }
  1311. if (!isset($settings['logic'])) {
  1312. $settings['logic'] = 'and';
  1313. }
  1314. if (!isset($contexts['logged-in-user'])) {
  1315. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1316. }
  1317. foreach ($settings['plugins'] as $test) {
  1318. $pass = FALSE;
  1319. $plugin = ctools_get_access_plugin($test['name']);
  1320. if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) {
  1321. // Do we need just some contexts or all of them?
  1322. if (!empty($plugin['all contexts'])) {
  1323. $test_contexts = $contexts;
  1324. }
  1325. else {
  1326. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1327. $context = isset($test['context']) ? $test['context'] : array();
  1328. $test_contexts = ctools_context_select($contexts, $required_context, $context);
  1329. }
  1330. $pass = $function($test['settings'], $test_contexts, $plugin);
  1331. if (!empty($test['not'])) {
  1332. $pass = !$pass;
  1333. }
  1334. }
  1335. if ($pass && $settings['logic'] == 'or') {
  1336. // Pass if 'or' and this rule passed.
  1337. return TRUE;
  1338. }
  1339. else if (!$pass && $settings['logic'] == 'and') {
  1340. // Fail if 'and' and htis rule failed.
  1341. return FALSE;
  1342. }
  1343. }
  1344. // Return TRUE if logic was and, meaning all rules passed.
  1345. // Return FALSE if logic was or, meaning no rule passed.
  1346. return $settings['logic'] == 'and';
  1347. }
  1348. /**
  1349. * Create default settings for a new access plugin.
  1350. *
  1351. * @param $plugin
  1352. * The access plugin being used.
  1353. *
  1354. * @return
  1355. * A default configured test that should be placed in $access['plugins'];
  1356. */
  1357. function ctools_access_new_test($plugin) {
  1358. $test = array(
  1359. 'name' => $plugin['name'],
  1360. 'settings' => array(),
  1361. );
  1362. // Set up required context defaults.
  1363. if (isset($plugin['required context'])) {
  1364. if (is_object($plugin['required context'])) {
  1365. $test['context'] = '';
  1366. }
  1367. else {
  1368. $test['context'] = array();
  1369. foreach ($plugin['required context'] as $required) {
  1370. $test['context'][] = '';
  1371. }
  1372. }
  1373. }
  1374. $default = NULL;
  1375. if (isset($plugin['default'])) {
  1376. $default = $plugin['default'];
  1377. }
  1378. elseif (isset($plugin['defaults'])) {
  1379. $default = $plugin['defaults'];
  1380. }
  1381. // Setup plugin defaults.
  1382. if (isset($default)) {
  1383. if (is_array($default)) {
  1384. $test['settings'] = $default;
  1385. }
  1386. else if (function_exists($default)) {
  1387. $test['settings'] = $default();
  1388. }
  1389. else {
  1390. $test['settings'] = array();
  1391. }
  1392. }
  1393. return $test;
  1394. }
  1395. /**
  1396. * Apply restrictions to contexts based upon the access control configured.
  1397. *
  1398. * These restrictions allow the UI to not show content that may not
  1399. * be relevant to all types of a particular context.
  1400. */
  1401. function ctools_access_add_restrictions($settings, $contexts) {
  1402. if (empty($settings['plugins'])) {
  1403. return;
  1404. }
  1405. if (!isset($settings['logic'])) {
  1406. $settings['logic'] = 'and';
  1407. }
  1408. // We're not going to try to figure out restrictions on the or.
  1409. if ($settings['logic'] == 'or' && count($settings['plugins']) > 1) {
  1410. return;
  1411. }
  1412. foreach ($settings['plugins'] as $test) {
  1413. $plugin = ctools_get_access_plugin($test['name']);
  1414. if ($plugin && $function = ctools_plugin_get_function($plugin, 'restrictions')) {
  1415. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1416. $context = isset($test['context']) ? $test['context'] : array();
  1417. $contexts = ctools_context_select($contexts, $required_context, $context);
  1418. $function($test['settings'], $contexts);
  1419. }
  1420. }
  1421. }