tripal_panes_generic.tpl.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * This template file provides the layout for the Tripal Panes modules. It
  4. * allows fields to be displayed inside of collapsible panes.
  5. *
  6. */
  7. // Add some necessary JavaScript dependencies.
  8. drupal_add_js('misc/form.js');
  9. drupal_add_js('misc/collapse.js');
  10. drupal_add_js(drupal_get_path('module','tripal_panes') . '/theme/js/tripal_panes.js');
  11. // Get the variables passed into this template.
  12. $bundle = $variables['element']['#bundle'];
  13. $bundle_type = $bundle->name . '-' . $bundle->label;
  14. $content = '';
  15. $toc = '';
  16. $has_base_pane_only = TRUE;
  17. get_content($variables, $bundle_type, $content, $toc, $has_base_pane_only);
  18. if ($has_base_pane_only) { ?>
  19. <div id ="tripal-<?php print $bundle_type?>-contents-box"> <?php
  20. // print the rendered content
  21. print $content; ?>
  22. </div> <?php
  23. }
  24. else { ?>
  25. <table id ="tripal-<?php print $bundle_type?>-contents-table" class="tripal-panes-table">
  26. <tr class="tripal-panes-table-tr">
  27. <td nowrap class="tripal-panes-table-td tripal-panes-table-td-toc" align="left"><?php
  28. print $toc; ?>
  29. </td>
  30. <td class="tripal-panes-table-td-data" align="left" width="100%"><?php
  31. // print the rendered content
  32. print $content; ?>
  33. </td>
  34. </tr>
  35. </table> <?php
  36. }
  37. /**
  38. * Iterates through the panes and generates a conent string in HTML format.
  39. *
  40. * @param $variables
  41. * @param $bundle_type
  42. * @param $content
  43. * @param $toc
  44. * @param $has_base_pane_only
  45. */
  46. function get_content($variables, $bundle_type, &$content, &$toc, &$has_base_pane_only) {
  47. $panes = $variables['element']['#panes'];
  48. $fields = $variables['element']['#fields'];
  49. // Iterate through all of the panes.
  50. foreach ($panes AS $pane_id => $pane) {
  51. // If we have a pane other than the base pane then we have more than
  52. // just the base.
  53. if ($pane->name != 'te_base') {
  54. $has_base_pane_only = FALSE;
  55. }
  56. $pane_settings = unserialize($pane->settings);
  57. $table_layout_group = key_exists('table_layout', $pane_settings) ? $pane_settings['table_layout'] : array();
  58. // Rearrange fields into groups for each pane
  59. $pane_fields = $fields[$pane_id];
  60. // Order the fields by their assigned weights.
  61. $ordered_fields = array();
  62. foreach ($pane_fields as $field) {
  63. $ordered_fields[$field['#weight'] . $field['#field_name']] = $field;
  64. }
  65. ksort($ordered_fields, SORT_NUMERIC);
  66. // Render fields
  67. $table_layout = array();
  68. $no_group = array();
  69. $output = '';
  70. $current_layout = '';
  71. $counter = 0;
  72. foreach ($ordered_fields AS $field) {
  73. //Add CSS Class
  74. $css_class = $field['#css_class'] ? ' ' . $field['#css_class'] : '';
  75. $field['#prefix'] = '<div class="tripal_panes-field-wrapper' . $css_class . '">';
  76. $field['#suffix'] = '</div>';
  77. // The field is in a table
  78. if (in_array($field['#field_name'], $table_layout_group)) {
  79. if ($counter != 0 && $current_layout != 'Table') {
  80. $output .= tripal_panes_generic_render_fields($no_group);
  81. $no_group = array();
  82. }
  83. $table_layout [$field['#weight'] . $field['#field_name']] = $field;
  84. $current_layout = 'Table';
  85. }
  86. // The field is not in a table
  87. else {
  88. if ($counter != 0 && $current_layout != 'Default') {
  89. $output .= tripal_panes_generic_render_table($table_layout, $bundle_type);
  90. $table_layout = array();
  91. }
  92. $no_group [$field['#weight'] . $field['#field_name']] = $field;
  93. $current_layout = 'Default';
  94. }
  95. $counter ++;
  96. }
  97. if ($current_layout == 'Table') {
  98. $output .= tripal_panes_generic_render_table($table_layout, $bundle_type);
  99. }
  100. else if ($current_layout == 'Default') {
  101. $output .= tripal_panes_generic_render_fields($no_group);
  102. }
  103. $pane_label = $pane->name == 'te_base' ? 'Summary' : $pane->label;
  104. $collapsible_item = array('element' => array());
  105. $collapsible_item['element']['#description'] = $output;
  106. $collapsible_item['element']['#title'] = $pane_label;
  107. $collapsible_item['element']['#children'] = '';
  108. $collapsible_item['element']['#attributes']['id'] = 'tripal_pane-fieldset-' . $pane->name;
  109. $collapsible_item['element']['#attributes']['class'][] = 'tripal_pane-fieldset';
  110. $collapsible_item['element']['#attributes']['class'][] = 'collapsible';
  111. if ($pane->name != 'te_base') {
  112. $collapsible_item['element']['#attributes']['class'][] = 'collapsed';
  113. }
  114. $toc_item_id = $pane_id;
  115. $toc .= "<div class=\"tripal-panes-toc-list-item\"><a id=\"" . $pane->name . "\" class=\"tripal_panes-toc-list-item-link\" href=\"?pane=" . $pane->name . "\">" . $pane_label . "</a></div>";
  116. $content .= theme('fieldset', $collapsible_item);
  117. if ($pane->name == 'te_base') {
  118. $content .= '<div class="tripal-panes-content-top"></div>';
  119. }
  120. }
  121. }
  122. /**
  123. * A wrapper function for placing fields inside of a Drupal themed table.
  124. *
  125. * @param $fields
  126. * The list of fields present in the pane.
  127. * @return
  128. * A string containing the HTMLified table.
  129. */
  130. function tripal_panes_generic_render_table($fields, $bundle_type) {
  131. // If we have no fields in table layout
  132. if (count($fields) == 0) {
  133. return '';
  134. }
  135. // Create the rows for the table.
  136. $header = array();
  137. $rows = array();
  138. foreach ($fields as $field) {
  139. // We may have multiple values for the field, so we need to iterate
  140. // through those values first and add each one.
  141. $value = '';
  142. foreach (element_children($field) as $index) {
  143. $eo = 'odd';
  144. if ($index % 2 == 0) {
  145. $eo = 'even';
  146. }
  147. $value .= "<div class=\"field-item $eo\">" . $field[$index]['#markup'] . '</div>';
  148. }
  149. // Add the new row.
  150. $rows[] = array(
  151. array(
  152. 'data' => '<div class="field-label">' . $field['#title'] . '</div>',
  153. 'header' => TRUE,
  154. 'width' => '20%',
  155. 'nowrap' => 'nowrap'
  156. ),
  157. $value,
  158. );
  159. }
  160. // Theme the table.
  161. return theme_table(array(
  162. 'header' => $header,
  163. 'rows' => $rows,
  164. 'attributes' => array(
  165. 'id' => 'tripal_panes-' . $bundle_type . '-table', // TODO: need to add an ID
  166. 'class' => 'tripal-data-horz-table'
  167. ),
  168. 'sticky' => FALSE,
  169. 'caption' => '',
  170. 'colgroups' => array(),
  171. 'empty' => '',
  172. ));
  173. }
  174. /**
  175. * A wrapper function for default rending of fields.
  176. *
  177. * @param $fields
  178. * An array of fields to be rendered
  179. * @return
  180. * A string containing the HTML of the rendered fields.
  181. */
  182. function tripal_panes_generic_render_fields($fields) {
  183. if (count($fields) == 0) {
  184. return '';
  185. }
  186. $content = '';
  187. foreach ($fields as $field) {
  188. $content .= render($field);
  189. }
  190. return $content;
  191. }