FormCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * FormCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * FormCommand generates a form view based on a specified model.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.cli.commands.shell
  15. * @since 1.0
  16. */
  17. class FormCommand extends CConsoleCommand
  18. {
  19. /**
  20. * @var string the directory that contains templates for the form command.
  21. * Defaults to null, meaning using 'framework/cli/views/shell/form'.
  22. * If you set this path and some views are missing in the directory,
  23. * the default views will be used.
  24. */
  25. public $templatePath;
  26. public function getHelp()
  27. {
  28. return <<<EOD
  29. USAGE
  30. form <model-class> <view-name> [scenario]
  31. DESCRIPTION
  32. This command generates a form view that can be used to collect inputs
  33. for the specified model.
  34. PARAMETERS
  35. * model-class: required, model class. This can be either the name of
  36. the model class (e.g. 'ContactForm') or the path alias of the model
  37. class file (e.g. 'application.models.ContactForm'). The former can
  38. be used only if the class can be autoloaded.
  39. * view-name: required, the name of the view to be generated. This should
  40. be the path alias of the view script (e.g. 'application.views.site.contact').
  41. * scenario: optional, the name of the scenario in which the model is used
  42. (e.g. 'update', 'login'). This determines which model attributes the
  43. generated form view will be used to collect user inputs for. If this
  44. is not provided, the scenario will be assumed to be '' (empty string).
  45. EXAMPLES
  46. * Generates the view script for the 'ContactForm' model:
  47. form ContactForm application.views.site.contact
  48. EOD;
  49. }
  50. /**
  51. * Execute the action.
  52. * @param array $args command line parameters specific for this command
  53. * @return integer|null non zero application exit code for help or null on success
  54. */
  55. public function run($args)
  56. {
  57. if(!isset($args[0],$args[1]))
  58. {
  59. echo "Error: both model class and view name are required.\n";
  60. echo $this->getHelp();
  61. return 1;
  62. }
  63. $scenario=isset($args[2]) ? $args[2] : '';
  64. $modelClass=Yii::import($args[0],true);
  65. $model=new $modelClass($scenario);
  66. $attributes=$model->getSafeAttributeNames();
  67. $templatePath=$this->templatePath===null?YII_PATH.'/cli/views/shell/form':$this->templatePath;
  68. $viewPath=Yii::getPathOfAlias($args[1]);
  69. $viewName=basename($viewPath);
  70. $viewPath.='.php';
  71. $params=array(
  72. 'modelClass'=>$modelClass,
  73. 'viewName'=>$viewName,
  74. 'attributes'=>$attributes,
  75. );
  76. $list=array(
  77. basename($viewPath)=>array(
  78. 'source'=>$templatePath.'/form.php',
  79. 'target'=>$viewPath,
  80. 'callback'=>array($this,'generateForm'),
  81. 'params'=>$params,
  82. ),
  83. );
  84. $this->copyFiles($list);
  85. $actionFile=$templatePath.'/action.php';
  86. if(!is_file($actionFile)) // fall back to default ones
  87. $actionFile=YII_PATH.'/cli/views/shell/form/action.php';
  88. echo "The following form view has been successfully created:\n";
  89. echo "\t$viewPath\n\n";
  90. echo "You may use the following code in your controller action:\n\n";
  91. echo $this->renderFile($actionFile,$params,true);
  92. echo "\n";
  93. }
  94. public function generateForm($source,$params)
  95. {
  96. if(!is_file($source)) // fall back to default ones
  97. $source=YII_PATH.'/cli/views/shell/form/'.basename($source);
  98. return $this->renderFile($source,$params,true);
  99. }
  100. public function class2id($className)
  101. {
  102. if(strrpos($className,'Form')===strlen($className)-4)
  103. $className=substr($className,0,strlen($className)-4);
  104. return trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/', '-\0', $className))),'-');
  105. }
  106. }