@ -6,6 +6,7 @@
namespace core\behaviors;
namespace core\behaviors;
use core\components\language\LanguageVirtualAbstract;
use yii\base\Behavior;
use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;
use yii\db\ActiveRecord;
@ -67,10 +68,10 @@ class LanguageBehavior extends Behavior
*/
*/
public $forceDelete = false;
public $forceDelete = false;
private $ ownerClassName;
private $_ ownerClassName;
private $ ownerClassShortName;
private $_ ownerClassShortName;
private $ ownerPrimaryKey;
private $_ ownerPrimaryKey;
private $ languageAttributes = [];
private $_ languageAttributes = [];
/**
/**
* Control events firing
* Control events firing
@ -83,16 +84,15 @@ class LanguageBehavior extends Behavior
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
//ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
];
}
}
public function attach( $owner ) {
public function attach($owner)
{
/** @var ActiveRecord $owner */
/** @var ActiveRecord $owner */
parent::attach($owner);
parent::attach($owner);
if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) {
if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) {
throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the '
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);
. get_class($this->owner) . ' or in the application parameters', 101);
}
}
if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array
if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array
@ -108,12 +108,12 @@ class LanguageBehavior extends Behavior
. get_class($this->owner), 103);
. get_class($this->owner), 103);
}
}
$this->ownerClassName = get_class($this->owner);
$this->_ownerClassName = get_class($this->owner);
$this-> ownerClassShortName = $this->getShortClassName($this->ownerClassName);
$this->_ ownerClassShortName = $this->getShortClassName($this->_ ownerClassName);
/** @var ActiveRecord $className */
/** @var ActiveRecord $className */
$className = $this-> ownerClassName;
$className = $this->_ ownerClassName;
$this-> ownerPrimaryKey = $className::primaryKey()[0];
$this->_ ownerPrimaryKey = $className::primaryKey()[0];
if (!isset($this->relativeField)) {
if (!isset($this->relativeField)) {
throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the '
throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the '
@ -295,7 +295,7 @@ class LanguageBehavior extends Behavior
public function setLanguageAttribute($name, $value)
public function setLanguageAttribute($name, $value)
{
{
$this-> languageAttributes[$name] = $value;
$this->_ languageAttributes[$name] = $value;
}
}
protected function indexByLanguage(array $records)
protected function indexByLanguage(array $records)
@ -305,6 +305,7 @@ class LanguageBehavior extends Behavior
$sorted[$record->{$this->languageField}] = $record;
$sorted[$record->{$this->languageField}] = $record;
}
}
unset($records);
unset($records);
return $sorted;
return $sorted;
}
}
@ -341,6 +342,7 @@ class LanguageBehavior extends Behavior
public function findTranslation($language = null)
public function findTranslation($language = null)
{
{
$language = $language ?: $this->defaultLanguage;
$language = $language ?: $this->defaultLanguage;
//$class = call_user_func(array($this->virtualClassName, 'getInstance'));
//$class = call_user_func(array($this->virtualClassName, 'getInstance'));
return $this->virtualClassName::find()
return $this->virtualClassName::find()
->andWhere([$this->relativeField => $this->owner->id])
->andWhere([$this->relativeField => $this->owner->id])
@ -350,12 +352,12 @@ class LanguageBehavior extends Behavior
public function hasLangAttribute($name)
public function hasLangAttribute($name)
{
{
return array_key_exists($name, $this-> languageAttributes);
return array_key_exists($name, $this->_ languageAttributes);
}
}
public function getLangAttribute($name)
public function getLangAttribute($name)
{
{
return $this->hasLangAttribute($name) ? $this->languageAttributes[$name] : null;
return $this->hasLangAttribute($name) ? $this->_ languageAttributes[$name] : null;
}
}
}
}