PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Table.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 531 lines | 291 code | 80 blank | 160 comment | 48 complexity | 612ab49d4b31881e422df5f553d945b4 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 - 2011, 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. {
  38. log_message('debug', "Table Class Initialized");
  39. }
  40. // --------------------------------------------------------------------
  41. /**
  42. * Set the template
  43. *
  44. * @access public
  45. * @param array
  46. * @return void
  47. */
  48. function set_template($template)
  49. {
  50. if ( ! is_array($template))
  51. {
  52. return FALSE;
  53. }
  54. $this->template = $template;
  55. }
  56. // --------------------------------------------------------------------
  57. /**
  58. * Set the table heading
  59. *
  60. * Can be passed as an array or discreet params
  61. *
  62. * @access public
  63. * @param mixed
  64. * @return void
  65. */
  66. function set_heading()
  67. {
  68. $args = func_get_args();
  69. $this->heading = $this->_prep_args($args);
  70. }
  71. // --------------------------------------------------------------------
  72. /**
  73. * Set columns. Takes a one-dimensional array as input and creates
  74. * a multi-dimensional array with a depth equal to the number of
  75. * columns. This allows a single array with many elements to be
  76. * displayed in a table that has a fixed column count.
  77. *
  78. * @access public
  79. * @param array
  80. * @param int
  81. * @return void
  82. */
  83. function make_columns($array = array(), $col_limit = 0)
  84. {
  85. if ( ! is_array($array) OR count($array) == 0)
  86. {
  87. return FALSE;
  88. }
  89. // Turn off the auto-heading feature since it's doubtful we
  90. // will want headings from a one-dimensional array
  91. $this->auto_heading = FALSE;
  92. if ($col_limit == 0)
  93. {
  94. return $array;
  95. }
  96. $new = array();
  97. while (count($array) > 0)
  98. {
  99. $temp = array_splice($array, 0, $col_limit);
  100. if (count($temp) < $col_limit)
  101. {
  102. for ($i = count($temp); $i < $col_limit; $i++)
  103. {
  104. $temp[] = '&nbsp;';
  105. }
  106. }
  107. $new[] = $temp;
  108. }
  109. return $new;
  110. }
  111. // --------------------------------------------------------------------
  112. /**
  113. * Set "empty" cells
  114. *
  115. * Can be passed as an array or discreet params
  116. *
  117. * @access public
  118. * @param mixed
  119. * @return void
  120. */
  121. function set_empty($value)
  122. {
  123. $this->empty_cells = $value;
  124. }
  125. // --------------------------------------------------------------------
  126. /**
  127. * Add a table row
  128. *
  129. * Can be passed as an array or discreet params
  130. *
  131. * @access public
  132. * @param mixed
  133. * @return void
  134. */
  135. function add_row()
  136. {
  137. $args = func_get_args();
  138. $this->rows[] = $this->_prep_args($args);
  139. }
  140. // --------------------------------------------------------------------
  141. /**
  142. * Prep Args
  143. *
  144. * Ensures a standard associative array format for all cell data
  145. *
  146. * @access public
  147. * @param type
  148. * @return type
  149. */
  150. function _prep_args($args)
  151. {
  152. // If there is no $args[0], skip this and treat as an associative array
  153. // This can happen if there is only a single key, for example this is passed to table->generate
  154. // array(array('foo'=>'bar'))
  155. if (isset($args[0]) AND (count($args) == 1 && is_array($args[0])))
  156. {
  157. // args sent as indexed array
  158. if ( ! isset($args[0]['data']))
  159. {
  160. foreach ($args[0] as $key => $val)
  161. {
  162. if (is_array($val) && isset($val['data']))
  163. {
  164. $args[$key] = $val;
  165. }
  166. else
  167. {
  168. $args[$key] = array('data' => $val);
  169. }
  170. }
  171. }
  172. }
  173. else
  174. {
  175. foreach ($args as $key => $val)
  176. {
  177. if ( ! is_array($val))
  178. {
  179. $args[$key] = array('data' => $val);
  180. }
  181. }
  182. }
  183. return $args;
  184. }
  185. // --------------------------------------------------------------------
  186. /**
  187. * Add a table caption
  188. *
  189. * @access public
  190. * @param string
  191. * @return void
  192. */
  193. function set_caption($caption)
  194. {
  195. $this->caption = $caption;
  196. }
  197. // --------------------------------------------------------------------
  198. /**
  199. * Generate the table
  200. *
  201. * @access public
  202. * @param mixed
  203. * @return string
  204. */
  205. function generate($table_data = NULL)
  206. {
  207. // The table data can optionally be passed to this function
  208. // either as a database result object or an array
  209. if ( ! is_null($table_data))
  210. {
  211. if (is_object($table_data))
  212. {
  213. $this->_set_from_object($table_data);
  214. }
  215. elseif (is_array($table_data))
  216. {
  217. $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
  218. $this->_set_from_array($table_data, $set_heading);
  219. }
  220. }
  221. // Is there anything to display? No? Smite them!
  222. if (count($this->heading) == 0 AND count($this->rows) == 0)
  223. {
  224. return 'Undefined table data';
  225. }
  226. // Compile and validate the template date
  227. $this->_compile_template();
  228. // set a custom cell manipulation function to a locally scoped variable so its callable
  229. $function = $this->function;
  230. // Build the table!
  231. $out = $this->template['table_open'];
  232. $out .= $this->newline;
  233. // Add any caption here
  234. if ($this->caption)
  235. {
  236. $out .= $this->newline;
  237. $out .= '<caption>' . $this->caption . '</caption>';
  238. $out .= $this->newline;
  239. }
  240. // Is there a table heading to display?
  241. if (count($this->heading) > 0)
  242. {
  243. $out .= $this->template['thead_open'];
  244. $out .= $this->newline;
  245. $out .= $this->template['heading_row_start'];
  246. $out .= $this->newline;
  247. foreach ($this->heading as $heading)
  248. {
  249. $temp = $this->template['heading_cell_start'];
  250. foreach ($heading as $key => $val)
  251. {
  252. if ($key != 'data')
  253. {
  254. $temp = str_replace('<th', "<th $key='$val'", $temp);
  255. }
  256. }
  257. $out .= $temp;
  258. $out .= isset($heading['data']) ? $heading['data'] : '';
  259. $out .= $this->template['heading_cell_end'];
  260. }
  261. $out .= $this->template['heading_row_end'];
  262. $out .= $this->newline;
  263. $out .= $this->template['thead_close'];
  264. $out .= $this->newline;
  265. }
  266. // Build the table rows
  267. if (count($this->rows) > 0)
  268. {
  269. $out .= $this->template['tbody_open'];
  270. $out .= $this->newline;
  271. $i = 1;
  272. foreach ($this->rows as $row)
  273. {
  274. if ( ! is_array($row))
  275. {
  276. break;
  277. }
  278. // We use modulus to alternate the row colors
  279. $name = (fmod($i++, 2)) ? '' : 'alt_';
  280. $out .= $this->template['row_'.$name.'start'];
  281. $out .= $this->newline;
  282. foreach ($row as $cell)
  283. {
  284. $temp = $this->template['cell_'.$name.'start'];
  285. foreach ($cell as $key => $val)
  286. {
  287. if ($key != 'data')
  288. {
  289. $temp = str_replace('<td', "<td $key='$val'", $temp);
  290. }
  291. }
  292. $cell = isset($cell['data']) ? $cell['data'] : '';
  293. $out .= $temp;
  294. if ($cell === "" OR $cell === NULL)
  295. {
  296. $out .= $this->empty_cells;
  297. }
  298. else
  299. {
  300. if ($function !== FALSE && is_callable($function))
  301. {
  302. $out .= call_user_func($function, $cell);
  303. }
  304. else
  305. {
  306. $out .= $cell;
  307. }
  308. }
  309. $out .= $this->template['cell_'.$name.'end'];
  310. }
  311. $out .= $this->template['row_'.$name.'end'];
  312. $out .= $this->newline;
  313. }
  314. $out .= $this->template['tbody_close'];
  315. $out .= $this->newline;
  316. }
  317. $out .= $this->template['table_close'];
  318. // Clear table class properties before generating the table
  319. $this->clear();
  320. return $out;
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Clears the table arrays. Useful if multiple tables are being generated
  325. *
  326. * @access public
  327. * @return void
  328. */
  329. function clear()
  330. {
  331. $this->rows = array();
  332. $this->heading = array();
  333. $this->auto_heading = TRUE;
  334. }
  335. // --------------------------------------------------------------------
  336. /**
  337. * Set table data from a database result object
  338. *
  339. * @access public
  340. * @param object
  341. * @return void
  342. */
  343. function _set_from_object($query)
  344. {
  345. if ( ! is_object($query))
  346. {
  347. return FALSE;
  348. }
  349. // First generate the headings from the table column names
  350. if (count($this->heading) == 0)
  351. {
  352. if ( ! method_exists($query, 'list_fields'))
  353. {
  354. return FALSE;
  355. }
  356. $this->heading = $this->_prep_args($query->list_fields());
  357. }
  358. // Next blast through the result array and build out the rows
  359. if ($query->num_rows() > 0)
  360. {
  361. foreach ($query->result_array() as $row)
  362. {
  363. $this->rows[] = $this->_prep_args($row);
  364. }
  365. }
  366. }
  367. // --------------------------------------------------------------------
  368. /**
  369. * Set table data from an array
  370. *
  371. * @access public
  372. * @param array
  373. * @return void
  374. */
  375. function _set_from_array($data, $set_heading = TRUE)
  376. {
  377. if ( ! is_array($data) OR count($data) == 0)
  378. {
  379. return FALSE;
  380. }
  381. $i = 0;
  382. foreach ($data as $row)
  383. {
  384. // If a heading hasn't already been set we'll use the first row of the array as the heading
  385. if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
  386. {
  387. $this->heading = $this->_prep_args($row);
  388. }
  389. else
  390. {
  391. $this->rows[] = $this->_prep_args($row);
  392. }
  393. $i++;
  394. }
  395. }
  396. // --------------------------------------------------------------------
  397. /**
  398. * Compile Template
  399. *
  400. * @access private
  401. * @return void
  402. */
  403. function _compile_template()
  404. {
  405. if ($this->template == NULL)
  406. {
  407. $this->template = $this->_default_template();
  408. return;
  409. }
  410. $this->temp = $this->_default_template();
  411. 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)
  412. {
  413. if ( ! isset($this->template[$val]))
  414. {
  415. $this->template[$val] = $this->temp[$val];
  416. }
  417. }
  418. }
  419. // --------------------------------------------------------------------
  420. /**
  421. * Default Template
  422. *
  423. * @access private
  424. * @return void
  425. */
  426. function _default_template()
  427. {
  428. return array (
  429. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  430. 'thead_open' => '<thead>',
  431. 'thead_close' => '</thead>',
  432. 'heading_row_start' => '<tr>',
  433. 'heading_row_end' => '</tr>',
  434. 'heading_cell_start' => '<th>',
  435. 'heading_cell_end' => '</th>',
  436. 'tbody_open' => '<tbody>',
  437. 'tbody_close' => '</tbody>',
  438. 'row_start' => '<tr>',
  439. 'row_end' => '</tr>',
  440. 'cell_start' => '<td>',
  441. 'cell_end' => '</td>',
  442. 'row_alt_start' => '<tr>',
  443. 'row_alt_end' => '</tr>',
  444. 'cell_alt_start' => '<td>',
  445. 'cell_alt_end' => '</td>',
  446. 'table_close' => '</table>'
  447. );
  448. }
  449. }
  450. /* End of file Table.php */
  451. /* Location: ./system/libraries/Table.php */