Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAYEC77 committed Apr 24, 2018
1 parent 8449cbd commit 7486492
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 126 deletions.
52 changes: 46 additions & 6 deletions src/behaviors/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,37 @@

use nullref\eav\models\Entity as EntityModel;
use yii\base\Behavior;
use yii\base\InvalidValueException;
use yii\db\ActiveRecord;
use yii\validators\Validator;

/**
* Entity behavior that allow to use dynamic attributes in model instance
*
* To use Entity behavior, insert the following code to your ActiveRecord class:
*
* ```php
* use nullref\eav\behaviors\Entity;
* use nullref\eav\models\Entity as EntityModel;
*
* public function behaviors()
* {
* return [
* 'eav' => [
* 'class' => Entity::class,
* 'entity' => function () {
* return new EntityModel([
* 'sets' => [
* Set::findOne(['code' => 'catalog']),
* ],
* ]);
* },
* ],
* ];
* }
*
* @package nullref\eav\behaviors
*/
class Entity extends Behavior
{
/** @var string */
Expand All @@ -35,36 +63,47 @@ public function events()
}

/**
*
* @throws \Exception
* @throws \Throwable
* @throws \yii\db\Exception
*/
public function afterFind()
{
$this->entityModel->find();
}

/**
*
* @throws \yii\db\Exception
*/
public function afterSave()
{
$this->entityModel->save();
}

/**
*
* @throws \Exception
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*/
public function afterDelete()
{
$this->entityModel->delete();
}

/**
* @param ActiveRecord $owner
* Configure entity model and update validators for owner model
*
* @param \yii\base\Component|ActiveRecord $owner
* @throws \yii\base\InvalidConfigException
*/
public function attach($owner)
{
parent::attach($owner);
$this->entityModel = EntityModel::create($this->entity, $owner);
$pk = $owner->primaryKey;
if (!is_scalar($pk)) {
throw new InvalidValueException('Entity primary key should be scalar');
}
$this->entityModel = EntityModel::create($this->entity, $pk);

$validators = $owner->getValidators();
$attributes = $this->entityModel->attributes();
Expand All @@ -74,6 +113,7 @@ public function attach($owner)
/**
* @param string $name
* @return EntityModel|mixed
* @throws \yii\base\UnknownPropertyException
*/
public function __get($name)
{
Expand All @@ -90,7 +130,7 @@ public function __get($name)
/**
* @param string $name
* @param mixed $value
* @return mixed|void
* @throws \yii\base\UnknownPropertyException
*/
public function __set($name, $value)
{
Expand Down
25 changes: 0 additions & 25 deletions src/components/EntityManager.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/controllers/admin/AttributeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/admin/SetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
Expand Down
108 changes: 54 additions & 54 deletions src/helpers/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace nullref\eav\helpers;


use nullref\eav\models\Attribute;
use nullref\eav\models\Entity;
use nullref\eav\widgets\ActiveRangeInputGroup;
use app\modules\eav\models\Attribute;
use app\modules\eav\models\Entity;
use app\modules\eav\widgets\ActiveRangeInputGroup;
use kartik\select2\Select2;
use mcms\xeditable\XEditableColumn;
use yii\base\Model;
Expand Down Expand Up @@ -47,8 +47,8 @@ public static function getGridColumnsInternal(Entity $entity, $searchModel = nul
continue;
}
$column = [
'attribute' => $code,
'label' => $item['name'],
'attribute' => $code,
];
if (isset($item['config']['editable']) && $item['config']['editable']) {
$column = self::getEditableConfig($column, $item);
Expand All @@ -62,6 +62,56 @@ public static function getGridColumnsInternal(Entity $entity, $searchModel = nul
return $result;
}

protected static function getEditableConfig($column, $item)
{
$column['class'] = XEditableColumn::class;
$column['editable'] = [];
$column['url'] = Url::to(['editable']);
$column['format'] = 'raw';

$column['dataType'] = 'text';

if (isset($item['items'])) {
$column['dataType'] = 'select';
$column['editable'] = [
'source' => $item['items']
];
}
return $column;
}

public static function addFilter($column, $item, $searchModel)
{
if ($searchModel) {
if (isset($item['items'])) {
$column['filter'] = Select2::widget([
'data' => $item['items'],
'attribute' => $column['attribute'],
'options' => ['placeholder' => ' '],
'model' => $searchModel,
'pluginOptions' => [
'allowClear' => true,
],
]);
}
$column['headerOptions'] = [
'style' => 'min-width: 100px',
];
if (in_array($item['type'], [Attribute::TYPE_INT, Attribute::TYPE_DECIMAL])) {

$column['filter'] = ActiveRangeInputGroup::widget([
'attributeFrom' => $column['attribute'] . '[from]',
'attributeTo' => $column['attribute'] . '[to]',
'model' => $searchModel,
'options' => [
'style' => 'min-width: 100px',
],
]);
}
}
return $column;
}

public static function addValue($column, $item)
{
if (isset($item['items'])) {
Expand Down Expand Up @@ -127,54 +177,4 @@ public static function addValue($column, $item)
}
return $column;
}

public static function addFilter($column, $item, $searchModel)
{
if ($searchModel) {
if (isset($item['items'])) {
$column['filter'] = Select2::widget([
'data' => $item['items'],
'attribute' => $column['attribute'],
'options' => ['placeholder' => ' '],
'model' => $searchModel,
'pluginOptions' => [
'allowClear' => true,
],
]);
}
$column['headerOptions'] = [
'style' => 'min-width: 100px',
];
if (in_array($item['type'], [Attribute::TYPE_INT, Attribute::TYPE_DECIMAL])) {

$column['filter'] = ActiveRangeInputGroup::widget([
'attributeFrom' => $column['attribute'] . '[from]',
'attributeTo' => $column['attribute'] . '[to]',
'model' => $searchModel,
'options' => [
'style' => 'min-width: 100px',
],
]);
}
}
return $column;
}

protected static function getEditableConfig($column, $item)
{
$column['class'] = XEditableColumn::class;
$column['editable'] = [];
$column['url'] = Url::to(['editable']);
$column['format'] = 'raw';

$column['dataType'] = 'text';

if (isset($item['items'])) {
$column['dataType'] = 'select';
$column['editable'] = [
'source' => $item['items']
];
}
return $column;
}
}
2 changes: 1 addition & 1 deletion src/helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class Helper
{
/**
* @return null|\yii\base\Module|\nullref\eav\Module
* @return \nullref\eav\Module|\yii\base\Module
*/
public static function getModule()
{
Expand Down
8 changes: 5 additions & 3 deletions src/helpers/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace nullref\eav\helpers;


use nullref\eav\models\Attribute;
use nullref\eav\models\attribute\Option;
use nullref\eav\models\attribute\Set;
use app\modules\eav\models\Attribute;
use app\modules\eav\models\attribute\Option;
use app\modules\eav\models\attribute\Set;

class Migration
{
Expand Down Expand Up @@ -79,4 +79,6 @@ public static function createOption($attributeId, $config)
return $option->save();
}


//@TODO add revert methods
}
8 changes: 0 additions & 8 deletions src/migrations/M170520151321EAV__create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ public function safeUp()
'set_id' => $this->integer(),
]);

//@TODO remove if it doesn't need
$this->createTable('{{%eav_entity_attribute}}', [
'attribute_id' => $this->primaryKey(),
'entity_id' => $this->integer(),
'sort_order' => $this->integer(),
]);

$this->createTable('{{%eav_attribute_option}}', [
'id' => $this->primaryKey(),
'attribute_id' => $this->integer(),
Expand Down Expand Up @@ -82,7 +75,6 @@ public function safeDown()
$this->dropTable('{{%eav_entity_value_decimal}}');
$this->dropTable('{{%eav_entity_value_int}}');
$this->dropTable('{{%eav_attribute_option}}');
$this->dropTable('{{%eav_entity_attribute}}');
$this->dropTable('{{%eav_attribute}}');
$this->dropTable('{{%eav_attribute_set}}');
}
Expand Down
8 changes: 5 additions & 3 deletions src/models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Attribute extends ActiveRecord
const TYPE_IMAGE = 'image';
const TYPE_URL = 'url';
const TYPE_TEXT = 'text';
const TYPE_JSON = 'json';

protected $_optionsMap = null;

Expand Down Expand Up @@ -80,7 +81,7 @@ public static function getTypesMap()
*/
public function getSet()
{
return $this->hasOne(Set::className(), ['id' => 'set_id']);
return $this->hasOne(Set::class, ['id' => 'set_id']);
}

/**
Expand Down Expand Up @@ -129,11 +130,11 @@ public function attributeLabels()
}

/**
* @return OptionQuery
* @return OptionQuery|\yii\db\ActiveQuery
*/
public function getOptions()
{
return $this->hasMany(Option::className(), ['attribute_id' => 'id']);
return $this->hasMany(Option::class, ['attribute_id' => 'id']);
}

/**
Expand Down Expand Up @@ -190,6 +191,7 @@ public function createValue()
/** @var Value $model */
$model = new $class;

$model->attribute_id = $this->id;
$model->attributeModel = $this;

return $model;
Expand Down
Loading

0 comments on commit 7486492

Please sign in to comment.