diff options
Diffstat (limited to 'framework/gii/generators/form')
| -rw-r--r-- | framework/gii/generators/form/FormCode.php | 94 | ||||
| -rw-r--r-- | framework/gii/generators/form/FormGenerator.php | 6 | ||||
| -rw-r--r-- | framework/gii/generators/form/templates/default/action.php | 33 | ||||
| -rw-r--r-- | framework/gii/generators/form/templates/default/form.php | 34 | ||||
| -rw-r--r-- | framework/gii/generators/form/views/index.php | 49 |
5 files changed, 216 insertions, 0 deletions
diff --git a/framework/gii/generators/form/FormCode.php b/framework/gii/generators/form/FormCode.php new file mode 100644 index 0000000..e1def8d --- /dev/null +++ b/framework/gii/generators/form/FormCode.php @@ -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."); + else if(!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(); + } +}
\ No newline at end of file diff --git a/framework/gii/generators/form/FormGenerator.php b/framework/gii/generators/form/FormGenerator.php new file mode 100644 index 0000000..a9ec2df --- /dev/null +++ b/framework/gii/generators/form/FormGenerator.php @@ -0,0 +1,6 @@ +<?php + +class FormGenerator extends CCodeGenerator +{ + public $codeModel='gii.generators.form.FormCode'; +}
\ No newline at end of file diff --git a/framework/gii/generators/form/templates/default/action.php b/framework/gii/generators/form/templates/default/action.php new file mode 100644 index 0000000..4e88e5b --- /dev/null +++ b/framework/gii/generators/form/templates/default/action.php @@ -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)); +}
\ No newline at end of file diff --git a/framework/gii/generators/form/templates/default/form.php b/framework/gii/generators/form/templates/default/form.php new file mode 100644 index 0000000..1405618 --- /dev/null +++ b/framework/gii/generators/form/templates/default/form.php @@ -0,0 +1,34 @@ +<?php +/** + * This is the template for generating a form script file. + * The following variables are available in this template: + * - $this: the FormCode object + */ +?> +<div class="form"> + +<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array( + 'id'=>'".$this->class2id($this->modelClass).'-'.basename($this->viewName)."-form', + '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 -->
\ No newline at end of file diff --git a/framework/gii/generators/form/views/index.php b/framework/gii/generators/form/views/index.php new file mode 100644 index 0000000..8dcdf33 --- /dev/null +++ b/framework/gii/generators/form/views/index.php @@ -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(); ?> |
