PageRenderTime 144ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Table.php

https://gitlab.com/zanderwong/admin
PHP | 466 lines | 226 code | 80 blank | 160 comment | 53 complexity | ce4a22ad20133163e63c55f001b3fc29 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.3.1
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * HTML Table Generating Class
  18. *
  19. * Lets you create tables manually or from database result objects, or arrays.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category HTML Tables
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/uri.html
  26. */
  27. class CI_Table {
  28. var $rows = array();
  29. var $heading = array();
  30. var $auto_heading = TRUE;
  31. var $caption = NULL;
  32. var $template = NULL;
  33. var $newline = "\n";
  34. var $empty_cells = "";
  35. var $function = FALSE;
  36. public function __construct() {
  37. log_message('debug', "Table Class Initialized");
  38. }
  39. // --------------------------------------------------------------------
  40. /**
  41. * Set the template
  42. *
  43. * @access public
  44. * @param array
  45. * @return void
  46. */
  47. function set_template($template) {
  48. if (!is_array($template)) {
  49. return FALSE;
  50. }
  51. $this->template = $template;
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * Set the table heading
  56. *
  57. * Can be passed as an array or discreet params
  58. *
  59. * @access public
  60. * @param mixed
  61. * @return void
  62. */
  63. function set_heading() {
  64. $args = func_get_args();
  65. $this->heading = $this->_prep_args($args);
  66. }
  67. // --------------------------------------------------------------------
  68. /**
  69. * Set columns. Takes a one-dimensional array as input and creates
  70. * a multi-dimensional array with a depth equal to the number of
  71. * columns. This allows a single array with many elements to be
  72. * displayed in a table that has a fixed column count.
  73. *
  74. * @access public
  75. * @param array
  76. * @param int
  77. * @return void
  78. */
  79. function make_columns($array = array(), $col_limit = 0) {
  80. if (!is_array($array) OR count($array) == 0) {
  81. return FALSE;
  82. }
  83. // Turn off the auto-heading feature since it's doubtful we
  84. // will want headings from a one-dimensional array
  85. $this->auto_heading = FALSE;
  86. if ($col_limit == 0) {
  87. return $array;
  88. }
  89. $new = array();
  90. while (count($array) > 0) {
  91. $temp = array_splice($array, 0, $col_limit);
  92. if (count($temp) < $col_limit) {
  93. for ($i = count($temp); $i < $col_limit; $i++) {
  94. $temp[] = '&nbsp;';
  95. }
  96. }
  97. $new[] = $temp;
  98. }
  99. return $new;
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Set "empty" cells
  104. *
  105. * Can be passed as an array or discreet params
  106. *
  107. * @access public
  108. * @param mixed
  109. * @return void
  110. */
  111. function set_empty($value) {
  112. $this->empty_cells = $value;
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Add a table row
  117. *
  118. * Can be passed as an array or discreet params
  119. *
  120. * @access public
  121. * @param mixed
  122. * @return void
  123. */
  124. function add_row() {
  125. $args = func_get_args();
  126. $this->rows[] = $this->_prep_args($args);
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Prep Args
  131. *
  132. * Ensures a standard associative array format for all cell data
  133. *
  134. * @access public
  135. * @param type
  136. * @return type
  137. */
  138. function _prep_args($args) {
  139. // If there is no $args[0], skip this and treat as an associative array
  140. // This can happen if there is only a single key, for example this is passed to table->generate
  141. // array(array('foo'=>'bar'))
  142. if (isset($args[0]) AND (count($args) == 1 && is_array($args[0]))) {
  143. // args sent as indexed array
  144. if (!isset($args[0]['data'])) {
  145. foreach ($args[0] as $key => $val) {
  146. if (is_array($val) && isset($val['data'])) {
  147. $args[$key] = $val;
  148. } else {
  149. $args[$key] = array('data' => $val);
  150. }
  151. }
  152. }
  153. } else {
  154. foreach ($args as $key => $val) {
  155. if (!is_array($val)) {
  156. $args[$key] = array('data' => $val);
  157. }
  158. }
  159. }
  160. return $args;
  161. }
  162. // --------------------------------------------------------------------
  163. /**
  164. * Add a table caption
  165. *
  166. * @access public
  167. * @param string
  168. * @return void
  169. */
  170. function set_caption($caption) {
  171. $this->caption = $caption;
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Generate the table
  176. *
  177. * @access public
  178. * @param mixed
  179. * @return string
  180. */
  181. function generate($table_data = NULL) {
  182. // The table data can optionally be passed to this function
  183. // either as a database result object or an array
  184. if (!is_null($table_data)) {
  185. if (is_object($table_data)) {
  186. $this->_set_from_object($table_data);
  187. } elseif (is_array($table_data)) {
  188. $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
  189. $this->_set_from_array($table_data, $set_heading);
  190. }
  191. }
  192. // Is there anything to display? No? Smite them!
  193. if (count($this->heading) == 0 AND count($this->rows) == 0) {
  194. return 'Undefined table data';
  195. }
  196. // Compile and validate the template date
  197. $this->_compile_template();
  198. // set a custom cell manipulation function to a locally scoped variable so its callable
  199. $function = $this->function;
  200. // Build the table!
  201. $out = $this->template['table_open'];
  202. $out .= $this->newline;
  203. // Add any caption here
  204. if ($this->caption) {
  205. $out .= $this->newline;
  206. $out .= '<caption>' . $this->caption . '</caption>';
  207. $out .= $this->newline;
  208. }
  209. // Is there a table heading to display?
  210. if (count($this->heading) > 0) {
  211. $out .= $this->template['thead_open'];
  212. $out .= $this->newline;
  213. $out .= $this->template['heading_row_start'];
  214. $out .= $this->newline;
  215. foreach ($this->heading as $heading) {
  216. $temp = $this->template['heading_cell_start'];
  217. foreach ($heading as $key => $val) {
  218. if ($key != 'data') {
  219. $temp = str_replace('<th', "<th $key='$val'", $temp);
  220. }
  221. }
  222. $out .= $temp;
  223. $out .= isset($heading['data']) ? $heading['data'] : '';
  224. $out .= $this->template['heading_cell_end'];
  225. }
  226. $out .= $this->template['heading_row_end'];
  227. $out .= $this->newline;
  228. $out .= $this->template['thead_close'];
  229. $out .= $this->newline;
  230. }
  231. // Build the table rows
  232. if (count($this->rows) > 0) {
  233. $out .= $this->template['tbody_open'];
  234. $out .= $this->newline;
  235. $i = 1;
  236. foreach ($this->rows as $row) {
  237. if (!is_array($row)) {
  238. break;
  239. }
  240. // We use modulus to alternate the row colors
  241. $name = (fmod($i++, 2)) ? '' : 'alt_';
  242. $out .= $this->template['row_' . $name . 'start'];
  243. $out .= $this->newline;
  244. foreach ($row as $cell) {
  245. $temp = $this->template['cell_' . $name . 'start'];
  246. foreach ($cell as $key => $val) {
  247. if ($key != 'data') {
  248. $temp = str_replace('<td', "<td $key='$val'", $temp);
  249. }
  250. }
  251. $cell = isset($cell['data']) ? $cell['data'] : '';
  252. $out .= $temp;
  253. if ($cell === "" OR $cell === NULL) {
  254. $out .= $this->empty_cells;
  255. } else {
  256. if ($function !== FALSE && is_callable($function)) {
  257. $out .= call_user_func($function, $cell);
  258. } else {
  259. $out .= $cell;
  260. }
  261. }
  262. $out .= $this->template['cell_' . $name . 'end'];
  263. }
  264. $out .= $this->template['row_' . $name . 'end'];
  265. $out .= $this->newline;
  266. }
  267. $out .= $this->template['tbody_close'];
  268. $out .= $this->newline;
  269. }
  270. $out .= $this->template['table_close'];
  271. // Clear table class properties before generating the table
  272. $this->clear();
  273. return $out;
  274. }
  275. // --------------------------------------------------------------------
  276. /**
  277. * Clears the table arrays. Useful if multiple tables are being generated
  278. *
  279. * @access public
  280. * @return void
  281. */
  282. function clear() {
  283. $this->rows = array();
  284. $this->heading = array();
  285. $this->auto_heading = TRUE;
  286. }
  287. // --------------------------------------------------------------------
  288. /**
  289. * Set table data from a database result object
  290. *
  291. * @access public
  292. * @param object
  293. * @return void
  294. */
  295. function _set_from_object($query) {
  296. if (!is_object($query)) {
  297. return FALSE;
  298. }
  299. // First generate the headings from the table column names
  300. if (count($this->heading) == 0) {
  301. if (!method_exists($query, 'list_fields')) {
  302. return FALSE;
  303. }
  304. $this->heading = $this->_prep_args($query->list_fields());
  305. }
  306. // Next blast through the result array and build out the rows
  307. if ($query->num_rows() > 0) {
  308. foreach ($query->result_array() as $row) {
  309. $this->rows[] = $this->_prep_args($row);
  310. }
  311. }
  312. }
  313. // --------------------------------------------------------------------
  314. /**
  315. * Set table data from an array
  316. *
  317. * @access public
  318. * @param array
  319. * @return void
  320. */
  321. function _set_from_array($data, $set_heading = TRUE) {
  322. if (!is_array($data) OR count($data) == 0) {
  323. return FALSE;
  324. }
  325. $i = 0;
  326. foreach ($data as $row) {
  327. // If a heading hasn't already been set we'll use the first row of the array as the heading
  328. if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) {
  329. $this->heading = $this->_prep_args($row);
  330. } else {
  331. $this->rows[] = $this->_prep_args($row);
  332. }
  333. $i++;
  334. }
  335. }
  336. // --------------------------------------------------------------------
  337. /**
  338. * Compile Template
  339. *
  340. * @access private
  341. * @return void
  342. */
  343. function _compile_template() {
  344. if ($this->template == NULL) {
  345. $this->template = $this->_default_template();
  346. return;
  347. }
  348. $this->temp = $this->_default_template();
  349. foreach (array('table_open', 'thead_open', 'thead_close', 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'tbody_open', 'tbody_close', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val) {
  350. if (!isset($this->template[$val])) {
  351. $this->template[$val] = $this->temp[$val];
  352. }
  353. }
  354. }
  355. // --------------------------------------------------------------------
  356. /**
  357. * Default Template
  358. *
  359. * @access private
  360. * @return void
  361. */
  362. function _default_template() {
  363. return array(
  364. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  365. 'thead_open' => '<thead>',
  366. 'thead_close' => '</thead>',
  367. 'heading_row_start' => '<tr>',
  368. 'heading_row_end' => '</tr>',
  369. 'heading_cell_start' => '<th>',
  370. 'heading_cell_end' => '</th>',
  371. 'tbody_open' => '<tbody>',
  372. 'tbody_close' => '</tbody>',
  373. 'row_start' => '<tr>',
  374. 'row_end' => '</tr>',
  375. 'cell_start' => '<td>',
  376. 'cell_end' => '</td>',
  377. 'row_alt_start' => '<tr>',
  378. 'row_alt_end' => '</tr>',
  379. 'cell_alt_start' => '<td>',
  380. 'cell_alt_end' => '</td>',
  381. 'table_close' => '</table>'
  382. );
  383. }
  384. }
  385. /* End of file Table.php */
  386. /* Location: ./system/libraries/Table.php */