context.inc 47 KB

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