PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/0.7
PHP | 364 lines | 164 code | 50 blank | 150 comment | 13 complexity | 900affc8adad9b5550dd73b8b131b262 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013, 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 int $idx
  156. * @return sly_Table_Column
  157. */
  158. public function getColumn($idx) {
  159. return sly_setarraytype($this->columns, $idx, 'raw', null);
  160. }
  161. /**
  162. * @param boolean $enable
  163. * @param array $getParams override default detection of GET parameters by giving an array instead of null
  164. */
  165. public function enableSearching($enable = true, array $getParams = null) {
  166. $this->enableSearching = (bool) $enable;
  167. $this->searchParams = $getParams;
  168. }
  169. /**
  170. * @return boolean
  171. */
  172. public function isSorting() {
  173. return $this->enableSorting;
  174. }
  175. /**
  176. * @return boolean
  177. */
  178. public function isSearching() {
  179. return $this->enableSearching;
  180. }
  181. /**
  182. * @param string $default
  183. * @return string
  184. */
  185. public function getSortKey($default = null) {
  186. return sly_get($this->id.'_sortby', 'string', $default);
  187. }
  188. /**
  189. * @param string $default
  190. * @return string
  191. */
  192. public function getDirection($default = 'asc') {
  193. return sly_get($this->id.'_direction', 'string', $default);
  194. }
  195. /**
  196. * Gibt true zurück, wenn ein Pager angezeigt werden soll.
  197. *
  198. * @return boolean
  199. */
  200. public function hasPager() {
  201. return $this->totalElements !== null && $this->totalElements > self::$perPage;
  202. }
  203. /**
  204. * Gibt true zurück, wenn die Suchmaske angezeigt werden soll.
  205. *
  206. * @return boolean
  207. */
  208. public function hasSearch() {
  209. // Die Suchfunktion ist immer dann aktiviert, wenn sie im Objekt
  210. // aktiviert wurde (enableSearching) und wenn der Tabellenmodus
  211. // nicht auf dodraganddrop steht.
  212. return $this->enableSearching;
  213. }
  214. /**
  215. * Gibt true zurück, ob Drag&Drop aktiviert werden soll.
  216. *
  217. * @return boolean
  218. */
  219. public function hasDragAndDrop() {
  220. // D&D ist nur aktiv, wenn es explizit aktiviert wurde. Zusätzlich:
  221. // D&D ist immer dann aktiv, wenn die Tabelle sich im dodraganddrop-Modus
  222. // befindet oder wenn weder Pager noch Suchfunktion aktiv sind. Also:
  223. // D&D = ACTIVE & (dodraganddrop || (!PAGER && !SEARCH))
  224. return $this->enableDragAndDrop && (self::isDragAndDropMode() || (!$this->hasPager() && !$this->enableSearching));
  225. }
  226. /**
  227. * @param string $tableName
  228. * @param boolean $hasPager
  229. * @param boolean $hasDragAndDrop
  230. * @return array
  231. */
  232. public static function getPagingParameters($tableName = 'table', $hasPager = false, $hasDragAndDrop = false) {
  233. $perPage = self::$perPage;
  234. $page = sly_get('p_'.$tableName, 'int', 0);
  235. $start = $page * $perPage;
  236. $elements = $perPage;
  237. $end = $start + $perPage;
  238. $getAll = array('page' => 0, 'start' => 0, 'end' => PHP_INT_MAX, 'elements' => PHP_INT_MAX);
  239. if (!$hasPager || ($hasDragAndDrop && self::isDragAndDropMode())) {
  240. return $getAll;
  241. }
  242. return array('page' => $page, 'start' => $start, 'end' => $end, 'elements' => $elements);
  243. }
  244. /**
  245. * @param string $tableName
  246. * @return string
  247. */
  248. public static function getSearchParameters($tableName = 'table') {
  249. return sly_get('q_'.$tableName, 'string');
  250. }
  251. /**
  252. * @param string $tableName
  253. * @param string $defaultColumn
  254. * @param array $enabledColumns
  255. * @param string $defaultDirection
  256. * @return array
  257. */
  258. public static function getSortingParameters($tableName, $defaultColumn, $enabledColumns = array(), $defaultDirection = 'asc') {
  259. // support the old interface: get($defaultColumn, $enabledColumns)
  260. if (empty($enabledColumns) && is_array($defaultColumn)) {
  261. $enabledColumns = $defaultColumn;
  262. $defaultColumn = $tableName;
  263. $tableName = 'table';
  264. }
  265. $sortby = sly_get($tableName.'_sortby', 'string', $defaultColumn);
  266. $direction = strtolower(sly_get($tableName.'_direction', 'string', $defaultDirection)) === 'desc' ? 'DESC' : 'ASC';
  267. if (!in_array($sortby, $enabledColumns)) {
  268. $sortby = $defaultColumn;
  269. }
  270. return array('sortby' => $sortby, 'direction' => $direction);
  271. }
  272. /**
  273. * @throws sly_Exception
  274. * @param string $file
  275. * @return string
  276. */
  277. protected function getViewFile($file) {
  278. $full = SLY_COREFOLDER.'/views/table/'.$file;
  279. if (file_exists($full)) return $full;
  280. throw new sly_Exception(t('view_not_found', $file));
  281. }
  282. /**
  283. * Add a new class to the form
  284. *
  285. * This method will add a CSS class to the form tag. You can give mutliple
  286. * classes at once, the method will split them up and add them one at a time,
  287. * ensuring that they are unique.
  288. *
  289. * @param string $class the CSS class
  290. * @return array the list of current classes (unique)
  291. */
  292. public function addClass($class) {
  293. $class = explode(' ', $class);
  294. foreach ($class as $c) $this->classes[] = $c;
  295. $this->classes = array_unique($this->classes);
  296. return $this->classes;
  297. }
  298. /**
  299. * Remove all classes
  300. *
  301. * This method removes all set CSS classes for this form.
  302. */
  303. public function clearClasses() {
  304. $this->classes = array();
  305. }
  306. /**
  307. * Get all classes
  308. *
  309. * @return array the list of CSS classes for this form
  310. */
  311. public function getClasses() {
  312. return $this->classes;
  313. }
  314. }