You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							102 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
	
	
							102 lines
						
					
					
						
							2.8 KiB
						
					
					
				| <?php | |
| /** | |
|  * Created by Error202 | |
|  * Date: 24.08.2018 | |
|  */ | |
|  | |
| 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 = []; | |
|  | |
| 	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']; | |
|  | |
| 					$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}; | |
| 					} | |
| 				} | |
| 			} | |
| 		} | |
| 	} | |
|  | |
| 	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; | |
| 	} | |
|  | |
| 	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; | |
| 	} | |
|  | |
| 	public function attributeLabels(){ | |
| 		return $this->new_labels; | |
| 	} | |
|  | |
| 	public function rules() { | |
| 		return $this->new_rules; | |
| 	} | |
|  | |
| 	public function getPublicAttributes () { | |
| 		return call_user_func('get_object_vars', $this); | |
| 	} | |
|  | |
| }
 | |
| 
 |