PageRenderTime 48ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/sally/core/lib/sly/Table.php

https://bitbucket.org/mediastuttgart/sallycms-0.6
PHP | 356 lines | 161 code | 49 blank | 146 comment | 13 complexity | dcdf3b54efae1059eef939122c386156 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /**
  11. * @ingroup table
  12. */
  13. class sly_Table extends sly_Viewable {
  14. protected $id; ///< string
  15. protected $columns; ///< array
  16. protected $classes; ///< array
  17. protected $isEmpty; ///< boolean
  18. protected $emptyNotice; ///< string
  19. protected $enableSorting; ///< boolean
  20. protected $enableSearching; ///< boolean
  21. protected $enableDragAndDrop; ///< boolean
  22. protected $dragAndDropHandler; ///< boolean
  23. protected $totalElements; ///< int
  24. protected $caption; ///< string
  25. protected $searchParams; ///< array
  26. private $content; ///< string
  27. protected static $perPage = 30; ///< int
  28. /**
  29. * @param string $id
  30. * @param array $classes list of additional CSS classes for the table tag
  31. */
  32. public function __construct($id = null, array $classes = array()) {
  33. // Das 'a'-Präfix verhindert, dass die ID mit einer Zahl beginnt, was in HTML nicht erlaubt ist.
  34. $this->id = $id === null ? 'a'.substr(md5(uniqid()), 0, 10) : $id;
  35. $this->columns = array();
  36. $this->classes = array('sly-table');
  37. $this->isEmpty = false;
  38. $this->emptyNotice = t('table_is_empty');
  39. $this->enableSorting = false;
  40. $this->enableSearching = false;
  41. $this->enableDragAndDrop = false;
  42. $this->dragAndDropHandler = '';
  43. $this->totalElements = null;
  44. $this->caption = null;
  45. $this->searchParams = null;
  46. foreach ($classes as $className) $this->addClass($className);
  47. }
  48. /**
  49. * @param string $id
  50. */
  51. public function setID($id) {
  52. $this->id = trim($id);
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function getID() {
  58. return $this->id;
  59. }
  60. /**
  61. * @return boolean
  62. */
  63. public static function isDragAndDropMode() {
  64. return sly_get('tableswitch', 'string') == 'dodraganddrop';
  65. }
  66. /**
  67. * @param int $perPage
  68. */
  69. public static function setElementsPerPageStatic($perPage) {
  70. self::$perPage = abs((int) $perPage);
  71. if (self::$perPage < 1) {
  72. self::$perPage = 1;
  73. }
  74. }
  75. /**
  76. * @param int $perPage
  77. */
  78. public function setElementsPerPage($perPage = 20) {
  79. self::setElementsPerPageStatic($perPage);
  80. }
  81. /**
  82. * @param int $totalElements leave this to null to disable the pager
  83. * @return string the rendered header
  84. */
  85. protected function renderHeader($totalElements = null) {
  86. $this->totalElements = $totalElements;
  87. return $this->renderView('header.phtml', compact('totalElements'));
  88. }
  89. /**
  90. * @return string the rendered footer
  91. */
  92. protected function renderFooter() {
  93. return $this->renderView('footer.phtml');
  94. }
  95. public function openBuffer() {
  96. ob_start();
  97. }
  98. public function closeBuffer() {
  99. $this->content = ob_get_clean();
  100. }
  101. /**
  102. * @param int $totalElements leave this to null to disable the pager
  103. * @return string the rendered content
  104. */
  105. public function render($totalElements = null) {
  106. ob_start();
  107. print $this->renderHeader($totalElements);
  108. print $this->content;
  109. print $this->renderFooter();
  110. return ob_get_clean();
  111. }
  112. /**
  113. * @param boolean $isEmpty
  114. */
  115. public function setIsEmpty($isEmpty) {
  116. $this->isEmpty = (bool) $isEmpty;
  117. }
  118. /**
  119. * @param string $caption
  120. */
  121. public function setCaption($caption) {
  122. $this->caption = $caption;
  123. }
  124. /**
  125. * @param string $notice
  126. */
  127. public function setEmptyNotice($notice) {
  128. $this->emptyNotice = $notice;
  129. }
  130. /**
  131. * @param boolean $enable
  132. */
  133. public function enableSorting($enable = true) {
  134. $this->enableSorting = (bool) $enable;
  135. }
  136. /**
  137. * @param boolean $enable
  138. */
  139. public function enableDragAndDrop($enable = true) {
  140. $this->enableDragAndDrop = (bool) $enable;
  141. }
  142. /**
  143. * @param string $function JS callable
  144. */
  145. public function setDragAndDropHandler($function) {
  146. $this->dragAndDropHandler = $function;
  147. }
  148. /**
  149. * @param sly_Table_Column $col
  150. */
  151. public function addColumn(sly_Table_Column $col) {
  152. $this->columns[] = $col;
  153. }
  154. /**
  155. * @param boolean $enable
  156. * @param array $getParams override default detection of GET parameters by giving an array instead of null
  157. */
  158. public function enableSearching($enable = true, array $getParams = null) {
  159. $this->enableSearching = (bool) $enable;
  160. $this->searchParams = $getParams;
  161. }
  162. /**
  163. * @return boolean
  164. */
  165. public function isSorting() {
  166. return $this->enableSorting;
  167. }
  168. /**
  169. * @return boolean
  170. */
  171. public function isSearching() {
  172. return $this->enableSearching;
  173. }
  174. /**
  175. * @param string $default
  176. * @return string
  177. */
  178. public function getSortKey($default = null) {
  179. return sly_get($this->id.'_sortby', 'string', $default);
  180. }
  181. /**
  182. * @param string $default
  183. * @return string
  184. */
  185. public function getDirection($default = 'asc') {
  186. return sly_get($this->id.'_direction', 'string', $default);
  187. }
  188. /**
  189. * Gibt true zurück, wenn ein Pager angezeigt werden soll.
  190. *
  191. * @return boolean
  192. */
  193. public function hasPager() {
  194. return $this->totalElements !== null && $this->totalElements > self::$perPage;
  195. }
  196. /**
  197. * Gibt true zurück, wenn die Suchmaske angezeigt werden soll.
  198. *
  199. * @return boolean
  200. */
  201. public function hasSearch() {
  202. // Die Suchfunktion ist immer dann aktiviert, wenn sie im Objekt
  203. // aktiviert wurde (enableSearching) und wenn der Tabellenmodus
  204. // nicht auf dodraganddrop steht.
  205. return $this->enableSearching;
  206. }
  207. /**
  208. * Gibt true zurück, ob Drag&Drop aktiviert werden soll.
  209. *
  210. * @return boolean
  211. */
  212. public function hasDragAndDrop() {
  213. // D&D ist nur aktiv, wenn es explizit aktiviert wurde. Zusätzlich:
  214. // D&D ist immer dann aktiv, wenn die Tabelle sich im dodraganddrop-Modus
  215. // befindet oder wenn weder Pager noch Suchfunktion aktiv sind. Also:
  216. // D&D = ACTIVE & (dodraganddrop || (!PAGER && !SEARCH))
  217. return $this->enableDragAndDrop && (self::isDragAndDropMode() || (!$this->hasPager() && !$this->enableSearching));
  218. }
  219. /**
  220. * @param string $tableName
  221. * @param boolean $hasPager
  222. * @param boolean $hasDragAndDrop
  223. * @return array
  224. */
  225. public static function getPagingParameters($tableName = 'table', $hasPager = false, $hasDragAndDrop = false) {
  226. $perPage = self::$perPage;
  227. $page = sly_get('p_'.$tableName, 'int', 0);
  228. $start = $page * $perPage;
  229. $elements = $perPage;
  230. $end = $start + $perPage;
  231. $getAll = array('page' => 0, 'start' => 0, 'end' => PHP_INT_MAX, 'elements' => PHP_INT_MAX);
  232. if (!$hasPager || ($hasDragAndDrop && self::isDragAndDropMode())) {
  233. return $getAll;
  234. }
  235. return array('page' => $page, 'start' => $start, 'end' => $end, 'elements' => $elements);
  236. }
  237. /**
  238. * @param string $tableName
  239. * @return string
  240. */
  241. public static function getSearchParameters($tableName = 'table') {
  242. return sly_get('q_'.$tableName, 'string');
  243. }
  244. /**
  245. * @param string $tableName
  246. * @param string $defaultColumn
  247. * @param array $enabledColumns
  248. * @param string $defaultDirection
  249. * @return array
  250. */
  251. public static function getSortingParameters($tableName, $defaultColumn, $enabledColumns = array(), $defaultDirection = 'asc') {
  252. // support the old interface: get($defaultColumn, $enabledColumns)
  253. if (empty($enabledColumns) && is_array($defaultColumn)) {
  254. $enabledColumns = $defaultColumn;
  255. $defaultColumn = $tableName;
  256. $tableName = 'table';
  257. }
  258. $sortby = sly_get($tableName.'_sortby', 'string', $defaultColumn);
  259. $direction = strtolower(sly_get($tableName.'_direction', 'string', $defaultDirection)) === 'desc' ? 'DESC' : 'ASC';
  260. if (!in_array($sortby, $enabledColumns)) {
  261. $sortby = $defaultColumn;
  262. }
  263. return array('sortby' => $sortby, 'direction' => $direction);
  264. }
  265. /**
  266. * @throws sly_Exception
  267. * @param string $file
  268. * @return string
  269. */
  270. protected function getViewFile($file) {
  271. $full = SLY_COREFOLDER.'/views/table/'.$file;
  272. if (file_exists($full)) return $full;
  273. throw new sly_Exception(t('view_not_found', $file));
  274. }
  275. /**
  276. * Add a new class to the form
  277. *
  278. * This method will add a CSS class to the form tag. You can give mutliple
  279. * classes at once, the method will split them up and add them one at a time,
  280. * ensuring that they are unique.
  281. *
  282. * @param string $class the CSS class
  283. * @return array the list of current classes (unique)
  284. */
  285. public function addClass($class) {
  286. $class = explode(' ', $class);
  287. foreach ($class as $c) $this->classes[] = $c;
  288. $this->classes = array_unique($this->classes);
  289. return $this->classes;
  290. }
  291. /**
  292. * Remove all classes
  293. *
  294. * This method removes all set CSS classes for this form.
  295. */
  296. public function clearClasses() {
  297. $this->classes = array();
  298. }
  299. /**
  300. * Get all classes
  301. *
  302. * @return array the list of CSS classes for this form
  303. */
  304. public function getClasses() {
  305. return $this->classes;
  306. }
  307. }