Skip to content

Commit

Permalink
improve TranslationBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAYEC77 committed Oct 4, 2016
1 parent a38a596 commit a8a4fc9
Showing 1 changed file with 80 additions and 16 deletions.
96 changes: 80 additions & 16 deletions src/behaviors/TranslationBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ class TranslationBehavior extends Behavior
*/
public $languages = [];

/**
* Delete related records when delete owner
* @var bool
*/
public $deleteTranslations = true;

protected $_newModels = [];

public function init()
{
parent::init();
if (!in_array($this->defaultLanguage, $this->languages)) {
if (!in_array($this->defaultLanguage, $this->languages, true)) {
throw new InvalidConfigException('Default language must be exist');
}
}
Expand All @@ -100,19 +106,26 @@ public function events()
return [
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
];
}

public function afterSave()
/**
* Delete related records when delete owner
*/
public function afterDelete()
{
/** @var ActiveRecord $owner */
$owner = $this->owner;

foreach ($this->getModels() as $model) {
$owner->link($this->relation, $model);
if ($this->deleteTranslations) {
/** @var ActiveRecord $owner */
foreach ($this->getModels() as $model) {
$model->delete();
}
}
}

/**
* @return ActiveRecord[]
*/
protected function getModels()
{
$selfModels = ArrayHelper::index($this->owner->{$this->relation}, function ($value) {
Expand All @@ -121,16 +134,37 @@ protected function getModels()
return array_merge($this->getNewModels(), $selfModels);
}

/**
* @return ActiveRecord[]
*/
protected function getNewModels()
{
if (empty($this->_newModels)) {
if (count($this->_newModels) === 0) {
foreach ($this->languages as $key => $language) {
$this->_newModels[$language] = new $this->langClassName([$this->languageField => $key]);
}
}
return $this->_newModels;
}

/**
* Save related translations
*/
public function afterSave()
{
/** @var ActiveRecord $owner */
$owner = $this->owner;

foreach ($this->getModels() as $model) {
$owner->link($this->relation, $model);
}
}

/**
* Modify owner model validators for translation attributes
* e.g. `['title', 'required']` convert to `[['title_en', 'title_fr'], 'required']`
* @param \yii\base\Component $owner
*/
public function attach($owner)
{
parent::attach($owner);
Expand All @@ -154,10 +188,15 @@ public function attach($owner)
}

$params = array_slice($rule, 2);
$validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params);
$validators->append(Validator::createValidator($rule[1], $owner, $rule_attributes, $params));
}
}

/**
* @param $attribute
* @param $language
* @return mixed|string
*/
private function getAttributeName($attribute, $language)
{
if ($this->attributeNamePattern instanceof \Closure) {
Expand All @@ -169,6 +208,12 @@ private function getAttributeName($attribute, $language)
]);
}

/**
* Check translation attributes
* @param string $name
* @param bool $checkVars
* @return bool
*/
public function canGetProperty($name, $checkVars = true)
{
if ($this->hasOwnProperty($name)) {
Expand All @@ -177,20 +222,31 @@ public function canGetProperty($name, $checkVars = true)
return parent::canGetProperty($name, $checkVars);
}

/**
* Check translation attributes
* @param $name
* @return bool
*/
public function hasOwnProperty($name)
{
foreach ($this->translationAttributes as $attribute) {
if ($name == $attribute) {
if ($name === $attribute) {
return true;
}
foreach ($this->languages as $language) {
if ($this->getAttributeName($attribute, $language) == $name) {
if ($this->getAttributeName($attribute, $language) === $name) {
return true;
}
}
}
}

/**
* Check translation attributes
* @param string $name
* @param bool $checkVars
* @return bool
*/
public function canSetProperty($name, $checkVars = true)
{
if ($this->hasOwnProperty($name)) {
Expand All @@ -199,36 +255,44 @@ public function canSetProperty($name, $checkVars = true)
return parent::canSetProperty($name, $checkVars);
}

/**
* @param string $name
* @return mixed
*/
public function __get($name)
{
$list = $this->getModels();


foreach ($this->translationAttributes as $attribute) {
if ($name == $attribute) {
if ($name === $attribute) {
return $list[$this->defaultLanguage]->{$attribute};
}
foreach ($list as $language) {
$langKey = $this->languages[$language->{$this->languageField}];
if ($this->getAttributeName($attribute, $langKey) == $name) {
if ($this->getAttributeName($attribute, $langKey) === $name) {
return $list[$langKey]->{$attribute};
}
}
}
return parent::__get($name);
}

/**
* @param string $name
* @param mixed $value
* @return mixed
*/
public function __set($name, $value)
{
$list = $this->getModels();

foreach ($this->translationAttributes as $attribute) {
if ($name == $attribute) {
if ($name === $attribute) {
return $list[$this->defaultLanguage]->{$attribute} = $value;
}
foreach ($list as $language) {
$langKey = $this->languages[$language->{$this->languageField}];
if ($this->getAttributeName($attribute, $langKey) == $name) {
if ($this->getAttributeName($attribute, $langKey) === $name) {
return $list[$langKey]->{$attribute} = $value;
}
}
Expand Down

0 comments on commit a8a4fc9

Please sign in to comment.