/OpenVBX/libraries/MY_Table.php

https://github.com/netconstructor/OpenVBX · PHP · 210 lines · 115 code · 36 blank · 59 comment · 16 complexity · 90f0c7c748b1f55f80b3d3f77559e6ff MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * "The contents of this file are subject to the Mozilla Public License
  4. * Version 1.1 (the "License"); you may not use this file except in
  5. * compliance with the License. You may obtain a copy of the License at
  6. * http://www.mozilla.org/MPL/
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. * The Original Code is OpenVBX, released June 15, 2010.
  12. * The Initial Developer of the Original Code is Twilio Inc.
  13. * Portions created by Twilio Inc. are Copyright (C) 2010.
  14. * All Rights Reserved.
  15. * Contributor(s):
  16. **/
  17. // ------------------------------------------------------------------------
  18. /**
  19. * HTML Table Generating Class: extended to include row/column ids
  20. *
  21. */
  22. class MY_Table extends CI_Table
  23. {
  24. var $row_ids = array(); // holds the ID values to be appended to each row
  25. var $id;
  26. function MY_Table($id = '') {
  27. parent::CI_Table();
  28. $this->id = empty($id) ? 'table_' . uniqid() : $id;
  29. }
  30. // --------------------------------------------------------------------
  31. /**
  32. * Add a table row
  33. *
  34. * The first parameter will be the {ID} value that is replaced on each row template.
  35. * Data can be passed as an array or discreet params
  36. *
  37. * @access public
  38. * @param mixed
  39. * @return void
  40. */
  41. function add_id_row($id, $data)
  42. {
  43. $args = func_get_args();
  44. array_shift($args);
  45. $this->rows[] = (is_array($data)) ? $data : $args;
  46. $index = count($this->rows) - 1;
  47. $this->row_ids[$index] = $id;
  48. }
  49. // --------------------------------------------------------------------
  50. /**
  51. * Generate the table
  52. *
  53. * @access public
  54. * @param mixed
  55. * @return string
  56. */
  57. function generate($table_data = NULL)
  58. {
  59. // The table data can optionally be passed to this function
  60. // either as a database result object or an array
  61. if ( ! is_null($table_data))
  62. {
  63. if (is_object($table_data))
  64. {
  65. $this->_set_from_object($table_data);
  66. }
  67. elseif (is_array($table_data))
  68. {
  69. $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
  70. $this->_set_from_array($table_data, $set_heading);
  71. }
  72. }
  73. // Is there anything to display? No? Smite them!
  74. if (count($this->heading) == 0 AND count($this->rows) == 0)
  75. {
  76. return 'Undefined table data';
  77. }
  78. // Compile and validate the template date
  79. $this->_compile_template();
  80. // Build the table!
  81. $out = str_replace('{ID}', $this->id, $this->template['table_open']);
  82. $out .= $this->newline;
  83. // Add any caption here
  84. if ($this->caption)
  85. {
  86. $out .= $this->newline;
  87. $out .= '<caption>' . $this->caption . '</caption>';
  88. $out .= $this->newline;
  89. }
  90. // Is there a table heading to display?
  91. if (count($this->heading) > 0)
  92. {
  93. $out .= $this->template['heading_row_start'];
  94. $out .= $this->newline;
  95. for($col = 0; $col < count($this->heading); $col++)
  96. {
  97. $out .= str_replace('{COL}', $col, $this->template['heading_cell_start']);
  98. $out .= $this->heading[$col];
  99. $out .= $this->template['heading_cell_end'];
  100. }
  101. $out .= $this->template['heading_row_end'];
  102. $out .= $this->newline;
  103. }
  104. // Build the table rows
  105. if (count($this->rows) > 0)
  106. {
  107. $i = 1;
  108. $row_find = array('{ROW}', '{ID}');
  109. for($r = 0; $r < count($this->rows); $r++)
  110. {
  111. $row = $this->rows[$r];
  112. if ( ! is_array($row)) break;
  113. $row_id = isset($this->row_ids[$r]) ? $this->row_ids[$r] : '';
  114. // We use modulus to alternate the row colors
  115. $name = (fmod($i++, 2)) ? '' : 'alt_';
  116. $out .= str_replace($row_find, array($r, $row_id), $this->template['row_'.$name.'start']);
  117. $out .= $this->newline;
  118. for($col = 0; $col < count($row); $col++)
  119. {
  120. $out .= str_replace('{COL}', $col, $this->template['cell_'.$name.'start']);
  121. $cell = $row[$col];
  122. if ($cell === "")
  123. {
  124. $out .= $this->empty_cells;
  125. }
  126. else
  127. {
  128. $out .= $cell;
  129. }
  130. $out .= $this->template['cell_'.$name.'end'];
  131. }
  132. $out .= $this->template['row_'.$name.'end'];
  133. $out .= $this->newline;
  134. }
  135. }
  136. $out .= $this->template['table_close'];
  137. return $out;
  138. }
  139. function clear()
  140. {
  141. parent::clear();
  142. $this->row_ids = array();
  143. }
  144. // --------------------------------------------------------------------
  145. /**
  146. * Default Template
  147. *
  148. * @access private
  149. * @return void
  150. */
  151. function _default_template()
  152. {
  153. return array (
  154. 'table_open' => '<table border="0" id="{ID}" class="grid">',
  155. 'heading_row_start' => '<thead><tr>',
  156. 'heading_row_end' => '</tr></thead><tbody>',
  157. 'heading_cell_start' => '<th class="col_{COL}">',
  158. 'heading_cell_end' => '</th>',
  159. 'row_start' => '<tr rel="{ID}" class="even_row row_{ROW}">',
  160. 'row_end' => '</tr>',
  161. 'cell_start' => '<td class="col_{COL}">',
  162. 'cell_end' => '</td>',
  163. 'row_alt_start' => '<tr rel="{ID}" class="odd_row row_{ROW}">',
  164. 'row_alt_end' => '</tr>',
  165. 'cell_alt_start' => '<td class="col_{COL}">',
  166. 'cell_alt_end' => '</td>',
  167. 'table_close' => '</tbody></table>'
  168. );
  169. }
  170. }