PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/www/app/AdminModule/components/Datagrid/Columns/ActionColumn.php

https://github.com/bazo/Mokuji
PHP | 194 lines | 75 code | 33 blank | 86 comment | 5 complexity | b0914a459767df1cc80d36254715f451 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. require_once dirname(__FILE__) . '/../DataGridColumn.php';
  3. /**
  4. * Representation of data grid action column.
  5. * If you want to write your own implementation you must inherit this class.
  6. *
  7. * @author Roman Sklenář
  8. * @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
  9. * @license New BSD License
  10. * @example http://nettephp.com/extras/datagrid
  11. * @package Nette\Extras\DataGrid
  12. * @version $Id$
  13. */
  14. class ActionColumn extends DataGridColumn implements ArrayAccess
  15. {
  16. /**
  17. * Action column constructor.
  18. * @param string column's textual caption
  19. * @return void
  20. */
  21. public function __construct($caption = 'Actions')
  22. {
  23. parent::__construct($caption);
  24. $this->addComponent(new ComponentContainer, 'actions');
  25. $this->removeComponent($this->getComponent('filters'));
  26. $this->orderable = FALSE;
  27. }
  28. /**
  29. * Has column filter box?
  30. * @return bool
  31. */
  32. public function hasFilter()
  33. {
  34. return FALSE;
  35. }
  36. /**
  37. * Returns column's filter.
  38. * @param bool throw exception if component doesn't exist?
  39. * @return IDataGridColumnFilter|NULL
  40. * @throws InvalidStateException
  41. */
  42. public function getFilter($need = TRUE)
  43. {
  44. if ($need == TRUE) {
  45. throw new InvalidStateException("ActionColumn cannot has filter.");
  46. }
  47. return NULL;
  48. }
  49. /**
  50. * Action factory.
  51. * @param string textual title
  52. * @param string textual link destination
  53. * @param Html element which is added to a generated link
  54. * @param bool use ajax? (add class self::$ajaxClass into generated link)
  55. * @param bool generate link with argument? (variable $keyName must be defined in data grid)
  56. * @return DataGridAction
  57. */
  58. public function addAction($title, $signal, $icon = NULL, $useAjax = FALSE, $type = DataGridAction::WITH_KEY)
  59. {
  60. $action = new DataGridAction($title, $signal, $icon, $useAjax, $type);
  61. $this[] = $action;
  62. return $action;
  63. }
  64. /**
  65. * Does column has any action?
  66. * @return bool
  67. */
  68. public function hasAction($type = NULL)
  69. {
  70. return count($this->getActions($type)) > 0;
  71. }
  72. /**
  73. * Returns column's action specified by name.
  74. * @param string action's name
  75. * @param bool throw exception if component doesn't exist?
  76. * @return IDataGridColumnAction|NULL
  77. */
  78. public function getAction($name = NULL, $need = TRUE)
  79. {
  80. return $this->getComponent('actions')->getComponent($name, $need);
  81. }
  82. /**
  83. * Iterates over all column's actions.
  84. * @param string
  85. * @return ArrayIterator|NULL
  86. */
  87. public function getActions($type = 'IDataGridAction')
  88. {
  89. $actions = new ArrayObject();
  90. foreach ($this->getComponent('actions')->getComponents(FALSE, $type) as $action) {
  91. $actions->append($action);
  92. }
  93. return $actions->getIterator();
  94. }
  95. /**
  96. * Formats cell's content.
  97. * @param mixed
  98. * @param DibiRow|array
  99. * @return string
  100. * @throws InvalidStateException
  101. */
  102. public function formatContent($value, $data = NULL)
  103. {
  104. throw new InvalidStateException("ActionColumn cannot be formated.");
  105. }
  106. /**
  107. * Filters data source.
  108. * @param mixed
  109. * @throws InvalidStateException
  110. * @return void
  111. */
  112. public function applyFilter($value)
  113. {
  114. throw new InvalidStateException("ActionColumn cannot be filtered.");
  115. }
  116. /********************* interface \ArrayAccess *********************/
  117. /**
  118. * Adds the component to the container.
  119. * @param string component name
  120. * @param IComponent
  121. * @return void.
  122. */
  123. final public function offsetSet($name, $component)
  124. {
  125. if (!$component instanceof IComponent) {
  126. throw new InvalidArgumentException("ActionColumn accepts only IComponent objects.");
  127. }
  128. $this->getComponent('actions')->addComponent($component, $name == NULL ? count($this->getActions()) : $name);
  129. }
  130. /**
  131. * Returns component specified by name. Throws exception if component doesn't exist.
  132. * @param string component name
  133. * @return IComponent
  134. * @throws InvalidArgumentException
  135. */
  136. final public function offsetGet($name)
  137. {
  138. return $this->getAction((string) $name, TRUE);
  139. }
  140. /**
  141. * Does component specified by name exists?
  142. * @param string component name
  143. * @return bool
  144. */
  145. final public function offsetExists($name)
  146. {
  147. return $this->getAction($name, FALSE) !== NULL;
  148. }
  149. /**
  150. * Removes component from the container. Throws exception if component doesn't exist.
  151. * @param string component name
  152. * @return void
  153. */
  154. final public function offsetUnset($name)
  155. {
  156. $component = $this->getAction($name, FALSE);
  157. if ($component !== NULL) {
  158. $this->getComponent('actions')->removeComponent($component);
  159. }
  160. }
  161. }