tripal_panes_generic.tpl.php 6.6 KB

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