tapir.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Contains the TAPIr code rolled into Ubercart core from Drupal contrib.
  5. */
  6. /**
  7. * Loads a table element for use in the Forms API or simply drupal_render().
  8. *
  9. * @param $table_id
  10. * The string identifying the table you want to display, like a form ID.
  11. * @param ...
  12. * Any additional arguments will be passed on to the table builder function
  13. * specified as the $table_id.
  14. *
  15. * @return
  16. * The table form element.
  17. */
  18. function tapir_get_table($table_id) {
  19. // Get any additional arguments.
  20. $args = func_get_args();
  21. array_shift($args);
  22. // Retrieve the table data and allow modules to alter it.
  23. $table = call_user_func_array($table_id, $args);
  24. array_unshift($args, $table_id);
  25. $data = &$table;
  26. // Pass the arguments in the table so that alter functions can use them.
  27. $data['#parameters'] = $args;
  28. drupal_alter('tapir_table', $data, $table_id);
  29. return $table;
  30. }
  31. /**
  32. * Pre-render callback for tapir_table elements.
  33. *
  34. * Gathers children of the tapir_table element into the #rows array. This
  35. * allows the element to render the children within the table as rows. Non-row
  36. * children are rendered after the table.
  37. */
  38. function tapir_gather_rows($element) {
  39. foreach (element_children($element) as $row) {
  40. foreach (array_keys($element['#columns']) as $col_id) {
  41. if (isset($element[$row][$col_id])) {
  42. $element['#rows'][$row][$col_id] = $element[$row][$col_id];
  43. unset($element[$row][$col_id]);
  44. }
  45. }
  46. $element['#rows'][$row]['#attributes'] = isset($element[$row]['#attributes']) ? $element[$row]['#attributes'] : array();
  47. }
  48. return $element;
  49. }
  50. /**
  51. * Themes form data into a table for display.
  52. *
  53. * @param $form
  54. * The array of form information needing to be rendered into the table.
  55. *
  56. * @return
  57. * The table output rendered in HTML.
  58. *
  59. * @ingroup themeable
  60. */
  61. function theme_tapir_table($variables) {
  62. $element = $variables['element'];
  63. $header = array();
  64. $rows = array();
  65. // First sort the columns by weight.
  66. uasort($element['#columns'], 'uc_weight_sort');
  67. // Loop through the columns and create the header array.
  68. foreach ($element['#columns'] as $col_id => $col_data) {
  69. // Add the cell if available.
  70. if (!isset($col_data['access']) || $col_data['access'] !== FALSE) {
  71. $header[] = $col_data['cell'];
  72. }
  73. }
  74. // Loop through the row data and create rows with the data in the right order.
  75. foreach ($element['#rows'] as $data) {
  76. $attributes = array();
  77. $row = array();
  78. // Loop through each column in the header.
  79. foreach ($element['#columns'] as $col_id => $col_data) {
  80. // If this row defines cell data for the current column...
  81. if ((!isset($col_data['access']) || $col_data['access'] !== FALSE) && isset($data[$col_id])) {
  82. $cell = array();
  83. if (isset($data[$col_id]['#cell_attributes']) && is_array($data[$col_id]['#cell_attributes'])) {
  84. foreach ($data[$col_id]['#cell_attributes'] as $property => $value) {
  85. if ($property == 'colspan' && $value == 'full') {
  86. // Extend full-width cells to the number of columns actually
  87. // displayed.
  88. $value = count($header);
  89. }
  90. $cell[$property] = $value;
  91. }
  92. $cell['data'] = drupal_render($data[$col_id]);
  93. }
  94. else {
  95. $cell = drupal_render($data[$col_id]);
  96. }
  97. // Add it to the row array.
  98. $row[] = $cell;
  99. }
  100. }
  101. // Merge the row data into a single row array along with the attributes.
  102. if (isset($data['#attributes'])) {
  103. $row = array_merge(array('data' => $row), (array)$data['#attributes']);
  104. }
  105. // Add the current row to the table rows array.
  106. $rows[] = $row;
  107. }
  108. // Return the rendered table.
  109. $options = array(
  110. 'header' => $header,
  111. 'rows' => $rows,
  112. );
  113. if (isset($element['#attributes'])) {
  114. $options['attributes'] = (array)$element['#attributes'];
  115. }
  116. if (isset($element['#title'])) {
  117. $options['caption'] = $element['#title'];
  118. }
  119. return theme('table', $options) . drupal_render_children($element);
  120. }