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/search.php
similarity index 100%
rename from framework/yii/gii/generators/crud/templates/model.php
rename to framework/yii/gii/generators/crud/templates/search.php
diff --git a/framework/yii/gii/generators/crud/templates/views/_form.php b/framework/yii/gii/generators/crud/templates/views/_form.php
new file mode 100644
index 0000000..caf6f2a
--- /dev/null
+++ b/framework/yii/gii/generators/crud/templates/views/_form.php
@@ -0,0 +1,49 @@
+
+
+/* @var $this getControllerClass(); ?> */
+/* @var $model getModelClass(); ?> */
+/* @var $form CActiveForm */
+?>
+
+
Fields with * are required.
+ + errorSummary(\$model); ?>\n"; ?> + +tableSchema->columns as $column) +{ + if($column->autoIncrement) + continue; +?> ++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')), +); +?> + +