diff --git a/backend/controllers/ModuleController.php b/backend/controllers/ModuleController.php index 47b16d6..5c81407 100644 --- a/backend/controllers/ModuleController.php +++ b/backend/controllers/ModuleController.php @@ -13,8 +13,16 @@ use yii\filters\VerbFilter; use yii\filters\AccessControl; use yii\web\NotFoundHttpException; +/** + * Modules Management + * Class ModuleController + * @package backend\controllers + */ class ModuleController extends Controller { + /** + * @var ModuleService Modules management service + */ private $_service; public function __construct(string $id, $module, ModuleService $service, array $config = []) @@ -51,6 +59,10 @@ class ModuleController extends Controller ]; } + /** + * List of modules + * @return string + */ public function actionList() { $modules = \Yii::$app->moduleManager->getModules(); diff --git a/console/controllers/ModuleController.php b/console/controllers/ModuleController.php index a39de01..a8ff1dd 100644 --- a/console/controllers/ModuleController.php +++ b/console/controllers/ModuleController.php @@ -29,6 +29,14 @@ class ModuleController extends Controller } /** + * First modules initialization + */ + public function actionInit() : void + { + \Yii::$app->moduleManager->getModules(); + } + + /** * Activate module and apply it migration if needed * @param $name */ diff --git a/core/behaviors/LanguageBehavior.php b/core/behaviors/LanguageBehavior.php index cc1418f..7156d0b 100644 --- a/core/behaviors/LanguageBehavior.php +++ b/core/behaviors/LanguageBehavior.php @@ -6,6 +6,7 @@ namespace core\behaviors; +use core\components\language\LanguageVirtualAbstract; use yii\base\Behavior; use yii\base\InvalidConfigException; use yii\db\ActiveRecord; @@ -13,235 +14,234 @@ use yii\db\ActiveQuery; class LanguageBehavior extends Behavior { - /** - * Attributes for translate, ex.: ['name', 'content'] - * @var array - */ - public $attributes; - - /** - * Class name for language active record entity - * @var string - */ - public $virtualClassName = 'VirtualTranslate'; - - /** - * Field name for language in translate table - * @var string - */ - public $languageField = 'language'; - - /** - * Field name for tables relative, ex.: 'post_id' - * @var string - */ - public $relativeField; - - /** - * Available languages, ex.: ['en', 'ru'] - * @var array - */ - public $translatedLanguages; - - /** - * Default language, ex.: 'en' - * @var string - */ - public $defaultLanguage; - - /** - * Translate table name, ex.: 'post_lng' - * @var string - */ - public $tableName; - - /** - * Abridge the language ID. - * @var boolean whether to abridge the language ID. - */ - public $abridge = true; - - /** - * Delete relative if foreign key not set on delete: cascade - * @var bool - */ - public $forceDelete = false; - - private $ownerClassName; - private $ownerClassShortName; - private $ownerPrimaryKey; - private $languageAttributes = []; - - /** - * Control events firing - * @return array - */ - public function events() - { - return [ - ActiveRecord::EVENT_AFTER_FIND => 'afterFind', - ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', - ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', - ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', - //ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate', - ]; - } - - public function attach( $owner ) { - /** @var ActiveRecord $owner */ - parent::attach($owner); - if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) { - throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' - . get_class($this->owner) . ' or in the application parameters', 101); - } - - if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array - $this->translatedLanguages = array_keys($this->translatedLanguages); - } - - if (!$this->defaultLanguage) { - throw new InvalidConfigException('Please specify default language for the ' . get_class($this)); - } - - if (empty($this->attributes) || !is_array($this->attributes)) { - throw new InvalidConfigException('Please specify translated attributes for the ' . get_class($this) . ' in the ' - . get_class($this->owner), 103); - } - - $this->ownerClassName = get_class($this->owner); - $this->ownerClassShortName = $this->getShortClassName($this->ownerClassName); - - /** @var ActiveRecord $className */ - $className = $this->ownerClassName; - $this->ownerPrimaryKey = $className::primaryKey()[0]; - - if (!isset($this->relativeField)) { - throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the ' - . get_class($this->owner), 105); - } - - //$rules = $owner->rules(); - //$validators = $owner->getValidators(); - /*foreach ($rules as $rule) { - if (in_array($rule[1], $this->excludedValidators)) - continue; - $rule_attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; - $attributes = array_intersect($this->attributes, $rule_attributes); - if (empty($attributes)) - continue; - $rule_attributes = []; - foreach ($attributes as $key => $attribute) { - foreach ($this->languages as $language) - if ($language != $this->defaultLanguage) - $rule_attributes[] = $this->getAttributeName($attribute, $language); - } - if (isset($rule['skipOnEmpty']) && !$rule['skipOnEmpty']) - $rule['skipOnEmpty'] = !$this->requireTranslations; - $params = array_slice($rule, 2); - if ($rule[1] !== 'required' || $this->requireTranslations) { - $validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params); - } elseif ($rule[1] === 'required') { - $validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params); - } - }*/ - - $this->createLanguageClass(); - $translation = new $this->virtualClassName; - - foreach ($this->translatedLanguages as $language) { - foreach ($this->attributes as $attribute) { - $attributeName = $attribute; - $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName}); - - //$this->owner->__set($attribute . '_' . $language, $translation->{$attributeName}); - //$this->owner->{$attribute . '_' . $language} = $translation->{$attributeName}; - //$this->owner->createProperty($attribute . '_' . $language, $translation->{$attributeName}); - - if ($language == $this->defaultLanguage) { - $this->setLanguageAttribute($attribute, $translation->{$attributeName}); - //$this->owner->__set($attribute, $translation->{$attributeName}); - } - } - } - } - - /** - * Insert event - */ - public function afterInsert() - { - $this->saveTranslations(); - } - - /** - * Update event - */ - public function afterUpdate() - { - /** @var ActiveRecord $owner */ - $owner = $this->owner; - //if ($owner->isRelationPopulated('translations')) { - if ($translationRecords = $owner->translations) { - $translations = $this->indexByLanguage($owner->getRelatedRecords()['translations']); - //$translations = $this->indexByLanguage($translationRecords); - $this->saveTranslations($translations); - } - } - - /** - * Find event - */ - public function afterFind() - { - /** @var ActiveRecord $owner */ - $owner = $this->owner; - if ($owner->isRelationPopulated('translations') && $related = $owner->getRelatedRecords()['translations']) { - $translations = $this->indexByLanguage($related); - foreach ($this->translatedLanguages as $language) { - foreach ($this->attributes as $attribute) { - foreach ($translations as $translation) { - if ($translation->{$this->languageField} == $language) { - $attributeName = $attribute; - $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName}); - if ($language == $this->defaultLanguage) { - $this->setLanguageAttribute($attribute, $translation->{$attributeName}); - } - } - } - } - } - } else { - if (!$owner->isRelationPopulated('translation')) { - $owner->translation; - } - $translation = $owner->getRelatedRecords()['translation']; - if ($translation) { - foreach ($this->attributes as $attribute) { - $attribute_name = $attribute; - $owner->setLanguageAttribute($attribute, $translation->$attribute_name); - } - } - } - foreach ($this->attributes as $attribute) { - if ($owner->hasAttribute($attribute) && $this->getLangAttribute($attribute)) { - $owner->setAttribute($attribute, $this->getLangAttribute($attribute)); - } - } - } - - public function afterDelete() - { - if ($this->forceDelete) { - /** @var ActiveRecord $owner */ - $owner = $this->owner; - $owner->unlinkAll('translations', true); - } - } - - public function createLanguageClass() - { - if (!class_exists($this->virtualClassName, false)) { - eval(' + /** + * Attributes for translate, ex.: ['name', 'content'] + * @var array + */ + public $attributes; + + /** + * Class name for language active record entity + * @var string + */ + public $virtualClassName = 'VirtualTranslate'; + + /** + * Field name for language in translate table + * @var string + */ + public $languageField = 'language'; + + /** + * Field name for tables relative, ex.: 'post_id' + * @var string + */ + public $relativeField; + + /** + * Available languages, ex.: ['en', 'ru'] + * @var array + */ + public $translatedLanguages; + + /** + * Default language, ex.: 'en' + * @var string + */ + public $defaultLanguage; + + /** + * Translate table name, ex.: 'post_lng' + * @var string + */ + public $tableName; + + /** + * Abridge the language ID. + * @var boolean whether to abridge the language ID. + */ + public $abridge = true; + + /** + * Delete relative if foreign key not set on delete: cascade + * @var bool + */ + public $forceDelete = false; + + private $_ownerClassName; + private $_ownerClassShortName; + private $_ownerPrimaryKey; + private $_languageAttributes = []; + + /** + * Control events firing + * @return array + */ + public function events() + { + return [ + ActiveRecord::EVENT_AFTER_FIND => 'afterFind', + ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', + ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', + ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', + ]; + } + + public function attach($owner) + { + /** @var ActiveRecord $owner */ + parent::attach($owner); + if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) { + throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' . get_class($this->owner) . ' or in the application parameters', 101); + } + + if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array + $this->translatedLanguages = array_keys($this->translatedLanguages); + } + + if (!$this->defaultLanguage) { + throw new InvalidConfigException('Please specify default language for the ' . get_class($this)); + } + + if (empty($this->attributes) || !is_array($this->attributes)) { + throw new InvalidConfigException('Please specify translated attributes for the ' . get_class($this) . ' in the ' + . get_class($this->owner), 103); + } + + $this->_ownerClassName = get_class($this->owner); + $this->_ownerClassShortName = $this->getShortClassName($this->_ownerClassName); + + /** @var ActiveRecord $className */ + $className = $this->_ownerClassName; + $this->_ownerPrimaryKey = $className::primaryKey()[0]; + + if (!isset($this->relativeField)) { + throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the ' + . get_class($this->owner), 105); + } + + //$rules = $owner->rules(); + //$validators = $owner->getValidators(); + /*foreach ($rules as $rule) { + if (in_array($rule[1], $this->excludedValidators)) + continue; + $rule_attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; + $attributes = array_intersect($this->attributes, $rule_attributes); + if (empty($attributes)) + continue; + $rule_attributes = []; + foreach ($attributes as $key => $attribute) { + foreach ($this->languages as $language) + if ($language != $this->defaultLanguage) + $rule_attributes[] = $this->getAttributeName($attribute, $language); + } + if (isset($rule['skipOnEmpty']) && !$rule['skipOnEmpty']) + $rule['skipOnEmpty'] = !$this->requireTranslations; + $params = array_slice($rule, 2); + if ($rule[1] !== 'required' || $this->requireTranslations) { + $validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params); + } elseif ($rule[1] === 'required') { + $validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params); + } + }*/ + + $this->createLanguageClass(); + $translation = new $this->virtualClassName; + + foreach ($this->translatedLanguages as $language) { + foreach ($this->attributes as $attribute) { + $attributeName = $attribute; + $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName}); + + //$this->owner->__set($attribute . '_' . $language, $translation->{$attributeName}); + //$this->owner->{$attribute . '_' . $language} = $translation->{$attributeName}; + //$this->owner->createProperty($attribute . '_' . $language, $translation->{$attributeName}); + + if ($language == $this->defaultLanguage) { + $this->setLanguageAttribute($attribute, $translation->{$attributeName}); + //$this->owner->__set($attribute, $translation->{$attributeName}); + } + } + } + } + + /** + * Insert event + */ + public function afterInsert() + { + $this->saveTranslations(); + } + + /** + * Update event + */ + public function afterUpdate() + { + /** @var ActiveRecord $owner */ + $owner = $this->owner; + //if ($owner->isRelationPopulated('translations')) { + if ($translationRecords = $owner->translations) { + $translations = $this->indexByLanguage($owner->getRelatedRecords()['translations']); + //$translations = $this->indexByLanguage($translationRecords); + $this->saveTranslations($translations); + } + } + + /** + * Find event + */ + public function afterFind() + { + /** @var ActiveRecord $owner */ + $owner = $this->owner; + if ($owner->isRelationPopulated('translations') && $related = $owner->getRelatedRecords()['translations']) { + $translations = $this->indexByLanguage($related); + foreach ($this->translatedLanguages as $language) { + foreach ($this->attributes as $attribute) { + foreach ($translations as $translation) { + if ($translation->{$this->languageField} == $language) { + $attributeName = $attribute; + $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName}); + if ($language == $this->defaultLanguage) { + $this->setLanguageAttribute($attribute, $translation->{$attributeName}); + } + } + } + } + } + } else { + if (!$owner->isRelationPopulated('translation')) { + $owner->translation; + } + $translation = $owner->getRelatedRecords()['translation']; + if ($translation) { + foreach ($this->attributes as $attribute) { + $attribute_name = $attribute; + $owner->setLanguageAttribute($attribute, $translation->$attribute_name); + } + } + } + foreach ($this->attributes as $attribute) { + if ($owner->hasAttribute($attribute) && $this->getLangAttribute($attribute)) { + $owner->setAttribute($attribute, $this->getLangAttribute($attribute)); + } + } + } + + public function afterDelete() + { + if ($this->forceDelete) { + /** @var ActiveRecord $owner */ + $owner = $this->owner; + $owner->unlinkAll('translations', true); + } + } + + public function createLanguageClass() + { + if (!class_exists($this->virtualClassName, false)) { + eval(' use yii\db\ActiveRecord; class ' . $this->virtualClassName . ' extends ActiveRecord { @@ -250,112 +250,114 @@ class LanguageBehavior extends Behavior return \'' . $this->tableName . '\'; } }'); - } - } - - private function saveTranslations($translations = []) - { - /** @var ActiveRecord $owner */ - $owner = $this->owner; - if (!isset($owner->_form) || !$owner->_form) { - return; - } - foreach ($this->translatedLanguages as $language) { - $isDefaultLanguage = $language == $this->defaultLanguage; - if (!isset($translations[$language])) { - /** @var ActiveRecord $translation */ - $translation = new $this->virtualClassName; - $translation->{$this->languageField} = $language; - $translation->{$this->relativeField} = $owner->getPrimaryKey(); - } else { - $translation = $translations[$language]; - } - $save = false; - foreach ($this->attributes as $attribute) { - //$value = $isDefaultLanguage ? $owner->$attribute : $owner->{$attribute . '_' . $language}; - $value = $isDefaultLanguage ? $owner->_form->$attribute : $owner->_form->{$attribute . '_' . $language}; - if ($value !== null) { - //$field = $isDefaultLanguage ? $attribute : $attribute . '_' . $language; - $field = $attribute; - $translation->$field = $value; - $save = true; - } - } - if ($translation->isNewRecord && !$save) { - continue; - } - $translation->save(); - } - } - - private function getShortClassName($className) - { - return substr($className, strrpos($className, '\\') + 1); - } - - public function setLanguageAttribute($name, $value) - { - $this->languageAttributes[$name] = $value; - } - - protected function indexByLanguage(array $records) - { - $sorted = []; - foreach ($records as $record) { - $sorted[$record->{$this->languageField}] = $record; - } - unset($records); - return $sorted; - } - - /** - * Relation to model translations - * @return ActiveQuery - */ - public function getTranslations() - { - /** @var ActiveRecord */ - return $this->owner->hasMany($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]); - } - - public function getTranslation($language = null) - { - //if (basename(\Yii::$app->getBasePath()) === 'backend') { - // $language = $language ?: $this->defaultLanguage; - //} - //else { - $language = $language ?: \Yii::$app->language; - //} - // if translate exists - $translate = $this->virtualClassName::find() - ->andWhere([$this->relativeField => $this->owner->id]) - ->andWhere([$this->languageField => $language]) - ->one(); - - $language = $translate ? $language : $this->defaultLanguage; - - return $this->owner->hasOne($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]) - ->where([$this->languageField => $language]); - } - - public function findTranslation($language = null) - { - $language = $language ?: $this->defaultLanguage; - //$class = call_user_func(array($this->virtualClassName, 'getInstance')); - return $this->virtualClassName::find() - ->andWhere([$this->relativeField => $this->owner->id]) - ->andWhere([$this->languageField => $language]) - ->one(); - } - - public function hasLangAttribute($name) - { - return array_key_exists($name, $this->languageAttributes); - } - - public function getLangAttribute($name) - { - return $this->hasLangAttribute($name) ? $this->languageAttributes[$name] : null; - } + } + } + + private function saveTranslations($translations = []) + { + /** @var ActiveRecord $owner */ + $owner = $this->owner; + if (!isset($owner->_form) || !$owner->_form) { + return; + } + foreach ($this->translatedLanguages as $language) { + $isDefaultLanguage = $language == $this->defaultLanguage; + if (!isset($translations[$language])) { + /** @var ActiveRecord $translation */ + $translation = new $this->virtualClassName; + $translation->{$this->languageField} = $language; + $translation->{$this->relativeField} = $owner->getPrimaryKey(); + } else { + $translation = $translations[$language]; + } + $save = false; + foreach ($this->attributes as $attribute) { + //$value = $isDefaultLanguage ? $owner->$attribute : $owner->{$attribute . '_' . $language}; + $value = $isDefaultLanguage ? $owner->_form->$attribute : $owner->_form->{$attribute . '_' . $language}; + if ($value !== null) { + //$field = $isDefaultLanguage ? $attribute : $attribute . '_' . $language; + $field = $attribute; + $translation->$field = $value; + $save = true; + } + } + if ($translation->isNewRecord && !$save) { + continue; + } + $translation->save(); + } + } + + private function getShortClassName($className) + { + return substr($className, strrpos($className, '\\') + 1); + } + + public function setLanguageAttribute($name, $value) + { + $this->_languageAttributes[$name] = $value; + } + + protected function indexByLanguage(array $records) + { + $sorted = []; + foreach ($records as $record) { + $sorted[$record->{$this->languageField}] = $record; + } + unset($records); + + return $sorted; + } + + /** + * Relation to model translations + * @return ActiveQuery + */ + public function getTranslations() + { + /** @var ActiveRecord */ + return $this->owner->hasMany($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]); + } + + public function getTranslation($language = null) + { + //if (basename(\Yii::$app->getBasePath()) === 'backend') { + // $language = $language ?: $this->defaultLanguage; + //} + //else { + $language = $language ?: \Yii::$app->language; + //} + // if translate exists + $translate = $this->virtualClassName::find() + ->andWhere([$this->relativeField => $this->owner->id]) + ->andWhere([$this->languageField => $language]) + ->one(); + + $language = $translate ? $language : $this->defaultLanguage; + + return $this->owner->hasOne($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]) + ->where([$this->languageField => $language]); + } + + public function findTranslation($language = null) + { + $language = $language ?: $this->defaultLanguage; + + //$class = call_user_func(array($this->virtualClassName, 'getInstance')); + return $this->virtualClassName::find() + ->andWhere([$this->relativeField => $this->owner->id]) + ->andWhere([$this->languageField => $language]) + ->one(); + } + + public function hasLangAttribute($name) + { + return array_key_exists($name, $this->_languageAttributes); + } + + public function getLangAttribute($name) + { + return $this->hasLangAttribute($name) ? $this->_languageAttributes[$name] : null; + } } diff --git a/core/components/LanguageDynamicModel.php b/core/components/LanguageDynamicModel.php index 74f2c1f..36dd494 100644 --- a/core/components/LanguageDynamicModel.php +++ b/core/components/LanguageDynamicModel.php @@ -6,97 +6,99 @@ namespace core\components; - use yii\base\DynamicModel; use Yii; use yii\db\ActiveRecord; class LanguageDynamicModel extends DynamicModel { - private $new_labels = []; - private $new_hints = []; - private $new_rules = []; + private $_new_labels = []; + private $_new_hints = []; + private $_new_rules = []; + + public function __construct(ActiveRecord $entity = null, array $attributes = [], array $config = []) + { + $used_attributes = $this->getStringAttributes(); + parent::__construct(array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config); - public function __construct( ActiveRecord $entity = null, array $attributes = [], array $config = [] ) - { - $used_attributes = $this->getStringAttributes(); - parent::__construct( array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config ); + if ($entity) { + foreach ($used_attributes as $used_attribute) { + foreach ($entity->translations as $translate) { + $defaultLanguage = basename(\Yii::$app->getBasePath()) === 'backend' + ? + Yii::$app->params['backendDefaultLanguage'] + : + Yii::$app->params['defaultLanguage']; - if ($entity) { - foreach ($used_attributes as $used_attribute) { - foreach ( $entity->translations as $translate ) { - $defaultLanguage = basename( \Yii::$app->getBasePath() ) === 'backend' - ? - Yii::$app->params['backendDefaultLanguage'] - : - Yii::$app->params['defaultLanguage']; + $languageAttribute = $used_attribute . '_' . $translate->language; - $languageAttribute = $used_attribute . '_' . $translate->language; + if ($translate->language === $defaultLanguage && isset($translate->{$used_attribute})) { + $this->{$used_attribute} = $translate->{$used_attribute}; + } elseif (isset($translate->{$used_attribute})) { + $this->{$languageAttribute} = $translate->{$used_attribute}; + } + } + } + } + } - if ( $translate->language === $defaultLanguage && isset($translate->{$used_attribute})) { - $this->{$used_attribute} = $translate->{$used_attribute}; - } - elseif (isset($translate->{$used_attribute})) { - $this->{$languageAttribute} = $translate->{$used_attribute}; - } - } - } - } - } + private function getStringAttributes() + { + $string_attributes = []; + foreach ($this->rules() as $rule) { + $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; + $type = $rule[1]; + if ($type == 'string') { + foreach ($attributes as $attribute) { + $string_attributes[] = $attribute; + } + } + } - private function getStringAttributes() - { - $string_attributes = []; - foreach ($this->rules() as $rule) { - $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; - $type = $rule[1]; - if ($type == 'string') { - foreach ($attributes as $attribute) { - $string_attributes[] = $attribute; - } - } - } - return $string_attributes; - } + return $string_attributes; + } - private function prepareLanguageAttributes() - { - $language_attributes = []; - $labels = $this->attributeLabels(); - $hints = $this->attributeHints(); - foreach ($this->rules() as $rule) { - $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; - $type = $rule[1]; - if ($type == 'string') { - foreach (Yii::$app->params['translatedLanguages'] as $language => $language_name) { - $rule_attributes = []; - foreach ($attributes as $attribute) { - // add attribute - $language_attributes[] = $attribute . '_' . $language; - $this->new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null; - $this->new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null; - $rule_attributes[] = $attribute . '_' . $language; - } - // add rule - if (!empty($rule_attributes)) { - $this->new_rules[] = [ $rule_attributes, $rule[1] ]; - } - } - } - } - return $language_attributes; - } + private function prepareLanguageAttributes() + { + $language_attributes = []; + $labels = $this->attributeLabels(); + $hints = $this->attributeHints(); + foreach ($this->rules() as $rule) { + $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; + $type = $rule[1]; + if ($type == 'string') { + foreach (Yii::$app->params['translatedLanguages'] as $language => $language_name) { + $rule_attributes = []; + foreach ($attributes as $attribute) { + // add attribute + $language_attributes[] = $attribute . '_' . $language; + $this->_new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null; + $this->_new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null; + $rule_attributes[] = $attribute . '_' . $language; + } + // add rule + if (!empty($rule_attributes)) { + $this->_new_rules[] = [$rule_attributes, $rule[1]]; + } + } + } + } - public function attributeLabels(){ - return $this->new_labels; - } + return $language_attributes; + } - public function rules() { - return $this->new_rules; - } + public function attributeLabels() + { + return $this->_new_labels; + } - public function getPublicAttributes () { - return call_user_func('get_object_vars', $this); - } + public function rules() + { + return $this->_new_rules; + } + public function getPublicAttributes() + { + return call_user_func('get_object_vars', $this); + } } diff --git a/core/components/modules/ModuleManager.php b/core/components/modules/ModuleManager.php index b5d7135..a0a9529 100644 --- a/core/components/modules/ModuleManager.php +++ b/core/components/modules/ModuleManager.php @@ -13,83 +13,86 @@ use yii\helpers\FileHelper; class ModuleManager { - public $moduleNames = []; - public $modules = []; + public $moduleNames = []; + public $modules = []; - private $service; + private $_service; - public function __construct(ModuleService $service) - { - $this->service = $service; - } + public function __construct(ModuleService $service) + { + $this->_service = $service; + } - public function appendToMigrationTable($name) { - $time = time(); - $connection = Yii::$app->getDb(); - $command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')"); - $command->execute(); - } + public function appendToMigrationTable($name) + { + $time = time(); + $connection = Yii::$app->getDb(); + $command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')"); + $command->execute(); + } - public function removeFromMigrationTable($name) { - $connection = Yii::$app->getDb(); - $command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'"); - $command->execute(); - } + public function removeFromMigrationTable($name) + { + $connection = Yii::$app->getDb(); + $command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'"); + $command->execute(); + } - public function getModules() - { - $modules = []; - $localModules = $this->getLocalModules(); - foreach ($localModules as $local_module) { - if ($this->isTableExist('modules') && !$db_module = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one()) { - $db_module = $this->service->create($local_module['name'], "common\\modules\\".$local_module['name']."\\" . $local_module['module']); - $db_module->description = $local_module['description']; - $modules[] = $db_module; - } - else { - $db_module->description = $local_module['description']; - $modules[] = $db_module; - } - } - return $modules; - } + public function getModules() : array + { + $modules = []; + $localModules = $this->getLocalModules(); + foreach ($localModules as $local_module) { + $db_module = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one(); + if ($this->isTableExist('modules') && !$db_module) { + $db_module = $this->_service->create($local_module['name'], 'common\\modules\\' . $local_module['name'] . '\\' . $local_module['module']); + $db_module->description = $local_module['description']; + $modules[] = $db_module; + } else { + $db_module->description = $local_module['description']; + $modules[] = $db_module; + } + } - public function getLocalModules() - { - $this->_getLocalModulesNames(); + return $modules; + } - if (empty($this->modules)) { - foreach ( $this->moduleNames as $module_name ) { - $manifest = Yii::getAlias( '@common/modules/' . $module_name . '/manifest.php' ); - if ( file_exists( $manifest ) ) { - $this->modules[] = require $manifest; - } - } - } - return $this->modules; - } + public function getLocalModules() + { + $this->getLocalModulesNames(); - private function _getLocalModulesNames(): void - { - if (!empty($this->moduleNames)) { - return; - } + if (empty($this->modules)) { + foreach ($this->moduleNames as $module_name) { + $manifest = Yii::getAlias('@common/modules/' . $module_name . '/manifest.php'); + if (file_exists($manifest)) { + $this->modules[] = require $manifest; + } + } + } - $names = []; - $modulePath = Yii::getAlias('@common/modules'); - $modules = file_exists($modulePath) ? FileHelper::findDirectories($modulePath, [ - 'recursive' => false, - ]) : []; - foreach ($modules as $module) { - $module = basename($module); - $names[] = $module; - } - $this->moduleNames = $names; - } + return $this->modules; + } - public function isTableExist($name): bool - { - return Yii::$app->db->schema->getTableSchema($name) !== null; - } + private function getLocalModulesNames(): void + { + if (!empty($this->moduleNames)) { + return; + } -} \ No newline at end of file + $names = []; + $modulePath = Yii::getAlias('@common/modules'); + $modules = file_exists($modulePath) ? FileHelper::findDirectories($modulePath, [ + 'recursive' => false, + ]) : []; + foreach ($modules as $module) { + $module = basename($module); + $names[] = $module; + } + $this->moduleNames = $names; + } + + public function isTableExist($name): bool + { + return Yii::$app->db->schema->getTableSchema($name) !== null; + } +} diff --git a/setup.php b/setup.php index 0d71c7a..13bf1fd 100644 --- a/setup.php +++ b/setup.php @@ -24,6 +24,8 @@ class Setup private $_http_protocol; private $_domain; + private $_systemModules = ['languages', 'pages', 'forms', 'links']; + private $_l = [ 'ru' => [ 'Connection failed. Try again' => 'Ошибка соединения. Попробуйте снова', @@ -102,6 +104,9 @@ class Setup // install modules $this->activateSystemModules(); + // remove garbage modules + // todo + // install system permissions $this->addPermissions(); @@ -374,13 +379,11 @@ SH; echo Console::log($this->l('Complete'), 'green') . PHP_EOL; } - private function activateSystemModules(): void + private function activateSystemModules() : void { Console::log($this->l('Activating modules: '), 'white'); - $systemModules = [ - 'languages', 'pages', 'forms', 'links' - ]; - foreach ($systemModules as $name) { + shell_exec('php ' . __DIR__ . '/yii module/init'); + foreach ($this->_systemModules as $name) { shell_exec('php ' . __DIR__ . '/yii module/activate "' . $name . '"'); } echo Console::log($this->l('Complete'), 'green') . PHP_EOL;