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

Language PHP Lines 380
MD5 Hash fcfb0bc04743ab3b1f5008bb050d020d Estimated Cost $4,204 (why?)
Repository https://bitbucket.org/SallyCMS/trunk View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * @ingroup table
 */
class sly_Table extends sly_Viewable {
	protected $id;                  ///< string
	protected $columns;             ///< array
	protected $classes;             ///< array
	protected $isEmpty;             ///< boolean
	protected $emptyNotice;         ///< string
	protected $enableSorting;       ///< boolean
	protected $enableSearching;     ///< boolean
	protected $enableDragAndDrop;   ///< boolean
	protected $dragAndDropHandler;  ///< boolean
	protected $totalElements;       ///< int
	protected $caption;             ///< string
	protected $searchParams;        ///< array

	private $content;               ///< string

	protected static $perPage = 30; ///< int

	/**
	 * @param string $id
	 * @param array  $classes  list of additional CSS classes for the table tag
	 */
	public function __construct($id = null, array $classes = array()) {
		// Das 'a'-Präfix verhindert, dass die ID mit einer Zahl beginnt, was in HTML nicht erlaubt ist.

		$this->id                 = $id === null ? 'a'.substr(md5(uniqid()), 0, 10) : $id;
		$this->columns            = array();
		$this->classes            = array('sly-table');
		$this->isEmpty            = false;
		$this->emptyNotice        = t('table_is_empty');
		$this->enableSorting      = false;
		$this->enableSearching    = false;
		$this->enableDragAndDrop  = false;
		$this->dragAndDropHandler = '';
		$this->totalElements      = null;
		$this->caption            = null;
		$this->searchParams       = null;

		foreach ($classes as $className) $this->addClass($className);
	}

	/**
	 * @param string $id
	 */
	public function setID($id) {
		$this->id = trim($id);
	}

	/**
	 * @return string
	 */
	public function getID() {
		return $this->id;
	}

	/**
	 * @param  sly_Request $request   the request to use or null for the global one
	 * @return boolean
	 */
	public static function isDragAndDropMode(sly_Request $request = null) {
		$request = $request ? $request : sly_Core::getRequest();
		return $request->get('tableswitch', 'string') === 'dodraganddrop';
	}

	/**
	 * @param int $perPage
	 */
	public static function setElementsPerPageStatic($perPage) {
		self::$perPage = abs((int) $perPage);

		if (self::$perPage < 1) {
			self::$perPage = 1;
		}
	}

	/**
	 * @param int $perPage
	 */
	public function setElementsPerPage($perPage = 20) {
		self::setElementsPerPageStatic($perPage);
	}

	/**
	 * @param  int         $totalElements  leave this to null to disable the pager
	 * @param  sly_Request $request        the request to use
	 * @return string                      the rendered header
	 */
	protected function renderHeader($totalElements = null, sly_Request $request) {
		$this->totalElements = $totalElements;
		return $this->renderView('header.phtml', compact('totalElements', 'request'));
	}

	/**
	 * @return string  the rendered footer
	 */
	protected function renderFooter() {
		return $this->renderView('footer.phtml');
	}

	public function openBuffer() {
		ob_start();
	}

	public function closeBuffer() {
		$this->content = ob_get_clean();
	}

	/**
	 * @param  int         $totalElements  leave this to null to disable the pager
	 * @param  sly_Request $request        the request to use or null for the global one
	 * @return string                      the rendered content
	 */
	public function render($totalElements = null, sly_Request $request = null) {
		ob_start();
		$request = $request ? $request : sly_Core::getRequest();
		print $this->renderHeader($totalElements, $request);
		print $this->content;
		print $this->renderFooter();
		return ob_get_clean();
	}

	/**
	 * @param boolean $isEmpty
	 */
	public function setIsEmpty($isEmpty) {
		$this->isEmpty = (bool) $isEmpty;
	}

	/**
	 * @param string $caption
	 */
	public function setCaption($caption) {
		$this->caption = $caption;
	}

	/**
	 * @param string $notice
	 */
	public function setEmptyNotice($notice) {
		$this->emptyNotice = $notice;
	}

	/**
	 * @param boolean $enable
	 */
	public function enableSorting($enable = true) {
		$this->enableSorting = (bool) $enable;
	}

	/**
	 * @param boolean $enable
	 */
	public function enableDragAndDrop($enable = true) {
		$this->enableDragAndDrop = (bool) $enable;
	}

	/**
	 * @param string $function  JS callable
	 */
	public function setDragAndDropHandler($function) {
		$this->dragAndDropHandler = $function;
	}

	/**
	 * @param sly_Table_Column $col
	 */
	public function addColumn(sly_Table_Column $col) {
		$this->columns[] = $col;
	}

	/**
	 * @param  int $idx
	 * @return sly_Table_Column
	 */
	public function getColumn($idx) {
		return sly_setarraytype($this->columns, $idx, 'raw', null);
	}

	/**
	 * @param boolean $enable
	 * @param array   $getParams  override default detection of GET parameters by giving an array instead of null
	 */
	public function enableSearching($enable = true, array $getParams = null) {
		$this->enableSearching = (bool) $enable;
		$this->searchParams    = $getParams;
	}

	/**
	 * @return boolean
	 */
	public function isSorting() {
		return $this->enableSorting;
	}

	/**
	 * @return boolean
	 */
	public function isSearching() {
		return $this->enableSearching;
	}

	/**
	 * @param  string      $default
	 * @param  sly_Request $request   the request to use or null for the global one
	 * @return string
	 */
	public function getSortKey($default = null, sly_Request $request = null) {
		$request = $request ? $request : sly_Core::getRequest();
		return $request->get($this->id.'_sortby', 'string', $default);
	}

	/**
	 * @param  string      $default
	 * @param  sly_Request $request   the request to use or null for the global one
	 * @return string
	 */
	public function getDirection($default = 'asc', sly_Request $request = null) {
		$request = $request ? $request : sly_Core::getRequest();
		return $request->get($this->id.'_direction', 'string', $default);
	}

	/**
	 * Gibt true zurück, wenn ein Pager angezeigt werden soll.
	 *
	 * @return boolean
	 */
	public function hasPager() {
		return $this->totalElements !== null && $this->totalElements > self::$perPage;
	}

	/**
	 * Gibt true zurück, wenn die Suchmaske angezeigt werden soll.
	 *
	 * @return boolean
	 */
	public function hasSearch() {
		// Die Suchfunktion ist immer dann aktiviert, wenn sie im Objekt
		// aktiviert wurde (enableSearching) und wenn der Tabellenmodus
		// nicht auf dodraganddrop steht.

		return $this->enableSearching;
	}

	/**
	 * Gibt true zurück, ob Drag&Drop aktiviert werden soll.
	 *
	 * @return boolean
	 */
	public function hasDragAndDrop() {
		// D&D ist nur aktiv, wenn es explizit aktiviert wurde. Zusätzlich:
		// D&D ist immer dann aktiv, wenn die Tabelle sich im dodraganddrop-Modus
		// befindet oder wenn weder Pager noch Suchfunktion aktiv sind. Also:

		// D&D = ACTIVE & (dodraganddrop || (!PAGER && !SEARCH))

		return $this->enableDragAndDrop && (self::isDragAndDropMode() || (!$this->hasPager() && !$this->enableSearching));
	}

	/**
	 * @param  string      $tableName
	 * @param  boolean     $hasPager
	 * @param  boolean     $hasDragAndDrop
	 * @param  sly_Request $request         the request to use or null for the global one
	 * @return array
	 */
	public static function getPagingParameters($tableName = 'table', $hasPager = false, $hasDragAndDrop = false, sly_Request $request = null) {
		$perPage = self::$perPage;

		$request  = $request ? $request : sly_Core::getRequest();
		$page     = $request->get('p_'.$tableName, 'int', 0);
		$start    = $page * $perPage;
		$elements = $perPage;
		$end      = $start + $perPage;
		$getAll   = array('page' => 0, 'start' => 0, 'end' => PHP_INT_MAX, 'elements' => PHP_INT_MAX);

		if (!$hasPager || ($hasDragAndDrop && self::isDragAndDropMode())) {
			return $getAll;
		}

		return array('page' => $page, 'start' => $start, 'end' => $end, 'elements' => $elements);
	}

	/**
	 * @param  string      $tableName
	 * @param  sly_Request $request    the request to use or null for the global one
	 * @return string
	 */
	public static function getSearchParameters($tableName = 'table', sly_Request $request = null) {
		$request = $request ? $request : sly_Core::getRequest();
		return $request->get('q_'.$tableName, 'string');
	}

	/**
	 * @param  string      $tableName
	 * @param  string      $defaultColumn
	 * @param  array       $enabledColumns
	 * @param  string      $defaultDirection
	 * @param  sly_Request $request           the request to use or null for the global one
	 * @return array
	 */
	public static function getSortingParameters($tableName, $defaultColumn, $enabledColumns = array(), $defaultDirection = 'asc', sly_Request $request = null) {
		// support the old interface: get($defaultColumn, $enabledColumns)
		if (empty($enabledColumns) && is_array($defaultColumn)) {
			$enabledColumns = $defaultColumn;
			$defaultColumn  = $tableName;
			$tableName      = 'table';
		}

		$request   = $request ? $request : sly_Core::getRequest();
		$sortby    = $request->get($tableName.'_sortby', 'string', $defaultColumn);
		$direction = strtolower($request->get($tableName.'_direction', 'string', $defaultDirection)) === 'desc' ? 'DESC' : 'ASC';

		if (!in_array($sortby, $enabledColumns)) {
			$sortby = $defaultColumn;
		}

		return array('sortby' => $sortby, 'direction' => $direction);
	}

	/**
	 * @throws sly_Exception
	 * @param  string $file
	 * @return string
	 */
	protected function getViewFile($file) {
		$full = SLY_COREFOLDER.'/views/table/'.$file;
		if (file_exists($full)) return $full;

		throw new sly_Exception(t('view_not_found', $file));
	}

	/**
	 * Add a new class to the form
	 *
	 * This method will add a CSS class to the form tag. You can give mutliple
	 * classes at once, the method will split them up and add them one at a time,
	 * ensuring that they are unique.
	 *
	 * @param  string $class  the CSS class
	 * @return array          the list of current classes (unique)
	 */
	public function addClass($class) {
		$class = explode(' ', $class);
		foreach ($class as $c) $this->classes[] = $c;
		$this->classes = array_unique($this->classes);
		return $this->classes;
	}

	/**
	 * Remove all classes
	 *
	 * This method removes all set CSS classes for this form.
	 */
	public function clearClasses() {
		$this->classes = array();
	}

	/**
	 * Get all classes
	 *
	 * @return array  the list of CSS classes for this form
	 */
	public function getClasses() {
		return $this->classes;
	}
}
Back to Top