context.module

  1. cis7 sites/all/modules/ulmus/context/context.module
  2. cle7 sites/all/modules/ulmus/context/context.module
  3. ecd7 sites/all/modules/ulmus/context/context.module
  4. elmsmedia7 sites/all/modules/ulmus/context/context.module
  5. harmony7 sites/all/modules/ulmus/context/context.module
  6. icor7 sites/all/modules/ulmus/context/context.module
  7. meedjum_blog7 sites/all/modules/ulmus/context/context.module
  8. mooc7 sites/all/modules/ulmus/context/context.module

Functions

Namesort descending Description
context_active_contexts Loads any active contexts with associated reactions. This should be run at a late stage of the page load to ensure that relevant contexts have been set.
context_block_view_alter hook_block_view_alter - if the context editor block is on this page, ensure that all blocks have some content so that empty blocks are not dropped
context_cache_get Wrapper around cache_get() to make it easier for context to pull different datastores from a single cache row.
context_cache_set Wrapper around cache_set() to make it easier for context to write different datastores to a single cache row.
context_clear Clears static context array() -- meant only for testing
context_conditions Get all context conditions.
context_condition_map Loads an associative array of conditions => context identifiers to allow contexts to be set by different conditions.
context_condition_met Queue or activate contexts that have met the specified condition.
context_context Master context function. Avoid calling this directly -- use one of the helper functions below.
context_context_list CTools list callback for bulk export.
context_context_plugins Implementation of hook_context_plugins().
context_context_registry Implementation of hook_context_registry().
context_ctools_plugin_type Implementation of hook_ctools_plugin_type().
context_delete Deletes an existing context.
context_empty Recursive helper function to determine whether an array and its children are entirely empty.
context_enabled_contexts Wrapper around context_load() that only returns enabled contexts.
context_exists Deprecated context_exists() function. Retained for backwards compatibility -- please use context_isset() instead.
context_export Exports the specified context.
context_flush_caches Implementation of hook_flush_caches().
context_get Retrieves a context by namespace + (optional) attribute.
context_get_plugin Get a plugin handler.
context_init Implementation of hook_init().
context_invalidate_cache Invalidates all context caches(). @TODO: Update to use a CTools API function for clearing plugin caches when/if it becomes available.
context_isset Returns a boolean for whether a context namespace + attribute have been set.
context_load Context loader.
context_page_alter implement hook_page_alter()
context_preprocess_block hook_block_view_alter - if the context editor block is on this page, ensure that all blocks have some content so that empty blocks are not dropped
context_preprocess_menu_link Implementation of hook_preprocess_menu_link().
context_reactions Get all context reactions.
context_save Inserts or updates a context object into the database. @TODO: should probably return the new cid on success -- make sure this doesn't break any checks elsewhere.
context_set Sets a context by namespace + attribute.
_context_registry Retrieves & caches the context registry.

Constants

File

sites/all/modules/ulmus/context/context.module
View source
  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', 'sitewide')) {
  154. $plugin->execute(1);
  155. }
  156. if ($plugin = context_get_plugin('condition', 'path')) {
  157. $plugin->execute();
  158. }
  159. if ($plugin = context_get_plugin('condition', 'query_string')) {
  160. $plugin->execute();
  161. }
  162. if ($plugin = context_get_plugin('condition', 'language')) {
  163. global $language;
  164. $plugin->execute($language->language);
  165. }
  166. if ($plugin = context_get_plugin('condition', 'user')) {
  167. global $user;
  168. $plugin->execute($user);
  169. }
  170. }
  171. /**
  172. * Implementation of hook_preprocess_menu_link().
  173. *
  174. * This allows menus that are not primary/secondary menus to get
  175. * the "active" class assigned to them. This assumes they are using
  176. * theme('menu_link') for the menu rendering to html.
  177. */
  178. function context_preprocess_menu_link(&$variables) {
  179. if($contexts = context_active_contexts()){
  180. foreach($contexts as $context){
  181. if((isset($context->reactions['menu']))){
  182. if ($variables['element']['#href'] == $context->reactions['menu']) {
  183. $variables['element']['#localized_options']['attributes']['class'][] = "active";
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Load & crud functions ==============================================
  191. */
  192. /**
  193. * Context loader.
  194. *
  195. * @param $name
  196. * The name for this context object.
  197. *
  198. * @return
  199. * Returns a fully-loaded context definition.
  200. */
  201. function context_load($name = NULL, $reset = FALSE) {
  202. ctools_include('export');
  203. static $contexts;
  204. static $altered;
  205. if (!isset($contexts) || $reset) {
  206. $contexts = $altered = array();
  207. if (!$reset && $contexts = context_cache_get('context')) {
  208. // Nothing here.
  209. }
  210. else {
  211. if ($reset) {
  212. ctools_export_load_object_reset('context');
  213. }
  214. $contexts = ctools_export_load_object('context', 'all');
  215. context_cache_set('context', $contexts);
  216. }
  217. }
  218. if (isset($name)) {
  219. // Allow other modules to alter the value just before it's returned.
  220. if (isset($contexts[$name]) && !isset($altered[$name])) {
  221. $altered[$name] = TRUE;
  222. drupal_alter('context_load', $contexts[$name]);
  223. }
  224. return isset($contexts[$name]) ? $contexts[$name] : FALSE;
  225. }
  226. return $contexts;
  227. }
  228. /**
  229. * Inserts or updates a context object into the database.
  230. * @TODO: should probably return the new cid on success -- make sure
  231. * this doesn't break any checks elsewhere.
  232. *
  233. * @param $context
  234. * The context object to be inserted.
  235. *
  236. * @return
  237. * Returns true on success, false on failure.
  238. */
  239. function context_save($context) {
  240. $existing = context_load($context->name, TRUE);
  241. if ($existing && ($existing->export_type & EXPORT_IN_DATABASE)) {
  242. drupal_write_record('context', $context, 'name');
  243. }
  244. else {
  245. drupal_write_record('context', $context);
  246. }
  247. context_load(NULL, TRUE);
  248. context_invalidate_cache();
  249. return TRUE;
  250. }
  251. /**
  252. * Deletes an existing context.
  253. *
  254. * @param $context
  255. * The context object to be deleted.
  256. *
  257. * @return
  258. * Returns true on success, false on failure.
  259. */
  260. function context_delete($context) {
  261. if (isset($context->name) && ($context->export_type & EXPORT_IN_DATABASE)) {
  262. db_query("DELETE FROM {context} WHERE name = :name", array(':name' => $context->name));
  263. context_invalidate_cache();
  264. return TRUE;
  265. }
  266. return FALSE;
  267. }
  268. /**
  269. * Exports the specified context.
  270. */
  271. function context_export($context, $indent = '') {
  272. $output = ctools_export_object('context', $context, $indent);
  273. $translatables = array();
  274. foreach (array('description', 'tag') as $key) {
  275. if (!empty($context->{$key})) {
  276. $translatables[] = $context->{$key};
  277. }
  278. }
  279. $translatables = array_filter(array_unique($translatables));
  280. if (!empty($translatables)) {
  281. $output .= "\n";
  282. $output .= "{$indent}// Translatables\n";
  283. $output .= "{$indent}// Included for use with string extractors like potx.\n";
  284. sort($translatables);
  285. foreach ($translatables as $string) {
  286. $output .= "{$indent}t(" . ctools_var_export($string) . ");\n";
  287. }
  288. }
  289. return $output;
  290. }
  291. /**
  292. * API FUNCTIONS ======================================================
  293. */
  294. /**
  295. * CTools list callback for bulk export.
  296. */
  297. function context_context_list() {
  298. $contexts = context_load(NULL, TRUE);
  299. $list = array();
  300. foreach ($contexts as $context) {
  301. $list[$context->name] = $context->name;
  302. }
  303. return $list;
  304. }
  305. /**
  306. * Wrapper around cache_get() to make it easier for context to pull different
  307. * datastores from a single cache row.
  308. */
  309. function context_cache_get($key, $reset = FALSE) {
  310. static $cache;
  311. if (!isset($cache) || $reset) {
  312. $cache = cache_get('context', 'cache');
  313. $cache = $cache ? $cache->data : array();
  314. }
  315. return !empty($cache[$key]) ? $cache[$key] : FALSE;
  316. }
  317. /**
  318. * Wrapper around cache_set() to make it easier for context to write different
  319. * datastores to a single cache row.
  320. */
  321. function context_cache_set($key, $value) {
  322. $cache = cache_get('context', 'cache');
  323. $cache = $cache ? $cache->data : array();
  324. $cache[$key] = $value;
  325. cache_set('context', $cache);
  326. }
  327. /**
  328. * Wrapper around context_load() that only returns enabled contexts.
  329. */
  330. function context_enabled_contexts($reset = FALSE) {
  331. $enabled = array();
  332. foreach (context_load(NULL, $reset) as $context) {
  333. if (empty($context->disabled)) {
  334. $enabled[$context->name] = $context;
  335. }
  336. }
  337. return $enabled;
  338. }
  339. /**
  340. * Queue or activate contexts that have met the specified condition.
  341. *
  342. * @param $context
  343. * The context object to queue or activate.
  344. * @param $condition
  345. * String. Name for the condition that has been met.
  346. * @param $reset
  347. * Reset flag for the queue static cache.
  348. */
  349. function context_condition_met($context, $condition, $reset = FALSE) {
  350. static $queue;
  351. if (!isset($queue) || $reset) {
  352. $queue = array();
  353. }
  354. if (!context_isset('context', $context->name)) {
  355. // Context is using AND mode. Queue it.
  356. if (isset($context->condition_mode) && $context->condition_mode == CONTEXT_CONDITION_MODE_AND) {
  357. $queue[$context->name][$condition] = $condition;
  358. // If all conditions have been met. set the context.
  359. if (!array_diff(array_keys($context->conditions), $queue[$context->name])) {
  360. context_set('context', $context->name, $context);
  361. }
  362. }
  363. // Context is using OR mode. Set it.
  364. else {
  365. context_set('context', $context->name, $context);
  366. }
  367. }
  368. }
  369. /**
  370. * Loads any active contexts with associated reactions. This should be run
  371. * at a late stage of the page load to ensure that relevant contexts have been set.
  372. */
  373. function context_active_contexts() {
  374. $contexts = context_get('context');
  375. return !empty($contexts) && is_array($contexts) ? $contexts : array();
  376. }
  377. /**
  378. * Loads an associative array of conditions => context identifiers to allow
  379. * contexts to be set by different conditions.
  380. */
  381. function context_condition_map($reset = FALSE) {
  382. static $condition_map;
  383. if (!isset($condition_map) || $reset) {
  384. if (!$reset && $cache = context_cache_get('condition_map')) {
  385. $condition_map = $cache;
  386. }
  387. else {
  388. $condition_map = array();
  389. foreach (array_keys(context_conditions()) as $condition) {
  390. if ($plugin = context_get_plugin('condition', $condition)) {
  391. foreach (context_enabled_contexts() as $context) {
  392. $values = $plugin->fetch_from_context($context, 'values');
  393. foreach ($values as $value) {
  394. if (!isset($condition_map[$condition][$value])) {
  395. $condition_map[$condition][$value] = array();
  396. }
  397. $condition_map[$condition][$value][] = $context->name;
  398. }
  399. }
  400. }
  401. }
  402. context_cache_set('condition_map', $condition_map);
  403. }
  404. }
  405. return $condition_map;
  406. }
  407. /**
  408. * Invalidates all context caches().
  409. * @TODO: Update to use a CTools API function for clearing plugin caches
  410. * when/if it becomes available.
  411. */
  412. function context_invalidate_cache() {
  413. cache_clear_all('context', 'cache', TRUE);
  414. cache_clear_all('plugins:context', 'cache', TRUE);
  415. }
  416. /**
  417. * Implementation of hook_flush_caches().
  418. */
  419. function context_flush_caches() {
  420. context_invalidate_cache();
  421. }
  422. /**
  423. * Recursive helper function to determine whether an array and its
  424. * children are entirely empty.
  425. */
  426. function context_empty($element) {
  427. $empty = TRUE;
  428. if (is_array($element)) {
  429. foreach ($element as $child) {
  430. $empty = $empty && context_empty($child);
  431. }
  432. }
  433. else {
  434. $empty = $empty && !isset($element);
  435. }
  436. return $empty;
  437. }
  438. /**
  439. * Get a plugin handler.
  440. */
  441. function context_get_plugin($type = 'condition', $key, $reset = FALSE) {
  442. static $cache = array();
  443. if (!isset($cache[$type][$key]) || $reset) {
  444. switch ($type) {
  445. case 'condition':
  446. $registry = context_conditions();
  447. break;
  448. case 'reaction':
  449. $registry = context_reactions();
  450. break;
  451. }
  452. if (isset($registry[$key], $registry[$key]['plugin'])) {
  453. ctools_include('plugins');
  454. $info = $registry[$key];
  455. $plugins = ctools_get_plugins('context', 'plugins');
  456. if (isset($plugins[$info['plugin']]) && $class = ctools_plugin_get_class($plugins[$info['plugin']], 'handler')) {
  457. // Check that class exists until CTools & registry issues are resolved.
  458. if (class_exists($class)) {
  459. $cache[$type][$key] = new $class($key, $info);
  460. }
  461. }
  462. }
  463. }
  464. return isset($cache[$type][$key]) ? $cache[$type][$key] : FALSE;
  465. }
  466. /**
  467. * Get all context conditions.
  468. */
  469. function context_conditions($reset = FALSE) {
  470. return _context_registry('conditions', $reset);
  471. }
  472. /**
  473. * Get all context reactions.
  474. */
  475. function context_reactions($reset = FALSE) {
  476. return _context_registry('reactions', $reset);
  477. }
  478. /**
  479. * Retrieves & caches the context registry.
  480. */
  481. function _context_registry($key = NULL, $reset = FALSE) {
  482. static $registry;
  483. if (!isset($registry) || $reset) {
  484. if (!$reset && $cache = context_cache_get('registry')) {
  485. $registry = $cache;
  486. }
  487. else {
  488. $registry = module_invoke_all('context_registry');
  489. drupal_alter('context_registry', $registry);
  490. context_cache_set('registry', $registry);
  491. }
  492. }
  493. if (isset($key)) {
  494. return isset($registry[$key]) ? $registry[$key] : array();
  495. }
  496. return $registry;
  497. }
  498. /**
  499. * hook_block_view_alter - if the context editor block is on this page,
  500. * ensure that all blocks have some content so that empty blocks are
  501. * not dropped
  502. */
  503. function context_block_view_alter(&$data, $block) {
  504. if (context_isset('context_ui', 'context_ui_editor_present') && empty($data['content'])) {
  505. $data['content']['#markup'] = "<div class='context-block-empty-content'>" . t('This block appears empty when displayed on this page.') . "</div>";
  506. $data['context_block_hidden'] = TRUE;
  507. }
  508. }
  509. /**
  510. * implement hook_page_alter()
  511. *
  512. * used for region context
  513. */
  514. function context_page_alter(&$page) {
  515. if ($plugin = context_get_plugin('reaction', 'region')) {
  516. $plugin->execute($page);
  517. }
  518. }
  519. /**
  520. * hook_block_view_alter - if the context editor block is on this page,
  521. * ensure that all blocks have some content so that empty blocks are
  522. * not dropped
  523. */
  524. function context_preprocess_block(&$vars) {
  525. if (isset($vars['block']->context_block_hidden)) {
  526. $vars['classes_array'][] = 'context-block-hidden';
  527. $vars['classes_array'][] = 'context-block-empty';
  528. }
  529. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.