From 7f4e02cb4599cfc17f4f3c3a64e74f452a3994f7 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 3 Sep 2013 19:36:23 -0400 Subject: [PATCH] crud generator WIP --- .../yii/gii/generators/controller/Generator.php | 2 +- framework/yii/gii/generators/crud/Generator.php | 85 +++++++++++++++++++--- .../generators/crud/templates/views/index-grid.php | 73 ------------------- 3 files changed, 77 insertions(+), 83 deletions(-) delete mode 100644 framework/yii/gii/generators/crud/templates/views/index-grid.php diff --git a/framework/yii/gii/generators/controller/Generator.php b/framework/yii/gii/generators/controller/Generator.php index c57b2b2..9de9c17 100644 --- a/framework/yii/gii/generators/controller/Generator.php +++ b/framework/yii/gii/generators/controller/Generator.php @@ -78,7 +78,7 @@ class Generator extends \yii\gii\Generator 'baseClass' => 'Base Class', 'controller' => 'Controller ID', 'actions' => 'Action IDs', - 'ns' => 'Namespace', + 'ns' => 'Controller Namespace', ); } diff --git a/framework/yii/gii/generators/crud/Generator.php b/framework/yii/gii/generators/crud/Generator.php index 7029e7c..983dbc7 100644 --- a/framework/yii/gii/generators/crud/Generator.php +++ b/framework/yii/gii/generators/crud/Generator.php @@ -7,9 +7,11 @@ namespace yii\gii\generators\crud; +use Yii; use yii\base\Model; use yii\db\ActiveRecord; use yii\gii\CodeFile; +use yii\helpers\Inflector; use yii\web\Controller; /** @@ -41,12 +43,15 @@ class Generator extends \yii\gii\Generator { return array_merge(parent::rules(), array( array('modelClass, searchModelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'), - array('modelClass, searchModelClass, controllerID, baseControllerClass', 'required'), + array('modelClass, 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.'), array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())), + array('enableSearch', 'boolean'), + array('indexWidgetType', 'in', 'range' => array('grid', 'list')), + array('searchModelClass', 'validateSearchModelClass'), )); } @@ -104,6 +109,13 @@ class Generator extends \yii\gii\Generator return array('baseControllerClass', 'indexWidgetType', 'enableSearch'); } + public function validateSearchModelClass() + { + if ($this->enableSearch && empty($this->searchModelClass)) { + $this->addError('searchModelClass', 'Search Model Class cannot be empty.'); + } + } + /** * @inheritdoc */ @@ -111,20 +123,75 @@ class Generator extends \yii\gii\Generator { $files = array(); $files[] = new CodeFile( - $this->controllerFile, + $this->getControllerFile(), $this->render('controller.php') ); + $viewPath = $this->getViewPath(); - $files = scandir($this->getTemplatePath()); - foreach ($files as $file) { - if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') { - $files[] = new CodeFile( - $this->viewPath . DIRECTORY_SEPARATOR . $file, - $this->render($templatePath . '/' . $file) - ); + $templatePath = $this->getTemplatePath() . '/views'; + foreach (scandir($templatePath) as $file) { + if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { + $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file")); } } + if ($this->enableSearch) { + + } + return $files; } + + + /** + * @return string the controller class name without the namespace part. + */ + public function getControllerClass() + { + return Inflector::id2camel($this->getControllerID()) . 'Controller'; + } + + /** + * @return string the controller ID (without the module ID prefix) + */ + public function getControllerID() + { + if (($pos = strrpos($this->controllerID, '/')) !== false) { + return substr($this->controllerID, $pos + 1); + } else { + return $this->controllerID; + } + } + + /** + * @return \yii\base\Module the module that the new controller belongs to + */ + public function getModule() + { + if (($pos = strpos($this->controllerID, '/')) !== false) { + $id = substr($this->controllerID, 0, $pos); + if (($module = Yii::$app->getModule($id)) !== null) { + return $module; + } + } + return Yii::$app; + } + + /** + * @return string the controller class file path + */ + public function getControllerFile() + { + $module = $this->getModule(); + return $module->getControllerPath() . '/' . $this->getControllerClass() . '.php'; + } + + /** + * @return string the action view file path + */ + public function getViewPath() + { + $module = $this->getModule(); + return $module->getViewPath() . '/' . $this->getControllerID() ; + } } diff --git a/framework/yii/gii/generators/crud/templates/views/index-grid.php b/framework/yii/gii/generators/crud/templates/views/index-grid.php deleted file mode 100644 index 1884515..0000000 --- a/framework/yii/gii/generators/crud/templates/views/index-grid.php +++ /dev/null @@ -1,73 +0,0 @@ - - -/* @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', - ), - ), -)); ?>