PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/grid/grid.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 470 lines | 210 code | 43 blank | 217 comment | 20 complexity | 36049a10fabda17f75d2574655f2a935 MD5 | raw file
  1. <?php
  2. /**
  3. * JGrid class to dynamically generate HTML tables
  4. *
  5. * @package Joomla.Platform
  6. * @subpackage Grid
  7. *
  8. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  9. * @license GNU General Public License version 2 or later; see LICENSE
  10. */
  11. defined('JPATH_PLATFORM') or die;
  12. /**
  13. * JGrid class to dynamically generate HTML tables
  14. *
  15. * @package Joomla.Platform
  16. * @subpackage Grid
  17. * @since 11.3
  18. */
  19. class JGrid
  20. {
  21. /**
  22. * Array of columns
  23. * @var array
  24. * @since 11.3
  25. */
  26. protected $columns = array();
  27. /**
  28. * Current active row
  29. * @var int
  30. * @since 11.3
  31. */
  32. protected $activeRow = 0;
  33. /**
  34. * Rows of the table (including header and footer rows)
  35. * @var array
  36. * @since 11.3
  37. */
  38. protected $rows = array();
  39. /**
  40. * Header and Footer row-IDs
  41. * @var array
  42. * @since 11.3
  43. */
  44. protected $specialRows = array('header' => array(), 'footer' => array());
  45. /**
  46. * Associative array of attributes for the table-tag
  47. * @var array
  48. * @since 11.3
  49. */
  50. protected $options;
  51. /**
  52. * Constructor for a JGrid object
  53. *
  54. * @param array $options Associative array of attributes for the table-tag
  55. *
  56. * @since 11.3
  57. */
  58. public function __construct($options = array())
  59. {
  60. $this->setTableOptions($options, true);
  61. }
  62. /**
  63. * Magic function to render this object as a table.
  64. *
  65. * @return string
  66. *
  67. * @since 11.3
  68. */
  69. public function __toString()
  70. {
  71. return $this->toString();
  72. }
  73. /**
  74. * Method to set the attributes for a table-tag
  75. *
  76. * @param array $options Associative array of attributes for the table-tag
  77. * @param bool $replace Replace possibly existing attributes
  78. *
  79. * @return JGrid This object for chaining
  80. *
  81. * @since 11.3
  82. */
  83. public function setTableOptions($options = array(), $replace = false)
  84. {
  85. if ($replace)
  86. {
  87. $this->options = $options;
  88. }
  89. else
  90. {
  91. $this->options = array_merge($this->options, $options);
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Get the Attributes of the current table
  97. *
  98. * @return array Associative array of attributes
  99. *
  100. * @since 11.3
  101. */
  102. public function getTableOptions()
  103. {
  104. return $this->options;
  105. }
  106. /**
  107. * Add new column name to process
  108. *
  109. * @param string $name Internal column name
  110. *
  111. * @return JGrid This object for chaining
  112. *
  113. * @since 11.3
  114. */
  115. public function addColumn($name)
  116. {
  117. $this->columns[] = $name;
  118. return $this;
  119. }
  120. /**
  121. * Returns the list of internal columns
  122. *
  123. * @return array List of internal columns
  124. *
  125. * @since 11.3
  126. */
  127. public function getColumns()
  128. {
  129. return $this->columns;
  130. }
  131. /**
  132. * Delete column by name
  133. *
  134. * @param string $name Name of the column to be deleted
  135. *
  136. * @return JGrid This object for chaining
  137. *
  138. * @since 11.3
  139. */
  140. public function deleteColumn($name)
  141. {
  142. $index = array_search($name, $this->columns);
  143. if ($index !== false)
  144. {
  145. unset($this->columns[$index]);
  146. $this->columns = array_values($this->columns);
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Method to set a whole range of columns at once
  152. * This can be used to re-order the columns, too
  153. *
  154. * @param array $columns List of internal column names
  155. *
  156. * @return JGrid This object for chaining
  157. *
  158. * @since 11.3
  159. */
  160. public function setColumns($columns)
  161. {
  162. $this->columns = array_values($columns);
  163. return $this;
  164. }
  165. /**
  166. * Adds a row to the table and sets the currently
  167. * active row to the new row
  168. *
  169. * @param array $options Associative array of attributes for the row
  170. * @param int $special 1 for a new row in the header, 2 for a new row in the footer
  171. *
  172. * @return JGrid This object for chaining
  173. *
  174. * @since 11.3
  175. */
  176. public function addRow($options = array(), $special = false)
  177. {
  178. $this->rows[]['_row'] = $options;
  179. $this->activeRow = count($this->rows) - 1;
  180. if ($special)
  181. {
  182. if ($special === 1)
  183. {
  184. $this->specialRows['header'][] = $this->activeRow;
  185. }
  186. else
  187. {
  188. $this->specialRows['footer'][] = $this->activeRow;
  189. }
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Method to get the attributes of the currently active row
  195. *
  196. * @return array Associative array of attributes
  197. *
  198. * @since 11.3
  199. */
  200. public function getRowOptions()
  201. {
  202. return $this->rows[$this->activeRow]['_row'];
  203. }
  204. /**
  205. * Method to set the attributes of the currently active row
  206. *
  207. * @param array $options Associative array of attributes
  208. *
  209. * @return JGrid This object for chaining
  210. *
  211. * @since 11.3
  212. */
  213. public function setRowOptions($options)
  214. {
  215. $this->rows[$this->activeRow]['_row'] = $options;
  216. return $this;
  217. }
  218. /**
  219. * Get the currently active row ID
  220. *
  221. * @return int ID of the currently active row
  222. *
  223. * @since 11.3
  224. */
  225. public function getActiveRow()
  226. {
  227. return $this->activeRow;
  228. }
  229. /**
  230. * Set the currently active row
  231. *
  232. * @param int $id ID of the row to be set to current
  233. *
  234. * @return JGrid This object for chaining
  235. *
  236. * @since 11.3
  237. */
  238. public function setActiveRow($id)
  239. {
  240. $this->activeRow = (int) $id;
  241. return $this;
  242. }
  243. /**
  244. * Set cell content for a specific column for the
  245. * currently active row
  246. *
  247. * @param string $name Name of the column
  248. * @param string $content Content for the cell
  249. * @param array $option Associative array of attributes for the td-element
  250. * @param bool $replace If false, the content is appended to the current content of the cell
  251. *
  252. * @return JGrid This object for chaining
  253. *
  254. * @since 11.3
  255. */
  256. public function setRowCell($name, $content, $option = array(), $replace = true)
  257. {
  258. if ($replace || !isset($this->rows[$this->activeRow][$name]))
  259. {
  260. $cell = new stdClass;
  261. $cell->options = $option;
  262. $cell->content = $content;
  263. $this->rows[$this->activeRow][$name] = $cell;
  264. }
  265. else
  266. {
  267. $this->rows[$this->activeRow][$name]->content .= $content;
  268. $this->rows[$this->activeRow][$name]->options = $option;
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Get all data for a row
  274. *
  275. * @param int $id ID of the row to return
  276. *
  277. * @return array Array of columns of a table row
  278. *
  279. * @since 11.3
  280. */
  281. public function getRow($id = false)
  282. {
  283. if ($id === false)
  284. {
  285. $id = $this->activeRow;
  286. }
  287. if (isset($this->rows[(int) $id]))
  288. {
  289. return $this->rows[(int) $id];
  290. }
  291. else
  292. {
  293. return false;
  294. }
  295. }
  296. /**
  297. * Get the IDs of all rows in the table
  298. *
  299. * @param int $special false for the standard rows, 1 for the header rows, 2 for the footer rows
  300. *
  301. * @return array Array of IDs
  302. *
  303. * @since 11.3
  304. */
  305. public function getRows($special = false)
  306. {
  307. if ($special)
  308. {
  309. if ($special === 1)
  310. {
  311. return $this->specialRows['header'];
  312. }
  313. else
  314. {
  315. return $this->specialRows['footer'];
  316. }
  317. }
  318. return array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
  319. }
  320. /**
  321. * Delete a row from the object
  322. *
  323. * @param int $id ID of the row to be deleted
  324. *
  325. * @return JGrid This object for chaining
  326. *
  327. * @since 11.3
  328. */
  329. public function deleteRow($id)
  330. {
  331. unset($this->rows[$id]);
  332. if (in_array($id, $this->specialRows['header']))
  333. {
  334. unset($this->specialRows['header'][array_search($id, $this->specialRows['header'])]);
  335. }
  336. if (in_array($id, $this->specialRows['footer']))
  337. {
  338. unset($this->specialRows['footer'][array_search($id, $this->specialRows['footer'])]);
  339. }
  340. if ($this->activeRow == $id)
  341. {
  342. end($this->rows);
  343. $this->activeRow = key($this->rows);
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Render the HTML table
  349. *
  350. * @return string The rendered HTML table
  351. *
  352. * @since 11.3
  353. */
  354. public function toString()
  355. {
  356. $output = array();
  357. $output[] = '<table' . $this->renderAttributes($this->getTableOptions()) . '>';
  358. if (count($this->specialRows['header']))
  359. {
  360. $output[] = $this->renderArea($this->specialRows['header'], 'thead', 'th');
  361. }
  362. if (count($this->specialRows['footer']))
  363. {
  364. $output[] = $this->renderArea($this->specialRows['footer'], 'tfoot');
  365. }
  366. $ids = array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
  367. if (count($ids))
  368. {
  369. $output[] = $this->renderArea($ids);
  370. }
  371. $output[] = '</table>';
  372. return implode('', $output);
  373. }
  374. /**
  375. * Render an area of the table
  376. *
  377. * @param array $ids IDs of the rows to render
  378. * @param string $area Name of the area to render. Valid: tbody, tfoot, thead
  379. * @param string $cell Name of the cell to render. Valid: td, th
  380. *
  381. * @return string The rendered table area
  382. *
  383. * @since 11.3
  384. */
  385. protected function renderArea($ids, $area = 'tbody', $cell = 'td')
  386. {
  387. $output = array();
  388. $output[] = '<' . $area . ">\n";
  389. foreach ($ids as $id)
  390. {
  391. $output[] = "\t<tr" . $this->renderAttributes($this->rows[$id]['_row']) . ">\n";
  392. foreach ($this->getColumns() as $name)
  393. {
  394. if (isset($this->rows[$id][$name]))
  395. {
  396. $column = $this->rows[$id][$name];
  397. $output[] = "\t\t<" . $cell . $this->renderAttributes($column->options) . '>' . $column->content . '</' . $cell . ">\n";
  398. }
  399. }
  400. $output[] = "\t</tr>\n";
  401. }
  402. $output[] = '</' . $area . '>';
  403. return implode('', $output);
  404. }
  405. /**
  406. * Renders an HTML attribute from an associative array
  407. *
  408. * @param array $attributes Associative array of attributes
  409. *
  410. * @return string The HTML attribute string
  411. *
  412. * @since 11.3
  413. */
  414. protected function renderAttributes($attributes)
  415. {
  416. if (count((array) $attributes) == 0)
  417. {
  418. return '';
  419. }
  420. $return = array();
  421. foreach ($attributes as $key => $option)
  422. {
  423. $return[] = $key . '="' . $option . '"';
  424. }
  425. return ' ' . implode(' ', $return);
  426. }
  427. }