Mokuji /www/app/AdminModule/components/Datagrid/DataGridColumn.php

Language PHP Lines 347
MD5 Hash b7dab6f832b83b63478654557de86b94
Repository https://github.com/bazo/Mokuji.git View Raw File View Project SPDX
  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
<?php

require_once dirname(__FILE__) . '/IDataGridColumn.php';



/**
 * Base class that implements the basic common functionality to data grid columns.
 *
 * @author     Roman Sklenář
 * @copyright  Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
 * @license    New BSD License
 * @example    http://nettephp.com/extras/datagrid
 * @package    Nette\Extras\DataGrid
 * @version    $Id$
 */
abstract class DataGridColumn extends ComponentContainer implements IDataGridColumn
{
	/** @var Html  table header element template */
	protected $header;

	/** @var Html  table cell element template */
	protected $cell;

	/** @var string */
	protected $caption;

	/** @var int */
	protected $maxLength = 100;

	/** @var array  of arrays('pattern' => 'replacement') */
	public $replacement;

	/** @var array  of callback functions */
	public $formatCallback = array();

	/** @var bool */
	public $orderable = TRUE;

	/** @var string */
	public static $ajaxClass = 'datagrid-ajax';


	/**
	 * Data grid column constructor.
	 * @param  string  textual caption of column
	 * @param  int     maximum number of dislayed characters
	 */
	public function __construct($caption = NULL, $maxLength = NULL)
	{
		parent::__construct();
		$this->addComponent(new ComponentContainer, 'filters');
		$this->header = Html::el();
		$this->cell = Html::el();
		$this->caption = $caption;
		if ($maxLength !== NULL) $this->maxLength = $maxLength;
		$this->monitor('DataGrid');
	}


	/**
	 * This method will be called when the component (or component's parent)
	 * becomes attached to a monitored object. Do not call this method yourself.
	 * @param  IComponent
	 * @return void
	 */
	protected function attached($component)
	{
		if ($component instanceof DataGrid) {
			$this->setParent($component);

			if ($this->caption === NULL) {
				$this->caption = $this->getName();
			}
		}
	}


	/**
	 * Returns DataGrid.
	 * @param  bool   throw exception if form doesn't exist?
	 * @return DataGrid
	 */
	public function getDataGrid($need = TRUE)
	{
		return $this->lookup('DataGrid', $need);
	}



	/********************* Html objects getters *********************/



	/**
	 * Returns headers's HTML element template.
	 * @return Html
	 */
	public function getHeaderPrototype()
	{
		return $this->header;
	}


	/**
	 * Returns table's cell HTML element template.
	 * @return Html
	 */
	public function getCellPrototype()
	{
		return $this->cell;
	}


	/**
	 * Setter / property method.
	 * @return string
	 */
	public function getCaption()
	{
		if ($this->caption instanceof Html && $this->caption->title) {
			return $this->caption->title($this->getDataGrid(TRUE)->translate($this->caption->title));
		} else {
			return $this->getDataGrid(TRUE)->translate($this->caption);
		}
	}



	/********************* interface \IDataGridColumn *********************/



	/**
	 * Is column orderable?
	 * @return bool
	 */
	public function isOrderable()
	{
		return $this->orderable;
	}


	/**
	 * Gets header link (order signal)
	 * @param  string  direction of sorting (a|d|NULL)
	 * @return string
	 */
	public function getOrderLink($dir = NULL)
	{
		return $this->getDataGrid(TRUE)->link('order', array('by' => $this->getName(), 'dir' => $dir));
	}


	/**
	 * Has column filter box?
	 * @return bool
	 */
	public function hasFilter()
	{
		return $this->getFilter(FALSE) instanceof IDataGridColumnFilter;
	}


	/**
	 * Returns column's filter.
	 * @param  bool   throw exception if component doesn't exist?
	 * @return IDataGridColumnFilter|NULL
	 */
	public function getFilter($need = TRUE)
	{
		return $this->getComponent('filters')->getComponent($this->getName(), $need);
	}


	/**
	 * Formats cell's content. Descendant can override this method to customize formating.
	 * @param  mixed
	 * @param  DibiRow|array
	 * @return string
	 */
	public function formatContent($value, $data = NULL)
	{
		return (string) $value;
	}


	/**
	 * Filters data source. Descendant can override this method to customize filtering.
	 * @param  mixed
	 * @return void
	 */
	public function applyFilter($value)
	{
		return;
	}



	/********************* Default sorting and filtering *********************/



	/**
	 * Adds default sorting to data grid.
	 * @param string
	 * @return DataGridColumn  provides a fluent interface
	 */
	public function addDefaultSorting($order = 'ASC')
	{
		$orders = array('ASC', 'DESC', 'asc', 'desc', 'A', 'D', 'a', 'd');
		if (!in_array($order, $orders)) {
			throw new InvalidArgumentException("Order must be in '" . implode(', ', $orders) . "', '$order' given.");
		}

		parse_str($this->getDataGrid()->defaultOrder, $list);
		$list[$this->getName()] = strtolower($order[0]);
		$this->getDataGrid()->defaultOrder = http_build_query($list, '', '&');

		return $this;
	}


	/**
	 * Adds default filtering to data grid.
	 * @param string
	 * @return DataGridColumn  provides a fluent interface
	 */
	public function addDefaultFiltering($value)
	{
		parse_str($this->getDataGrid()->defaultFilters, $list);
		$list[$this->getName()] = $value;
		$this->getDataGrid()->defaultFilters = http_build_query($list, '', '&');

		return $this;
	}


	/**
	 * Removes data grid's default sorting.
	 * @return DataGridColumn  provides a fluent interface
	 */
	public function removeDefaultSorting()
	{
		parse_str($this->getDataGrid()->defaultOrder, $list);
		if (isset($list[$this->getName()])) unset($list[$this->getName()]);
		$this->getDataGrid()->defaultOrder = http_build_query($list, '', '&');

		return $this;
	}


	/**
	 * Removes data grid's default filtering.
	 * @return DataGridColumn  provides a fluent interface
	 */
	public function removeDefaultFiltering()
	{
		parse_str($this->getDataGrid()->defaultFilters, $list);
		if (isset($list[$this->getName()])) unset($list[$this->getName()]);
		$this->getDataGrid()->defaultFilters = http_build_query($list, '', '&');

		return $this;
	}




	/********************* filter factories *********************/



	/**
	 * Alias for method addTextFilter().
	 * @return IDataGridColumnFilter
	 */
	public function addFilter()
	{
		return $this->addTextFilter();
	}


	/**
	 * Adds single-line text filter input to data grid.
	 * @return IDataGridColumnFilter
	 * @throws InvalidArgumentException
	 */
	public function addTextFilter()
	{
		$this->_addFilter(new TextFilter);
		return $this->getFilter();
	}


	/**
	 * Adds single-line text date filter input to data grid.
	 * Optional dependency on DatePicker class (@link http://nettephp.com/extras/datepicker)
	 * @return IDataGridColumnFilter
	 * @throws InvalidArgumentException
	 */
	public function addDateFilter()
	{
		$this->_addFilter(new DateFilter);
		return $this->getFilter();
	}


	/**
	 * Adds check box filter input to data grid.
	 * @return IDataGridColumnFilter
	 * @throws InvalidArgumentException
	 */
	public function addCheckboxFilter()
	{
		$this->_addFilter(new CheckboxFilter);
		return $this->getFilter();
	}


	/**
	 * Adds select box filter input to data grid.
	 * @param  array   items from which to choose
	 * @param  bool    add empty first item to selectbox?
	 * @param  bool    translate all items in selectbox?
	 * @return IDataGridColumnFilter
	 * @throws InvalidArgumentException
	 */
	public function addSelectboxFilter($items = NULL, $firstEmpty = TRUE, $translateItems = TRUE)
	{
		$this->_addFilter(new SelectboxFilter($items, $firstEmpty));
		return $this->getFilter()->translateItems($translateItems);
	}


	/**
	 * Internal filter adding routine.
	 * @param  IDataGridColumnFilter $filter
	 * @return void
	 */
	private function _addFilter(IDataGridColumnFilter $filter)
	{
		if ($this->hasFilter()) {
			$this->getComponent('filters')->removeComponent($this->getFilter());
		}
		$this->getComponent('filters')->addComponent($filter, $this->getName());
	}
}
Back to Top