PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/simpus/vendor/yiisoft/yii2/grid/ActionColumn.php

https://gitlab.com/isdzulqor/Slis-Dev
PHP | 182 lines | 76 code | 10 blank | 96 comment | 7 complexity | 08d770c4846bf4b39fc09f996b7c23ce MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\grid;
  8. use Yii;
  9. use Closure;
  10. use yii\helpers\Html;
  11. use yii\helpers\Url;
  12. /**
  13. * ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
  14. *
  15. * To add an ActionColumn to the gridview, add it to the [[GridView::columns|columns]] configuration as follows:
  16. *
  17. * ```php
  18. * 'columns' => [
  19. * // ...
  20. * [
  21. * 'class' => ActionColumn::className(),
  22. * // you may configure additional properties here
  23. * ],
  24. * ]
  25. * ```
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @since 2.0
  29. */
  30. class ActionColumn extends Column
  31. {
  32. /**
  33. * @var string the ID of the controller that should handle the actions specified here.
  34. * If not set, it will use the currently active controller. This property is mainly used by
  35. * [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
  36. * to each action name to form the route of the action.
  37. */
  38. public $controller;
  39. /**
  40. * @var string the template used for composing each cell in the action column.
  41. * Tokens enclosed within curly brackets are treated as controller action IDs (also called *button names*
  42. * in the context of action column). They will be replaced by the corresponding button rendering callbacks
  43. * specified in [[buttons]]. For example, the token `{view}` will be replaced by the result of
  44. * the callback `buttons['view']`. If a callback cannot be found, the token will be replaced with an empty string.
  45. *
  46. * As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as follows:
  47. *
  48. * ```
  49. * ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],
  50. * ```
  51. *
  52. * @see buttons
  53. */
  54. public $template = '{view} {update} {delete}';
  55. /**
  56. * @var array button rendering callbacks. The array keys are the button names (without curly brackets),
  57. * and the values are the corresponding button rendering callbacks. The callbacks should use the following
  58. * signature:
  59. *
  60. * ```php
  61. * function ($url, $model, $key) {
  62. * // return the button HTML code
  63. * }
  64. * ```
  65. *
  66. * where `$url` is the URL that the column creates for the button, `$model` is the model object
  67. * being rendered for the current row, and `$key` is the key of the model in the data provider array.
  68. *
  69. * You can add further conditions to the button, for example only display it, when the model is
  70. * editable (here assuming you have a status field that indicates that):
  71. *
  72. * ```php
  73. * [
  74. * 'update' => function ($url, $model, $key) {
  75. * return $model->status === 'editable' ? Html::a('Update', $url) : '';
  76. * },
  77. * ],
  78. * ```
  79. */
  80. public $buttons = [];
  81. /**
  82. * @var callable a callback that creates a button URL using the specified model information.
  83. * The signature of the callback should be the same as that of [[createUrl()]].
  84. * If this property is not set, button URLs will be created using [[createUrl()]].
  85. */
  86. public $urlCreator;
  87. /**
  88. * @var array html options to be applied to the [[initDefaultButtons()|default buttons]].
  89. * @since 2.0.4
  90. */
  91. public $buttonOptions = [];
  92. /**
  93. * @inheritdoc
  94. */
  95. public function init()
  96. {
  97. parent::init();
  98. $this->initDefaultButtons();
  99. }
  100. /**
  101. * Initializes the default button rendering callbacks.
  102. */
  103. protected function initDefaultButtons()
  104. {
  105. if (!isset($this->buttons['view'])) {
  106. $this->buttons['view'] = function ($url, $model, $key) {
  107. $options = array_merge([
  108. 'title' => Yii::t('yii', 'View'),
  109. 'aria-label' => Yii::t('yii', 'View'),
  110. 'data-pjax' => '0',
  111. ], $this->buttonOptions);
  112. return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
  113. };
  114. }
  115. if (!isset($this->buttons['update'])) {
  116. $this->buttons['update'] = function ($url, $model, $key) {
  117. $options = array_merge([
  118. 'title' => Yii::t('yii', 'Update'),
  119. 'aria-label' => Yii::t('yii', 'Update'),
  120. 'data-pjax' => '0',
  121. ], $this->buttonOptions);
  122. return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
  123. };
  124. }
  125. if (!isset($this->buttons['delete'])) {
  126. $this->buttons['delete'] = function ($url, $model, $key) {
  127. $options = array_merge([
  128. 'title' => Yii::t('yii', 'Delete'),
  129. 'aria-label' => Yii::t('yii', 'Delete'),
  130. 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
  131. 'data-method' => 'post',
  132. 'data-pjax' => '0',
  133. ], $this->buttonOptions);
  134. return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
  135. };
  136. }
  137. }
  138. /**
  139. * Creates a URL for the given action and model.
  140. * This method is called for each button and each row.
  141. * @param string $action the button name (or action ID)
  142. * @param \yii\db\ActiveRecord $model the data model
  143. * @param mixed $key the key associated with the data model
  144. * @param integer $index the current row index
  145. * @return string the created URL
  146. */
  147. public function createUrl($action, $model, $key, $index)
  148. {
  149. if ($this->urlCreator instanceof Closure) {
  150. return call_user_func($this->urlCreator, $action, $model, $key, $index);
  151. } else {
  152. $params = is_array($key) ? $key : ['id' => (string) $key];
  153. $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
  154. return Url::toRoute($params);
  155. }
  156. }
  157. /**
  158. * @inheritdoc
  159. */
  160. protected function renderDataCellContent($model, $key, $index)
  161. {
  162. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
  163. $name = $matches[1];
  164. if (isset($this->buttons[$name])) {
  165. $url = $this->createUrl($name, $model, $key, $index);
  166. return call_user_func($this->buttons[$name], $url, $model, $key);
  167. } else {
  168. return '';
  169. }
  170. }, $this->template);
  171. }
  172. }