Generator.php 3.88 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\gii\generators\form;

Qiang Xue committed
10 11 12 13
use Yii;
use yii\base\Model;
use yii\gii\CodeFile;

Qiang Xue committed
14
/**
15
 * This generator will generate an action view file based on the specified model class.
Qiang Xue committed
16 17 18 19 20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Generator extends \yii\gii\Generator
{
Qiang Xue committed
22 23 24
	public $modelClass;
	public $viewPath = '@app/views';
	public $viewName;
Qiang Xue committed
25
	public $scenarioName;
Qiang Xue committed
26 27 28 29 30


	/**
	 * @inheritdoc
	 */
Qiang Xue committed
31 32 33 34 35
	public function getName()
	{
		return 'Form Generator';
	}

Qiang Xue committed
36 37 38
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
39 40 41 42
	public function getDescription()
	{
		return 'This generator generates a view script file that displays a form to collect input for the specified model class.';
	}
43 44 45 46 47 48

	/**
	 * @inheritdoc
	 */
	public function generate()
	{
Alexander Makarov committed
49
		$files = [];
Qiang Xue committed
50 51
		$files[] = new CodeFile(
			Yii::getAlias($this->viewPath) . '/' . $this->viewName . '.php',
Qiang Xue committed
52
			$this->render('form.php')
Qiang Xue committed
53 54 55 56
		);
		return $files;
	}

Qiang Xue committed
57 58 59
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
60 61
	public function rules()
	{
Alexander Makarov committed
62 63 64 65 66 67 68 69 70 71
		return array_merge(parent::rules(), [
			['modelClass, viewName, scenarioName, viewPath', 'filter', 'filter' => 'trim'],
			['modelClass, viewName, viewPath', 'required'],
			['modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
			['modelClass', 'validateClass', 'params' => ['extends' => Model::className()]],
			['viewName', 'match', 'pattern' => '/^\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'],
			['viewPath', 'match', 'pattern' => '/^@?\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'],
			['viewPath', 'validateViewPath'],
			['scenarioName', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
		]);
Qiang Xue committed
72 73
	}

Qiang Xue committed
74 75 76
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
77 78
	public function attributeLabels()
	{
Alexander Makarov committed
79
		return [
Qiang Xue committed
80 81 82 83
			'modelClass' => 'Model Class',
			'viewName' => 'View Name',
			'viewPath' => 'View Path',
			'scenarioName' => 'Scenario',
Alexander Makarov committed
84
		];
Qiang Xue committed
85 86
	}

Qiang Xue committed
87 88 89
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
90 91
	public function requiredTemplates()
	{
Alexander Makarov committed
92
		return ['form.php', 'action.php'];
Qiang Xue committed
93 94 95 96 97 98 99
	}

	/**
	 * @inheritdoc
	 */
	public function stickyAttributes()
	{
Alexander Makarov committed
100
		return ['viewPath', 'scenarioName'];
Qiang Xue committed
101 102 103 104 105 106 107
	}

	/**
	 * @inheritdoc
	 */
	public function hints()
	{
Alexander Makarov committed
108
		return [
Qiang Xue committed
109 110 111 112
			'modelClass' => 'This is the model class for collecting the form input. You should provide a fully qualified class name, e.g., <code>app\models\Post</code>.',
			'viewName' => 'This is the view name with respect to the view path. For example, <code>site/index</code> would generate a <code>site/index.php</code> view file under the view path.',
			'viewPath' => 'This is the root view path to keep the generated view files. You may provide either a directory or a path alias, e.g., <code>@app/views</code>.',
			'scenarioName' => 'This is the scenario to be used by the model when collecting the form input. If empty, the default scenario will be used.',
Alexander Makarov committed
113
		];
Qiang Xue committed
114 115
	}

Qiang Xue committed
116 117 118
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
119 120
	public function successMessage()
	{
Qiang Xue committed
121
		$code = highlight_string($this->render('action.php'), true);
Qiang Xue committed
122
		return <<<EOD
Qiang Xue committed
123 124
<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>
125
<pre>$code</pre>
Qiang Xue committed
126 127 128
EOD;
	}

Qiang Xue committed
129 130 131
	/**
	 * Validates [[viewPath]] to make sure it is a valid path or path alias and exists.
	 */
Qiang Xue committed
132 133 134 135 136 137 138 139
	public function validateViewPath()
	{
		$path = Yii::getAlias($this->viewPath, false);
		if ($path === false || !is_dir($path)) {
			$this->addError('viewPath', 'View path does not exist.');
		}
	}

Qiang Xue committed
140 141 142
	/**
	 * @return array list of safe attributes of [[modelClass]]
	 */
Qiang Xue committed
143 144 145 146
	public function getModelAttributes()
	{
		/** @var Model $model */
		$model = new $this->modelClass;
Qiang Xue committed
147 148 149
		if (!empty($this->scenarioName)) {
			$model->setScenario($this->scenarioName);
		}
Qiang Xue committed
150
		return $model->safeAttributes();
151
	}
Qiang Xue committed
152
}