1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Attribute; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Hideyo\Ecommerce\Framework\Services\Attribute\Entity\AttributeRepository; |
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\Attribute\Entity\AttributeGroupRepository; |
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
9
|
|
|
|
10
|
|
|
class AttributeService extends BaseService |
11
|
|
|
{ |
12
|
|
|
public function __construct(AttributeRepository $attribute, AttributeGroupRepository $attributeGroup) |
13
|
|
|
{ |
14
|
|
|
$this->repo = $attribute; |
|
|
|
|
15
|
|
|
$this->repoGroup = $attributeGroup; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* laravel validation rules for attribute |
20
|
|
|
* @param boolean $id |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
private function rules($id = false) |
24
|
|
|
{ |
25
|
|
|
$rules = array( |
26
|
|
|
'value' => 'required|unique_with:'.$this->repo->getModel()->getTable().',attribute_group_id' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
if ($id) { |
30
|
|
|
$rules['value'] = $rules['value'].','.$id; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $rules; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* laravel validation rules for attributeGroup |
38
|
|
|
* @param boolean $id |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
private function rulesGroup($id = false) |
42
|
|
|
{ |
43
|
|
|
$rules = array( |
44
|
|
|
'title' => 'required|between:1,65|unique_with:'.$this->repoGroup->getModel()->getTable().', shop_id' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if ($id) { |
48
|
|
|
$rules['title'] = $rules['title'].','.$id.' = id'; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $rules; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* create a attribute |
56
|
|
|
* @param array $attributes |
57
|
|
|
* @param integer $attributeGroupId |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function create(array $attributes, $attributeGroupId) |
61
|
|
|
{ |
62
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
63
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
64
|
|
|
$attributes['attribute_group_id'] = $attributeGroupId; |
65
|
|
|
|
66
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
67
|
|
|
|
68
|
|
|
if ($validator->fails()) { |
69
|
|
|
return $validator; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->repo->getModel()->fill($attributes); |
73
|
|
|
$this->repo->getModel()->save(); |
74
|
|
|
return $this->repo->getModel(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* create a attribute group |
79
|
|
|
* @param array $attributes |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function createGroup(array $attributes) |
83
|
|
|
{ |
84
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
85
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
86
|
|
|
$validator = Validator::make($attributes, $this->rulesGroup()); |
87
|
|
|
|
88
|
|
|
if ($validator->fails()) { |
89
|
|
|
return $validator; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->repoGroup->getModel()->fill($attributes); |
93
|
|
|
$this->repoGroup->getModel()->save(); |
94
|
|
|
return $this->repoGroup->getModel(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function updateById(array $attributes, $attributeGroupId, $id) |
98
|
|
|
{ |
99
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
100
|
|
|
$attributes['attribute_group_id'] = $attributeGroupId; |
101
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
102
|
|
|
|
103
|
|
|
$validator = Validator::make($attributes, $this->rules($id)); |
104
|
|
|
|
105
|
|
|
if ($validator->fails()) { |
106
|
|
|
return $validator; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$model = $this->find($id); |
110
|
|
|
$model->fill($attributes); |
111
|
|
|
$model->save(); |
112
|
|
|
|
113
|
|
|
return $model; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function updateGroupById(array $attributes, $id) |
117
|
|
|
{ |
118
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
119
|
|
|
$validator = Validator::make($attributes, $this->rulesGroup($id)); |
120
|
|
|
if ($validator->fails()) { |
121
|
|
|
return $validator; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$model = $this->findGroup($id); |
125
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
126
|
|
|
|
127
|
|
|
$model->fill($attributes); |
128
|
|
|
$model->save(); |
129
|
|
|
|
130
|
|
|
return $model; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* select all attribute Groups |
135
|
|
|
* @return object |
136
|
|
|
*/ |
137
|
|
|
public function selectAllGroups() |
138
|
|
|
{ |
139
|
|
|
return $this->repoGroup->selectAll(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* get attribute group model |
144
|
|
|
* @return object |
145
|
|
|
*/ |
146
|
|
|
public function getGroupModel() |
147
|
|
|
{ |
148
|
|
|
return $this->repoGroup->getModel(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* find a attribute group model |
153
|
|
|
* @return object |
154
|
|
|
*/ |
155
|
|
|
public function findGroup($groupId) |
156
|
|
|
{ |
157
|
|
|
return $this->repoGroup->find($groupId); |
158
|
|
|
} |
159
|
|
|
} |