PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/trunk
PHP | 379 lines | 171 code | 50 blank | 158 comment | 12 complexity | fcfb0bc04743ab3b1f5008bb050d020d MD5 | raw file
  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. * @param sly_Request $request the request to use or null for the global one
  62. * @return boolean
  63. */
  64. public static function isDragAndDropMode(sly_Request $request = null) {
  65. $request = $request ? $request : sly_Core::getRequest();
  66. return $request->get('tableswitch', 'string') === 'dodraganddrop';
  67. }
  68. /**
  69. * @param int $perPage
  70. */
  71. public static function setElementsPerPageStatic($perPage) {
  72. self::$perPage = abs((int) $perPage);
  73. if (self::$perPage < 1) {
  74. self::$perPage = 1;
  75. }
  76. }
  77. /**
  78. * @param int $perPage
  79. */
  80. public function setElementsPerPage($perPage = 20) {
  81. self::setElementsPerPageStatic($perPage);
  82. }
  83. /**
  84. * @param int $totalElements leave this to null to disable the pager
  85. * @param sly_Request $request the request to use
  86. * @return string the rendered header
  87. */
  88. protected function renderHeader($totalElements = null, sly_Request $request) {
  89. $this->totalElements = $totalElements;
  90. return $this->renderView('header.phtml', compact('totalElements', 'request'));
  91. }
  92. /**
  93. * @return string the rendered footer
  94. */
  95. protected function renderFooter() {
  96. return $this->renderView('footer.phtml');
  97. }
  98. public function openBuffer() {
  99. ob_start();
  100. }
  101. public function closeBuffer() {
  102. $this->content = ob_get_clean();
  103. }
  104. /**
  105. * @param int $totalElements leave this to null to disable the pager
  106. * @param sly_Request $request the request to use or null for the global one
  107. * @return string the rendered content
  108. */
  109. public function render($totalElements = null, sly_Request $request = null) {
  110. ob_start();
  111. $request = $request ? $request : sly_Core::getRequest();
  112. print $this->renderHeader($totalElements, $request);
  113. print $this->content;
  114. print $this->renderFooter();
  115. return ob_get_clean();
  116. }
  117. /**
  118. * @param boolean $isEmpty
  119. */
  120. public function setIsEmpty($isEmpty) {
  121. $this->isEmpty = (bool) $isEmpty;
  122. }
  123. /**
  124. * @param string $caption
  125. */
  126. public function setCaption($caption) {
  127. $this->caption = $caption;
  128. }
  129. /**
  130. * @param string $notice
  131. */
  132. public function setEmptyNotice($notice) {
  133. $this->emptyNotice = $notice;
  134. }
  135. /**
  136. * @param boolean $enable
  137. */
  138. public function enableSorting($enable = true) {
  139. $this->enableSorting = (bool) $enable;
  140. }
  141. /**
  142. * @param boolean $enable
  143. */
  144. public function enableDragAndDrop($enable = true) {
  145. $this->enableDragAndDrop = (bool) $enable;
  146. }
  147. /**
  148. * @param string $function JS callable
  149. */
  150. public function setDragAndDropHandler($function) {
  151. $this->dragAndDropHandler = $function;
  152. }
  153. /**
  154. * @param sly_Table_Column $col
  155. */
  156. public function addColumn(sly_Table_Column $col) {
  157. $this->columns[] = $col;
  158. }
  159. /**
  160. * @param int $idx
  161. * @return sly_Table_Column
  162. */
  163. public function getColumn($idx) {
  164. return sly_setarraytype($this->columns, $idx, 'raw', null);
  165. }
  166. /**
  167. * @param boolean $enable
  168. * @param array $getParams override default detection of GET parameters by giving an array instead of null
  169. */
  170. public function enableSearching($enable = true, array $getParams = null) {
  171. $this->enableSearching = (bool) $enable;
  172. $this->searchParams = $getParams;
  173. }
  174. /**
  175. * @return boolean
  176. */
  177. public function isSorting() {
  178. return $this->enableSorting;
  179. }
  180. /**
  181. * @return boolean
  182. */
  183. public function isSearching() {
  184. return $this->enableSearching;
  185. }
  186. /**
  187. * @param string $default
  188. * @param sly_Request $request the request to use or null for the global one
  189. * @return string
  190. */
  191. public function getSortKey($default = null, sly_Request $request = null) {
  192. $request = $request ? $request : sly_Core::getRequest();
  193. return $request->get($this->id.'_sortby', 'string', $default);
  194. }
  195. /**
  196. * @param string $default
  197. * @param sly_Request $request the request to use or null for the global one
  198. * @return string
  199. */
  200. public function getDirection($default = 'asc', sly_Request $request = null) {
  201. $request = $request ? $request : sly_Core::getRequest();
  202. return $request->get($this->id.'_direction', 'string', $default);
  203. }
  204. /**
  205. * Gibt true zurück, wenn ein Pager angezeigt werden soll.
  206. *
  207. * @return boolean
  208. */
  209. public function hasPager() {
  210. return $this->totalElements !== null && $this->totalElements > self::$perPage;
  211. }
  212. /**
  213. * Gibt true zurück, wenn die Suchmaske angezeigt werden soll.
  214. *
  215. * @return boolean
  216. */
  217. public function hasSearch() {
  218. // Die Suchfunktion ist immer dann aktiviert, wenn sie im Objekt
  219. // aktiviert wurde (enableSearching) und wenn der Tabellenmodus
  220. // nicht auf dodraganddrop steht.
  221. return $this->enableSearching;
  222. }
  223. /**
  224. * Gibt true zurück, ob Drag&Drop aktiviert werden soll.
  225. *
  226. * @return boolean
  227. */
  228. public function hasDragAndDrop() {
  229. // D&D ist nur aktiv, wenn es explizit aktiviert wurde. Zusätzlich:
  230. // D&D ist immer dann aktiv, wenn die Tabelle sich im dodraganddrop-Modus
  231. // befindet oder wenn weder Pager noch Suchfunktion aktiv sind. Also:
  232. // D&D = ACTIVE & (dodraganddrop || (!PAGER && !SEARCH))
  233. return $this->enableDragAndDrop && (self::isDragAndDropMode() || (!$this->hasPager() && !$this->enableSearching));
  234. }
  235. /**
  236. * @param string $tableName
  237. * @param boolean $hasPager
  238. * @param boolean $hasDragAndDrop
  239. * @param sly_Request $request the request to use or null for the global one
  240. * @return array
  241. */
  242. public static function getPagingParameters($tableName = 'table', $hasPager = false, $hasDragAndDrop = false, sly_Request $request = null) {
  243. $perPage = self::$perPage;
  244. $request = $request ? $request : sly_Core::getRequest();
  245. $page = $request->get('p_'.$tableName, 'int', 0);
  246. $start = $page * $perPage;
  247. $elements = $perPage;
  248. $end = $start + $perPage;
  249. $getAll = array('page' => 0, 'start' => 0, 'end' => PHP_INT_MAX, 'elements' => PHP_INT_MAX);
  250. if (!$hasPager || ($hasDragAndDrop && self::isDragAndDropMode())) {
  251. return $getAll;
  252. }
  253. return array('page' => $page, 'start' => $start, 'end' => $end, 'elements' => $elements);
  254. }
  255. /**
  256. * @param string $tableName
  257. * @param sly_Request $request the request to use or null for the global one
  258. * @return string
  259. */
  260. public static function getSearchParameters($tableName = 'table', sly_Request $request = null) {
  261. $request = $request ? $request : sly_Core::getRequest();
  262. return $request->get('q_'.$tableName, 'string');
  263. }
  264. /**
  265. * @param string $tableName
  266. * @param string $defaultColumn
  267. * @param array $enabledColumns
  268. * @param string $defaultDirection
  269. * @param sly_Request $request the request to use or null for the global one
  270. * @return array
  271. */
  272. public static function getSortingParameters($tableName, $defaultColumn, $enabledColumns = array(), $defaultDirection = 'asc', sly_Request $request = null) {
  273. // support the old interface: get($defaultColumn, $enabledColumns)
  274. if (empty($enabledColumns) && is_array($defaultColumn)) {
  275. $enabledColumns = $defaultColumn;
  276. $defaultColumn = $tableName;
  277. $tableName = 'table';
  278. }
  279. $request = $request ? $request : sly_Core::getRequest();
  280. $sortby = $request->get($tableName.'_sortby', 'string', $defaultColumn);
  281. $direction = strtolower($request->get($tableName.'_direction', 'string', $defaultDirection)) === 'desc' ? 'DESC' : 'ASC';
  282. if (!in_array($sortby, $enabledColumns)) {
  283. $sortby = $defaultColumn;
  284. }
  285. return array('sortby' => $sortby, 'direction' => $direction);
  286. }
  287. /**
  288. * @throws sly_Exception
  289. * @param string $file
  290. * @return string
  291. */
  292. protected function getViewFile($file) {
  293. $full = SLY_COREFOLDER.'/views/table/'.$file;
  294. if (file_exists($full)) return $full;
  295. throw new sly_Exception(t('view_not_found', $file));
  296. }
  297. /**
  298. * Add a new class to the form
  299. *
  300. * This method will add a CSS class to the form tag. You can give mutliple
  301. * classes at once, the method will split them up and add them one at a time,
  302. * ensuring that they are unique.
  303. *
  304. * @param string $class the CSS class
  305. * @return array the list of current classes (unique)
  306. */
  307. public function addClass($class) {
  308. $class = explode(' ', $class);
  309. foreach ($class as $c) $this->classes[] = $c;
  310. $this->classes = array_unique($this->classes);
  311. return $this->classes;
  312. }
  313. /**
  314. * Remove all classes
  315. *
  316. * This method removes all set CSS classes for this form.
  317. */
  318. public function clearClasses() {
  319. $this->classes = array();
  320. }
  321. /**
  322. * Get all classes
  323. *
  324. * @return array the list of CSS classes for this form
  325. */
  326. public function getClasses() {
  327. return $this->classes;
  328. }
  329. }