FormCode.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class FormCode extends CCodeModel
  3. {
  4. public $model;
  5. public $viewPath='application.views';
  6. public $viewName;
  7. public $scenario;
  8. private $_modelClass;
  9. public function rules()
  10. {
  11. return array_merge(parent::rules(), array(
  12. array('model, viewName, scenario', 'filter', 'filter'=>'trim'),
  13. array('model, viewName, viewPath', 'required'),
  14. array('model, viewPath', 'match', 'pattern'=>'/^\w+[\.\w+]*$/', 'message'=>'{attribute} should only contain word characters and dots.'),
  15. array('viewName', 'match', 'pattern'=>'/^\w+[\\/\w+]*$/', 'message'=>'{attribute} should only contain word characters and slashes.'),
  16. array('model', 'validateModel'),
  17. array('viewPath', 'validateViewPath'),
  18. array('scenario', 'match', 'pattern'=>'/^\w+$/', 'message'=>'{attribute} should only contain word characters.'),
  19. array('viewPath', 'sticky'),
  20. ));
  21. }
  22. public function attributeLabels()
  23. {
  24. return array_merge(parent::attributeLabels(), array(
  25. 'model'=>'Model Class',
  26. 'viewName'=>'View Name',
  27. 'viewPath'=>'View Path',
  28. 'scenario'=>'Scenario',
  29. ));
  30. }
  31. public function requiredTemplates()
  32. {
  33. return array(
  34. 'form.php',
  35. 'action.php',
  36. );
  37. }
  38. public function successMessage()
  39. {
  40. $output=<<<EOD
  41. <p>The form has been generated successfully.</p>
  42. <p>You may add the following code in an appropriate controller class to invoke the view:</p>
  43. EOD;
  44. $code="<?php\n".$this->render($this->templatePath.'/action.php');
  45. return $output.highlight_string($code,true);
  46. }
  47. public function validateModel($attribute,$params)
  48. {
  49. if($this->hasErrors('model'))
  50. return;
  51. $class=@Yii::import($this->model,true);
  52. if(!is_string($class) || !$this->classExists($class))
  53. $this->addError('model', "Class '{$this->model}' does not exist or has syntax error.");
  54. elseif(!is_subclass_of($class,'CModel'))
  55. $this->addError('model', "'{$this->model}' must extend from CModel.");
  56. else
  57. $this->_modelClass=$class;
  58. }
  59. public function validateViewPath($attribute,$params)
  60. {
  61. if($this->hasErrors('viewPath'))
  62. return;
  63. if(Yii::getPathOfAlias($this->viewPath)===false)
  64. $this->addError('viewPath','View Path must be a valid path alias.');
  65. }
  66. public function prepare()
  67. {
  68. $templatePath=$this->templatePath;
  69. $this->files[]=new CCodeFile(
  70. Yii::getPathOfAlias($this->viewPath).'/'.$this->viewName.'.php',
  71. $this->render($templatePath.'/form.php')
  72. );
  73. }
  74. public function getModelClass()
  75. {
  76. return $this->_modelClass;
  77. }
  78. public function getModelAttributes()
  79. {
  80. $model=new $this->_modelClass($this->scenario);
  81. return $model->getSafeAttributeNames();
  82. }
  83. }