forked from DevGroup-ru/dotplant2
/
OptionGenerate.php
102 lines (91 loc) · 3.1 KB
/
OptionGenerate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace app\modules\shop\widgets;
use app\models\Object;
use app\models\Property;
use app\models\PropertyGroup;
use kartik\helpers\Html;
use kartik\icons\Icon;
use Yii;
use yii\base\Widget;
use yii\db\Query;
use yii\helpers\Json;
use yii\helpers\Url;
class OptionGenerate extends Widget
{
public $viewFile = 'OptionGenerate';
public $genButton;
public $addButton;
public $model;
public $form;
public $property_groups_to_add;
public $object;
public $footer;
public function run()
{
$this->genButton = Html::a(
Icon::show('edit') . Yii::t('app', 'Generate'),
'#',
['class' => 'btn btn-success', 'id' => 'btn-generate']
);
$parent_id = $this->model->main_category_id;
$owner_id = $this->model->id;
$this->addButton = Html::a(
Icon::show('plus') . Yii::t('app', 'Add'),
Url::toRoute(['/shop/backend-product/edit',
'parent_id' => $parent_id,
'owner_id' => $owner_id,
'returnUrl' => \app\backend\components\Helper::getReturnUrl(),
]),
['class' => 'btn btn-success', 'id' => 'btn-add']
);
if (!empty($this->footer)) {
$this->footer = Html::tag(
'div',
$this->addButton.' '.$this->genButton,
['class'=>'widget-footer']
);
}
$this->object = Object::getForClass(get_class($this->model));
$rest_pg = (new Query())
->select('id, name')
->from(PropertyGroup::tableName())
->where(
['object_id' => $this->object->id]
)
->orderBy('sort_order')
->all();
$this->property_groups_to_add = [];
foreach ($rest_pg as $row) {
$this->property_groups_to_add[$row['id']] = $row['name'];
}
$optionGenerate = Json::decode($this->model->option_generate);
if (null === PropertyGroup::findOne($optionGenerate['group'])) {
$this->model->option_generate = $optionGenerate = null;
}
$groupModel = null;
if (isset($optionGenerate['group'])) {
$groupModel = PropertyGroup::findOne($optionGenerate['group']);
$properties = Property::getForGroupId($optionGenerate['group']);
} else {
$group_ids = array_keys($this->property_groups_to_add);
$group_id = array_shift($group_ids);
$groupModel = PropertyGroup::findOne($group_id);
$properties = Property::getForGroupId($group_id);
}
if (is_null($groupModel)) {
$groupModel = new PropertyGroup();
}
return $this->render(
$this->viewFile,
[
'model' => $this->model,
'form' => $this->form,
'groups' => $this->property_groups_to_add,
'groupModel' => $groupModel,
'properties' => $properties,
'optionGenerate' => $optionGenerate,
'footer' => $this->footer,
]
);
}
}