diff --git a/vendor/blobt/generators/crud/Generator.php b/vendor/blobt/generators/crud/Generator.php new file mode 100644 index 0000000..e306536 --- /dev/null +++ b/vendor/blobt/generators/crud/Generator.php @@ -0,0 +1,186 @@ +controllerClass, '\\')) . '.php'); + + $files = [ + new CodeFile($controllerFile, $this->render('controller.php')), + ]; + + if (!empty($this->searchModelClass)) { + $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php')); + $files[] = new CodeFile($searchModel, $this->render('search.php')); + } + + $viewPath = $this->getViewPath(); + $templatePath = $this->getTemplatePath() . '/views'; + foreach (scandir($templatePath) as $file) { + if (empty($this->searchModelClass) && $file === '_search.php') { + continue; + } + if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { + $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file")); + } + } + + return $files; + } + + /** + * @return string the controller ID (without the module ID prefix) + */ + public function getControllerID() { + $pos = strrpos($this->controllerClass, '\\'); + $class = substr(substr($this->controllerClass, $pos + 1), 0, -10); + + return Inflector::camel2id($class, '-', $this->strictInflector); + } + + /** + * @return string the controller view path + */ + public function getViewPath() { + if (empty($this->viewPath)) { + return Yii::getAlias('@app/views/' . $this->getControllerID()); + } + + return Yii::getAlias(str_replace('\\', '/', $this->viewPath)); + } + + /** + * @return string + */ + public function getNameAttribute() { + foreach ($this->getColumnNames() as $name) { + if (!strcasecmp($name, 'name') || !strcasecmp($name, 'title')) { + return $name; + } + } + /* @var $class \yii\db\ActiveRecord */ + $class = $this->modelClass; + $pk = $class::primaryKey(); + + return $pk[0]; + } + + /** + * Generates code for active field + * @param string $attribute + * @return string + */ + public function generateActiveField($attribute) { + $tableSchema = $this->getTableSchema(); + if ($tableSchema === false || !isset($tableSchema->columns[$attribute])) { + if (preg_match('/^(password|pass|passwd|passcode)$/i', $attribute)) { + return "\$form->field(\$model, '$attribute')->passwordInput()"; + } + + return "\$form->field(\$model, '$attribute')"; + } + $column = $tableSchema->columns[$attribute]; + if ($column->phpType === 'boolean') { + return "\$form->field(\$model, '$attribute')->checkbox()"; + } + + if ($column->type === 'text') { + return "\$form->field(\$model, '$attribute')->textarea(['rows' => 6])"; + } + + if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) { + $input = 'passwordInput'; + } else { + $input = 'textInput'; + } + + if (is_array($column->enumValues) && count($column->enumValues) > 0) { + $dropDownOptions = []; + foreach ($column->enumValues as $enumValue) { + $dropDownOptions[$enumValue] = Inflector::humanize($enumValue); + } + return "\$form->field(\$model, '$attribute')->dropDownList(" + . preg_replace("/\n\s*/", ' ', VarDumper::export($dropDownOptions)) . ", ['prompt' => ''])"; + } + + if ($column->phpType !== 'string' || $column->size === null) { + return "\$form->field(\$model, '$attribute')->$input()"; + } + + return "\$form->field(\$model, '$attribute')->$input(['maxlength' => true])"; + } + +} diff --git a/vendor/blobt/generators/crud/default/views/_search.php b/vendor/blobt/generators/crud/default/views/_search.php index b65231f..e7efb17 100644 --- a/vendor/blobt/generators/crud/default/views/_search.php +++ b/vendor/blobt/generators/crud/default/views/_search.php @@ -17,33 +17,29 @@ use yii\widgets\ActiveForm; /* @var $form yii\widgets\ActiveForm */ ?> - \ No newline at end of file