Commit 7f4e02cb by Qiang Xue

crud generator WIP

parent 3f2e7fa6
......@@ -78,7 +78,7 @@ class Generator extends \yii\gii\Generator
'baseClass' => 'Base Class',
'controller' => 'Controller ID',
'actions' => 'Action IDs',
'ns' => 'Namespace',
'ns' => 'Controller Namespace',
);
}
......
......@@ -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() ;
}
}
<?php
/**
* The following variables are available in this template:
* - $this: the CrudCode object
*/
?>
<?php echo "<?php\n"; ?>
/* @var $this <?php echo $this->getControllerClass(); ?> */
/* @var $model <?php echo $this->getModelClass(); ?> */
<?php
$label=$this->pluralize($this->class2name($this->modelClass));
echo "\$this->breadcrumbs=array(
'$label'=>array('index'),
'Manage',
);\n";
?>
$this->menu=array(
array('label'=>'List <?php echo $this->modelClass; ?>', 'url'=>array('index')),
array('label'=>'Create <?php echo $this->modelClass; ?>', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#<?php echo $this->class2id($this->modelClass); ?>-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage <?php echo $this->pluralize($this->class2name($this->modelClass)); ?></h1>
<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo "<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>"; ?>
<div class="search-form" style="display:none">
<?php echo "<?php \$this->renderPartial('_search',array(
'model'=>\$model,
)); ?>\n"; ?>
</div><!-- search-form -->
<?php echo "<?php"; ?> $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'<?php echo $this->class2id($this->modelClass); ?>-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
<?php
$count=0;
foreach($this->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',
),
),
)); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment