PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/bazo/Mokuji
PHP | 347 lines | 139 code | 78 blank | 130 comment | 10 complexity | b7dab6f832b83b63478654557de86b94 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. require_once dirname(__FILE__) . '/IDataGridColumn.php';
  3. /**
  4. * Base class that implements the basic common functionality to data grid columns.
  5. *
  6. * @author Roman Sklenář
  7. * @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
  8. * @license New BSD License
  9. * @example http://nettephp.com/extras/datagrid
  10. * @package Nette\Extras\DataGrid
  11. * @version $Id$
  12. */
  13. abstract class DataGridColumn extends ComponentContainer implements IDataGridColumn
  14. {
  15. /** @var Html table header element template */
  16. protected $header;
  17. /** @var Html table cell element template */
  18. protected $cell;
  19. /** @var string */
  20. protected $caption;
  21. /** @var int */
  22. protected $maxLength = 100;
  23. /** @var array of arrays('pattern' => 'replacement') */
  24. public $replacement;
  25. /** @var array of callback functions */
  26. public $formatCallback = array();
  27. /** @var bool */
  28. public $orderable = TRUE;
  29. /** @var string */
  30. public static $ajaxClass = 'datagrid-ajax';
  31. /**
  32. * Data grid column constructor.
  33. * @param string textual caption of column
  34. * @param int maximum number of dislayed characters
  35. */
  36. public function __construct($caption = NULL, $maxLength = NULL)
  37. {
  38. parent::__construct();
  39. $this->addComponent(new ComponentContainer, 'filters');
  40. $this->header = Html::el();
  41. $this->cell = Html::el();
  42. $this->caption = $caption;
  43. if ($maxLength !== NULL) $this->maxLength = $maxLength;
  44. $this->monitor('DataGrid');
  45. }
  46. /**
  47. * This method will be called when the component (or component's parent)
  48. * becomes attached to a monitored object. Do not call this method yourself.
  49. * @param IComponent
  50. * @return void
  51. */
  52. protected function attached($component)
  53. {
  54. if ($component instanceof DataGrid) {
  55. $this->setParent($component);
  56. if ($this->caption === NULL) {
  57. $this->caption = $this->getName();
  58. }
  59. }
  60. }
  61. /**
  62. * Returns DataGrid.
  63. * @param bool throw exception if form doesn't exist?
  64. * @return DataGrid
  65. */
  66. public function getDataGrid($need = TRUE)
  67. {
  68. return $this->lookup('DataGrid', $need);
  69. }
  70. /********************* Html objects getters *********************/
  71. /**
  72. * Returns headers's HTML element template.
  73. * @return Html
  74. */
  75. public function getHeaderPrototype()
  76. {
  77. return $this->header;
  78. }
  79. /**
  80. * Returns table's cell HTML element template.
  81. * @return Html
  82. */
  83. public function getCellPrototype()
  84. {
  85. return $this->cell;
  86. }
  87. /**
  88. * Setter / property method.
  89. * @return string
  90. */
  91. public function getCaption()
  92. {
  93. if ($this->caption instanceof Html && $this->caption->title) {
  94. return $this->caption->title($this->getDataGrid(TRUE)->translate($this->caption->title));
  95. } else {
  96. return $this->getDataGrid(TRUE)->translate($this->caption);
  97. }
  98. }
  99. /********************* interface \IDataGridColumn *********************/
  100. /**
  101. * Is column orderable?
  102. * @return bool
  103. */
  104. public function isOrderable()
  105. {
  106. return $this->orderable;
  107. }
  108. /**
  109. * Gets header link (order signal)
  110. * @param string direction of sorting (a|d|NULL)
  111. * @return string
  112. */
  113. public function getOrderLink($dir = NULL)
  114. {
  115. return $this->getDataGrid(TRUE)->link('order', array('by' => $this->getName(), 'dir' => $dir));
  116. }
  117. /**
  118. * Has column filter box?
  119. * @return bool
  120. */
  121. public function hasFilter()
  122. {
  123. return $this->getFilter(FALSE) instanceof IDataGridColumnFilter;
  124. }
  125. /**
  126. * Returns column's filter.
  127. * @param bool throw exception if component doesn't exist?
  128. * @return IDataGridColumnFilter|NULL
  129. */
  130. public function getFilter($need = TRUE)
  131. {
  132. return $this->getComponent('filters')->getComponent($this->getName(), $need);
  133. }
  134. /**
  135. * Formats cell's content. Descendant can override this method to customize formating.
  136. * @param mixed
  137. * @param DibiRow|array
  138. * @return string
  139. */
  140. public function formatContent($value, $data = NULL)
  141. {
  142. return (string) $value;
  143. }
  144. /**
  145. * Filters data source. Descendant can override this method to customize filtering.
  146. * @param mixed
  147. * @return void
  148. */
  149. public function applyFilter($value)
  150. {
  151. return;
  152. }
  153. /********************* Default sorting and filtering *********************/
  154. /**
  155. * Adds default sorting to data grid.
  156. * @param string
  157. * @return DataGridColumn provides a fluent interface
  158. */
  159. public function addDefaultSorting($order = 'ASC')
  160. {
  161. $orders = array('ASC', 'DESC', 'asc', 'desc', 'A', 'D', 'a', 'd');
  162. if (!in_array($order, $orders)) {
  163. throw new InvalidArgumentException("Order must be in '" . implode(', ', $orders) . "', '$order' given.");
  164. }
  165. parse_str($this->getDataGrid()->defaultOrder, $list);
  166. $list[$this->getName()] = strtolower($order[0]);
  167. $this->getDataGrid()->defaultOrder = http_build_query($list, '', '&');
  168. return $this;
  169. }
  170. /**
  171. * Adds default filtering to data grid.
  172. * @param string
  173. * @return DataGridColumn provides a fluent interface
  174. */
  175. public function addDefaultFiltering($value)
  176. {
  177. parse_str($this->getDataGrid()->defaultFilters, $list);
  178. $list[$this->getName()] = $value;
  179. $this->getDataGrid()->defaultFilters = http_build_query($list, '', '&');
  180. return $this;
  181. }
  182. /**
  183. * Removes data grid's default sorting.
  184. * @return DataGridColumn provides a fluent interface
  185. */
  186. public function removeDefaultSorting()
  187. {
  188. parse_str($this->getDataGrid()->defaultOrder, $list);
  189. if (isset($list[$this->getName()])) unset($list[$this->getName()]);
  190. $this->getDataGrid()->defaultOrder = http_build_query($list, '', '&');
  191. return $this;
  192. }
  193. /**
  194. * Removes data grid's default filtering.
  195. * @return DataGridColumn provides a fluent interface
  196. */
  197. public function removeDefaultFiltering()
  198. {
  199. parse_str($this->getDataGrid()->defaultFilters, $list);
  200. if (isset($list[$this->getName()])) unset($list[$this->getName()]);
  201. $this->getDataGrid()->defaultFilters = http_build_query($list, '', '&');
  202. return $this;
  203. }
  204. /********************* filter factories *********************/
  205. /**
  206. * Alias for method addTextFilter().
  207. * @return IDataGridColumnFilter
  208. */
  209. public function addFilter()
  210. {
  211. return $this->addTextFilter();
  212. }
  213. /**
  214. * Adds single-line text filter input to data grid.
  215. * @return IDataGridColumnFilter
  216. * @throws InvalidArgumentException
  217. */
  218. public function addTextFilter()
  219. {
  220. $this->_addFilter(new TextFilter);
  221. return $this->getFilter();
  222. }
  223. /**
  224. * Adds single-line text date filter input to data grid.
  225. * Optional dependency on DatePicker class (@link http://nettephp.com/extras/datepicker)
  226. * @return IDataGridColumnFilter
  227. * @throws InvalidArgumentException
  228. */
  229. public function addDateFilter()
  230. {
  231. $this->_addFilter(new DateFilter);
  232. return $this->getFilter();
  233. }
  234. /**
  235. * Adds check box filter input to data grid.
  236. * @return IDataGridColumnFilter
  237. * @throws InvalidArgumentException
  238. */
  239. public function addCheckboxFilter()
  240. {
  241. $this->_addFilter(new CheckboxFilter);
  242. return $this->getFilter();
  243. }
  244. /**
  245. * Adds select box filter input to data grid.
  246. * @param array items from which to choose
  247. * @param bool add empty first item to selectbox?
  248. * @param bool translate all items in selectbox?
  249. * @return IDataGridColumnFilter
  250. * @throws InvalidArgumentException
  251. */
  252. public function addSelectboxFilter($items = NULL, $firstEmpty = TRUE, $translateItems = TRUE)
  253. {
  254. $this->_addFilter(new SelectboxFilter($items, $firstEmpty));
  255. return $this->getFilter()->translateItems($translateItems);
  256. }
  257. /**
  258. * Internal filter adding routine.
  259. * @param IDataGridColumnFilter $filter
  260. * @return void
  261. */
  262. private function _addFilter(IDataGridColumnFilter $filter)
  263. {
  264. if ($this->hasFilter()) {
  265. $this->getComponent('filters')->removeComponent($this->getFilter());
  266. }
  267. $this->getComponent('filters')->addComponent($filter, $this->getName());
  268. }
  269. }