context.inc 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. <?php
  2. /**
  3. * @file
  4. * Contains code related to the ctools system of 'context'.
  5. *
  6. * Context, originally from Panels, is a method of packaging objects into
  7. * a more generic bundle and providing a plugin system so that a UI can
  8. * take advantage of them. The idea is that the context objects
  9. * represent 'the context' that a given operation (usually a page view)
  10. * is operating in or on.
  11. *
  12. * For example, when viewing a page, the 'context' is a node object. When
  13. * viewing a user, the 'context' is a user object. Contexts can also
  14. * have related contexts. For example, when viewing a 'node' you may need
  15. * to know something about the node author. Therefore, the node author
  16. * is a related context.
  17. */
  18. /**
  19. * The context object is largely a wrapper around some other object, with
  20. * an interface to finding out what is contained and getting to both
  21. * the object and information about the object.
  22. *
  23. * Each context object has its own information, but some things are very
  24. * common, such as titles, data, keywords, etc. In particulare, the 'type'
  25. * of the context is important.
  26. */
  27. class ctools_context {
  28. /**
  29. * @var string|array
  30. * A string naming this specific context type. The values 'any' and 'none'
  31. * are special:
  32. * - 'any': used in is_type() to match any other type.
  33. * - 'none': used to signal the type is not defined.
  34. */
  35. public $type;
  36. /**
  37. * @var mixed
  38. * The data payload for this context object.
  39. */
  40. public $data;
  41. /**
  42. * @var string
  43. * The title of this object.
  44. */
  45. public $title;
  46. /**
  47. * @var string
  48. * The title of the page if this object exists
  49. */
  50. public $page_title;
  51. /**
  52. * @var string
  53. * The identifier (in the UI) of this object.
  54. */
  55. public $identifier;
  56. /**
  57. * @var
  58. */
  59. public $argument;
  60. /**
  61. * @var string
  62. */
  63. public $keyword;
  64. /**
  65. * @var
  66. */
  67. public $original_argument;
  68. /**
  69. * @var array
  70. */
  71. public $restrictions;
  72. /**
  73. * @var bool
  74. */
  75. public $empty;
  76. /**
  77. * The ctools_context constructor.
  78. *
  79. * @param string $type
  80. * The type name of this context. Should be unique. Use the machine_name
  81. * conventions: lowercase, short, underscores and no spaces.
  82. * @param mixed $data
  83. * The data payload, if required for this context.
  84. */
  85. public function __construct($type = 'none', $data = NULL) {
  86. $this->type = $type;
  87. $this->data = $data;
  88. $this->title = t('Unknown context');
  89. $this->page_title = '';
  90. $this->identifier = '';
  91. $this->keyword = '';
  92. $this->restrictions = array();
  93. $this->empty = FALSE;
  94. // Other vars are NULL.
  95. }
  96. /**
  97. * Determine whether this object is of type @var $type .
  98. *
  99. * Both the internal value ($this->type) and the supplied value ($type) can
  100. * be a string or an array of strings, and if one or both are arrays the match
  101. * succeeds if at least one common element is found.
  102. *
  103. * Type names
  104. *
  105. * @param string|array $type
  106. * 'type' can be:
  107. * - 'any' to match all types (this is true of the internal value too).
  108. * - an array of type name strings, when more than one type is acceptable.
  109. *
  110. * @return bool
  111. * True if the type matches, False otherwise.
  112. */
  113. public function is_type($type) {
  114. if ($type === 'any' || $this->type === 'any') {
  115. return TRUE;
  116. }
  117. $a = is_array($type) ? $type : array($type);
  118. $b = is_array($this->type) ? $this->type : array($this->type);
  119. return (bool) array_intersect($a, $b);
  120. }
  121. /**
  122. * Return the argument.
  123. *
  124. * @return mixed
  125. * The value of $argument.
  126. */
  127. public function get_argument() {
  128. return $this->argument;
  129. }
  130. /**
  131. * Return the value of argument (or arg) variable as it was passed in.
  132. *
  133. * For example see ctools_plugin_load_function() and ctools_terms_context().
  134. *
  135. * @return mixed
  136. * The original arg value.
  137. */
  138. public function get_original_argument() {
  139. if (!is_null($this->original_argument)) {
  140. return $this->original_argument;
  141. }
  142. return $this->argument;
  143. }
  144. /**
  145. * Return the keyword.
  146. *
  147. * @return mixed
  148. * The value of $keyword.
  149. */
  150. public function get_keyword() {
  151. return $this->keyword;
  152. }
  153. /**
  154. * Return the identifier.
  155. *
  156. * @return mixed
  157. * The value of $identifier.
  158. */
  159. public function get_identifier() {
  160. return $this->identifier;
  161. }
  162. /**
  163. * Return the title.
  164. *
  165. * @return mixed
  166. * The value of $title.
  167. */
  168. public function get_title() {
  169. return $this->title;
  170. }
  171. /**
  172. * Return the page title.
  173. *
  174. * @return mixed
  175. * The value of $page_title.
  176. */
  177. public function get_page_title() {
  178. return $this->page_title;
  179. }
  180. }
  181. /**
  182. * Used to create a method of comparing if a list of contexts
  183. * match a required context type.
  184. */
  185. class ctools_context_required {
  186. /**
  187. * @var array
  188. * Keyword strings associated with the context.
  189. */
  190. public $keywords;
  191. /**
  192. * If set, the title will be used in the selector to identify
  193. * the context. This is very useful when multiple contexts
  194. * are required to inform the user will be used for what.
  195. */
  196. public $title;
  197. /**
  198. * Test to see if this context is required.
  199. */
  200. public $required = TRUE;
  201. /**
  202. * If TRUE, skip the check in ctools_context_required::select()
  203. * for contexts whose names may have changed.
  204. */
  205. public $skip_name_check = FALSE;
  206. /**
  207. * The ctools_context_required constructor.
  208. *
  209. * Note: Constructor accepts a variable number of arguments, with optional
  210. * type-dependent args at the end of the list and one required argument,
  211. * the title. Note in particular that skip_name_check MUST be passed in as
  212. * a boolean (and not, for example, as an integer).
  213. *
  214. * @param string $title
  215. * The title of the context for use in UI selectors when multiple contexts
  216. * qualify.
  217. * @param string $keywords
  218. * One or more keywords to use for matching which contexts are allowed.
  219. * @param array $restrictions
  220. * Array of context restrictions.
  221. * @param bool $skip_name_check
  222. * If True, skip the check in select() for contexts whose names may have
  223. * changed.
  224. */
  225. public function __construct($title) {
  226. // If it was possible, using variadic syntax this should be:
  227. // __construct($title, string ...$keywords, array $restrictions = NULL, bool $skip = NULL)
  228. // but that form isn't allowed.
  229. $args = func_get_args();
  230. $this->title = array_shift($args);
  231. // If we have a boolean value at the end for $skip_name_check, store it.
  232. if (is_bool(end($args))) {
  233. $this->skip_name_check = array_pop($args);
  234. }
  235. // If we were given restrictions at the end, store them.
  236. if (count($args) > 1 && is_array(end($args))) {
  237. $this->restrictions = array_pop($args);
  238. }
  239. if (count($args) === 1) {
  240. $args = array_shift($args);
  241. }
  242. $this->keywords = $args;
  243. }
  244. /**
  245. * Filter the contexts to determine which apply in the current environment.
  246. *
  247. * A context passes the filter if:
  248. * - the context matches 'type' of the required keywords (uses
  249. * ctools_context::is_type(), so includes 'any' matches, etc).
  250. * - AND if restrictions are present, there are some common elements between
  251. * the requirement and the context.
  252. *
  253. * @param array $contexts
  254. * An array of ctools_context objects (or something which will cast to an
  255. * array of them). The contexts to apply the filter on.
  256. *
  257. * @return array
  258. * An array of context objects, keyed with the same keys used for $contexts,
  259. * which pass the filter.
  260. *
  261. * @see ctools_context::is_type()
  262. */
  263. public function filter($contexts) {
  264. $result = array();
  265. /**
  266. * See which of these contexts are valid.
  267. * @var ctools_context $context
  268. */
  269. foreach ((array) $contexts as $cid => $context) {
  270. if ($context->is_type($this->keywords)) {
  271. // Compare to see if our contexts were met.
  272. if (!empty($this->restrictions) && !empty($context->restrictions)) {
  273. foreach ($this->restrictions as $key => $values) {
  274. // If we have a restriction, the context must either not have that
  275. // restriction listed, which means we simply don't know what it is,
  276. // or there must be an intersection of the restricted values on
  277. // both sides.
  278. if (!is_array($values)) {
  279. $values = array($values);
  280. }
  281. if (!empty($context->restrictions[$key])
  282. && !array_intersect($values, $context->restrictions[$key])
  283. ) {
  284. // Break out to check next context; this one fails the filter.
  285. continue 2;
  286. }
  287. }
  288. }
  289. // This context passes the filter.
  290. $result[$cid] = $context;
  291. }
  292. }
  293. return $result;
  294. }
  295. /**
  296. * Select one context from the list of contexts, accounting for changed IDs.
  297. *
  298. * Fundamentally, this returns $contexts[$context] or FALSE if that does not
  299. * exist. Additional logic accounts for changes in context names and dealing
  300. * with a $contexts parameter that is not an array.
  301. *
  302. * If we had requested a $context but that $context doesn't exist in our
  303. * context list, there is a good chance that what happened is the context
  304. * IDs changed. Look for another context that satisfies our requirements,
  305. * unless $skip_name_check is set.
  306. *
  307. * @param ctools_context|array $contexts
  308. * A context, or an array of ctools_context.
  309. * @param string $context
  310. * A context ID.
  311. *
  312. * @return bool|ctools_context
  313. * The matching ctools_context, or False if no such context was found.
  314. */
  315. public function select($contexts, $context) {
  316. // Easier to deal with a standalone object as a 1-element array of objects.
  317. if (!is_array($contexts)) {
  318. if (is_object($contexts) && $contexts instanceof ctools_context) {
  319. $contexts = array($contexts->id => $contexts);
  320. }
  321. else {
  322. $contexts = array($contexts);
  323. }
  324. }
  325. // If we had requested a $context but that $context doesn't exist in our
  326. // context list, there is a good chance that what happened is the context
  327. // IDs changed. Check for another context that satisfies our requirements.
  328. if (!$this->skip_name_check
  329. && !empty($context) && !isset($contexts[$context])
  330. ) {
  331. $choices = $this->filter($contexts);
  332. // If we got a hit, take the first one that matches.
  333. if ($choices) {
  334. $keys = array_keys($choices);
  335. $context = reset($keys);
  336. }
  337. }
  338. if (empty($context) || empty($contexts[$context])) {
  339. return FALSE;
  340. }
  341. return $contexts[$context];
  342. }
  343. }
  344. /**
  345. * Used to compare to see if a list of contexts match an optional context. This
  346. * can produce empty contexts to use as placeholders.
  347. */
  348. class ctools_context_optional extends ctools_context_required {
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public $required = FALSE;
  353. /**
  354. * Add the 'empty' context to the existing set.
  355. *
  356. * @param array &$contexts
  357. * An array of ctools_context objects.
  358. */
  359. public function add_empty(&$contexts) {
  360. $context = new ctools_context('any');
  361. $context->title = t('No context');
  362. $context->identifier = t('No context');
  363. $contexts['empty'] = $context;
  364. }
  365. /**
  366. * Filter the contexts to determine which apply in the current environment.
  367. *
  368. * As for ctools_context_required, but we add the empty context to those
  369. * passed in so the check is optional (i.e. if nothing else matches, the
  370. * empty context will, and so there will always be at least one matched).
  371. *
  372. * @param array $contexts
  373. * An array of ctools_context objects (or something which will cast to an
  374. * array of them). The contexts to apply the filter on.
  375. *
  376. * @return array
  377. * An array of context objects, keyed with the same keys used for $contexts,
  378. * which pass the filter.
  379. *
  380. * @see ctools_context::is_type()
  381. */
  382. public function filter($contexts) {
  383. /**
  384. * @todo We are assuming here that $contexts is actually an array, whereas
  385. * ctools_context_required::filter only requires $contexts is convertible
  386. * to an array.
  387. */
  388. $this->add_empty($contexts);
  389. return parent::filter($contexts);
  390. }
  391. /**
  392. * Select and return one context from the list of applicable contexts.
  393. *
  394. * Fundamentally, this returns $contexts[$context] or the empty context if
  395. * that does not exist.
  396. *
  397. * @param array $contexts
  398. * The applicable contexts to check.
  399. * @param string $context
  400. * The context id to check for.
  401. *
  402. * @return bool|ctools_context
  403. * The matching ctools_context, or False if no such context was found.
  404. *
  405. * @see ctools_context_required::select()
  406. */
  407. public function select($contexts, $context) {
  408. /**
  409. * @todo We are assuming here that $contexts is actually an array, whereas
  410. * ctools_context_required::select permits ctools_context objects as well.
  411. */
  412. $this->add_empty($contexts);
  413. if (empty($context)) {
  414. return $contexts['empty'];
  415. }
  416. $result = parent::select($contexts, $context);
  417. // Don't flip out if it can't find the context; this is optional, put
  418. // in an empty.
  419. if ($result === FALSE) {
  420. $result = $contexts['empty'];
  421. }
  422. return $result;
  423. }
  424. }
  425. /**
  426. * Return a keyed array of context that match the given 'required context'
  427. * filters.
  428. *
  429. * Functions or systems that require contexts of a particular type provide a
  430. * ctools_context_required or ctools_context_optional object. This function
  431. * examines that object and an array of contexts to determine which contexts
  432. * match the filter.
  433. *
  434. * Since multiple contexts can be required, this function will accept either
  435. * an array of all required contexts, or just a single required context object.
  436. *
  437. * @param array $contexts
  438. * A keyed array of all available contexts.
  439. * @param array|ctools_context_required|ctools_context_optional $required
  440. * A *_required or *_optional object, or an array of such objects, which
  441. * define the selection condition.
  442. *
  443. * @return array
  444. * A keyed array of contexts that match the filter.
  445. */
  446. function ctools_context_filter($contexts, $required) {
  447. if (is_array($required)) {
  448. $result = array();
  449. foreach ($required as $item) {
  450. $result = array_merge($result, _ctools_context_filter($contexts, $item));
  451. }
  452. return $result;
  453. }
  454. return _ctools_context_filter($contexts, $required);
  455. }
  456. /**
  457. * Helper function for ctools_context_filter().
  458. *
  459. * Used to transform the required context during the merge into the final array.
  460. *
  461. * @internal This function DOES NOT form part of the CTools API.
  462. *
  463. * @param array $contexts
  464. * A keyed array of all available contexts.
  465. * @param ctools_context_required|ctools_context_optional $required
  466. * A ctools_context_required or ctools_context_optional object, although if
  467. * given something else will return an empty array.
  468. *
  469. * @return array
  470. */
  471. function _ctools_context_filter($contexts, $required) {
  472. $result = array();
  473. if (is_object($required)) {
  474. $result = $required->filter($contexts);
  475. }
  476. return $result;
  477. }
  478. /**
  479. * Create a select box to choose possible contexts.
  480. *
  481. * This only creates a selector if there is actually a choice; if there
  482. * is only one possible context, that one is silently assigned.
  483. *
  484. * If an array of required contexts is provided, one selector will be
  485. * provided for each context.
  486. *
  487. * @param array $contexts
  488. * A keyed array of all available contexts.
  489. * @param array|ctools_context_required|ctools_context_optional $required
  490. * The required context object or array of objects.
  491. * @param array|string $default
  492. * The default value for the select object, suitable for a #default_value
  493. * render key. Where $required is an array, this is an array keyed by the
  494. * same key values as $required for all keys where an empty string is not a
  495. * suitable default. Otherwise it is just the default value.
  496. *
  497. * @return array
  498. * A form element, or NULL if there are no contexts that satisfy the
  499. * requirements.
  500. */
  501. function ctools_context_selector($contexts, $required, $default) {
  502. if (is_array($required)) {
  503. $result = array('#tree' => TRUE);
  504. $count = 1;
  505. foreach ($required as $id => $item) {
  506. $result[] = _ctools_context_selector(
  507. $contexts, $item, isset($default[$id]) ? $default[$id] : '', $count++
  508. );
  509. }
  510. return $result;
  511. }
  512. return _ctools_context_selector($contexts, $required, $default);
  513. }
  514. /**
  515. * Helper function for ctools_context_selector().
  516. *
  517. * @internal This function DOES NOT form part of the CTools API. Use the API
  518. * function ctools_context_selector() instead.
  519. *
  520. * @param array $contexts
  521. * A keyed array of all available contexts.
  522. * @param ctools_context_required|ctools_context_optional $required
  523. * The required context object.
  524. * @param $default
  525. * The default value for the select object, suitable for a #default_value
  526. * render key.
  527. * @param int $num
  528. * If supplied and non-zero, the title of the select form element will be
  529. * "Context $num", otherwise it will be "Context".
  530. *
  531. * @return array
  532. * A form element, or NULL if there are no contexts that satisfy the
  533. * requirements.
  534. */
  535. function _ctools_context_selector($contexts, $required, $default, $num = 0) {
  536. $filtered = ctools_context_filter($contexts, $required);
  537. $count = count($filtered);
  538. $form = array();
  539. if ($count >= 1) {
  540. // If there's more than one to choose from, create a select widget.
  541. foreach ($filtered as $cid => $context) {
  542. $options[$cid] = $context->get_identifier();
  543. }
  544. if (!empty($required->title)) {
  545. $title = $required->title;
  546. }
  547. else {
  548. $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
  549. }
  550. $form = array(
  551. '#type' => 'select',
  552. '#options' => $options,
  553. '#title' => $title,
  554. '#default_value' => $default,
  555. );
  556. }
  557. return $form;
  558. }
  559. /**
  560. * Are there enough contexts for a plugin?
  561. *
  562. * Some plugins can have a 'required contexts' item which can either
  563. * be a context requirement object or an array of them. When contexts
  564. * are required, items that do not have enough contexts should not
  565. * appear. This tests an item to see if it has enough contexts
  566. * to actually appear.
  567. *
  568. * @param $contexts
  569. * A keyed array of all available contexts.
  570. * @param $required
  571. * The required context object or array of objects.
  572. *
  573. * @return bool
  574. * True if there are enough contexts, otherwise False.
  575. */
  576. function ctools_context_match_requirements($contexts, $required) {
  577. if (!is_array($required)) {
  578. $required = array($required);
  579. }
  580. // Get the keys to avoid bugs in PHP 5.0.8 with keys and loops.
  581. // And use it to remove optional contexts.
  582. $keys = array_keys($required);
  583. foreach ($keys as $key) {
  584. if (empty($required[$key]->required)) {
  585. unset($required[$key]);
  586. }
  587. }
  588. $count = count($required);
  589. return (count(ctools_context_filter($contexts, $required)) >= $count);
  590. }
  591. /**
  592. * Create a select box to choose possible contexts.
  593. *
  594. * This only creates a selector if there is actually a choice; if there
  595. * is only one possible context, that one is silently assigned.
  596. *
  597. * If an array of required contexts is provided, one selector will be
  598. * provided for each context.
  599. *
  600. * @param $contexts
  601. * A keyed array of all available contexts.
  602. * @param $required
  603. * The required context object or array of objects.
  604. * @param array|string $default
  605. * The default value for the select object, suitable for a #default_value
  606. * render key. Where $required is an array, this is an array keyed by the
  607. * same key values as $required for all keys where an empty string is not a
  608. * suitable default. Otherwise it is just the default value.
  609. *
  610. * @return array
  611. * A form element, or NULL if there are no contexts that satisfy the
  612. * requirements.
  613. */
  614. function ctools_context_converter_selector($contexts, $required, $default) {
  615. if (is_array($required)) {
  616. $result = array('#tree' => TRUE);
  617. $count = 1;
  618. foreach ($required as $id => $dependency) {
  619. $default_id = isset($default[$id]) ? $default[$id] : '';
  620. $result[] = _ctools_context_converter_selector(
  621. $contexts, $dependency, $default_id, $count++
  622. );
  623. }
  624. return $result;
  625. }
  626. return _ctools_context_converter_selector($contexts, $required, $default);
  627. }
  628. /**
  629. * Helper function for ctools_context_converter_selector().
  630. *
  631. * @internal This function DOES NOT form part of the CTools API. Use the API
  632. * function ctools_context_converter_selector() instead.
  633. *
  634. * @param array $contexts
  635. * A keyed array of all available contexts.
  636. * @param ctools_context $required
  637. * The required context object.
  638. * @param $default
  639. * The default value for the select object, suitable for a #default_value
  640. * render key.
  641. * @param int $num
  642. * If supplied and non-zero, the title of the select form element will be
  643. * "Context $num", otherwise it will be "Context".
  644. *
  645. * @return array|null
  646. * A form element, or NULL if there are no contexts that satisfy the
  647. * requirements.
  648. */
  649. function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) {
  650. $filtered = ctools_context_filter($contexts, $required);
  651. $count = count($filtered);
  652. if ($count > 1) {
  653. // If there's more than one to choose from, create a select widget.
  654. $options = array();
  655. foreach ($filtered as $cid => $context) {
  656. if ($context->type === 'any') {
  657. $options[''] = t('No context');
  658. continue;
  659. }
  660. $key = $context->get_identifier();
  661. if ($converters = ctools_context_get_converters($cid . '.', $context)) {
  662. $options[$key] = $converters;
  663. }
  664. }
  665. if (empty($options)) {
  666. return array(
  667. '#type' => 'value',
  668. '#value' => 'any',
  669. );
  670. }
  671. if (!empty($required->title)) {
  672. $title = $required->title;
  673. }
  674. else {
  675. $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
  676. }
  677. return array(
  678. '#type' => 'select',
  679. '#options' => $options,
  680. '#title' => $title,
  681. '#description' => t('Please choose which context and how you would like it converted.'),
  682. '#default_value' => $default,
  683. );
  684. }
  685. else {
  686. // Not enough choices to need a selector, so don't make one.
  687. return NULL;
  688. }
  689. }
  690. /**
  691. * Get a list of converters available for a given context.
  692. *
  693. * @param string $cid
  694. * A context ID.
  695. * @param ctools_context $context
  696. * The context for which converters are needed.
  697. *
  698. * @return array
  699. * A list of context converters.
  700. */
  701. function ctools_context_get_converters($cid, $context) {
  702. if (empty($context->plugin)) {
  703. return array();
  704. }
  705. return _ctools_context_get_converters($cid, $context->plugin);
  706. }
  707. /**
  708. * Get a list of converters available for a given context.
  709. *
  710. * @internal This function DOES NOT form part of the CTools API. Use the API
  711. * function ctools_context_get_converters() instead.
  712. *
  713. * @param string $id
  714. * A context ID.
  715. * @param string $plugin_name
  716. * The name of the context plugin.
  717. *
  718. * @return array
  719. * A list of context converters.
  720. */
  721. function _ctools_context_get_converters($id, $plugin_name) {
  722. $plugin = ctools_get_context($plugin_name);
  723. if (empty($plugin['convert list'])) {
  724. return array();
  725. }
  726. $converters = array();
  727. if (is_array($plugin['convert list'])) {
  728. $converters = $plugin['convert list'];
  729. }
  730. elseif ($function = ctools_plugin_get_function($plugin, 'convert list')) {
  731. $converters = (array) $function($plugin);
  732. }
  733. foreach (module_implements('ctools_context_convert_list_alter') as $module) {
  734. $function = $module . '_ctools_context_convert_list_alter';
  735. $function($plugin, $converters);
  736. }
  737. // Now, change them all to include the plugin:
  738. $return = array();
  739. foreach ($converters as $key => $title) {
  740. $return[$id . $key] = $title;
  741. }
  742. natcasesort($return);
  743. return $return;
  744. }
  745. /**
  746. * Get a list of all contexts converters available.
  747. *
  748. * For all contexts returned by ctools_get_contexts(), return the converter
  749. * for all contexts that have one.
  750. *
  751. * @return array
  752. * A list of context converters, keyed by the title of the converter.
  753. */
  754. function ctools_context_get_all_converters() {
  755. $contexts = ctools_get_contexts();
  756. $converters = array();
  757. foreach ($contexts as $name => $context) {
  758. if (empty($context['no required context ui'])) {
  759. $context_converters = _ctools_context_get_converters($name . '.', $name);
  760. if ($context_converters) {
  761. $converters[$context['title']] = $context_converters;
  762. }
  763. }
  764. }
  765. return $converters;
  766. }
  767. /**
  768. * Let the context convert an argument based upon the converter that was given.
  769. *
  770. * @param ctools_context $context
  771. * The context object.
  772. * @param string $converter
  773. * The type of converter to use, which should be a string provided by the
  774. * converter list function.
  775. * @param array $converter_options
  776. * An array of options to pass on to the generation function. For contexts
  777. * that use token module, of particular use is 'sanitize' => FALSE which can
  778. * get raw tokens. This should ONLY be used in values that will later be
  779. * treated as unsafe user input since these values are by themselves unsafe.
  780. * It is particularly useful to get raw values from Field API.
  781. *
  782. * @return string|null
  783. */
  784. function ctools_context_convert_context($context, $converter, $converter_options = array()) {
  785. // Contexts without plugins might be optional placeholders.
  786. if (empty($context->plugin)) {
  787. return NULL;
  788. }
  789. $value = $context->argument;
  790. $plugin = ctools_get_context($context->plugin);
  791. if ($function = ctools_plugin_get_function($plugin, 'convert')) {
  792. $value = $function($context, $converter, $converter_options);
  793. }
  794. foreach (module_implements('ctools_context_converter_alter') as $module) {
  795. $function = $module . '_ctools_context_converter_alter';
  796. $function($context, $converter, $value, $converter_options);
  797. }
  798. return $value;
  799. }
  800. /**
  801. * Choose a context or contexts based upon the selection made via
  802. * ctools_context_filter.
  803. *
  804. * @param array $contexts
  805. * A keyed array of all available contexts.
  806. * @param array|ctools_context_required $required
  807. * The required context object(s) provided by the plugin.
  808. * @param $context
  809. * The selection made using ctools_context_selector().
  810. *
  811. * @return ctools_context|array|false
  812. * Returns FALSE if $required is not an object, or array of objects, or
  813. * the value of $required->select() for the context, or an array of those (if
  814. * passed an array in $required).
  815. */
  816. function ctools_context_select($contexts, $required, $context) {
  817. if (is_array($required)) {
  818. /**
  819. * @var array $required
  820. * Array of required context objects.
  821. * @var ctools_context_required $item
  822. * A required context object.
  823. */
  824. $result = array();
  825. foreach ($required as $id => $item) {
  826. // @todo What's the difference between the following and "empty($item)" ?
  827. if (empty($required[$id])) {
  828. continue;
  829. }
  830. if (($result[] = _ctools_context_select($contexts, $item, $context[$id])) === FALSE) {
  831. return FALSE;
  832. }
  833. }
  834. return $result;
  835. }
  836. return _ctools_context_select($contexts, $required, $context);
  837. }
  838. /**
  839. * Helper function for calling the required context object's selection function.
  840. *
  841. * This function DOES NOT form part of the CTools API.
  842. *
  843. * @param array $contexts
  844. * A keyed array of all available contexts.
  845. * @param ctools_context_required $required
  846. * The required context object provided by the plugin.
  847. * @param $context
  848. * The selection made using ctools_context_selector().
  849. *
  850. * @return ctools_context|bool
  851. * FALSE if the $required is not an object. A ctools_context object if one
  852. * matched.
  853. */
  854. function _ctools_context_select($contexts, $required, $context) {
  855. if (!is_object($required)) {
  856. return FALSE;
  857. }
  858. return $required->select($contexts, $context);
  859. }
  860. /**
  861. * Create a new context object.
  862. *
  863. * @param string $type
  864. * The type of context to create; this loads a plugin.
  865. * @param mixed $data
  866. * The data to put into the context.
  867. * @param $conf
  868. * A configuration structure if this context was created via UI.
  869. *
  870. * @return ctools_context
  871. * A $context or NULL if one could not be created.
  872. */
  873. function ctools_context_create($type, $data = NULL, $conf = FALSE) {
  874. ctools_include('plugins');
  875. $plugin = ctools_get_context($type);
  876. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  877. return $function(FALSE, $data, $conf, $plugin);
  878. }
  879. }
  880. /**
  881. * Create an empty context object.
  882. *
  883. * Empty context objects are primarily used as placeholders in the UI where
  884. * the actual contents of a context object may not be known. It may have
  885. * additional text embedded to give the user clues as to how the context
  886. * is used.
  887. *
  888. * @param $type
  889. * The type of context to create; this loads a plugin.
  890. *
  891. * @return ctools_context
  892. * A $context or NULL if one could not be created.
  893. */
  894. function ctools_context_create_empty($type) {
  895. $plugin = ctools_get_context($type);
  896. if ($function = ctools_plugin_get_function($plugin, 'context')) {
  897. $context = $function(TRUE, NULL, FALSE, $plugin);
  898. if (is_object($context)) {
  899. $context->empty = TRUE;
  900. }
  901. return $context;
  902. }
  903. }
  904. /**
  905. * Perform keyword and context substitutions.
  906. *
  907. * @param string $string
  908. * The string in which to replace keywords.
  909. * @param array $keywords
  910. * Array of keyword-replacement pairs.
  911. * @param array $contexts
  912. *
  913. * @param array $converter_options
  914. * Options to pass on to ctools_context_convert_context(), defaults to an
  915. * empty array.
  916. *
  917. * @return string
  918. * The returned string, with substitutions performed.
  919. */
  920. function ctools_context_keyword_substitute($string, $keywords, $contexts, array $converter_options = array()) {
  921. // Ensure a default keyword exists:
  922. $keywords['%%'] = '%';
  923. // Match contexts to the base keywords:
  924. $context_keywords = array();
  925. foreach ($contexts as $context) {
  926. if (isset($context->keyword)) {
  927. $context_keywords[$context->keyword] = $context;
  928. }
  929. }
  930. // Look for context matches we we only have to convert known matches.
  931. $matches = array();
  932. if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) {
  933. foreach ($matches[1] as $keyword) {
  934. // Ignore anything it finds with %%.
  935. if ($keyword[0] == '%') {
  936. continue;
  937. }
  938. // If the keyword is already set by something passed in, don't try to
  939. // overwrite it.
  940. if (array_key_exists('%' . $keyword, $keywords)) {
  941. continue;
  942. }
  943. // Figure out our keyword and converter, if specified.
  944. if (strpos($keyword, ':')) {
  945. list($context, $converter) = explode(':', $keyword, 2);
  946. }
  947. else {
  948. $context = $keyword;
  949. if (isset($context_keywords[$keyword])) {
  950. $plugin = ctools_get_context($context_keywords[$context]->plugin);
  951. // Fall back to a default converter, if specified.
  952. if ($plugin && !empty($plugin['convert default'])) {
  953. $converter = $plugin['convert default'];
  954. }
  955. }
  956. }
  957. if (!isset($context_keywords[$context])) {
  958. $keywords['%' . $keyword] = '%' . $keyword;
  959. }
  960. else {
  961. if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
  962. $keywords['%' . $keyword] = '';
  963. }
  964. else {
  965. if (!empty($converter)) {
  966. $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options);
  967. }
  968. else {
  969. $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
  970. }
  971. }
  972. }
  973. }
  974. }
  975. return strtr($string, $keywords);
  976. }
  977. /**
  978. * Determine a unique context ID for a context.
  979. *
  980. * Often contexts of many different types will be placed into a list. This
  981. * ensures that even though contexts of multiple types may share IDs, they
  982. * are unique in the final list.
  983. */
  984. function ctools_context_id($context, $type = 'context') {
  985. // If not set, FALSE or empty.
  986. if (!isset($context['id']) || !$context['id']) {
  987. $context['id'] = 1;
  988. }
  989. // @todo is '' the appropriate default value?
  990. $name = isset($context['name']) ? $context['name'] : '';
  991. return $type . '_' . $name . '_' . $context['id'];
  992. }
  993. /**
  994. * Get the next id available given a list of already existing objects.
  995. *
  996. * This finds the next id available for the named object.
  997. *
  998. * @param array $objects
  999. * A list of context descriptor objects, i.e, arguments, relationships,
  1000. * contexts, etc.
  1001. * @param string $name
  1002. * The name being used.
  1003. *
  1004. * @return int
  1005. * The next integer id available.
  1006. */
  1007. function ctools_context_next_id($objects, $name) {
  1008. $id = 0;
  1009. // Figure out which instance of this argument we're creating.
  1010. if (!$objects) {
  1011. return $id + 1;
  1012. }
  1013. foreach ($objects as $object) {
  1014. if (isset($object['name']) && $object['name'] === $name) {
  1015. if (isset($object['id']) && $object['id'] > $id) {
  1016. $id = $object['id'];
  1017. }
  1018. // @todo If obj has no 'id', should we increment local id? $id = $id + 1;
  1019. }
  1020. }
  1021. return $id + 1;
  1022. }
  1023. // ---------------------------------------------------------------------------
  1024. // Functions related to contexts from arguments.
  1025. /**
  1026. * Fetch metadata for a specific argument plugin.
  1027. *
  1028. * @param $argument
  1029. * Name of an argument plugin.
  1030. *
  1031. * @return array
  1032. * An array with information about the requested argument plugin.
  1033. */
  1034. function ctools_get_argument($argument) {
  1035. ctools_include('plugins');
  1036. return ctools_get_plugins('ctools', 'arguments', $argument);
  1037. }
  1038. /**
  1039. * Fetch metadata for all argument plugins.
  1040. *
  1041. * @return array
  1042. * An array of arrays with information about all available argument plugins.
  1043. */
  1044. function ctools_get_arguments() {
  1045. ctools_include('plugins');
  1046. return ctools_get_plugins('ctools', 'arguments');
  1047. }
  1048. /**
  1049. * Get a context from an argument.
  1050. *
  1051. * @param $argument
  1052. * The configuration of an argument. It must contain the following data:
  1053. * - name: The name of the argument plugin being used.
  1054. * - argument_settings: The configuration based upon the plugin forms.
  1055. * - identifier: The human readable identifier for this argument, usually
  1056. * defined by the UI.
  1057. * - keyword: The keyword used for this argument for substitutions.
  1058. *
  1059. * @param $arg
  1060. * The actual argument received. This is expected to be a string from a URL
  1061. * but this does not have to be the only source of arguments.
  1062. * @param $empty
  1063. * If true, the $arg will not be used to load the context. Instead, an empty
  1064. * placeholder context will be loaded.
  1065. *
  1066. * @return ctools_context
  1067. * A context object if one can be loaded.
  1068. */
  1069. function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) {
  1070. ctools_include('plugins');
  1071. if (empty($argument['name'])) {
  1072. return NULL;
  1073. }
  1074. $function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context');
  1075. if ($function) {
  1076. // Backward compatibility: Merge old style settings into new style:
  1077. if (!empty($argument['settings'])) {
  1078. $argument += $argument['settings'];
  1079. unset($argument['settings']);
  1080. }
  1081. $context = $function($arg, $argument, $empty);
  1082. if (is_object($context)) {
  1083. $context->identifier = $argument['identifier'];
  1084. $context->page_title = isset($argument['title']) ? $argument['title'] : '';
  1085. $context->keyword = $argument['keyword'];
  1086. $context->id = ctools_context_id($argument, 'argument');
  1087. $context->original_argument = $arg;
  1088. if (!empty($context->empty)) {
  1089. $context->placeholder = array(
  1090. 'type' => 'argument',
  1091. 'conf' => $argument,
  1092. );
  1093. }
  1094. }
  1095. return $context;
  1096. }
  1097. }
  1098. /**
  1099. * Retrieve a list of empty contexts for all arguments.
  1100. *
  1101. * @param array $arguments
  1102. *
  1103. * @return array
  1104. *
  1105. * @see ctools_context_get_context_from_arguments()
  1106. */
  1107. function ctools_context_get_placeholders_from_argument($arguments) {
  1108. $contexts = array();
  1109. foreach ($arguments as $argument) {
  1110. $context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
  1111. if ($context) {
  1112. $contexts[ctools_context_id($argument, 'argument')] = $context;
  1113. }
  1114. }
  1115. return $contexts;
  1116. }
  1117. /**
  1118. * Load the contexts for a given list of arguments.
  1119. *
  1120. * @param array $arguments
  1121. * The array of argument definitions.
  1122. * @param array &$contexts
  1123. * The array of existing contexts. New contexts will be added to this array.
  1124. * @param array $args
  1125. * The arguments to load.
  1126. *
  1127. * @return bool
  1128. * TRUE if all is well, FALSE if an argument wants to 404.
  1129. *
  1130. * @see ctools_context_get_context_from_argument()
  1131. */
  1132. function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) {
  1133. foreach ($arguments as $argument) {
  1134. // Pull the argument off the list.
  1135. $arg = array_shift($args);
  1136. $id = ctools_context_id($argument, 'argument');
  1137. // For % arguments embedded in the URL, our context is already loaded.
  1138. // There is no need to go and load it again.
  1139. if (empty($contexts[$id])) {
  1140. if ($context = ctools_context_get_context_from_argument($argument, $arg)) {
  1141. $contexts[$id] = $context;
  1142. }
  1143. }
  1144. else {
  1145. $context = $contexts[$id];
  1146. }
  1147. if ((empty($context) || empty($context->data))
  1148. && !empty($argument['default'])
  1149. && $argument['default'] === '404'
  1150. ) {
  1151. return FALSE;
  1152. }
  1153. }
  1154. return TRUE;
  1155. }
  1156. // ---------------------------------------------------------------------------
  1157. // Functions related to contexts from relationships.
  1158. /**
  1159. * Fetch plugin metadata for a specific relationship plugin.
  1160. *
  1161. * @param $relationship
  1162. * Name of a panel content type.
  1163. *
  1164. * @return array
  1165. * An array with information about the requested relationship.
  1166. *
  1167. * @see ctools_get_relationships()
  1168. */
  1169. function ctools_get_relationship($relationship) {
  1170. ctools_include('plugins');
  1171. return ctools_get_plugins('ctools', 'relationships', $relationship);
  1172. }
  1173. /**
  1174. * Fetch metadata for all relationship plugins.
  1175. *
  1176. * @return array
  1177. * An array of arrays with information about all available relationships.
  1178. *
  1179. * @see ctools_get_relationship()
  1180. */
  1181. function ctools_get_relationships() {
  1182. ctools_include('plugins');
  1183. return ctools_get_plugins('ctools', 'relationships');
  1184. }
  1185. /**
  1186. * Return a context from a relationship.
  1187. *
  1188. * @param array $relationship
  1189. * The configuration of a relationship. It must contain the following data:
  1190. * - name: The name of the relationship plugin being used.
  1191. * - relationship_settings: The configuration based upon the plugin forms.
  1192. * - identifier: The human readable identifier for this relationship, usually
  1193. * defined by the UI.
  1194. * - keyword: The keyword used for this relationship for substitutions.
  1195. *
  1196. * @param ctools_context $source_context
  1197. * The context this relationship is based upon.
  1198. * @param bool $placeholders
  1199. * If TRUE, placeholders are acceptable.
  1200. *
  1201. * @return ctools_context|null
  1202. * A context object if one can be loaded, otherwise NULL.
  1203. *
  1204. * @see ctools_context_get_relevant_relationships()
  1205. * @see ctools_context_get_context_from_relationships()
  1206. */
  1207. function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) {
  1208. ctools_include('plugins');
  1209. $function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context');
  1210. if ($function) {
  1211. // Backward compatibility: Merge old style settings into new style:
  1212. if (!empty($relationship['relationship_settings'])) {
  1213. $relationship += $relationship['relationship_settings'];
  1214. unset($relationship['relationship_settings']);
  1215. }
  1216. $context = $function($source_context, $relationship, $placeholders);
  1217. if ($context) {
  1218. $context->identifier = $relationship['identifier'];
  1219. $context->page_title = isset($relationship['title']) ? $relationship['title'] : '';
  1220. $context->keyword = $relationship['keyword'];
  1221. if (!empty($context->empty)) {
  1222. $context->placeholder = array(
  1223. 'type' => 'relationship',
  1224. 'conf' => $relationship,
  1225. );
  1226. }
  1227. return $context;
  1228. }
  1229. }
  1230. return NULL;
  1231. }
  1232. /**
  1233. * Fetch all relevant relationships.
  1234. *
  1235. * Relevant relationships are any relationship that can be created based upon
  1236. * the list of existing contexts. For example, the 'node author' relationship
  1237. * is relevant if there is a 'node' context, but makes no sense if there is
  1238. * not one.
  1239. *
  1240. * @param $contexts
  1241. * An array of contexts used to figure out which relationships are relevant.
  1242. *
  1243. * @return array
  1244. * An array of relationship keys that are relevant for the given set of
  1245. * contexts.
  1246. *
  1247. * @see ctools_context_filter()
  1248. * @see ctools_context_get_context_from_relationship()
  1249. * @see ctools_context_get_context_from_relationships()
  1250. */
  1251. function ctools_context_get_relevant_relationships($contexts) {
  1252. $relevant = array();
  1253. $relationships = ctools_get_relationships();
  1254. // Go through each relationship.
  1255. foreach ($relationships as $rid => $relationship) {
  1256. // For each relationship, see if there is a context that satisfies it.
  1257. if (empty($relationship['no ui'])
  1258. && ctools_context_filter($contexts, $relationship['required context'])
  1259. ) {
  1260. $relevant[$rid] = $relationship['title'];
  1261. }
  1262. }
  1263. return $relevant;
  1264. }
  1265. /**
  1266. * Fetch all active relationships.
  1267. *
  1268. * @param $relationships
  1269. * An keyed array of relationship data including:
  1270. * - name: name of relationship
  1271. * - context: context id relationship belongs to. This will be used to
  1272. * identify which context in the $contexts array to use to create the
  1273. * relationship context.
  1274. *
  1275. * @param $contexts
  1276. * A keyed array of contexts used to figure out which relationships
  1277. * are relevant. New contexts will be added to this.
  1278. *
  1279. * @param $placeholders
  1280. * If TRUE, placeholders are acceptable.
  1281. *
  1282. * @see ctools_context_get_context_from_relationship()
  1283. * @see ctools_context_get_relevant_relationships()
  1284. */
  1285. function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) {
  1286. foreach ($relationships as $rdata) {
  1287. if (!isset($rdata['context'])) {
  1288. continue;
  1289. }
  1290. if (is_array($rdata['context'])) {
  1291. $rcontexts = array();
  1292. foreach ($rdata['context'] as $cid) {
  1293. if (empty($contexts[$cid])) {
  1294. continue 2;
  1295. }
  1296. $rcontexts[] = $contexts[$cid];
  1297. }
  1298. }
  1299. else {
  1300. if (empty($contexts[$rdata['context']])) {
  1301. continue;
  1302. }
  1303. $rcontexts = $contexts[$rdata['context']];
  1304. }
  1305. $cid = ctools_context_id($rdata, 'relationship');
  1306. if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) {
  1307. $contexts[$cid] = $context;
  1308. }
  1309. }
  1310. }
  1311. // ---------------------------------------------------------------------------
  1312. // Functions related to loading contexts from simple context definitions.
  1313. /**
  1314. * Fetch metadata on a specific context plugin.
  1315. *
  1316. * @param string $context
  1317. * Name of a context.
  1318. *
  1319. * @return array
  1320. * An array with information about the requested panel context.
  1321. */
  1322. function ctools_get_context($context) {
  1323. static $gate = array();
  1324. ctools_include('plugins');
  1325. $plugin = ctools_get_plugins('ctools', 'contexts', $context);
  1326. if (empty($gate['context']) && !empty($plugin['superceded by'])) {
  1327. // This gate prevents infinite loops.
  1328. $gate[$context] = TRUE;
  1329. $new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']);
  1330. $gate[$context] = FALSE;
  1331. // If a new plugin was returned, return it. Otherwise fall through and
  1332. // return the original we fetched.
  1333. if ($new_plugin) {
  1334. return $new_plugin;
  1335. }
  1336. }
  1337. return $plugin;
  1338. }
  1339. /**
  1340. * Fetch metadata for all context plugins.
  1341. *
  1342. * @return array
  1343. * An array of arrays with information about all available panel contexts.
  1344. */
  1345. function ctools_get_contexts() {
  1346. ctools_include('plugins');
  1347. return ctools_get_plugins('ctools', 'contexts');
  1348. }
  1349. /**
  1350. * Return a context object from a context definition array.
  1351. *
  1352. * The input $context contains the information needed to identify and invoke
  1353. * the context plugin and create the plugin context from that.
  1354. *
  1355. * @param array $context
  1356. * The configuration of a context. It must contain the following data:
  1357. * - name: The name of the context plugin being used.
  1358. * - context_settings: The configuration based upon the plugin forms.
  1359. * - identifier: The human readable identifier for this context, usually
  1360. * defined by the UI.
  1361. * - keyword: The keyword used for this context for substitutions.
  1362. * @param string $type
  1363. * This is either 'context' which indicates the context will be loaded
  1364. * from data in the settings, or 'requiredcontext' which means the
  1365. * context must be acquired from an external source. This is the method
  1366. * used to pass pure contexts from one system to another.
  1367. * @param mixed $argument
  1368. * Optional information passed to the plugin context via the arg defined in
  1369. * the plugin's "placeholder name" field.
  1370. *
  1371. * @return ctools_context|null
  1372. * A context object if one can be loaded.
  1373. *
  1374. * @see ctools_get_context()
  1375. * @see ctools_plugin_get_function()
  1376. */
  1377. function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) {
  1378. ctools_include('plugins');
  1379. $plugin = ctools_get_context($context['name']);
  1380. $function = ctools_plugin_get_function($plugin, 'context');
  1381. if ($function) {
  1382. // Backward compatibility: Merge old style settings into new style:
  1383. if (!empty($context['context_settings'])) {
  1384. $context += $context['context_settings'];
  1385. unset($context['context_settings']);
  1386. }
  1387. if (isset($argument) && isset($plugin['placeholder name'])) {
  1388. $context[$plugin['placeholder name']] = $argument;
  1389. }
  1390. $return = $function($type == 'requiredcontext', $context, TRUE, $plugin);
  1391. if ($return) {
  1392. $return->identifier = $context['identifier'];
  1393. $return->page_title = isset($context['title']) ? $context['title'] : '';
  1394. $return->keyword = $context['keyword'];
  1395. if (!empty($context->empty)) {
  1396. $context->placeholder = array(
  1397. 'type' => 'context',
  1398. 'conf' => $context,
  1399. );
  1400. }
  1401. return $return;
  1402. }
  1403. }
  1404. return NULL;
  1405. }
  1406. /**
  1407. * Retrieve a list of base contexts based upon a simple 'contexts' definition.
  1408. *
  1409. * For required contexts this will always retrieve placeholders.
  1410. *
  1411. * @param $contexts
  1412. * The list of contexts defined in the UI.
  1413. * @param $type
  1414. * Either 'context' or 'requiredcontext', which indicates whether the contexts
  1415. * are loaded from internal data or copied from an external source.
  1416. * @param $placeholders
  1417. * If True, placeholders are acceptable.
  1418. *
  1419. * @return array
  1420. * Array of contexts, keyed by context ID.
  1421. */
  1422. function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) {
  1423. $return = array();
  1424. foreach ($contexts as $context) {
  1425. $ctext = ctools_context_get_context_from_context($context, $type);
  1426. if ($ctext) {
  1427. if ($placeholders) {
  1428. $ctext->placeholder = TRUE;
  1429. }
  1430. $return[ctools_context_id($context, $type)] = $ctext;
  1431. }
  1432. }
  1433. return $return;
  1434. }
  1435. /**
  1436. * Match up external contexts to our required contexts.
  1437. *
  1438. * This function is used to create a list of contexts with proper IDs based
  1439. * upon a list of required contexts.
  1440. *
  1441. * These contexts passed in should match the numeric positions of the required
  1442. * contexts. The caller must ensure this has already happened correctly as this
  1443. * function will not detect errors here.
  1444. *
  1445. * @param $required
  1446. * A list of required contexts as defined by the UI.
  1447. * @param $contexts
  1448. * A list of matching contexts as passed in from the calling system.
  1449. *
  1450. * @return array
  1451. * Array of contexts, keyed by context ID.
  1452. */
  1453. function ctools_context_match_required_contexts($required, $contexts) {
  1454. $return = array();
  1455. if (!is_array($required)) {
  1456. return $return;
  1457. }
  1458. foreach ($required as $r) {
  1459. $context = clone array_shift($contexts);
  1460. $context->identifier = $r['identifier'];
  1461. $context->page_title = isset($r['title']) ? $r['title'] : '';
  1462. $context->keyword = $r['keyword'];
  1463. $return[ctools_context_id($r, 'requiredcontext')] = $context;
  1464. }
  1465. return $return;
  1466. }
  1467. /**
  1468. * Load a full array of contexts for an object.
  1469. *
  1470. * Not all of the types need to be supported by this object.
  1471. *
  1472. * This function is not used to load contexts from external data, but may be
  1473. * used to load internal contexts and relationships. Otherwise it can also be
  1474. * used to generate a full set of placeholders for UI purposes.
  1475. *
  1476. * @param object $object
  1477. * An object that contains some or all of the following variables:
  1478. *
  1479. * - requiredcontexts: A list of UI configured contexts that are required
  1480. * from an external source. Since these require external data, they will
  1481. * only be added if $placeholders is set to TRUE, and empty contexts will
  1482. * be created.
  1483. * - arguments: A list of UI configured arguments that will create contexts.
  1484. * As these require external data, they will only be added if $placeholders
  1485. * is set to TRUE.
  1486. * - contexts: A list of UI configured contexts that have no external source,
  1487. * and are essentially hardcoded. For example, these might configure a
  1488. * particular node or a particular taxonomy term.
  1489. * - relationships: A list of UI configured contexts to be derived from other
  1490. * contexts that already exist from other sources. For example, these might
  1491. * be used to get a user object from a node via the node author
  1492. * relationship.
  1493. * @param bool $placeholders
  1494. * If True, this will generate placeholder objects for any types this function
  1495. * cannot load.
  1496. * @param array $contexts
  1497. * An array of pre-existing contexts that will be part of the return value.
  1498. *
  1499. * @return array
  1500. * Merged output of all results of ctools_context_get_context_from_contexts().
  1501. */
  1502. function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) {
  1503. if (!empty($object->base_contexts)) {
  1504. $contexts += $object->base_contexts;
  1505. }
  1506. if ($placeholders) {
  1507. // This will load empty contexts as placeholders for arguments that come
  1508. // from external sources. If this isn't set, it's assumed these context
  1509. // will already have been matched up and loaded.
  1510. if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) {
  1511. $contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders);
  1512. }
  1513. if (!empty($object->arguments) && is_array($object->arguments)) {
  1514. $contexts += ctools_context_get_placeholders_from_argument($object->arguments);
  1515. }
  1516. }
  1517. if (!empty($object->contexts) && is_array($object->contexts)) {
  1518. $contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders);
  1519. }
  1520. // Add contexts from relationships.
  1521. if (!empty($object->relationships) && is_array($object->relationships)) {
  1522. ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders);
  1523. }
  1524. return $contexts;
  1525. }
  1526. /**
  1527. * Return the first context with a form id from a list of contexts.
  1528. *
  1529. * This function is used to figure out which contexts represents 'the form'
  1530. * from a list of contexts. Only one contexts can actually be 'the form' for
  1531. * a given page, since the @code{<form>} tag can not be embedded within
  1532. * itself.
  1533. */
  1534. function ctools_context_get_form($contexts) {
  1535. if (!empty($contexts)) {
  1536. foreach ($contexts as $id => $context) {
  1537. // If a form shows its id as being a 'required context' that means the
  1538. // the context is external to this display and does not count.
  1539. if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') {
  1540. return $context;
  1541. }
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. * Replace placeholders with real contexts using data extracted from a form
  1547. * for the purposes of previews.
  1548. *
  1549. * @param $contexts
  1550. * All of the contexts, including the placeholders.
  1551. * @param $arguments
  1552. * The arguments. These will be acquired from $form_state['values'] and the
  1553. * keys must match the context IDs.
  1554. *
  1555. * @return array
  1556. * A new $contexts array containing the replaced contexts. Not all contexts
  1557. * may be replaced if, for example, an argument was unable to be converted
  1558. * into a context.
  1559. */
  1560. function ctools_context_replace_placeholders($contexts, $arguments) {
  1561. foreach ($contexts as $cid => $context) {
  1562. if (empty($context->empty)) {
  1563. continue;
  1564. }
  1565. $new_context = NULL;
  1566. switch ($context->placeholder['type']) {
  1567. case 'relationship':
  1568. $relationship = $context->placeholder['conf'];
  1569. if (isset($contexts[$relationship['context']])) {
  1570. $new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]);
  1571. }
  1572. break;
  1573. case 'argument':
  1574. if (isset($arguments[$cid]) && $arguments[$cid] !== '') {
  1575. $argument = $context->placeholder['conf'];
  1576. $new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]);
  1577. }
  1578. break;
  1579. case 'context':
  1580. if (!empty($arguments[$cid])) {
  1581. $context_info = $context->placeholder['conf'];
  1582. $new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]);
  1583. }
  1584. break;
  1585. }
  1586. if ($new_context && empty($new_context->empty)) {
  1587. $contexts[$cid] = $new_context;
  1588. }
  1589. }
  1590. return $contexts;
  1591. }
  1592. /**
  1593. * Provide a form array for getting data to replace placeholder contexts
  1594. * with real data.
  1595. */
  1596. function ctools_context_replace_form(&$form, $contexts) {
  1597. foreach ($contexts as $cid => $context) {
  1598. if (empty($context->empty)) {
  1599. continue;
  1600. }
  1601. // Get plugin info from the context which should have been set when the
  1602. // empty context was created.
  1603. $info = NULL;
  1604. $plugin = NULL;
  1605. $settings = NULL;
  1606. switch ($context->placeholder['type']) {
  1607. case 'argument':
  1608. $info = $context->placeholder['conf'];
  1609. $plugin = ctools_get_argument($info['name']);
  1610. break;
  1611. case 'context':
  1612. $info = $context->placeholder['conf'];
  1613. $plugin = ctools_get_context($info['name']);
  1614. break;
  1615. }
  1616. // Ask the plugin where the form is.
  1617. if ($plugin && isset($plugin['placeholder form'])) {
  1618. if (is_array($plugin['placeholder form'])) {
  1619. $form[$cid] = $plugin['placeholder form'];
  1620. }
  1621. else {
  1622. if (function_exists($plugin['placeholder form'])) {
  1623. $widget = $plugin['placeholder form']($info);
  1624. if ($widget) {
  1625. $form[$cid] = $widget;
  1626. }
  1627. }
  1628. }
  1629. if (!empty($form[$cid])) {
  1630. $form[$cid]['#title'] = t('@identifier (@keyword)', array(
  1631. '@keyword' => '%' . $context->keyword,
  1632. '@identifier' => $context->identifier,
  1633. ));
  1634. }
  1635. }
  1636. }
  1637. }
  1638. // ---------------------------------------------------------------------------
  1639. // Functions related to loading access control plugins.
  1640. /**
  1641. * Fetch metadata on a specific access control plugin.
  1642. *
  1643. * @param $name
  1644. * Name of a plugin.
  1645. *
  1646. * @return array
  1647. * An array with information about the requested access control plugin.
  1648. */
  1649. function ctools_get_access_plugin($name) {
  1650. ctools_include('plugins');
  1651. return ctools_get_plugins('ctools', 'access', $name);
  1652. }
  1653. /**
  1654. * Fetch metadata for all access control plugins.
  1655. *
  1656. * @return array
  1657. * An array of arrays with information about all available access control plugins.
  1658. */
  1659. function ctools_get_access_plugins() {
  1660. ctools_include('plugins');
  1661. return ctools_get_plugins('ctools', 'access');
  1662. }
  1663. /**
  1664. * Fetch a list of access plugins that are available for a given list of
  1665. * contexts.
  1666. *
  1667. * If 'logged-in-user' is not in the list of contexts, it will be added as
  1668. * this is required.
  1669. *
  1670. * @param array $contexts
  1671. * Array of ctools_context objects with which to select access plugins.
  1672. *
  1673. * @return array
  1674. * Array of applicable access plugins. Can be empty.
  1675. */
  1676. function ctools_get_relevant_access_plugins($contexts) {
  1677. if (!isset($contexts['logged-in-user'])) {
  1678. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1679. }
  1680. $all_plugins = ctools_get_access_plugins();
  1681. $plugins = array();
  1682. foreach ($all_plugins as $id => $plugin) {
  1683. if (!empty($plugin['required context'])
  1684. && !ctools_context_match_requirements($contexts, $plugin['required context'])
  1685. ) {
  1686. continue;
  1687. }
  1688. $plugins[$id] = $plugin;
  1689. }
  1690. return $plugins;
  1691. }
  1692. /**
  1693. * Create a context for the logged in user.
  1694. */
  1695. function ctools_access_get_loggedin_context() {
  1696. $context = ctools_context_create('entity:user', array('type' => 'current'), TRUE);
  1697. $context->identifier = t('Logged in user');
  1698. $context->keyword = 'viewer';
  1699. $context->id = 0;
  1700. return $context;
  1701. }
  1702. /**
  1703. * Get a summary of an access plugin's settings.
  1704. *
  1705. * @return string
  1706. * The summary text.
  1707. */
  1708. function ctools_access_summary($plugin, $contexts, $test) {
  1709. if (!isset($contexts['logged-in-user'])) {
  1710. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1711. }
  1712. $description = '';
  1713. if ($function = ctools_plugin_get_function($plugin, 'summary')) {
  1714. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1715. $context = isset($test['context']) ? $test['context'] : array();
  1716. $selected_context = ctools_context_select($contexts, $required_context, $context);
  1717. $description = $function($test['settings'], $selected_context, $plugin);
  1718. }
  1719. if (!empty($test['not'])) {
  1720. $description = "NOT ($description)";
  1721. }
  1722. return $description;
  1723. }
  1724. /**
  1725. * Get a summary of a group of access plugin's settings.
  1726. *
  1727. * @param $access
  1728. * An array of settings theoretically set by the user, including the array
  1729. * of plugins to check:
  1730. * - 'plugins': the array of plugin metadata info to check
  1731. * - 'logic': (optional) either 'and' or 'or', indicating how to combine
  1732. * restrictions. Defaults to 'or'.
  1733. * @param array $contexts
  1734. * An array of zero or more contexts that may be used to determine if
  1735. * the user has access.
  1736. *
  1737. * @return string
  1738. * The summary text. Can be NULL if there are no plugins defined.
  1739. */
  1740. function ctools_access_group_summary($access, $contexts) {
  1741. if (empty($access['plugins']) || !is_array($access['plugins'])) {
  1742. return NULL;
  1743. }
  1744. $descriptions = array();
  1745. foreach ($access['plugins'] as $id => $test) {
  1746. $plugin = ctools_get_access_plugin($test['name']);
  1747. $descriptions[] = ctools_access_summary($plugin, $contexts, $test);
  1748. }
  1749. $separator =
  1750. (isset($access['logic']) && $access['logic'] === 'and')
  1751. ? t(', and ') : t(', or ');
  1752. return implode($separator, $descriptions);
  1753. }
  1754. /**
  1755. * Determine if the current user has access via a plugin.
  1756. *
  1757. * @param array $settings
  1758. * An array of settings theoretically set by the user, including the array
  1759. * of plugins to check:
  1760. * - 'plugins': the array of plugin metadata info to check
  1761. * - 'logic': (optional) either 'and' or 'or', indicating how to combine
  1762. * restrictions. The 'or' case is not fully implemented and returns the
  1763. * input contexts unchanged.
  1764. *
  1765. * @param array $contexts
  1766. * An array of zero or more contexts that may be used to determine if
  1767. * the user has access.
  1768. *
  1769. * @return bool
  1770. * TRUE if access is granted, FALSE if otherwise.
  1771. */
  1772. function ctools_access($settings, $contexts = array()) {
  1773. if (empty($settings['plugins'])) {
  1774. return TRUE;
  1775. }
  1776. if (!isset($settings['logic'])) {
  1777. $settings['logic'] = 'and';
  1778. }
  1779. if (!isset($contexts['logged-in-user'])) {
  1780. $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  1781. }
  1782. foreach ($settings['plugins'] as $test) {
  1783. $pass = FALSE;
  1784. $plugin = ctools_get_access_plugin($test['name']);
  1785. if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) {
  1786. // Do we need just some contexts or all of them?
  1787. if (!empty($plugin['all contexts'])) {
  1788. $test_contexts = $contexts;
  1789. }
  1790. else {
  1791. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1792. $context = isset($test['context']) ? $test['context'] : array();
  1793. $test_contexts = ctools_context_select($contexts, $required_context, $context);
  1794. }
  1795. $pass = $function($test['settings'], $test_contexts, $plugin);
  1796. if (!empty($test['not'])) {
  1797. $pass = !$pass;
  1798. }
  1799. }
  1800. if ($pass && $settings['logic'] == 'or') {
  1801. // Pass if 'or' and this rule passed.
  1802. return TRUE;
  1803. }
  1804. elseif (!$pass && $settings['logic'] == 'and') {
  1805. // Fail if 'and' and this rule failed.
  1806. return FALSE;
  1807. }
  1808. }
  1809. // Return TRUE if logic was and, meaning all rules passed.
  1810. // Return FALSE if logic was or, meaning no rule passed.
  1811. return ($settings['logic'] === 'and');
  1812. }
  1813. /**
  1814. * Create default settings for a new access plugin.
  1815. *
  1816. * @param $plugin
  1817. * The access plugin being used.
  1818. *
  1819. * @return array
  1820. * A default configured test that should be placed in $access['plugins'];
  1821. */
  1822. function ctools_access_new_test($plugin) {
  1823. $test = array(
  1824. 'name' => $plugin['name'],
  1825. 'settings' => array(),
  1826. );
  1827. // Set up required context defaults.
  1828. if (isset($plugin['required context'])) {
  1829. if (is_object($plugin['required context'])) {
  1830. $test['context'] = '';
  1831. }
  1832. else {
  1833. $test['context'] = array();
  1834. foreach ($plugin['required context'] as $required) {
  1835. $test['context'][] = '';
  1836. }
  1837. }
  1838. }
  1839. $default = NULL;
  1840. if (isset($plugin['default'])) {
  1841. $default = $plugin['default'];
  1842. }
  1843. elseif (isset($plugin['defaults'])) {
  1844. $default = $plugin['defaults'];
  1845. }
  1846. // Setup plugin defaults.
  1847. if (isset($default)) {
  1848. if (is_array($default)) {
  1849. $test['settings'] = $default;
  1850. }
  1851. elseif (function_exists($default)) {
  1852. $test['settings'] = $default();
  1853. }
  1854. else {
  1855. $test['settings'] = array();
  1856. }
  1857. }
  1858. return $test;
  1859. }
  1860. /**
  1861. * Apply restrictions to contexts based upon the access control configured.
  1862. *
  1863. * These restrictions allow the UI to not show content that may not be relevant
  1864. * to all types of a particular context.
  1865. *
  1866. * @param array $settings
  1867. * Array of keys specifying the settings:
  1868. * - 'plugins': the array of plugin metadata info to check. If not set, or
  1869. * not an array, the function returns with no action.
  1870. * - 'logic': (optional) either 'and' or 'or', indicating how to combine
  1871. * restrictions. Defaults to 'and'.
  1872. * The 'or' case is not fully implemented and returns with no action if
  1873. * there is more than one plugin.
  1874. *
  1875. * @param array $contexts
  1876. * Array of available contexts.
  1877. */
  1878. function ctools_access_add_restrictions($settings, $contexts) {
  1879. if (empty($settings['plugins']) || !is_array($settings['plugins'])) {
  1880. return;
  1881. }
  1882. if (!isset($settings['logic'])) {
  1883. $settings['logic'] = 'and';
  1884. }
  1885. // We're not going to try to figure out restrictions on the or.
  1886. if ($settings['logic'] === 'or' && count($settings['plugins']) > 1) {
  1887. return;
  1888. }
  1889. foreach ($settings['plugins'] as $test) {
  1890. $plugin = ctools_get_access_plugin($test['name']);
  1891. // $plugin is 'array()' on error.
  1892. if ($plugin
  1893. && $function = ctools_plugin_get_function($plugin, 'restrictions')
  1894. ) {
  1895. $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
  1896. $context = isset($test['context']) ? $test['context'] : array();
  1897. $contexts = ctools_context_select($contexts, $required_context, $context);
  1898. if ($contexts !== FALSE) {
  1899. $function($test['settings'], $contexts);
  1900. }
  1901. }
  1902. }
  1903. }