com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_tables.php

Documentation is available at lib_tables.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains everything that is connected to table elements.
  9. * @author Andreas Launila
  10. * @version $Revision: 1.7 $
  11. * @package com.lokorin.lokorin.lib
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14.  
  15. /**
  16. * For access to common constants and functions.
  17. */
  18. require_once('common.php');
  19.  
  20. CssHandler::getInstance()->includeCss('tables');
  21. /**
  22. * Describes a Table.
  23. * @author Andreas Launila
  24. * @package com.lokorin.lokorin.lib
  25. */
  26. class Table {
  27. /**
  28. * Contains the header (if any) for the Table.
  29. * @var Header
  30. */
  31. private $tableHeader;
  32. /**
  33. * Contains an array which maps ints onto other ints. The mapping can
  34. * rearange the order in which the row entries are displayed.
  35. * @var access
  36. */
  37. private $entryMap;
  38. /**
  39. * Contains an array which contains an array for each row. The key of the
  40. * rows should be numeric, the value should be the value that should be
  41. * displayed.
  42. * @var array
  43. */
  44. private $rows;
  45. /**
  46. * The CSS class to use for the header when displaying.
  47. * @var string
  48. */
  49. private $cssClass;
  50. /**
  51. * Constructor for Table.
  52. */
  53. public function __construct() {
  54. $this->tableHeader = null;
  55. $this->rows = array();
  56. $this->entryMap = '';
  57. }
  58. /**
  59. * Sets a specified header as the new header that should be used by the
  60. * table.
  61. * @param Header $newHeader The new header that should be used for the
  62. * table.
  63. */
  64. public function setHeader(Header $newHeader) {
  65. $this->tableHeader = $newHeader;
  66. }
  67. /**
  68. * Gets the header of the table.
  69. * @return Header The Header instance for the table.
  70. */
  71. public function getHeader() {
  72. return $this->tableHeader;
  73. }
  74. /**
  75. * Adds a new row to the table.
  76. * @param array $row The row that should be added. The values of the array
  77. * is the ones that should be added.
  78. */
  79. public function addRow($row) {
  80. if(!is_array($row)) {
  81. return;
  82. }
  83. $this->rows[] = $row;
  84. }
  85. /**
  86. * Gets the table in HTML format.
  87. * @return string The describes table in HTML format.
  88. */
  89. public function getTable() {
  90. if($this->cssClass != '') {
  91. $class = ' class="'.$this->cssClass.'"';
  92. } else {
  93. $class = '';
  94. }
  95. $output = '<table'.$class.'>';
  96. if(isset($this->tableHeader)) {
  97. $header = $this->tableHeader;
  98. $output .= $header->getAsHTML()."\n";
  99. $this->rows = $header->performSort($this->rows);
  100. }
  101. if(is_array($this->entryMap)) {
  102. $arr = array_flip($this->entryMap);
  103. $rows = array();
  104. foreach($this->rows as $k => $row) {
  105. if(isset($arr[$k])) {
  106. $rows[$arr[$k]] = $row;
  107. } else {
  108. $rows[$k] = $row;
  109. }
  110. }
  111. } else {
  112. $rows = $this->rows;
  113. }
  114. foreach($rows as $k => $row) {
  115. if($k % 2 == 1) {
  116. //So that one can alternate colors
  117. $output .= '<tr class="odd">';
  118. } else {
  119. $output .= '<tr>';
  120. }
  121. foreach($row as $value) {
  122. $output .= '<td>'.$value.'</td>'."\n";
  123. }
  124. $output .= '</tr>';
  125. }
  126. $output .= '</table>';
  127. return $output;
  128. }
  129. /**
  130. * Sets a row mapping for the table to use. The row map rearranges the
  131. * values of a row into a different order.
  132. * @param array $map The map should define value for all keys corresponding
  133. * to the columns that it want changed in each row. The value should
  134. * be the column to which the value should be moved. For instance the
  135. * array (1 => 4, 4 => 1) will rearrange the values so that the second
  136. * columns' values are moved to the fifth column and vice versa.
  137. * Observe that the first column has index 0 and so on.
  138. */
  139. public function setRowMapping($map) {
  140. $this->entryMap = $map;
  141. }
  142. /**
  143. * Sets the CSS class that the table should use.
  144. * @param string $class The name of the CSS class to use.
  145. */
  146. public function setCssClass($class) {
  147. $this->cssClass = $class;
  148. }
  149. }
  150.  
  151. /**
  152. * Descibes an Header.
  153. * @author Andreas Launila
  154. * @package com.lokorin.lokorin.lib
  155. */
  156. class Header {
  157. /**
  158. * Contains the captions for the header.
  159. * @var array
  160. */
  161. private $captions;
  162. /**
  163. * Constructor for Header.
  164. * @param array $caption An optional param to define the captions for the
  165. * header. All captions have to be unique.
  166. */
  167. public function __construct($captions = array()) {
  168. $this->captions = array();
  169. if(is_array($captions)) {
  170. foreach($captions as $caption) {
  171. $this->add($caption);
  172. }
  173. }
  174. }
  175. /**
  176. * Adds an new caption to the header.
  177. * @param string $caption The new caption to add. This caption has to be
  178. * unique, it may not already exist in the table's captions.
  179. */
  180. public function add($caption) {
  181. if(!in_array($caption, $this->captions)) {
  182. $this->captions[] = $caption;
  183. }
  184. }
  185. /**
  186. * Removes a caption from the header, assuming that the caption exists.
  187. * @param string $caption The caption to attempt to remove.
  188. */
  189. public function remove($caption) {
  190. if(in_array($caption, $this->captions)) {
  191. unset($this->captions[$caption]);
  192. }
  193. }
  194. /**
  195. * Gets the header as HTML, including the row tags.
  196. * I.e. &lt;tr&gt;&lt;th&gt;...&lt;/th&gt;...&lt;/tr&gt;
  197. * @return string An table header in HTML format.
  198. */
  199. public function getAsHtml() {
  200. doInclude('lib_util');
  201. $html = '<tr>';
  202. foreach($this->captions as $caption) {
  203. if(isset($_GET['tsort']) && ($_GET['tsort'] == 'a'.$caption)) {
  204. $params = addQueryParams(array('tsort' => 'd'.$caption));
  205. } else {
  206. $params = addQueryParams(array('tsort' => 'a'.$caption));
  207. }
  208. $html .= '<th><a href="'.htmlspecialchars(SELF.'?'.$params).'">'.
  209. $caption.'</a></th>';
  210. }
  211. return $html.'</tr>';
  212. }
  213. /**
  214. * Performs a sort, if the user has requested one, on the specified rows.
  215. * @param array $rows The rows that should be sorted. The rows should have
  216. * as many columns as the header has captions.
  217. * @return The rows, sorted in the specified way.
  218. */
  219. public function performSort($rows) {
  220. if(isset($_GET['tsort'])) {
  221. /*
  222. * The parameter is composed of two parts, the first character is
  223. * either 'a' or 'd' (ascending or descending). The rest of the
  224. * parameter is the caption for the column that should be sorted.
  225. */
  226. if($_GET['tsort']{0} == 'a') {
  227. $order = SORT_ASC;
  228. } else if($_GET['tsort']{0} == 'd') {
  229. $order = SORT_DESC;
  230. } else {
  231. //Invalid order type specified.
  232. return $rows;
  233. }
  234. $captionToUse = substr($_GET['tsort'], 1);
  235. } else {
  236. //The user has not requested any sorting.
  237. return $rows;
  238. }
  239. $columnIndex = -1;
  240. foreach($this->captions as $key => $caption) {
  241. if($caption == $captionToUse) {
  242. $columnIndex = $key;
  243. break;
  244. }
  245. }
  246. if($columnIndex == -1) {
  247. //The specified column does not exist.
  248. return $rows;
  249. }
  250. //Structure the rows as columns. Take the opportunity to see if the
  251. //sort column is numeric. If it is then we will want to sort it as one.
  252. $numeric = true;
  253. $sortColumn = array();
  254. foreach($rows as $row) {
  255. foreach($row as $column => $value) {
  256. if($column != $columnIndex) {
  257. continue;
  258. }
  259. if($numeric &&
  260. !$this->isConvertableToNumerical(strip_tags($value))) {
  261. //The whole column is not numeric.
  262. $numeric = false;
  263. }
  264. $sortColumn[] = $value;
  265. }
  266. }
  267. if($numeric) {
  268. //Convert the sort column to its numeric form.
  269. foreach($sortColumn as $key => $value) {
  270. $sortColumn[$key] = $this->convertToNumerical(
  271. strip_tags($value));
  272. }
  273. }
  274. if($numeric) {
  275. array_multisort($rows, SORT_NUMERIC, $sortColumn);
  276. } else {
  277. array_multisort($rows, SORT_STRING, $sortColumn);
  278. }
  279. if($order == SORT_DESC) {
  280. //Because I can't get SORT_DESC along with SORT_STRING in
  281. //array_multisort to work.
  282. $rows = array_reverse($rows);
  283. }
  284. return $rows;
  285. }
  286. /**
  287. * Checks if a value could be converted to a numerical value. I.e. if it
  288. * is a numerical value or possible a formatted numerical value. E.g.
  289. * '+3,000.50' counts as a convertable value, but '+31 DKP' does not.
  290. * @param mixed $value The value that should be checked.
  291. * @return boolean True if the value can be converted to a valid numerical
  292. * value (one that will make is_numeric true).
  293. */
  294. private function isConvertableToNumerical($value) {
  295. return preg_match('/^(-|\+)?\d[\.,\d]*$/', $value);
  296. }
  297. /**
  298. * Converts the value to the corresponding numerical one. The conversion
  299. * will only be reliable if the value return true with
  300. * isConvertableToNumerical().
  301. * @param mixed $value The value that should be converted.
  302. * @return integer The value in a numerical form.
  303. */
  304. private function convertToNumerical($value) {
  305. return preg_replace('/[^\d-]/', '', $value);
  306. }
  307. }
  308.  
  309. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:57 +0200 by phpDocumentor 1.3.0RC4