ActionColumn.php 2.69 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 35 36 37
	public $urlCreator;

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

	protected function initDefaultButtons()
	{
		if (!isset($this->buttons['view'])) {
			$this->buttons['view'] = function ($model, $column) {
				/** @var ActionColumn $column */
				$url = $column->createUrl($model, '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 44 45 46
			};
		}
		if (!isset($this->buttons['update'])) {
			$this->buttons['update'] = function ($model, $column) {
				/** @var ActionColumn $column */
				$url = $column->createUrl($model, '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 53 54 55
			};
		}
		if (!isset($this->buttons['delete'])) {
			$this->buttons['delete'] = function ($model, $column) {
				/** @var ActionColumn $column */
				$url = $column->createUrl($model, '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 67 68 69 70 71 72 73 74 75 76
			};
		}
	}

	/**
	 * @param \yii\db\ActiveRecord $model
	 * @param string $action
	 * @return string
	 */
	public function createUrl($model, $action)
	{
		if ($this->urlCreator instanceof Closure) {
			return call_user_func($this->urlCreator, $model, $action);
		} else {
			$params = $model->getPrimaryKey(true);
			if (count($params) === 1) {
Alexander Makarov committed
77
				$params = ['id' => reset($params)];
Qiang Xue committed
78
			}
79
			return Yii::$app->controller->createUrl($action, $params);
Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90
		}
	}

	/**
	 * Renders the data cell content.
	 * @param mixed $model the data model
	 * @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]].
	 * @return string the rendering result
	 */
	protected function renderDataCellContent($model, $index)
	{
resurtm committed
91
		return preg_replace_callback('/\\{(\w+)\\}/', function ($matches) use ($model) {
Qiang Xue committed
92
			$name = $matches[1];
93 94
			if (isset($this->buttons[$name])) {
				return call_user_func($this->buttons[$name], $model, $this);
Qiang Xue committed
95 96 97 98 99 100
			} else {
				return '';
			}
		}, $this->template);
	}
}