QuickViewContent.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Class for tab content of type "view" - this is for rendering a view as tab
  4. * content.
  5. */
  6. class QuickViewContent extends QuickContent {
  7. // Each view that we render, whether via ajax or not, will need a unique DOM
  8. // id. Unfortunately we can only control the ones that Quicktabs is responsible
  9. // for, so if there are other views on the page, there may be duplicate ids.
  10. static $view_dom_id = 1;
  11. public static function getType() {
  12. return 'view';
  13. }
  14. public function optionsForm($delta, $qt) {
  15. $tab = $this->settings;
  16. $form = array();
  17. $views = quicktabs_get_views();
  18. $views_keys = array_keys($views);
  19. $selected_view = (isset($tab['vid']) ? $tab['vid'] : (isset($views_keys[0]) ? $views_keys[0] : ''));
  20. $form['view']['vid'] = array(
  21. '#type' => 'select',
  22. '#options' => $views,
  23. '#default_value' => $selected_view,
  24. '#title' => t('Select a view'),
  25. '#ajax' => array(
  26. 'callback' => '_quicktabs_replace_view_displays_callback',
  27. ),
  28. );
  29. $form['view']['display'] = array(
  30. '#type' => 'select',
  31. '#title' => 'display',
  32. '#options' => _quicktabs_get_views_displays($selected_view),
  33. '#default_value' => isset($tab['display']) ? $tab['display'] : '',
  34. '#prefix' => '<div id="view-display-dropdown-' . $delta . '">',
  35. '#suffix' => '</div>'
  36. );
  37. $form['view']['args'] = array(
  38. '#type' => 'textfield',
  39. '#title' => 'arguments',
  40. '#size' => '40',
  41. '#required' => FALSE,
  42. '#default_value' => isset($tab['args']) ? $tab['args'] : '',
  43. '#description' => t('Additional arguments to send to the view as if they were part of the URL in the form of arg1/arg2/arg3. You may use %0, %1, ..., %N to grab arguments from the URL.'),
  44. );
  45. return $form;
  46. }
  47. public function __construct($item) {
  48. parent::__construct($item);
  49. if (module_exists('views')) views_add_js('ajax_view');
  50. $this->settings['view_path'] = rawurlencode($_GET['q']);
  51. $this->settings['view_dom_id'] = self::$view_dom_id++;
  52. if (isset($item['args'])) {
  53. $url_args = arg();
  54. $args = $item['args'];
  55. foreach ($url_args as $id => $arg) {
  56. $args = str_replace("%$id", $arg, $args);
  57. }
  58. $args = preg_replace(',/?(%\d),', '', $args);
  59. if (!empty($args)) {
  60. $this->settings['ajax_args'] = rawurlencode($args);
  61. $args_array = explode('/', $args);
  62. }
  63. else {
  64. $this->settings['ajax_args'] = '';
  65. $args_array = array();
  66. }
  67. $this->settings['actual_args'] = $args_array;
  68. }
  69. }
  70. public function render($hide_empty = FALSE, $args = array()) {
  71. if (!empty($args)) {
  72. // The args have been passed in from an ajax request. We use Views' own
  73. // ajax functionality to get the view.
  74. // The first element of the args array is the qt_name, which we don't need
  75. // for this content type.
  76. array_shift($args);
  77. // The order of these arguments corresponds to the array returned in
  78. // $this->getAjaxKeys().
  79. $_REQUEST['view_name'] = array_shift($args);
  80. $_REQUEST['view_display_id'] = array_shift($args);
  81. $_REQUEST['view_dom_id'] = array_shift($args);
  82. $view_path = array_shift($args);
  83. $_REQUEST['view_path'] = rawurldecode($view_path);
  84. if (!empty($args)) {
  85. $view_args = array_shift($args);
  86. $_REQUEST['view_args'] = rawurldecode($view_args);
  87. }
  88. module_load_include('inc', 'views', 'includes/ajax');
  89. $view = views_ajax();
  90. foreach ($view['#commands'] as $command) {
  91. if ($command['command'] == 'insert') {
  92. return array('#markup' => trim($command['data']));
  93. }
  94. }
  95. return array();
  96. }
  97. // Non-ajax rendering of a view.
  98. if ($this->rendered_content) {
  99. return $this->rendered_content;
  100. }
  101. $item = $this->settings;
  102. $output = array();
  103. if (isset($item['vid'])) {
  104. if (module_exists('views')) {
  105. if ($view = views_get_view($item['vid'])) {
  106. if ($view->access($item['display'])) {
  107. $view->set_display($item['display']);
  108. $view->set_arguments($item['actual_args']);
  109. $view_output = $view->preview();
  110. if (!empty($view->result) || $view->display_handler->get_option('empty') || !empty($view->style_plugin->definition['even empty'])) {
  111. $output['#markup'] = $view_output;
  112. }
  113. }
  114. elseif (!$hide_empty) {
  115. $output['#markup'] = theme('quicktabs_tab_access_denied', array('tab' => $item));
  116. }
  117. $view->destroy();
  118. }
  119. }
  120. elseif (!$hide_empty) {
  121. $output['#markup'] = t('Views module is not enabled, cannot display content.');
  122. }
  123. }
  124. $this->rendered_content = $output;
  125. return $output;
  126. }
  127. public function getAjaxKeys() {
  128. return array('vid', 'display', 'view_dom_id', 'view_path', 'ajax_args');
  129. }
  130. }