/src/libraries/joomla/grid/grid.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 480 lines · 210 code · 56 blank · 214 comment · 20 complexity · 3202999b84f90775205c4012f81d11b5 MD5 · raw file

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