Browse Source

crud generator WIP

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
7f4e02cb45
  1. 2
      framework/yii/gii/generators/controller/Generator.php
  2. 85
      framework/yii/gii/generators/crud/Generator.php
  3. 73
      framework/yii/gii/generators/crud/templates/views/index-grid.php

2
framework/yii/gii/generators/controller/Generator.php

@ -78,7 +78,7 @@ class Generator extends \yii\gii\Generator
'baseClass' => 'Base Class', 'baseClass' => 'Base Class',
'controller' => 'Controller ID', 'controller' => 'Controller ID',
'actions' => 'Action IDs', 'actions' => 'Action IDs',
'ns' => 'Namespace', 'ns' => 'Controller Namespace',
); );
} }

85
framework/yii/gii/generators/crud/Generator.php

@ -7,9 +7,11 @@
namespace yii\gii\generators\crud; namespace yii\gii\generators\crud;
use Yii;
use yii\base\Model; use yii\base\Model;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector;
use yii\web\Controller; use yii\web\Controller;
/** /**
@ -41,12 +43,15 @@ class Generator extends \yii\gii\Generator
{ {
return array_merge(parent::rules(), array( return array_merge(parent::rules(), array(
array('modelClass, searchModelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'), 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, searchModelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())), array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())),
array('controllerID', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'), 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', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())), 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'); return array('baseControllerClass', 'indexWidgetType', 'enableSearch');
} }
public function validateSearchModelClass()
{
if ($this->enableSearch && empty($this->searchModelClass)) {
$this->addError('searchModelClass', 'Search Model Class cannot be empty.');
}
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
@ -111,20 +123,75 @@ class Generator extends \yii\gii\Generator
{ {
$files = array(); $files = array();
$files[] = new CodeFile( $files[] = new CodeFile(
$this->controllerFile, $this->getControllerFile(),
$this->render('controller.php') $this->render('controller.php')
); );
$viewPath = $this->getViewPath();
$files = scandir($this->getTemplatePath()); $templatePath = $this->getTemplatePath() . '/views';
foreach ($files as $file) { foreach (scandir($templatePath) as $file) {
if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') { if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$files[] = new CodeFile( $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file"));
$this->viewPath . DIRECTORY_SEPARATOR . $file,
$this->render($templatePath . '/' . $file)
);
} }
} }
if ($this->enableSearch) {
}
return $files; 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() ;
}
} }

73
framework/yii/gii/generators/crud/templates/views/index-grid.php

@ -1,73 +0,0 @@
<?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',
),
),
)); ?>
Loading…
Cancel
Save