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