@ -6,6 +6,7 @@
namespace core\behaviors;
use core\components\language\LanguageVirtualAbstract;
use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;
@ -67,10 +68,10 @@ class LanguageBehavior extends Behavior
*/
public $forceDelete = false;
private $ ownerClassName;
private $ ownerClassShortName;
private $ ownerPrimaryKey;
private $ languageAttributes = [];
private $_ ownerClassName;
private $_ ownerClassShortName;
private $_ ownerPrimaryKey;
private $_ languageAttributes = [];
/**
* Control events firing
@ -83,16 +84,15 @@ class LanguageBehavior extends Behavior
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
//ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
public function attach( $owner ) {
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);
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
@ -108,12 +108,12 @@ class LanguageBehavior extends Behavior
. get_class($this->owner), 103);
}
$this->ownerClassName = get_class($this->owner);
$this-> ownerClassShortName = $this->getShortClassName($this->ownerClassName);
$this->_ownerClassName = get_class($this->owner);
$this->_ ownerClassShortName = $this->getShortClassName($this->_ ownerClassName);
/** @var ActiveRecord $className */
$className = $this-> ownerClassName;
$this-> ownerPrimaryKey = $className::primaryKey()[0];
$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 '
@ -295,7 +295,7 @@ class LanguageBehavior extends Behavior
public function setLanguageAttribute($name, $value)
{
$this-> languageAttributes[$name] = $value;
$this->_ languageAttributes[$name] = $value;
}
protected function indexByLanguage(array $records)
@ -305,6 +305,7 @@ class LanguageBehavior extends Behavior
$sorted[$record->{$this->languageField}] = $record;
}
unset($records);
return $sorted;
}
@ -341,6 +342,7 @@ class LanguageBehavior extends Behavior
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])
@ -350,12 +352,12 @@ class LanguageBehavior extends Behavior
public function hasLangAttribute($name)
{
return array_key_exists($name, $this-> languageAttributes);
return array_key_exists($name, $this->_ languageAttributes);
}
public function getLangAttribute($name)
{
return $this->hasLangAttribute($name) ? $this->languageAttributes[$name] : null;
return $this->hasLangAttribute($name) ? $this->_ languageAttributes[$name] : null;
}
}