From cd969509e6be6f89f2eda7b85727c073b06c1cd8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 24 Sep 2013 21:46:31 -0400 Subject: [PATCH] Added ActionColumn. crud generator WIP. --- .../gii/generators/crud/templates/controller.php | 13 +++ .../gii/generators/crud/templates/views/index.php | 4 + .../gii/generators/crud/templates/views/view.php | 6 +- framework/yii/grid/ActionColumn.php | 102 +++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 framework/yii/grid/ActionColumn.php diff --git a/framework/yii/gii/generators/crud/templates/controller.php b/framework/yii/gii/generators/crud/templates/controller.php index fd33eda..f53f819 100644 --- a/framework/yii/gii/generators/crud/templates/controller.php +++ b/framework/yii/gii/generators/crud/templates/controller.php @@ -28,12 +28,25 @@ use searchModelClass, '\\'); ?>; use yii\data\ActiveDataProvider; use baseControllerClass, '\\'); ?>; use yii\web\HttpException; +use yii\web\VerbFilter; /** * implements the CRUD actions for model. */ class extends baseControllerClass) . "\n"; ?> { + public function behaviors() + { + return array( + 'verbs' => array( + 'class' => VerbFilter::className(), + 'actions' => array( + 'delete' => array('post'), + ), + ), + ); + } + /** * Lists all models. * @return mixed diff --git a/framework/yii/gii/generators/crud/templates/views/index.php b/framework/yii/gii/generators/crud/templates/views/index.php index 98d35b3..5f55e84 100644 --- a/framework/yii/gii/generators/crud/templates/views/index.php +++ b/framework/yii/gii/generators/crud/templates/views/index.php @@ -54,6 +54,10 @@ foreach ($generator->getTableSchema()->columns as $column) { } } ?> + + array( + 'class' => 'yii\grid\ActionColumn', + ), ), )); ?> diff --git a/framework/yii/gii/generators/crud/templates/views/view.php b/framework/yii/gii/generators/crud/templates/views/view.php index d08ec23..a3b6cad 100644 --- a/framework/yii/gii/generators/crud/templates/views/view.php +++ b/framework/yii/gii/generators/crud/templates/views/view.php @@ -31,7 +31,11 @@ $this->params['breadcrumbs'][] = $this->title;
echo Html::a('Update', array('update', ), array('class' => 'btn btn-danger')); ?> - echo Html::a('Delete', array('delete', ), array('class' => 'btn btn-danger')); ?> + echo Html::a('Delete', array('delete', ), array( + 'class' => 'btn btn-danger', + 'data-confirm' => Yii::t('app', 'Are you sure to delete this item?'), + 'data-method' => 'post', + )); ?>
echo DetailView::widget(array( diff --git a/framework/yii/grid/ActionColumn.php b/framework/yii/grid/ActionColumn.php new file mode 100644 index 0000000..72fafb6 --- /dev/null +++ b/framework/yii/grid/ActionColumn.php @@ -0,0 +1,102 @@ + + * @since 2.0 + */ +class ActionColumn extends Column +{ + public $template = '{view} {update} {delete}'; + public $buttons = array(); + 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'); + return Html::a('', $url, array( + 'title' => Yii::t('yii', 'View'), + )); + }; + } + if (!isset($this->buttons['update'])) { + $this->buttons['update'] = function ($model, $column) { + /** @var ActionColumn $column */ + $url = $column->createUrl($model, 'update'); + return Html::a('', $url, array( + 'title' => Yii::t('yii', 'Update'), + )); + }; + } + if (!isset($this->buttons['delete'])) { + $this->buttons['delete'] = function ($model, $column) { + /** @var ActionColumn $column */ + $url = $column->createUrl($model, 'delete'); + return Html::a('', $url, array( + 'title' => Yii::t('yii', 'Delete'), + 'data-confirm' => Yii::t('yii', 'Are you sure to delete this item?'), + 'data-method' => 'post', + )); + }; + } + } + + /** + * @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 { + $route = Inflector::camel2id(StringHelper::basename(get_class($model))) . '/' . $action; + $params = $model->getPrimaryKey(true); + if (count($params) === 1) { + $params = array('id' => reset($params)); + } + return Yii::$app->getUrlManager()->createUrl($route, $params); + } + } + + /** + * 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) + { + $column = $this; + return preg_replace_callback('/\\{(\w+)\\}/', function ($matches) use ($model, $column) { + $name = $matches[1]; + if (isset($column->buttons[$name])) { + return call_user_func($column->buttons[$name], $model, $column); + } else { + return ''; + } + }, $this->template); + } +}