ActionColumn.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  12. * ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
  13. *
  14. * @author Qiang Xue <[email protected]>
  15. * @since 2.0
  16. */
  17. class ActionColumn extends Column
  18. {
  19. /**
  20. * @var string the ID of the controller that should handle the actions specified here.
  21. * If not set, it will use the currently active controller. This property is mainly used by
  22. * [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
  23. * to each action name to form the route of the action.
  24. */
  25. public $controller;
  26. /**
  27. * @var string the template used for composing each cell in the action column.
  28. * Tokens enclosed within curly brackets are treated as controller action IDs (also called *button names*
  29. * in the context of action column). They will be replaced by the corresponding button rendering callbacks
  30. * specified in [[buttons]]. For example, the token `{view}` will be replaced by the result of
  31. * the callback `buttons['view']`. If a callback cannot be found, the token will be replaced with an empty string.
  32. * @see buttons
  33. */
  34. public $template = '{view} {update} {delete}';
  35. /**
  36. * @var array button rendering callbacks. The array keys are the button names (without curly brackets),
  37. * and the values are the corresponding button rendering callbacks. The callbacks should use the following
  38. * signature:
  39. *
  40. * ```php
  41. * function ($url, $model) {
  42. * // return the button HTML code
  43. * }
  44. * ```
  45. *
  46. * where `$url` is the URL that the column creates for the button, and `$model` is the model object
  47. * being rendered for the current row.
  48. */
  49. public $buttons = [];
  50. /**
  51. * @var callback a callback that creates a button URL using the specified model information.
  52. * The signature of the callback should be the same as that of [[createUrl()]].
  53. * If this property is not set, button URLs will be created using [[createUrl()]].
  54. */
  55. public $urlCreator;
  56. /**
  57. * @inheritdoc
  58. */
  59. public function init()
  60. {
  61. parent::init();
  62. $this->initDefaultButtons();
  63. }
  64. /**
  65. * Initializes the default button rendering callbacks
  66. */
  67. protected function initDefaultButtons()
  68. {
  69. if (!isset($this->buttons['view'])) {
  70. $this->buttons['view'] = function ($url, $model) {
  71. return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
  72. 'title' => Yii::t('yii', 'View'),
  73. ]);
  74. };
  75. }
  76. if (!isset($this->buttons['update'])) {
  77. $this->buttons['update'] = function ($url, $model) {
  78. return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
  79. 'title' => Yii::t('yii', 'Update'),
  80. ]);
  81. };
  82. }
  83. if (!isset($this->buttons['delete'])) {
  84. $this->buttons['delete'] = function ($url, $model) {
  85. return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
  86. 'title' => Yii::t('yii', 'Delete'),
  87. 'data-confirm' => Yii::t('yii', 'Are you sure to delete this item?'),
  88. 'data-method' => 'post',
  89. ]);
  90. };
  91. }
  92. }
  93. /**
  94. * Creates a URL for the given action and model.
  95. * This method is called for each button and each row.
  96. * @param string $action the button name (or action ID)
  97. * @param \yii\db\ActiveRecord $model the data model
  98. * @param mixed $key the key associated with the data model
  99. * @param integer $index the current row index
  100. * @return string the created URL
  101. */
  102. public function createUrl($action, $model, $key, $index)
  103. {
  104. if ($this->urlCreator instanceof Closure) {
  105. return call_user_func($this->urlCreator, $action, $model, $key, $index);
  106. } else {
  107. $params = is_array($key) ? $key : ['id' => $key];
  108. $route = $this->controller ? $this->controller . '/' . $action : $action;
  109. return Yii::$app->controller->createUrl($route, $params);
  110. }
  111. }
  112. /**
  113. * @inheritdoc
  114. */
  115. protected function renderDataCellContent($model, $key, $index)
  116. {
  117. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
  118. $name = $matches[1];
  119. if (isset($this->buttons[$name])) {
  120. $url = $this->createUrl($name, $model, $key, $index);
  121. return call_user_func($this->buttons[$name], $url, $model);
  122. } else {
  123. return '';
  124. }
  125. }, $this->template);
  126. }
  127. }