From 3f2e7fa604a1af8a852780d2e9cf06b3baf35b18 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 2 Sep 2013 20:25:39 -0400 Subject: [PATCH] crud wip --- framework/yii/gii/generators/crud/Generator.php | 22 ++- framework/yii/gii/generators/crud/form.php | 6 + .../gii/generators/crud/templates/controller.php | 182 ++++++++++++++++++++- .../yii/gii/generators/crud/templates/model.php | 8 - .../yii/gii/generators/crud/templates/search.php | 8 + .../gii/generators/crud/templates/views/_form.php | 49 ++++++ .../generators/crud/templates/views/_search.php | 38 +++++ .../gii/generators/crud/templates/views/_view.php | 31 ++++ .../gii/generators/crud/templates/views/create.php | 27 +++ .../generators/crud/templates/views/index-grid.php | 73 +++++++++ .../gii/generators/crud/templates/views/index.php | 29 ++++ .../gii/generators/crud/templates/views/update.php | 31 ++++ .../gii/generators/crud/templates/views/view.php | 39 +++++ framework/yii/gii/views/default/view.php | 1 + 14 files changed, 527 insertions(+), 17 deletions(-) delete mode 100644 framework/yii/gii/generators/crud/templates/model.php create mode 100644 framework/yii/gii/generators/crud/templates/search.php create mode 100644 framework/yii/gii/generators/crud/templates/views/_form.php create mode 100644 framework/yii/gii/generators/crud/templates/views/_search.php create mode 100644 framework/yii/gii/generators/crud/templates/views/_view.php create mode 100644 framework/yii/gii/generators/crud/templates/views/create.php create mode 100644 framework/yii/gii/generators/crud/templates/views/index-grid.php create mode 100644 framework/yii/gii/generators/crud/templates/views/index.php create mode 100644 framework/yii/gii/generators/crud/templates/views/update.php create mode 100644 framework/yii/gii/generators/crud/templates/views/view.php diff --git a/framework/yii/gii/generators/crud/Generator.php b/framework/yii/gii/generators/crud/Generator.php index 2b6697d..7029e7c 100644 --- a/framework/yii/gii/generators/crud/Generator.php +++ b/framework/yii/gii/generators/crud/Generator.php @@ -7,6 +7,7 @@ namespace yii\gii\generators\crud; +use yii\base\Model; use yii\db\ActiveRecord; use yii\gii\CodeFile; use yii\web\Controller; @@ -21,6 +22,9 @@ class Generator extends \yii\gii\Generator public $modelClass; public $controllerID; public $baseControllerClass = 'yii\web\Controller'; + public $indexWidgetType = 'grid'; + public $enableSearch = true; + public $searchModelClass; public function getName() { @@ -36,9 +40,9 @@ class Generator extends \yii\gii\Generator public function rules() { return array_merge(parent::rules(), array( - array('modelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'), - array('modelClass, controllerID, baseControllerClass', 'required'), - array('modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), + array('modelClass, searchModelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'), + array('modelClass, searchModelClass, controllerID, baseControllerClass', 'required'), + array('modelClass, searchModelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())), array('controllerID', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'), array('baseControllerClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), @@ -52,6 +56,9 @@ class Generator extends \yii\gii\Generator 'modelClass' => 'Model Class', 'controllerID' => 'Controller ID', 'baseControllerClass' => 'Base Controller Class', + 'indexWidgetType' => 'Widget Used in Index Page', + 'enableSearch' => 'Enable Search', + 'searchModelClass' => 'Search Model Class', )); } @@ -72,6 +79,13 @@ class Generator extends \yii\gii\Generator ', 'baseControllerClass' => 'This is the class that the new CRUD controller class will extend from. You should provide a fully qualified class name, e.g., yii\web\Controller.', + 'indexWidgetType' => 'This is the widget type to be used in the index page to display list of the models. + You may choose either GridView or ListView', + 'enableSearch' => 'Whether to enable the search functionality on the index page. When search is enabled, + a search form will be displayed on the index page, and the index page will display the search results.', + 'searchModelClass' => 'This is the class representing the data being collecting in the search form. + A fully qualified namespaced class name is required, e.g., app\models\PostSearchForm. + This is only used when search is enabled.', ); } @@ -87,7 +101,7 @@ class Generator extends \yii\gii\Generator */ public function stickyAttributes() { - return array('baseControllerClass'); + return array('baseControllerClass', 'indexWidgetType', 'enableSearch'); } /** diff --git a/framework/yii/gii/generators/crud/form.php b/framework/yii/gii/generators/crud/form.php index 0951695..095791f 100644 --- a/framework/yii/gii/generators/crud/form.php +++ b/framework/yii/gii/generators/crud/form.php @@ -8,3 +8,9 @@ echo $form->field($generator, 'modelClass'); echo $form->field($generator, 'controllerID'); echo $form->field($generator, 'baseControllerClass'); +echo $form->field($generator, 'indexWidgetType')->dropDownList(array( + 'grid' => 'GridView', + 'list' => 'ListView', +)); +echo $form->field($generator, 'enableSearch')->checkbox(); +echo $form->field($generator, 'searchModelClass'); diff --git a/framework/yii/gii/generators/crud/templates/controller.php b/framework/yii/gii/generators/crud/templates/controller.php index f372629..47c193f 100644 --- a/framework/yii/gii/generators/crud/templates/controller.php +++ b/framework/yii/gii/generators/crud/templates/controller.php @@ -1,8 +1,180 @@ + + +class controllerClass; ?> extends baseControllerClass."\n"; ?> +{ + /** + * @var string the default layout for the views. Defaults to '//layouts/column2', meaning + * using two-column layout. See 'protected/views/layouts/column2.php'. + */ + public $layout='//layouts/column2'; + + /** + * @return array action filters + */ + public function filters() + { + return array( + 'accessControl', // perform access control for CRUD operations + 'postOnly + delete', // we only allow deletion via POST request + ); + } + + /** + * Specifies the access control rules. + * This method is used by the 'accessControl' filter. + * @return array access control rules + */ + public function accessRules() + { + return array( + array('allow', // allow all users to perform 'index' and 'view' actions + 'actions'=>array('index','view'), + 'users'=>array('*'), + ), + array('allow', // allow authenticated user to perform 'create' and 'update' actions + 'actions'=>array('create','update'), + 'users'=>array('@'), + ), + array('allow', // allow admin user to perform 'admin' and 'delete' actions + 'actions'=>array('admin','delete'), + 'users'=>array('admin'), + ), + array('deny', // deny all users + 'users'=>array('*'), + ), + ); + } + + /** + * Displays a particular model. + * @param integer $id the ID of the model to be displayed + */ + public function actionView($id) + { + $this->render('view',array( + 'model'=>$this->loadModel($id), + )); + } + + /** + * Creates a new model. + * If creation is successful, the browser will be redirected to the 'view' page. + */ + public function actionCreate() + { + $model=new modelClass; ?>; + + // Uncomment the following line if AJAX validation is needed + // $this->performAjaxValidation($model); + + if(isset($_POST['modelClass; ?>'])) + { + $model->attributes=$_POST['modelClass; ?>']; + if($model->save()) + $this->redirect(array('view','id'=>$model->tableSchema->primaryKey; ?>)); + } + + $this->render('create',array( + 'model'=>$model, + )); + } + + /** + * Updates a particular model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id the ID of the model to be updated + */ + public function actionUpdate($id) + { + $model=$this->loadModel($id); + + // Uncomment the following line if AJAX validation is needed + // $this->performAjaxValidation($model); + + if(isset($_POST['modelClass; ?>'])) + { + $model->attributes=$_POST['modelClass; ?>']; + if($model->save()) + $this->redirect(array('view','id'=>$model->tableSchema->primaryKey; ?>)); + } + + $this->render('update',array( + 'model'=>$model, + )); + } + + /** + * Deletes a particular model. + * If deletion is successful, the browser will be redirected to the 'admin' page. + * @param integer $id the ID of the model to be deleted + */ + public function actionDelete($id) + { + $this->loadModel($id)->delete(); + + // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser + if(!isset($_GET['ajax'])) + $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); + } + + /** + * Lists all models. + */ + public function actionIndex() + { + $dataProvider=new CActiveDataProvider('modelClass; ?>'); + $this->render('index',array( + 'dataProvider'=>$dataProvider, + )); + } + + /** + * Manages all models. + */ + public function actionAdmin() + { + $model=new modelClass; ?>('search'); + $model->unsetAttributes(); // clear any default values + if(isset($_GET['modelClass; ?>'])) + $model->attributes=$_GET['modelClass; ?>']; + + $this->render('admin',array( + 'model'=>$model, + )); + } + + /** + * Returns the data model based on the primary key given in the GET variable. + * If the data model is not found, an HTTP exception will be raised. + * @param integer $id the ID of the model to be loaded + * @return modelClass; ?> the loaded model + * @throws CHttpException + */ + public function loadModel($id) + { + $model=modelClass; ?>::model()->findByPk($id); + if($model===null) + throw new CHttpException(404,'The requested page does not exist.'); + return $model; + } + + /** + * Performs the AJAX validation. + * @param modelClass; ?> $model the model to be validated + */ + protected function performAjaxValidation($model) + { + if(isset($_POST['ajax']) && $_POST['ajax']==='class2id($this->modelClass); ?>-form') + { + echo CActiveForm::validate($model); + Yii::app()->end(); + } + } +} diff --git a/framework/yii/gii/generators/crud/templates/model.php b/framework/yii/gii/generators/crud/templates/model.php deleted file mode 100644 index f372629..0000000 --- a/framework/yii/gii/generators/crud/templates/model.php +++ /dev/null @@ -1,8 +0,0 @@ - + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ +/* @var $form CActiveForm */ +?> + +
+ +beginWidget('CActiveForm', array( + 'id'=>'".$this->class2id($this->modelClass)."-form', + // Please note: When you enable ajax validation, make sure the corresponding + // controller action is handling ajax validation correctly. + // There is a call to performAjaxValidation() commented in generated controller code. + // See class documentation of CActiveForm for details on this. + 'enableAjaxValidation'=>false, +)); ?>\n"; ?> + +

Fields with * are required.

+ + errorSummary(\$model); ?>\n"; ?> + +tableSchema->columns as $column) +{ + if($column->autoIncrement) + continue; +?> +
+ generateActiveLabel($this->modelClass,$column)."; ?>\n"; ?> + generateActiveField($this->modelClass,$column)."; ?>\n"; ?> + error(\$model,'{$column->name}'); ?>\n"; ?> +
+ + +
+ isNewRecord ? 'Create' : 'Save'); ?>\n"; ?> +
+ +endWidget(); ?>\n"; ?> + +
\ No newline at end of file diff --git a/framework/yii/gii/generators/crud/templates/views/_search.php b/framework/yii/gii/generators/crud/templates/views/_search.php new file mode 100644 index 0000000..a9679f1 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/_search.php @@ -0,0 +1,38 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ +/* @var $form CActiveForm */ +?> + +
+ +beginWidget('CActiveForm', array( + 'action'=>Yii::app()->createUrl(\$this->route), + 'method'=>'get', +)); ?>\n"; ?> + +tableSchema->columns as $column): ?> +generateInputField($this->modelClass,$column); + if(strpos($field,'password')!==false) + continue; +?> +
+ label(\$model,'{$column->name}'); ?>\n"; ?> + generateActiveField($this->modelClass,$column)."; ?>\n"; ?> +
+ + +
+ \n"; ?> +
+ +endWidget(); ?>\n"; ?> + +
\ No newline at end of file diff --git a/framework/yii/gii/generators/crud/templates/views/_view.php b/framework/yii/gii/generators/crud/templates/views/_view.php new file mode 100644 index 0000000..0f11051 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/_view.php @@ -0,0 +1,31 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $data getModelClass(); ?> */ +?> + +
+ +getAttributeLabel('{$this->tableSchema->primaryKey}')); ?>:\n"; +echo "\t{$this->tableSchema->primaryKey}), array('view', 'id'=>\$data->{$this->tableSchema->primaryKey})); ?>\n\t
\n\n"; +$count=0; +foreach($this->tableSchema->columns as $column) +{ + if($column->isPrimaryKey) + continue; + if(++$count==7) + echo "\tgetAttributeLabel('{$column->name}')); ?>:\n"; + echo "\t{$column->name}); ?>\n\t
\n\n"; +} +if($count>=7) + echo "\t*/ ?>\n"; +?> + +
\ No newline at end of file diff --git a/framework/yii/gii/generators/crud/templates/views/create.php b/framework/yii/gii/generators/crud/templates/views/create.php new file mode 100644 index 0000000..9fd3ccf --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/create.php @@ -0,0 +1,27 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ + +pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + 'Create', +);\n"; +?> + +$this->menu=array( + array('label'=>'List modelClass; ?>', 'url'=>array('index')), + array('label'=>'Manage modelClass; ?>', 'url'=>array('admin')), +); +?> + +

Create modelClass; ?>

+ +renderPartial('_form', array('model'=>\$model)); ?>"; ?> diff --git a/framework/yii/gii/generators/crud/templates/views/index-grid.php b/framework/yii/gii/generators/crud/templates/views/index-grid.php new file mode 100644 index 0000000..1884515 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/index-grid.php @@ -0,0 +1,73 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ + +pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + 'Manage', +);\n"; +?> + +$this->menu=array( + array('label'=>'List modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), +); + +Yii::app()->clientScript->registerScript('search', " +$('.search-button').click(function(){ + $('.search-form').toggle(); + return false; +}); +$('.search-form form').submit(function(){ + $('#class2id($this->modelClass); ?>-grid').yiiGridView('update', { + data: $(this).serialize() + }); + return false; +}); +"); +?> + +

Manage pluralize($this->class2name($this->modelClass)); ?>

+ +

+You may optionally enter a comparison operator (<, <=, >, >=, <> +or =) at the beginning of each of your search values to specify how the comparison should be done. +

+ +'search-button')); ?>"; ?> + + + + $this->widget('zii.widgets.grid.CGridView', array( + 'id'=>'class2id($this->modelClass); ?>-grid', + 'dataProvider'=>$model->search(), + 'filter'=>$model, + 'columns'=>array( +tableSchema->columns as $column) +{ + if(++$count==7) + echo "\t\t/*\n"; + echo "\t\t'".$column->name."',\n"; +} +if($count>=7) + echo "\t\t*/\n"; +?> + array( + 'class'=>'CButtonColumn', + ), + ), +)); ?> diff --git a/framework/yii/gii/generators/crud/templates/views/index.php b/framework/yii/gii/generators/crud/templates/views/index.php new file mode 100644 index 0000000..a115251 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/index.php @@ -0,0 +1,29 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $dataProvider CActiveDataProvider */ + +pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label', +);\n"; +?> + +$this->menu=array( + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), + array('label'=>'Manage modelClass; ?>', 'url'=>array('admin')), +); +?> + +

+ + $this->widget('zii.widgets.CListView', array( + 'dataProvider'=>$dataProvider, + 'itemView'=>'_view', +)); ?> diff --git a/framework/yii/gii/generators/crud/templates/views/update.php b/framework/yii/gii/generators/crud/templates/views/update.php new file mode 100644 index 0000000..4adc72d --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/update.php @@ -0,0 +1,31 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ + +guessNameColumn($this->tableSchema->columns); +$label=$this->pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + \$model->{$nameColumn}=>array('view','id'=>\$model->{$this->tableSchema->primaryKey}), + 'Update', +);\n"; +?> + +$this->menu=array( + array('label'=>'List modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), + array('label'=>'View modelClass; ?>', 'url'=>array('view', 'id'=>$model->tableSchema->primaryKey; ?>)), + array('label'=>'Manage modelClass; ?>', 'url'=>array('admin')), +); +?> + +

Update modelClass." {$this->tableSchema->primaryKey}; ?>"; ?>

+ +renderPartial('_form', array('model'=>\$model)); ?>"; ?> \ No newline at end of file diff --git a/framework/yii/gii/generators/crud/templates/views/view.php b/framework/yii/gii/generators/crud/templates/views/view.php new file mode 100644 index 0000000..3363130 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/views/view.php @@ -0,0 +1,39 @@ + + +/* @var $this getControllerClass(); ?> */ +/* @var $model getModelClass(); ?> */ + +guessNameColumn($this->tableSchema->columns); +$label=$this->pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + \$model->{$nameColumn}, +);\n"; +?> + +$this->menu=array( + array('label'=>'List modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), + array('label'=>'Update modelClass; ?>', 'url'=>array('update', 'id'=>$model->tableSchema->primaryKey; ?>)), + array('label'=>'Delete modelClass; ?>', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->tableSchema->primaryKey; ?>),'confirm'=>'Are you sure you want to delete this item?')), + array('label'=>'Manage modelClass; ?>', 'url'=>array('admin')), +); +?> + +

View modelClass." #{$this->tableSchema->primaryKey}; ?>"; ?>

+ + $this->widget('zii.widgets.CDetailView', array( + 'data'=>$model, + 'attributes'=>array( +tableSchema->columns as $column) + echo "\t\t'".$column->name."',\n"; +?> + ), +)); ?> diff --git a/framework/yii/gii/views/default/view.php b/framework/yii/gii/views/default/view.php index c61d5d0..bf05e84 100644 --- a/framework/yii/gii/views/default/view.php +++ b/framework/yii/gii/views/default/view.php @@ -30,6 +30,7 @@ foreach ($generator->templates as $name => $path) { "$id-generator", + 'successCssClass' => '', 'fieldConfig' => array('class' => ActiveField::className()), )); ?>