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