ActionColumn.php 2.64 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\grid;

use Yii;
use Closure;
use yii\helpers\Html;

/**
15 16
 * ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
 *
Qiang Xue committed
17 18 19 20 21 22
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActionColumn extends Column
{
	public $template = '{view} {update} {delete}';
Alexander Makarov committed
23
	public $buttons = [];
Qiang Xue committed
24 25 26 27 28 29 30 31 32 33 34
	public $urlCreator;

	public function init()
	{
		parent::init();
		$this->initDefaultButtons();
	}

	protected function initDefaultButtons()
	{
		if (!isset($this->buttons['view'])) {
35
			$this->buttons['view'] = function ($model, $key, $index, $column) {
Qiang Xue committed
36
				/** @var ActionColumn $column */
37
				$url = $column->createUrl($model, $key, $index, 'view');
Alexander Makarov committed
38
				return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
Qiang Xue committed
39
					'title' => Yii::t('yii', 'View'),
Alexander Makarov committed
40
				]);
Qiang Xue committed
41 42 43
			};
		}
		if (!isset($this->buttons['update'])) {
44
			$this->buttons['update'] = function ($model, $key, $index, $column) {
Qiang Xue committed
45
				/** @var ActionColumn $column */
46
				$url = $column->createUrl($model, $key, $index, 'update');
Alexander Makarov committed
47
				return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
Qiang Xue committed
48
					'title' => Yii::t('yii', 'Update'),
Alexander Makarov committed
49
				]);
Qiang Xue committed
50 51 52
			};
		}
		if (!isset($this->buttons['delete'])) {
53
			$this->buttons['delete'] = function ($model, $key, $index, $column) {
Qiang Xue committed
54
				/** @var ActionColumn $column */
55
				$url = $column->createUrl($model, $key, $index, 'delete');
Alexander Makarov committed
56
				return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
Qiang Xue committed
57 58 59
					'title' => Yii::t('yii', 'Delete'),
					'data-confirm' => Yii::t('yii', 'Are you sure to delete this item?'),
					'data-method' => 'post',
Alexander Makarov committed
60
				]);
Qiang Xue committed
61 62 63 64 65 66
			};
		}
	}

	/**
	 * @param \yii\db\ActiveRecord $model
67 68
	 * @param mixed $key the key associated with the data model
	 * @param integer $index
Qiang Xue committed
69 70 71
	 * @param string $action
	 * @return string
	 */
72
	public function createUrl($model, $key, $index, $action)
Qiang Xue committed
73 74
	{
		if ($this->urlCreator instanceof Closure) {
75
			return call_user_func($this->urlCreator, $model, $key, $index, $action);
Qiang Xue committed
76
		} else {
77
			$params = is_array($key) ? $key : ['id' => $key];
78
			return Yii::$app->controller->createUrl($action, $params);
Qiang Xue committed
79 80 81 82
		}
	}

	/**
83
	 * @inheritdoc
Qiang Xue committed
84
	 */
85
	protected function renderDataCellContent($model, $key, $index)
Qiang Xue committed
86
	{
87
		return preg_replace_callback('/\\{(\w+)\\}/', function ($matches) use ($model, $key, $index) {
Qiang Xue committed
88
			$name = $matches[1];
89
			if (isset($this->buttons[$name])) {
90
				return call_user_func($this->buttons[$name], $model, $key, $index, $this);
Qiang Xue committed
91 92 93 94 95 96
			} else {
				return '';
			}
		}, $this->template);
	}
}