1
0

Added new (clean) yii boilerplate

This commit is contained in:
2014-05-13 12:40:42 +02:00
parent 1d6d975a16
commit 99d29b432b
1983 changed files with 653465 additions and 17 deletions

View File

@@ -0,0 +1,94 @@
<?php
class FormCode extends CCodeModel
{
public $model;
public $viewPath='application.views';
public $viewName;
public $scenario;
private $_modelClass;
public function rules()
{
return array_merge(parent::rules(), array(
array('model, viewName, scenario', 'filter', 'filter'=>'trim'),
array('model, viewName, viewPath', 'required'),
array('model, viewPath', 'match', 'pattern'=>'/^\w+[\.\w+]*$/', 'message'=>'{attribute} should only contain word characters and dots.'),
array('viewName', 'match', 'pattern'=>'/^\w+[\\/\w+]*$/', 'message'=>'{attribute} should only contain word characters and slashes.'),
array('model', 'validateModel'),
array('viewPath', 'validateViewPath'),
array('scenario', 'match', 'pattern'=>'/^\w+$/', 'message'=>'{attribute} should only contain word characters.'),
array('viewPath', 'sticky'),
));
}
public function attributeLabels()
{
return array_merge(parent::attributeLabels(), array(
'model'=>'Model Class',
'viewName'=>'View Name',
'viewPath'=>'View Path',
'scenario'=>'Scenario',
));
}
public function requiredTemplates()
{
return array(
'form.php',
'action.php',
);
}
public function successMessage()
{
$output=<<<EOD
<p>The form has been generated successfully.</p>
<p>You may add the following code in an appropriate controller class to invoke the view:</p>
EOD;
$code="<?php\n".$this->render($this->templatePath.'/action.php');
return $output.highlight_string($code,true);
}
public function validateModel($attribute,$params)
{
if($this->hasErrors('model'))
return;
$class=@Yii::import($this->model,true);
if(!is_string($class) || !$this->classExists($class))
$this->addError('model', "Class '{$this->model}' does not exist or has syntax error.");
elseif(!is_subclass_of($class,'CModel'))
$this->addError('model', "'{$this->model}' must extend from CModel.");
else
$this->_modelClass=$class;
}
public function validateViewPath($attribute,$params)
{
if($this->hasErrors('viewPath'))
return;
if(Yii::getPathOfAlias($this->viewPath)===false)
$this->addError('viewPath','View Path must be a valid path alias.');
}
public function prepare()
{
$templatePath=$this->templatePath;
$this->files[]=new CCodeFile(
Yii::getPathOfAlias($this->viewPath).'/'.$this->viewName.'.php',
$this->render($templatePath.'/form.php')
);
}
public function getModelClass()
{
return $this->_modelClass;
}
public function getModelAttributes()
{
$model=new $this->_modelClass($this->scenario);
return $model->getSafeAttributeNames();
}
}

View File

@@ -0,0 +1,6 @@
<?php
class FormGenerator extends CCodeGenerator
{
public $codeModel='gii.generators.form.FormCode';
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* This is the template for generating the action script for the form.
* - $this: the CrudCode object
*/
?>
<?php
$viewName=basename($this->viewName);
?>
public function action<?php echo ucfirst(trim($viewName,'_')); ?>()
{
$model=new <?php echo $this->modelClass; ?><?php echo empty($this->scenario) ? '' : "('{$this->scenario}')"; ?>;
// uncomment the following code to enable ajax-based validation
/*
if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($this->modelClass); ?>-<?php echo $viewName; ?>-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
*/
if(isset($_POST['<?php echo $this->modelClass; ?>']))
{
$model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
if($model->validate())
{
// form inputs are valid, do something here
return;
}
}
$this->render('<?php echo $viewName; ?>',array('model'=>$model));
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* This is the template for generating a form script file.
* The following variables are available in this template:
* - $this: the FormCode object
*/
?>
<?php echo "<?php\n"; ?>
/* @var $this <?php echo $this->getModelClass(); ?>Controller */
/* @var $model <?php echo $this->getModelClass(); ?> */
/* @var $form CActiveForm */
?>
<div class="form">
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array(
'id'=>'".$this->class2id($this->modelClass).'-'.basename($this->viewName)."-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// See class documentation of CActiveForm for details on this,
// you need to use the performAjaxValidation()-method described there.
'enableAjaxValidation'=>false,
)); ?>\n"; ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo "<?php echo \$form->errorSummary(\$model); ?>\n"; ?>
<?php foreach($this->getModelAttributes() as $attribute): ?>
<div class="row">
<?php echo "<?php echo \$form->labelEx(\$model,'$attribute'); ?>\n"; ?>
<?php echo "<?php echo \$form->textField(\$model,'$attribute'); ?>\n"; ?>
<?php echo "<?php echo \$form->error(\$model,'$attribute'); ?>\n"; ?>
</div>
<?php endforeach; ?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton('Submit'); ?>\n"; ?>
</div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?>
</div><!-- form -->

View File

@@ -0,0 +1,49 @@
<h1>Form Generator</h1>
<p>This generator generates a view script file that displays a form to collect input for the specified model class.</p>
<?php $form=$this->beginWidget('CCodeForm', array('model'=>$model)); ?>
<div class="row">
<?php echo $form->labelEx($model,'model'); ?>
<?php echo $form->textField($model,'model', array('size'=>65)); ?>
<div class="tooltip">
Model class is case-sensitive. It can be either a class name (e.g. <code>Post</code>)
or the path alias of the class file (e.g. <code>application.models.LoginForm</code>).
Note that if the former, the class must be auto-loadable.
</div>
<?php echo $form->error($model,'model'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'viewName'); ?>
<?php echo $form->textField($model,'viewName', array('size'=>65)); ?>
<div class="tooltip">
This refers to the name of the view script to be generated, for example,
<code>site/contact</code>, <code>user/login</code>. The actual view script file will be generated
under the View Path specified below.
</div>
<?php echo $form->error($model,'viewName'); ?>
</div>
<div class="row sticky">
<?php echo $form->labelEx($model,'viewPath'); ?>
<?php echo $form->textField($model,'viewPath', array('size'=>65)); ?>
<div class="tooltip">
This refers to the directory that the new view script file should be generated under.
It should be specified in the form of a path alias, for example, <code>application.views</code>,
<code>mymodule.views</code>.
</div>
<?php echo $form->error($model,'viewPath'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'scenario'); ?>
<?php echo $form->textField($model,'scenario', array('size'=>65)); ?>
<div class="tooltip">
This refers to the scenario in which the model should be used to collect user input.
For example, a <code>User</code> model can be used in both <code>login</code> and <code>register</code> scenarios.
To create a form for the login purpose, the scenario should be specified as <code>login</code>.
Leave this empty if the model does not need to differentiate scenarios.
</div>
<?php echo $form->error($model,'scenario'); ?>
</div>
<?php $this->endWidget(); ?>