Generator.php 4.01 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
 * @property array $modelAttributes List of safe attributes of [[modelClass]]. This property is read-only.
 *
Qiang Xue committed
19 20 21 22 23
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Generator extends \yii\gii\Generator
{
Qiang Xue committed
24 25 26
	public $modelClass;
	public $viewPath = '@app/views';
	public $viewName;
Qiang Xue committed
27
	public $scenarioName;
Qiang Xue committed
28 29 30


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

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

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

Qiang Xue committed
59
	/**
Qiang Xue committed
60
	 * @inheritdoc
Qiang Xue committed
61
	 */
Qiang Xue committed
62 63
	public function rules()
	{
Alexander Makarov committed
64
		return array_merge(parent::rules(), [
65 66 67 68 69 70 71 72
			[['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.'],
Alexander Makarov committed
73
		]);
Qiang Xue committed
74 75
	}

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

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

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

	/**
Qiang Xue committed
106
	 * @inheritdoc
Qiang Xue committed
107 108 109
	 */
	public function hints()
	{
Alexander Makarov committed
110
		return [
Qiang Xue committed
111 112 113 114
			'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
115
		];
Qiang Xue committed
116 117
	}

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

Qiang Xue committed
131 132 133
	/**
	 * Validates [[viewPath]] to make sure it is a valid path or path alias and exists.
	 */
Qiang Xue committed
134 135 136 137 138 139 140 141
	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
142 143 144
	/**
	 * @return array list of safe attributes of [[modelClass]]
	 */
Qiang Xue committed
145 146 147 148
	public function getModelAttributes()
	{
		/** @var Model $model */
		$model = new $this->modelClass;
Qiang Xue committed
149 150 151
		if (!empty($this->scenarioName)) {
			$model->setScenario($this->scenarioName);
		}
Qiang Xue committed
152
		return $model->safeAttributes();
153
	}
Qiang Xue committed
154
}