diff --git a/composer.json b/composer.json
index 48a9b23..12bd39c 100644
--- a/composer.json
+++ b/composer.json
@@ -1,5 +1,5 @@
{
- "name": "drobnitsa/dictionary-yii2-generator",
+ "name": "drobnitsa/yii2-gii-dictionary-generator",
"description": "Генератор справочников для проектов",
"type": "yii2-extension",
"authors": [
@@ -13,7 +13,7 @@
},
"autoload": {
"psr-4": {
- "drobnitsa\\DictionaryYii2Generator\\": "src/"
+ "drobnitsa\\dictionaryGenerator\\": "src/"
}
}
}
diff --git a/src/Generator.php b/src/Generator.php
index cabdf3c..8317e83 100644
--- a/src/Generator.php
+++ b/src/Generator.php
@@ -1,26 +1,170 @@
'/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
+ ['modelName', 'validateModelClass'],
+ ['generateMigration', 'boolean'],
+ [['migrationName', 'controllerId'], 'string'],
+ ['attributes', 'safe']
+// ['attributes', 'string', 'length' => [0, 1024]],
+ ];
+ }
- // создаем файл
- if (!file_exists(dirname($filePath))) {
- mkdir(dirname($filePath), 0777, true);
+ public function validateModelClass($attribute, $params)
+ {
+ if (!class_exists($this->modelClass)) {
+ $this->addError($attribute, 'Модель не найдена.');
+ }
+ }
+
+ public function getModelClass()
+ {
+ return $this->modelNs . '\\' . $this->modelName;
+ }
+
+ public function getControllerNs()
+ {
+ return $this->moduleId . '\controllers';
+ }
+
+ public function generate()
+ {
+ $controllerName = Inflector::id2camel($this->controllerId).'Controller';
+
+
+ $params = [
+ 'modelName' => $this->modelName,
+ 'modelClass' => $this->modelClass,
+ 'controllerNs' => $this->controllerNs,
+ 'controllerId' => $this->controllerId,
+ 'controllerName' => Inflector::id2camel($this->controllerId),
+ 'attributes' => $this->attributes,
+ 'migrationName' => $this->migrationName,
+ 'moduleId' => $this->moduleId
+ ];
+
+ $controllerCode = $this->render('controller.php', $params);
+ $viewCode = $this->render('view.php', $params);
+ $searchCode = $this->render('_search.php', $params);
+
+ $nsPath = str_replace('\\', '/', $this->controllerNs);
+
+ $controllerPath = \Yii::getAlias("@$nsPath/{$controllerName}.php");
+ $viewDir = \Yii::getAlias("@backend/views/{$this->controllerId}");
+
+ $files = [
+ new CodeFile($controllerPath, $controllerCode),
+ new CodeFile("$viewDir/index.php", $viewCode),
+ new CodeFile("$viewDir/_search.php", $searchCode),
+ ];
+
+ if($this->generateMigration && $this->migrationName) {
+ $nsPath = str_replace('\\', '/', $this->migrationNs);
+ $migrationPath = \Yii::getAlias("@$nsPath/{$this->migrationName}.php");
+ $migrationCode = $this->render('migration.php', $params);
+ $files[] = new CodeFile($migrationPath, $migrationCode);
}
- file_put_contents($filePath, $content);
+ return $files;
+ }
- return $filePath;
+ /**
+ * Возвращает список атрибутов модели для выбора в форме
+ */
+ public function getModelAttributes($plain = true)
+ {
+ if (!class_exists($this->modelClass)) {
+ return [];
+ }
+ $model = new $this->modelClass();
+ if(!$plain) {
+ $result = [];
+ foreach ($model->attributes() as $attribute) {
+ $result[$attribute] = $attribute;
+ }
+ return $result;
+ }
+ return $model->attributes();
+// return $model->attributeLabels();
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function attributeLabels()
+ {
+ return array_merge(parent::attributeLabels(), [
+ 'modelNs' => 'Model namespace',
+ 'moduleId' => 'Module',
+ ]);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function hints()
+ {
+ return array_merge(parent::hints(), [
+ 'modelNs' => 'This is the namespace for the source model, e.g., app\models',
+ 'moduleId' => 'This is the module name for the controller and view to be generated, e.g., app\controllers',
+ ]);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function stickyAttributes()
+ {
+ return array_merge(
+ parent::stickyAttributes(),
+ [
+ 'modelNs',
+ 'moduleId',
+ 'migrationNs'
+ ]
+ );
+ }
+
+ public function actionGetAttributes()
+ {
+ if(class_exists($this->modelClass)) {
+ return json_encode([
+ 'result' => true,
+ 'controller_id' => Inflector::pluralize(Inflector::camel2id($this->modelName)),
+ 'attributes' => $this->getModelAttributes()
+ ]);
+ }
+ return json_encode(['result' => false]);
}
}
\ No newline at end of file
diff --git a/src/assets/MainAsset.php b/src/assets/MainAsset.php
new file mode 100644
index 0000000..0f326d8
--- /dev/null
+++ b/src/assets/MainAsset.php
@@ -0,0 +1,13 @@
+ {
+ autoUpdateController = false;
+ });
+
+ modelInput && modelInput.addEventListener('input', () => {
+ if (debounceTimer) clearTimeout(debounceTimer);
+ debounceTimer = setTimeout(() => {
+ const modelNs = modelNsInput.value.trim();
+ if (modelNs.length === 0) return;
+
+ const modelClassName = modelInput.value.trim();
+ if (modelClassName.length === 0) return;
+
+ const data = new FormData();
+ data.append('Generator[modelNs]', modelNs)
+ data.append('Generator[modelName]', modelClassName)
+ data.append(yii.getCsrfParam(), yii.getCsrfToken());
+
+ // AJAX-запрос на получение атрибутов
+ fetch('default/action?id=dictionary&name=GetAttributes', {
+ method: 'POST',
+ body: data,
+ })
+ .then(res => res.json())
+ .then(data => {
+ if(data.result) {
+ if (attributesContainer) {
+ attributesContainer.innerHTML = '';
+ data.attributes.forEach(attr => {
+
+ let label = document.createElement('label');
+ label.className = 'checkbox-label';
+
+ let checkbox = document.createElement('input');
+ checkbox.type = 'checkbox';
+ checkbox.name = 'Generator[attributes][]';
+ checkbox.value = attr;
+
+ label.appendChild(checkbox);
+ label.appendChild(document.createTextNode(' ' + attr));
+ attributesContainer.appendChild(label);
+ attributesContainer.appendChild(document.createElement('br'));
+ });
+ }
+
+ controllerInput.value = data.controller_id;
+ generateMigrationName(data.controller_id);
+ }
+
+ });
+ }, 500);
+ });
+
+ function generateMigrationName(controllerId) {
+ let migrationNameInput = document.querySelector('#generator-migrationname');
+ // Получаем текущие дата и время
+ const now = new Date();
+
+ // Форматируем дату и время в виде "yymmdd_hhmmss"
+ const year = now.getFullYear().toString().slice(-2);
+ const month = ('0' + (now.getMonth() + 1)).slice(-2);
+ const day = ('0' + now.getDate()).slice(-2);
+ const hours = ('0' + now.getHours()).slice(-2);
+ const minutes = ('0' + now.getMinutes()).slice(-2);
+ const seconds = ('0' + now.getSeconds()).slice(-2);
+
+ const timestamp = `${year}${month}${day}_${hours}${minutes}${seconds}`;
+
+ // Модифицируем имя таблицы: заменяем дефисы на подчеркивания, убираем расширения
+ // Предположим, что tableName — это строка с дефисами, например: "furniture-packs"
+ const formattedName = controllerId.replace(/-/g, '_');
+
+ migrationNameInput.value = `m${timestamp}_insert_${formattedName}_permissions`;
+ }
+
+})();
\ No newline at end of file
diff --git a/src/default/_search.php b/src/default/_search.php
new file mode 100644
index 0000000..f3299fb
--- /dev/null
+++ b/src/default/_search.php
@@ -0,0 +1,53 @@
+
+use yii\helpers\Html;
+use common\widgets\ActiveForm;
+
+/* @var $this yii\web\View */
+/* @var $model common\models\Addition */
+/* @var $form yii\widgets\ActiveForm */
+
+?>
+
+