views_php_plugin_cache.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Caching plugin that provides cache control based on custom PHP code.
  4. *
  5. * @ingroup views_cache_plugins
  6. */
  7. class views_php_plugin_cache extends views_plugin_cache {
  8. /**
  9. * Implements views_plugin_cache#summary_title().
  10. */
  11. function summary_title() {
  12. return t('PHP');
  13. }
  14. /**
  15. * Implements views_object#option_definition().
  16. */
  17. function option_definition() {
  18. $options = parent::option_definition();
  19. $options['php_cache_results'] = array('default' => '');
  20. $options['php_cache_output'] = array('default' => '');
  21. return $options;
  22. }
  23. /**
  24. * Implements views_plugin#options_form().
  25. */
  26. function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $form += views_php_form_element($this,
  29. FALSE,
  30. array('php_cache_results', t('Result cache code'), t('The code must return TRUE if the cache is still fresh, FALSE otherwise.'), FALSE),
  31. array('$view', '$plugin', '$cache')
  32. );
  33. $form += views_php_form_element($this,
  34. FALSE,
  35. array('php_cache_output', t('Output cache code'), t('The code must return TRUE if the cache is still fresh, FALSE otherwise.'), FALSE),
  36. array('$view', '$plugin', '$cache')
  37. );
  38. }
  39. /**
  40. * Implements views_plugin#options_submit()
  41. */
  42. function options_submit(&$form, &$form_state) {
  43. $form_state['values']['cache_options'] += $form_state['values']['options'];
  44. }
  45. /**
  46. * Implements views_plugin_cache#cache_get()
  47. */
  48. function cache_get($type) {
  49. //$cutoff = $this->cache_expire($type);
  50. switch ($type) {
  51. case 'query':
  52. // Not supported currently, but this is certainly where we'd put it.
  53. return FALSE;
  54. case 'results':
  55. $cache = cache_get($this->get_results_key(), $this->table);
  56. $fresh = !empty($cache);
  57. if ($fresh && !empty($this->options['php_cache_results'])) {
  58. $function = create_function('$view, $plugin, $cache', $this->options['php_cache_results'] . ';');
  59. ob_start();
  60. $fresh = $function($this->view, $this, $cache);
  61. ob_end_clean();
  62. }
  63. // Values to set: $view->result, $view->total_rows, $view->execute_time,
  64. // $view->current_page.
  65. if ($fresh) {
  66. //if (!$cutoff || $cache->created > $cutoff) {
  67. $this->view->result = $cache->data['result'];
  68. $this->view->total_rows = $cache->data['total_rows'];
  69. $this->view->set_current_page = $cache->data['current_page'];
  70. $this->view->execute_time = 0;
  71. return TRUE;
  72. //}
  73. }
  74. return FALSE;
  75. case 'output':
  76. $cache = cache_get($this->get_output_key(), $this->table);
  77. $fresh = !empty($cache);
  78. if ($fresh && !empty($this->options['php_cache_output'])) {
  79. $function = create_function('$view, $plugin, $cache', $this->options['php_cache_output'] . ';');
  80. ob_start();
  81. $fresh = $function($this->view, $this, $cache);
  82. ob_end_clean();
  83. }
  84. if ($fresh) {
  85. //if (!$cutoff || $cache->created > $cutoff) {
  86. $this->storage = $cache->data;
  87. $this->view->display_handler->output = $cache->data['output'];
  88. $this->restore_headers();
  89. return TRUE;
  90. //}
  91. }
  92. return FALSE;
  93. }
  94. }
  95. }