1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Shop |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\Shop; |
10
|
|
|
use |
11
|
|
|
cs\Cache\Prefix, |
12
|
|
|
cs\Config, |
13
|
|
|
cs\Event, |
14
|
|
|
cs\Language, |
15
|
|
|
cs\User, |
16
|
|
|
cs\CRUD_helpers, |
17
|
|
|
cs\Singleton; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Provides next events:<br> |
21
|
|
|
* Shop/Categories/get<code> |
22
|
|
|
* [ |
23
|
|
|
* 'data' => &$data |
24
|
|
|
* ]</code> |
25
|
|
|
* |
26
|
|
|
* Shop/Categories/get_for_user<code> |
27
|
|
|
* [ |
28
|
|
|
* 'data' => &$data, |
29
|
|
|
* 'user' => $user |
30
|
|
|
* ]</code> |
31
|
|
|
* |
32
|
|
|
* Shop/Categories/add<code> |
33
|
|
|
* [ |
34
|
|
|
* 'id' => $id |
35
|
|
|
* ]</code> |
36
|
|
|
* |
37
|
|
|
* Shop/Categories/set<code> |
38
|
|
|
* [ |
39
|
|
|
* 'id' => $id |
40
|
|
|
* ]</code> |
41
|
|
|
* |
42
|
|
|
* Shop/Categories/del<code> |
43
|
|
|
* [ |
44
|
|
|
* 'id' => $id |
45
|
|
|
* ]</code> |
46
|
|
|
* |
47
|
|
|
* @method static $this instance($check = false) |
48
|
|
|
*/ |
49
|
|
|
class Categories { |
50
|
|
|
use |
51
|
|
|
CRUD_helpers, |
52
|
|
|
Singleton; |
53
|
|
|
|
54
|
|
|
const VISIBLE = 1; |
55
|
|
|
const INVISIBLE = 0; |
56
|
|
|
|
57
|
|
|
protected $data_model = [ |
58
|
|
|
'id' => 'int:1', |
59
|
|
|
'parent' => 'int:0', |
60
|
|
|
'title' => 'ml:text', |
61
|
|
|
'description' => 'ml:html', |
62
|
|
|
'title_attribute' => 'int:1', |
63
|
|
|
'description_attribute' => 'int:1', |
64
|
|
|
'image' => 'string', |
65
|
|
|
'visible' => 'int:0..1', |
66
|
|
|
'attributes' => [ |
67
|
|
|
'data_model' => [ |
68
|
|
|
'id' => 'int:1', |
69
|
|
|
'attribute' => 'int:1' |
70
|
|
|
] |
71
|
|
|
] |
72
|
|
|
]; |
73
|
|
|
protected $table = '[prefix]shop_categories'; |
74
|
|
|
protected $data_model_ml_group = 'Shop/categories'; |
75
|
|
|
protected $data_model_files_tag_prefix = 'Shop/categories'; |
76
|
|
|
/** |
77
|
|
|
* @var Prefix |
78
|
|
|
*/ |
79
|
|
|
protected $cache; |
80
|
|
|
|
81
|
|
|
protected function construct () { |
82
|
|
|
$this->cache = new Prefix('Shop/categories'); |
83
|
|
|
} |
84
|
|
|
/** |
85
|
|
|
* Returns database index |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
protected function cdb () { |
90
|
|
|
return Config::instance()->module('Shop')->db('shop'); |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* Get category |
94
|
|
|
* |
95
|
|
|
* @param int|int[] $id |
96
|
|
|
* |
97
|
|
|
* @return array|false |
98
|
|
|
*/ |
99
|
|
|
function get ($id) { |
100
|
|
|
if (is_array($id)) { |
101
|
|
|
foreach ($id as &$i) { |
102
|
|
|
$i = $this->get($i); |
103
|
|
|
} |
104
|
|
|
return $id; |
105
|
|
|
} |
106
|
|
|
$L = Language::instance(); |
107
|
|
|
$id = (int)$id; |
108
|
|
|
$data = $this->cache->get( |
109
|
|
|
"$id/$L->clang", |
110
|
|
|
function () use ($id) { |
111
|
|
|
$data = $this->read($id); |
112
|
|
|
if ($data) { |
113
|
|
|
$data['attributes'] = $this->clean_nonexistent_attributes($data['attributes']); |
114
|
|
|
} |
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
); |
118
|
|
|
if (!Event::instance()->fire( |
119
|
|
|
'Shop/Categories/get', |
120
|
|
|
[ |
121
|
|
|
'data' => &$data |
122
|
|
|
] |
123
|
|
|
) |
124
|
|
|
) { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
return $data; |
128
|
|
|
} |
129
|
|
|
/** |
130
|
|
|
* Get category data for specific user (some categories may be restricted and so on) |
131
|
|
|
* |
132
|
|
|
* @param int|int[] $id |
133
|
|
|
* @param bool|int $user |
134
|
|
|
* |
135
|
|
|
* @return array|false |
136
|
|
|
*/ |
137
|
|
|
function get_for_user ($id, $user = false) { |
138
|
|
|
if (is_array($id)) { |
139
|
|
|
foreach ($id as $index => &$i) { |
140
|
|
|
$i = $this->get_for_user($i, $user); |
141
|
|
|
if ($i === false) { |
142
|
|
|
unset($id[$index]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
return $id; |
146
|
|
|
} |
147
|
|
|
$user = (int)$user ?: User::instance()->id; |
148
|
|
|
$data = $this->get($id); |
149
|
|
|
if (!Event::instance()->fire( |
150
|
|
|
'Shop/Categories/get_for_user', |
151
|
|
|
[ |
152
|
|
|
'data' => &$data, |
153
|
|
|
'user' => $user |
154
|
|
|
] |
155
|
|
|
) |
156
|
|
|
) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
return $data; |
160
|
|
|
} |
161
|
|
|
/** |
162
|
|
|
* Get array of all categories |
163
|
|
|
* |
164
|
|
|
* @return int[] Array of categories ids |
165
|
|
|
*/ |
166
|
|
|
function get_all () { |
167
|
|
|
return $this->cache->get( |
168
|
|
|
'all', |
169
|
|
|
function () { |
170
|
|
|
return $this->search([], 1, PHP_INT_MAX, 'id', true) ?: []; |
171
|
|
|
} |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* @param int[] $attributes |
176
|
|
|
* |
177
|
|
|
* @return int[] |
178
|
|
|
*/ |
179
|
|
|
protected function clean_nonexistent_attributes ($attributes) { |
180
|
|
|
if (!$attributes) { |
181
|
|
|
return []; |
182
|
|
|
} |
183
|
|
|
$Attributes = Attributes::instance(); |
184
|
|
|
/** |
185
|
|
|
* Remove nonexistent attributes |
186
|
|
|
*/ |
187
|
|
|
foreach ($attributes as $i => &$attribute) { |
188
|
|
|
if (!$Attributes->get($attribute)) { |
189
|
|
|
unset($attributes[$i]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
return $attributes; |
193
|
|
|
} |
194
|
|
|
/** |
195
|
|
|
* Add new category |
196
|
|
|
* |
197
|
|
|
* @param int $parent |
198
|
|
|
* @param string $title |
199
|
|
|
* @param string $description |
200
|
|
|
* @param int $title_attribute Attribute that will be considered as title |
201
|
|
|
* @param int $description_attribute Attribute that will be considered as description |
202
|
|
|
* @param string $image |
203
|
|
|
* @param int $visible `Categories::VISIBLE` or `Categories::INVISIBLE` |
204
|
|
|
* @param int[] $attributes Array of attributes ids used in category |
205
|
|
|
* |
206
|
|
|
* @return false|int Id of created category on success of <b>false</> on failure |
|
|
|
|
207
|
|
|
*/ |
208
|
|
|
function add ($parent, $title, $description, $title_attribute, $description_attribute, $image, $visible, $attributes) { |
209
|
|
|
$attributes = $this->clean_nonexistent_attributes($attributes); |
210
|
|
|
$id = $this->create( |
211
|
|
|
$parent, |
212
|
|
|
trim($title), |
213
|
|
|
trim($description), |
214
|
|
|
in_array($title_attribute, $attributes) ? $title_attribute : $attributes[0], |
215
|
|
|
in_array($description_attribute, $attributes) || $description == 0 ? $description_attribute : $attributes[0], |
216
|
|
|
$image, |
217
|
|
|
$visible, |
218
|
|
|
$attributes |
219
|
|
|
); |
220
|
|
|
if ($id) { |
221
|
|
|
unset($this->cache->all); |
222
|
|
|
Event::instance()->fire( |
223
|
|
|
'Shop/Categories/add', |
224
|
|
|
[ |
225
|
|
|
'id' => $id |
226
|
|
|
] |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
return $id; |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Set data of specified category |
233
|
|
|
* |
234
|
|
|
* @param int $id |
235
|
|
|
* @param int $parent |
236
|
|
|
* @param string $title |
237
|
|
|
* @param string $description |
238
|
|
|
* @param int $title_attribute Attribute that will be considered as title |
239
|
|
|
* @param int $description_attribute Attribute that will be considered as description |
240
|
|
|
* @param string $image |
241
|
|
|
* @param int $visible `Categories::VISIBLE` or `Categories::INVISIBLE` |
242
|
|
|
* @param int[] $attributes Array of attributes ids used in category |
243
|
|
|
* |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
function set ($id, $parent, $title, $description, $title_attribute, $description_attribute, $image, $visible, $attributes) { |
247
|
|
|
$id = (int)$id; |
248
|
|
|
$attributes = $this->clean_nonexistent_attributes($attributes); |
249
|
|
|
$result = $this->update( |
250
|
|
|
$id, |
251
|
|
|
$parent, |
252
|
|
|
trim($title), |
253
|
|
|
trim($description), |
254
|
|
|
in_array($title_attribute, $attributes) ? $title_attribute : $attributes[0], |
255
|
|
|
in_array($description_attribute, $attributes) || $description == 0 ? $description_attribute : $attributes[0], |
256
|
|
|
$image, |
257
|
|
|
$visible |
258
|
|
|
); |
259
|
|
|
if ($result) { |
260
|
|
|
$L = Language::instance(); |
261
|
|
|
unset( |
262
|
|
|
$this->cache->{"$id/$L->clang"}, |
263
|
|
|
$this->cache->all |
264
|
|
|
); |
265
|
|
|
Event::instance()->fire( |
266
|
|
|
'Shop/Categories/set', |
267
|
|
|
[ |
268
|
|
|
'id' => $id |
269
|
|
|
] |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
return $result; |
273
|
|
|
} |
274
|
|
|
/** |
275
|
|
|
* Delete specified category |
276
|
|
|
* |
277
|
|
|
* @param int $id |
278
|
|
|
* |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
function del ($id) { |
282
|
|
|
$id = (int)$id; |
283
|
|
|
$result = $this->delete($id); |
284
|
|
|
if ($result) { |
285
|
|
|
unset( |
286
|
|
|
$this->cache->$id, |
287
|
|
|
$this->cache->all |
288
|
|
|
); |
289
|
|
|
Event::instance()->fire( |
290
|
|
|
'Shop/Categories/del', |
291
|
|
|
[ |
292
|
|
|
'id' => $id |
293
|
|
|
] |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
return $result; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.