context.inc 47 KB

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