context.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. require('context.core.inc');
  3. define('CONTEXT_GET', 0);
  4. define('CONTEXT_SET', 1);
  5. define('CONTEXT_ISSET', 2);
  6. define('CONTEXT_CLEAR', 3);
  7. define('CONTEXT_CONDITION_MODE_OR', 0);
  8. define('CONTEXT_CONDITION_MODE_AND', 1);
  9. /**
  10. * Master context function. Avoid calling this directly -- use one of the helper functions below.
  11. *
  12. * @param $op
  13. * The operation to perform - handled by the context helper functions. Use them.
  14. * @param $namespace
  15. * A string to be used as the namespace for the context information.
  16. * @param $attribute
  17. * Usually a string to be used as a key to set/retrieve context information. An array can
  18. * also be used when setting context to establish an entire context namespace at once.
  19. * (At some point objects may also be accepted, but currently functionaliy isn't complete.)
  20. * @param $value
  21. * A value to set for the provided key. If omitted the value will be set to true.
  22. *
  23. * @return
  24. * Either the requested value, or false if the operation fails.
  25. */
  26. function context_context($op = CONTEXT_GET, $namespace = NULL, $attribute = NULL, $value = NULL) {
  27. static $context;
  28. $context = !$context ? array() : $context;
  29. switch ($op) {
  30. case CONTEXT_GET:
  31. // return entire context
  32. if (!$namespace) {
  33. return $context;
  34. }
  35. // return entire space if set
  36. elseif (isset($context[(string) $namespace])) {
  37. // return val of key from space
  38. if (is_array($context[(string) $namespace]) && isset($context[(string) $namespace][(string) $attribute])) {
  39. return $context[(string) $namespace][(string) $attribute];
  40. }
  41. elseif (!$attribute) {
  42. return $context[(string) $namespace];
  43. }
  44. }
  45. break;
  46. case CONTEXT_SET:
  47. // bail if invalid space is specified or context is already set
  48. if (is_string($namespace) || is_int($namespace)) {
  49. // initialize namespace if no key is specified
  50. if (!$attribute) {
  51. $context[(string) $namespace] = array();
  52. return TRUE;
  53. }
  54. // set to true if key is a usable identifier. otherwise, allow a key or object to be inserted
  55. if ($value === NULL) {
  56. if (is_string($attribute) || is_int($attribute)) {
  57. $context[(string) $namespace][(string) $attribute] = TRUE;
  58. return TRUE;
  59. }
  60. elseif (is_array($attribute) || is_object($attribute)) {
  61. $context[(string) $namespace] = $attribute;
  62. return TRUE;
  63. }
  64. }
  65. // set value if key is valid
  66. if ((is_string($attribute) || is_int($attribute)) && $value !== NULL) {
  67. $context[$namespace][$attribute] = $value;
  68. return TRUE;
  69. }
  70. }
  71. break;
  72. case CONTEXT_ISSET:
  73. // return entire context
  74. if (!$namespace) return FALSE;
  75. if (!$attribute) {
  76. // return entire space if set
  77. return isset($context[$namespace]);
  78. }
  79. // return val of key from space
  80. return isset($context[$namespace][$attribute]);
  81. case CONTEXT_CLEAR:
  82. $context = array();
  83. return TRUE;
  84. }
  85. return FALSE;
  86. }
  87. /**
  88. * Sets a context by namespace + attribute.
  89. */
  90. function context_set($namespace, $attribute = NULL, $value = NULL) {
  91. return context_context(CONTEXT_SET, $namespace, $attribute, $value);
  92. }
  93. /**
  94. * Retrieves a context by namespace + (optional) attribute.
  95. */
  96. function context_get($namespace = NULL, $attribute = NULL) {
  97. return context_context(CONTEXT_GET, $namespace, $attribute, NULL);
  98. }
  99. /**
  100. * Returns a boolean for whether a context namespace + attribute have been set.
  101. */
  102. function context_isset($namespace = NULL, $attribute = NULL) {
  103. return context_context(CONTEXT_ISSET, $namespace, $attribute, NULL);
  104. }
  105. /**
  106. * Deprecated context_exists() function. Retained for backwards
  107. * compatibility -- please use context_isset() instead.
  108. */
  109. function context_exists($namespace = NULL, $attribute = NULL) {
  110. return context_context(CONTEXT_ISSET, $namespace, $attribute, NULL);
  111. }
  112. /**
  113. * Clears static context array() -- meant only for testing
  114. */
  115. function context_clear() {
  116. return context_context(CONTEXT_CLEAR);
  117. }
  118. /**
  119. * Implemented hooks ==================================================
  120. */
  121. /**
  122. * Implementation of hook_ctools_plugin_type().
  123. */
  124. function context_ctools_plugin_type() {
  125. return array(
  126. 'plugins' => array(
  127. 'cache' => TRUE,
  128. 'use hooks' => TRUE,
  129. 'classes' => array('handler'),
  130. ),
  131. );
  132. }
  133. /**
  134. * Implementation of hook_context_plugins().
  135. *
  136. * This is a ctools plugins hook.
  137. */
  138. function context_context_plugins() {
  139. module_load_include('inc', 'context', 'context.plugins');
  140. return _context_context_plugins();
  141. }
  142. /**
  143. * Implementation of hook_context_registry().
  144. */
  145. function context_context_registry() {
  146. module_load_include('inc', 'context', 'context.plugins');
  147. return _context_context_registry();
  148. }
  149. /**
  150. * Implementation of hook_init().
  151. */
  152. function context_init() {
  153. if ($plugin = context_get_plugin('condition', 'path')) {
  154. $plugin->execute();
  155. }
  156. if ($plugin = context_get_plugin('condition', 'language')) {
  157. global $language;
  158. $plugin->execute($language->language);
  159. }
  160. if ($plugin = context_get_plugin('condition', 'user')) {
  161. global $user;
  162. $plugin->execute($user);
  163. }
  164. }
  165. /**
  166. * Implementation of hook_preprocess_menu_link().
  167. *
  168. * This allows menus that are not primary/secondary menus to get
  169. * the "active" class assigned to them. This assumes they are using
  170. * theme('menu_link') for the menu rendering to html.
  171. */
  172. function context_preprocess_menu_link(&$variables) {
  173. if($contexts = context_active_contexts()){
  174. foreach($contexts as $context){
  175. if((isset($context->reactions['menu']))){
  176. if ($variables['element']['#href'] == $context->reactions['menu']) {
  177. $variables['element']['#localized_options']['attributes']['class'][] = "active";
  178. }
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * Load & crud functions ==============================================
  185. */
  186. /**
  187. * Context loader.
  188. *
  189. * @param $name
  190. * The name for this context object.
  191. *
  192. * @return
  193. * Returns a fully-loaded context definition.
  194. */
  195. function context_load($name = NULL, $reset = FALSE) {
  196. ctools_include('export');
  197. static $contexts;
  198. static $altered;
  199. if (!isset($contexts) || $reset) {
  200. $contexts = $altered = array();
  201. if (!$reset && $contexts = context_cache_get('context')) {
  202. // Nothing here.
  203. }
  204. else {
  205. if ($reset) {
  206. ctools_export_load_object_reset('context');
  207. }
  208. $contexts = ctools_export_load_object('context', 'all');
  209. context_cache_set('context', $contexts);
  210. }
  211. }
  212. if (isset($name)) {
  213. // Allow other modules to alter the value just before it's returned.
  214. if (isset($contexts[$name]) && !isset($altered[$name])) {
  215. $altered[$name] = TRUE;
  216. drupal_alter('context_load', $contexts[$name]);
  217. }
  218. return isset($contexts[$name]) ? $contexts[$name] : FALSE;
  219. }
  220. return $contexts;
  221. }
  222. /**
  223. * Inserts or updates a context object into the database.
  224. * @TODO: should probably return the new cid on success -- make sure
  225. * this doesn't break any checks elsewhere.
  226. *
  227. * @param $context
  228. * The context object to be inserted.
  229. *
  230. * @return
  231. * Returns true on success, false on failure.
  232. */
  233. function context_save($context) {
  234. $existing = context_load($context->name, TRUE);
  235. if ($existing && ($existing->export_type & EXPORT_IN_DATABASE)) {
  236. drupal_write_record('context', $context, 'name');
  237. }
  238. else {
  239. drupal_write_record('context', $context);
  240. }
  241. context_load(NULL, TRUE);
  242. context_invalidate_cache();
  243. return TRUE;
  244. }
  245. /**
  246. * Deletes an existing context.
  247. *
  248. * @param $context
  249. * The context object to be deleted.
  250. *
  251. * @return
  252. * Returns true on success, false on failure.
  253. */
  254. function context_delete($context) {
  255. if (isset($context->name) && ($context->export_type & EXPORT_IN_DATABASE)) {
  256. db_query("DELETE FROM {context} WHERE name = :name", array(':name' => $context->name));
  257. context_invalidate_cache();
  258. return TRUE;
  259. }
  260. return FALSE;
  261. }
  262. /**
  263. * Exports the specified context.
  264. */
  265. function context_export($context, $indent = '') {
  266. $output = ctools_export_object('context', $context, $indent);
  267. $translatables = array();
  268. foreach (array('description', 'tag') as $key) {
  269. if (!empty($context->{$key})) {
  270. $translatables[] = $context->{$key};
  271. }
  272. }
  273. $translatables = array_filter(array_unique($translatables));
  274. if (!empty($translatables)) {
  275. $output .= "\n";
  276. $output .= "{$indent}// Translatables\n";
  277. $output .= "{$indent}// Included for use with string extractors like potx.\n";
  278. sort($translatables);
  279. foreach ($translatables as $string) {
  280. $output .= "{$indent}t(" . ctools_var_export($string) . ");\n";
  281. }
  282. }
  283. return $output;
  284. }
  285. /**
  286. * API FUNCTIONS ======================================================
  287. */
  288. /**
  289. * CTools list callback for bulk export.
  290. */
  291. function context_context_list() {
  292. $contexts = context_load(NULL, TRUE);
  293. $list = array();
  294. foreach ($contexts as $context) {
  295. $list[$context->name] = $context->name;
  296. }
  297. return $list;
  298. }
  299. /**
  300. * Wrapper around cache_get() to make it easier for context to pull different
  301. * datastores from a single cache row.
  302. */
  303. function context_cache_get($key, $reset = FALSE) {
  304. static $cache;
  305. if (!isset($cache) || $reset) {
  306. $cache = cache_get('context', 'cache');
  307. $cache = $cache ? $cache->data : array();
  308. }
  309. return !empty($cache[$key]) ? $cache[$key] : FALSE;
  310. }
  311. /**
  312. * Wrapper around cache_set() to make it easier for context to write different
  313. * datastores to a single cache row.
  314. */
  315. function context_cache_set($key, $value) {
  316. $cache = cache_get('context', 'cache');
  317. $cache = $cache ? $cache->data : array();
  318. $cache[$key] = $value;
  319. cache_set('context', $cache);
  320. }
  321. /**
  322. * Wrapper around context_load() that only returns enabled contexts.
  323. */
  324. function context_enabled_contexts($reset = FALSE) {
  325. $enabled = array();
  326. foreach (context_load(NULL, $reset) as $context) {
  327. if (empty($context->disabled)) {
  328. $enabled[$context->name] = $context;
  329. }
  330. }
  331. return $enabled;
  332. }
  333. /**
  334. * Queue or activate contexts that have met the specified condition.
  335. *
  336. * @param $context
  337. * The context object to queue or activate.
  338. * @param $condition
  339. * String. Name for the condition that has been met.
  340. * @param $reset
  341. * Reset flag for the queue static cache.
  342. */
  343. function context_condition_met($context, $condition, $reset = FALSE) {
  344. static $queue;
  345. if (!isset($queue) || $reset) {
  346. $queue = array();
  347. }
  348. if (!context_isset('context', $context->name)) {
  349. // Context is using AND mode. Queue it.
  350. if (isset($context->condition_mode) && $context->condition_mode == CONTEXT_CONDITION_MODE_AND) {
  351. $queue[$context->name][$condition] = $condition;
  352. // If all conditions have been met. set the context.
  353. if (!array_diff(array_keys($context->conditions), $queue[$context->name])) {
  354. context_set('context', $context->name, $context);
  355. }
  356. }
  357. // Context is using OR mode. Set it.
  358. else {
  359. context_set('context', $context->name, $context);
  360. }
  361. }
  362. }
  363. /**
  364. * Loads any active contexts with associated reactions. This should be run
  365. * at a late stage of the page load to ensure that relevant contexts have been set.
  366. */
  367. function context_active_contexts() {
  368. $contexts = context_get('context');
  369. return !empty($contexts) && is_array($contexts) ? $contexts : array();
  370. }
  371. /**
  372. * Loads an associative array of conditions => context identifiers to allow
  373. * contexts to be set by different conditions.
  374. */
  375. function context_condition_map($reset = FALSE) {
  376. static $condition_map;
  377. if (!isset($condition_map) || $reset) {
  378. if (!$reset && $cache = context_cache_get('condition_map')) {
  379. $condition_map = $cache;
  380. }
  381. else {
  382. $condition_map = array();
  383. foreach (array_keys(context_conditions()) as $condition) {
  384. if ($plugin = context_get_plugin('condition', $condition)) {
  385. foreach (context_enabled_contexts() as $context) {
  386. $values = $plugin->fetch_from_context($context, 'values');
  387. foreach ($values as $value) {
  388. if (!isset($condition_map[$condition][$value])) {
  389. $condition_map[$condition][$value] = array();
  390. }
  391. $condition_map[$condition][$value][] = $context->name;
  392. }
  393. }
  394. }
  395. }
  396. context_cache_set('condition_map', $condition_map);
  397. }
  398. }
  399. return $condition_map;
  400. }
  401. /**
  402. * Invalidates all context caches().
  403. * @TODO: Update to use a CTools API function for clearing plugin caches
  404. * when/if it becomes available.
  405. */
  406. function context_invalidate_cache() {
  407. cache_clear_all('context', 'cache', TRUE);
  408. cache_clear_all('plugins:context', 'cache', TRUE);
  409. }
  410. /**
  411. * Implementation of hook_flush_caches().
  412. */
  413. function context_flush_caches() {
  414. context_invalidate_cache();
  415. }
  416. /**
  417. * Recursive helper function to determine whether an array and its
  418. * children are entirely empty.
  419. */
  420. function context_empty($element) {
  421. $empty = TRUE;
  422. if (is_array($element)) {
  423. foreach ($element as $child) {
  424. $empty = $empty && context_empty($child);
  425. }
  426. }
  427. else {
  428. $empty = $empty && empty($element);
  429. }
  430. return $empty;
  431. }
  432. /**
  433. * Get a plugin handler.
  434. */
  435. function context_get_plugin($type = 'condition', $key, $reset = FALSE) {
  436. static $cache = array();
  437. if (!isset($cache[$type][$key]) || $reset) {
  438. switch ($type) {
  439. case 'condition':
  440. $registry = context_conditions();
  441. break;
  442. case 'reaction':
  443. $registry = context_reactions();
  444. break;
  445. }
  446. if (isset($registry[$key], $registry[$key]['plugin'])) {
  447. ctools_include('plugins');
  448. $info = $registry[$key];
  449. $plugins = ctools_get_plugins('context', 'plugins');
  450. if (isset($plugins[$info['plugin']]) && $class = ctools_plugin_get_class($plugins[$info['plugin']], 'handler')) {
  451. // Check that class exists until CTools & registry issues are resolved.
  452. if (class_exists($class)) {
  453. $cache[$type][$key] = new $class($key, $info);
  454. }
  455. }
  456. }
  457. }
  458. return isset($cache[$type][$key]) ? $cache[$type][$key] : FALSE;
  459. }
  460. /**
  461. * Get all context conditions.
  462. */
  463. function context_conditions($reset = FALSE) {
  464. return _context_registry('conditions', $reset);
  465. }
  466. /**
  467. * Get all context reactions.
  468. */
  469. function context_reactions($reset = FALSE) {
  470. return _context_registry('reactions', $reset);
  471. }
  472. /**
  473. * Retrieves & caches the context registry.
  474. */
  475. function _context_registry($key = NULL, $reset = FALSE) {
  476. static $registry;
  477. if (!isset($registry) || $reset) {
  478. if (!$reset && $cache = context_cache_get('registry')) {
  479. $registry = $cache;
  480. }
  481. else {
  482. $registry = module_invoke_all('context_registry');
  483. drupal_alter('context_registry', $registry);
  484. context_cache_set('registry', $registry);
  485. }
  486. }
  487. if (isset($key)) {
  488. return isset($registry[$key]) ? $registry[$key] : array();
  489. }
  490. return $registry;
  491. }
  492. /**
  493. * hook_block_view_alter - if the context editor block is on this page,
  494. * ensure that all blocks have some content so that empty blocks are
  495. * not dropped
  496. */
  497. function context_block_view_alter(&$data, $block) {
  498. if (context_isset('context_ui', 'context_ui_editor_present') && empty($data['content'])) {
  499. $data['content']['#markup'] = "<div class='context-block-empty-content'>" . t('This block appears empty when displayed on this page.') . "</div>";
  500. $data['context_block_hidden'] = TRUE;
  501. }
  502. }
  503. /**
  504. * implement hook_page_alter()
  505. *
  506. * used for region context
  507. */
  508. function context_page_alter(&$page) {
  509. if ($plugin = context_get_plugin('reaction', 'region')) {
  510. $plugin->execute($page);
  511. }
  512. }
  513. /**
  514. * hook_block_view_alter - if the context editor block is on this page,
  515. * ensure that all blocks have some content so that empty blocks are
  516. * not dropped
  517. */
  518. function context_preprocess_block(&$vars) {
  519. if (isset($vars['block']->context_block_hidden)) {
  520. $vars['classes_array'][] = 'context-block-hidden';
  521. $vars['classes_array'][] = 'context-block-empty';
  522. }
  523. }