simple.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @file
  4. * Provides a simple time-based caching option for panel panes.
  5. */
  6. // Plugin definition
  7. $plugin = array(
  8. 'title' => t("Simple cache"),
  9. 'description' => t('Simple caching is a time-based cache. This is a hard limit, and once cached it will remain that way until the time limit expires.'),
  10. 'cache get' => 'panels_simple_cache_get_cache',
  11. 'cache set' => 'panels_simple_cache_set_cache',
  12. 'cache clear' => 'panels_simple_cache_clear_cache',
  13. 'settings form' => 'panels_simple_cache_settings_form',
  14. 'settings form submit' => 'panels_simple_cache_settings_form_submit',
  15. 'defaults' => array(
  16. 'lifetime' => 15,
  17. 'granularity' => 'none',
  18. ),
  19. );
  20. /**
  21. * Get cached content.
  22. */
  23. function panels_simple_cache_get_cache($conf, $display, $args, $contexts, $pane = NULL) {
  24. $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  25. $cache = cache_get($cid, 'cache');
  26. if (!$cache) {
  27. return FALSE;
  28. }
  29. if ((time() - $cache->created) > $conf['lifetime']) {
  30. return FALSE;
  31. }
  32. return $cache->data;
  33. }
  34. /**
  35. * Set cached content.
  36. */
  37. function panels_simple_cache_set_cache($conf, $content, $display, $args, $contexts, $pane = NULL) {
  38. $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  39. cache_set($cid, $content);
  40. }
  41. /**
  42. * Clear cached content.
  43. *
  44. * Cache clears are always for an entire display, regardless of arguments.
  45. */
  46. function panels_simple_cache_clear_cache($display) {
  47. $cid = 'panels_simple_cache';
  48. // This is used in case this is an in-code display, which means did will be something like 'new-1'.
  49. if (isset($display->owner) && isset($display->owner->id)) {
  50. $cid .= ':' . $display->owner->id;
  51. }
  52. $cid .= ':' . $display->did;
  53. cache_clear_all($cid, 'cache', TRUE);
  54. }
  55. /**
  56. * Figure out an id for our cache based upon input and settings.
  57. */
  58. function panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane) {
  59. $id = 'panels_simple_cache';
  60. // If the panel is stored in the database it'll have a numeric did value.
  61. if (is_numeric($display->did)) {
  62. $id .= ':' . $display->did;
  63. }
  64. // Exported panels won't have a numeric did but may have a usable cache_key.
  65. elseif (!empty($display->cache_key)) {
  66. $id .= ':' . str_replace('panel_context:', '', $display->cache_key);
  67. }
  68. // Alternatively use the css_id.
  69. elseif (!empty($display->css_id)) {
  70. $id .= ':' . $display->css_id;
  71. }
  72. // Failover to just appending the did, which may be the completely unusable
  73. // string 'new'.
  74. else {
  75. $id .= ':' . $display->did;
  76. }
  77. if ($pane) {
  78. $id .= ':' . $pane->pid;
  79. }
  80. if (user_access('view pane admin links')) {
  81. $id .= ':admin';
  82. }
  83. switch ($conf['granularity']) {
  84. case 'args':
  85. foreach ($args as $arg) {
  86. $id .= ':' . $arg;
  87. }
  88. break;
  89. case 'context':
  90. if (!is_array($contexts)) {
  91. $contexts = array($contexts);
  92. }
  93. foreach ($contexts as $context) {
  94. if (isset($context->argument)) {
  95. $id .= ':' . $context->argument;
  96. }
  97. }
  98. }
  99. if (module_exists('locale')) {
  100. global $language;
  101. $id .= ':' . $language->language;
  102. }
  103. if(!empty($pane->configuration['use_pager']) && !empty($_GET['page'])) {
  104. $id .= ':p' . check_plain($_GET['page']);
  105. }
  106. return $id;
  107. }
  108. function panels_simple_cache_settings_form($conf, $display, $pid) {
  109. $options = drupal_map_assoc(array(15, 30, 60, 120, 180, 240, 300, 600, 900, 1200, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 172800, 259200, 345600, 604800), 'format_interval');
  110. $form['lifetime'] = array(
  111. '#title' => t('Lifetime'),
  112. '#type' => 'select',
  113. '#options' => $options,
  114. '#default_value' => $conf['lifetime'],
  115. );
  116. $form['granularity'] = array(
  117. '#title' => t('Granularity'),
  118. '#type' => 'select',
  119. '#options' => array(
  120. 'args' => t('Arguments'),
  121. 'context' => t('Context'),
  122. 'none' => t('None'),
  123. ),
  124. '#description' => t('If "arguments" are selected, this content will be cached per individual argument to the entire display; if "contexts" are selected, this content will be cached per unique context in the pane or display; if "neither" there will be only one cache for this pane.'),
  125. '#default_value' => $conf['granularity'],
  126. );
  127. return $form;
  128. }