tripal_panes_generic.tpl.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $panes = $variables['element']['#panes'];
  13. $fields = $variables['element']['#fields'];
  14. // Process fields in panes
  15. $content = '';
  16. $toc = '';
  17. $has_base_pane_only = TRUE;
  18. foreach ($panes AS $pane_id => $pane) {
  19. if ($pane->name != 'te_base') {
  20. $has_base_pane_only = FALSE;
  21. }
  22. $pane_settings = unserialize($pane->settings);
  23. $table_layout_group = key_exists('table_layout', $pane_settings) ? $pane_settings['table_layout'] : array();
  24. // Rearrange fields into groups for each pane
  25. $pane_fields = $fields[$pane_id];
  26. // Keyed by field's '#weight' and '#field_name so we can ksort() by weight
  27. $weighed_fields = array();
  28. foreach ($pane_fields as $field) {
  29. $weighed_fields [$field['#weight'] . $field['#field_name']] = $field;
  30. }
  31. ksort($weighed_fields, SORT_NUMERIC);
  32. // Render weighed fields
  33. $table_layout = array();
  34. $no_group = array();
  35. $output = '';
  36. $current_layout = '';
  37. $counter = 0;
  38. foreach ($weighed_fields AS $field) {
  39. // The field is in a table
  40. if (in_array($field['#field_name'], $table_layout_group)) {
  41. if ($counter != 0 && $current_layout != 'Table') {
  42. $output .= tripal_panes_generic_render_fields($no_group);
  43. $no_group = array();
  44. }
  45. $table_layout [$field['#weight'] . $field['#field_name']] = $field;
  46. $current_layout = 'Table';
  47. }
  48. // The field is not in a table
  49. else {
  50. if ($counter != 0 && $current_layout != 'Default') {
  51. $output .= tripal_panes_generic_render_table($table_layout);
  52. $table_layout = array();
  53. }
  54. $no_group [$field['#weight'] . $field['#field_name']] = $field;
  55. $current_layout = 'Default';
  56. }
  57. $counter ++;
  58. }
  59. if ($current_layout == 'Table') {
  60. $output .= tripal_panes_generic_render_table($table_layout);
  61. }
  62. else if ($current_layout == 'Default') {
  63. $output .= tripal_panes_generic_render_fields($no_group);
  64. }
  65. // If this is a base content, do not organize the content in a fieldset
  66. if ($pane->name == 'te_base') {
  67. $content .= '<div class="tripal_pane-base_pane">' . $output . '</div>';
  68. }
  69. else {
  70. $collapsible_item = array('element' => array());
  71. $collapsible_item['element']['#description'] = $output;
  72. $collapsible_item['element']['#title'] = $pane->label;
  73. $collapsible_item['element']['#children'] = '';
  74. $collapsible_item['element']['#attributes']['id'] = 'tripal_pane-fieldset-' . $pane->name;
  75. $collapsible_item['element']['#attributes']['class'][] = 'tripal_pane-fieldset';
  76. $collapsible_item['element']['#attributes']['class'][] = 'collapsible';
  77. $collapsible_item['element']['#attributes']['class'][] = 'collapsed';
  78. $toc_item_id = $pane_id;
  79. $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>";
  80. $content .= theme('fieldset', $collapsible_item);
  81. }
  82. }
  83. // TODO: need to add the bundle type
  84. $bundle_type = '';
  85. if ($has_base_pane_only) { ?>
  86. <div id ="tripal-<?php print $bundle_type?>-contents-box"> <?php
  87. // print the rendered content
  88. print $content; ?>
  89. </div> <?php
  90. }
  91. else { ?>
  92. <table id ="tripal-<?php print $bundle_type?>-contents-table" class="tripal-panes-table">
  93. <tr class="tripal-panes-table-tr">
  94. <td nowrap class="tripal-panes-table-td tripal-panes-table-td-toc" align="left"><?php
  95. print $toc; ?>
  96. </td>
  97. <td class="tripal-panes-table-td-data" align="left" width="100%"> <?php
  98. // print the rendered content
  99. print $content; ?>
  100. </td>
  101. </tr>
  102. </table> <?php
  103. }
  104. /**
  105. * A wrapper function for placing fields inside of a Drupal themed table.
  106. *
  107. * @param $fields
  108. * The list of fields present in the pane.
  109. * @return
  110. * A string containing the HTMLified table.
  111. */
  112. function tripal_panes_generic_render_table($fields) {
  113. // If we have no fields in table layout
  114. if (count($fields) == 0) {
  115. return '';
  116. }
  117. // Create the rows for the table.
  118. $header = array();
  119. $rows = array();
  120. foreach ($fields as $field) {
  121. $rows[] = array(
  122. array(
  123. 'data' => $field['#title'],
  124. 'header' => TRUE,
  125. 'width' => '20%',
  126. 'nowrap' => 'nowrap'
  127. ),
  128. '<span class="field field-name-' . preg_replace('/_/', '-', $field['#field_name']) . '">' . $field[0]['#markup'] . '</span>'
  129. );
  130. }
  131. // Theme the table.
  132. return theme_table(array(
  133. 'header' => $header,
  134. 'rows' => $rows,
  135. 'attributes' => array(
  136. 'id' => '', // TODO: need to add an ID
  137. 'class' => 'tripal-data-horz-table'
  138. ),
  139. 'sticky' => FALSE,
  140. 'caption' => '',
  141. 'colgroups' => array(),
  142. 'empty' => '',
  143. ));
  144. }
  145. /**
  146. * A wrapper function for default rending of fields.
  147. *
  148. * @param $fields
  149. * An array of fields to be rendered
  150. * @return
  151. * A string containing the HTML of the rendered fields.
  152. */
  153. function tripal_panes_generic_render_fields($fields) {
  154. if (count($fields) == 0) {
  155. return '';
  156. }
  157. $content = '';
  158. foreach ($fields as $field) {
  159. $content .= render($field);
  160. }
  161. return $content;
  162. }