QuickQuicktabs.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Renders the content using the original Quicktabs mechanism of previous versions.
  4. * Includes support for ajax rendered content.
  5. */
  6. class QuickQuicktabs extends QuickRenderer {
  7. public function render() {
  8. $quickset = $this->quickset;
  9. $render_array = array();
  10. $active_tab = $quickset->getActiveTab();
  11. if ($tabs = $this->build_tablinks($active_tab)) {
  12. $render_array['#attached'] = $this->add_attached();
  13. $qt_name = $quickset->getName();
  14. $settings = $quickset->getSettings();
  15. $contents = $quickset->getContents();
  16. $render_array['content'] = array(
  17. '#theme' => 'qt_quicktabs',
  18. '#options' => array('attributes' => array(
  19. 'id' => 'quicktabs-' . $qt_name,
  20. 'class' => 'quicktabs-wrapper quicktabs-style-' . drupal_strtolower($settings['style']),
  21. )),
  22. 'tabs' => array('#theme' => 'qt_quicktabs_tabset', '#options' => array('active' => $active_tab, 'style' => drupal_strtolower($settings['style'])), 'tablinks' => $tabs),
  23. // The main content area, each quicktab container needs a unique id.
  24. 'container' => array(
  25. '#prefix' => '<div id="quicktabs-container-' . $qt_name .'" class="quicktabs_main quicktabs-style-' . drupal_strtolower($settings['style']) .'">',
  26. '#suffix' => '</div>',
  27. 'divs' => array(),
  28. ),
  29. );
  30. // If in ajax mode, we'll only be rendering one tab, otherwise all of them.
  31. $tabs_to_render = $settings['ajax'] ? array($active_tab => $contents[$active_tab]) : $contents;
  32. foreach ($tabs_to_render as $key => $tab) {
  33. if (!empty($tab)) {
  34. $attribs = array(
  35. 'id' => 'quicktabs-tabpage-'. $qt_name . '-'. $key,
  36. 'class' => array('quicktabs-tabpage', ($active_tab == $key ? '' : 'quicktabs-hide')),
  37. );
  38. $render_array['content']['container']['divs'][] = array(
  39. '#prefix' => '<div '. drupal_attributes($attribs) .'>',
  40. '#suffix' => '</div>',
  41. 'content' => $tab->render(),
  42. );
  43. }
  44. }
  45. }
  46. return $render_array;
  47. }
  48. /**
  49. * Build the actual tab links, with appropriate href, title and attributes.
  50. *
  51. * @param $active_tab The index of the active tab.
  52. */
  53. protected function build_tablinks($active_tab) {
  54. $quickset = $this->quickset;
  55. $settings = $quickset->getSettings();
  56. $tabs = array();
  57. foreach ($quickset->getContents() as $i => $tab) {
  58. if (!empty($tab)) {
  59. $tablink = array(
  60. '#type' => 'link',
  61. '#title' => $quickset->translateString($tab->getTitle(), 'tab', $i),
  62. '#href' => $_GET['q'],
  63. '#options' => $this->construct_link_options($i),
  64. );
  65. if ($settings['ajax']) {
  66. $tab_settings = $tab->getSettings();
  67. $ajax_keys = $tab->getAjaxKeys();
  68. $ajax_args = array();
  69. foreach ($ajax_keys as $key) {
  70. $ajax_args[] = $tab_settings[$key];
  71. }
  72. $ajax_path = $quickset->getAjaxPath($i, $tab->getType());
  73. $ajax_href = $ajax_path . '/'. implode('/', $ajax_args);
  74. $tablink['#ajax'] = array(
  75. 'progress' => array('message' => '', 'type' => 'throbber'),
  76. 'path' => $ajax_href,
  77. );
  78. }
  79. $tabs[$i] = $tablink;
  80. }
  81. }
  82. return $tabs;
  83. }
  84. /**
  85. * Add any necessary js, css and libraries for the render array.
  86. */
  87. protected function add_attached() {
  88. $attached = array(
  89. 'css' => array(
  90. array('data' => drupal_get_path('module', 'quicktabs') .'/css/quicktabs.css'),
  91. ),
  92. 'js' => array(
  93. array('data' => drupal_get_path('module', 'quicktabs') . '/js/quicktabs.js'),
  94. array('data' => 'misc/progress.js', 'weight' => JS_LIBRARY),
  95. ),
  96. );
  97. $settings = $this->quickset->getSettings();
  98. // Add the custom style css if a custom style has been set.
  99. $style_css = quicktabs_get_css($settings['style']);
  100. if (!empty($style_css)) {
  101. $attached['css'][] = $style_css;
  102. }
  103. // Prepare a tab_settings array for passing the tab info to our JavaScript.
  104. $tab_settings = array();
  105. foreach ($this->quickset->getContents() as $i => $content) {
  106. if (!empty($content)) {
  107. $tab_settings[$i] = $content->getSettings();
  108. }
  109. }
  110. // Add our JS settings
  111. $javascript = drupal_add_js();
  112. foreach ($javascript['settings']['data'] as $key => $settings) {
  113. if (key($settings) == 'quicktabs') {
  114. $qtkey = $key;
  115. break;
  116. }
  117. }
  118. $name = $this->quickset->getName();
  119. if (!isset($qtkey) || (isset($javascript['settings']['data'][$qtkey]['quicktabs'])
  120. && !array_key_exists('qt_' . $name, $javascript['settings']['data'][$qtkey]['quicktabs']))) {
  121. $quicktabs_array = array_merge(array('name' => $name, 'tabs' => $tab_settings), $settings);
  122. $attached['js'][] = array('data' => array('quicktabs' => array('qt_' . $name => $quicktabs_array)), 'type' => 'setting');
  123. }
  124. return $attached;
  125. }
  126. /**
  127. * Helper function to construct link options for tab links.
  128. */
  129. protected function construct_link_options($tabkey) {
  130. $qt_name = $this->quickset->getName();
  131. $id = 'quicktabs-tab-' . implode('-', array($qt_name, $tabkey));
  132. // Need to construct the correct querystring for the tab links.
  133. $query = drupal_get_query_parameters(NULL, array("qt-$qt_name", 'q', 'page'));
  134. $query["qt-{$qt_name}"] = $tabkey;
  135. $link_options = array(
  136. 'attributes' => array(
  137. 'id' => $id,
  138. ),
  139. 'query' => $query,
  140. 'fragment' => 'qt-' . $qt_name,
  141. );
  142. return $link_options;
  143. }
  144. }