1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The term is the core element of the taxonomies system. |
5
|
|
|
*/ |
6
|
|
|
namespace Rocket\Taxonomy; |
7
|
|
|
|
8
|
|
|
use ArrayAccess; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Rocket\Taxonomy\Exception\UndefinedLanguageException; |
11
|
|
|
use Rocket\Taxonomy\Model\TermData; |
12
|
|
|
use Rocket\Taxonomy\Support\Laravel5\Facade as T; |
13
|
|
|
use Rocket\Translation\Support\Laravel5\Facade as I18N; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Taxonomy term |
17
|
|
|
* |
18
|
|
|
* The terms are instantiated automatically. |
19
|
|
|
* |
20
|
|
|
* you can then use them in multiple ways |
21
|
|
|
* |
22
|
|
|
* $term = new Term... |
23
|
|
|
* |
24
|
|
|
* echo $term; |
25
|
|
|
* -> will output the term in the current language |
26
|
|
|
* |
27
|
|
|
* $term->title() |
28
|
|
|
* $term['title'] |
29
|
|
|
* -> will return the term in the current language |
30
|
|
|
* |
31
|
|
|
* $term->title('en') |
32
|
|
|
* -> will output the term in English |
33
|
|
|
* |
34
|
|
|
* $term->description() |
35
|
|
|
* $term['description'] |
36
|
|
|
* -> will return the term's content in the current language |
37
|
|
|
* |
38
|
|
|
* $term->description('en') |
39
|
|
|
* -> will output the term's content in English |
40
|
|
|
* |
41
|
|
|
* $term->translated() |
42
|
|
|
* $term['translated'] |
43
|
|
|
* -> true if it was translated in the current language |
44
|
|
|
* |
45
|
|
|
* $term->translated('en') |
46
|
|
|
* -> true if it was translated in english |
47
|
|
|
*/ |
48
|
|
|
class Term implements ArrayAccess, Arrayable |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Data for the term |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $container = [ |
56
|
|
|
'has_translations' => true, |
57
|
|
|
'type' => 0, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create the term |
62
|
|
|
* |
63
|
69 |
|
* @param $data |
64
|
|
|
*/ |
65
|
69 |
|
public function __construct($data) |
66
|
69 |
|
{ |
67
|
|
|
$this->container = array_merge($this->container, $data); |
68
|
|
|
foreach ($this->container as $key => &$value) { |
69
|
|
|
if (strpos($key, 'lang') === false) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($value instanceof TermData) { |
74
|
3 |
|
continue; |
75
|
|
|
} |
76
|
3 |
|
|
77
|
|
|
$value = new TermData($value); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns true if the vocabulary of the term can be translated |
83
|
|
|
* |
84
|
33 |
|
* @return bool |
85
|
|
|
*/ |
86
|
33 |
|
protected function hasTranslations() |
87
|
|
|
{ |
88
|
|
|
return $this->container['has_translations']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns true if the term is a (sub)category |
93
|
|
|
* |
94
|
6 |
|
* @return bool |
95
|
|
|
*/ |
96
|
6 |
|
public function isSubcategory() |
97
|
|
|
{ |
98
|
|
|
return $this->container['type'] == T::TERM_CATEGORY; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the type of the term (category or term) |
103
|
|
|
* |
104
|
3 |
|
* @return mixed |
105
|
|
|
*/ |
106
|
3 |
|
public function getType() |
107
|
|
|
{ |
108
|
|
|
return $this->container['type']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the term id |
113
|
|
|
* |
114
|
21 |
|
* @return int |
115
|
|
|
*/ |
116
|
21 |
|
public function id() |
117
|
|
|
{ |
118
|
|
|
return $this->container['term_id']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the term's title |
123
|
|
|
* |
124
|
|
|
* @param string $language |
125
|
27 |
|
* @return string |
126
|
|
|
*/ |
127
|
27 |
|
public function title($language = '') |
128
|
|
|
{ |
129
|
|
|
return $this->string('title', $language); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the term's description |
134
|
|
|
* |
135
|
|
|
* @param string $language |
136
|
15 |
|
* @return string |
137
|
|
|
*/ |
138
|
15 |
|
public function description($language = '') |
139
|
|
|
{ |
140
|
|
|
return $this->string('description', $language); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return the textual version of the term |
145
|
|
|
* |
146
|
|
|
* @param string $key |
147
|
|
|
* @param string $language |
148
|
30 |
|
* @return string |
149
|
|
|
*/ |
150
|
30 |
|
public function string($key, $language = '') |
151
|
6 |
|
{ |
152
|
|
|
if (!$this->hasTranslations()) { |
153
|
|
|
return $this->container['lang'][$key]; |
154
|
24 |
|
} |
155
|
24 |
|
|
156
|
24 |
|
if ($language == '') { |
157
|
|
|
$language = I18N::getCurrent(); |
158
|
24 |
|
} |
159
|
21 |
|
|
160
|
|
|
if (array_key_exists('lang_' . $language, $this->container)) { |
161
|
|
|
return $this->container['lang_' . $language][$key]; |
162
|
6 |
|
} |
163
|
|
|
|
164
|
|
|
return ''; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Is it translated in this language ? |
169
|
|
|
* |
170
|
|
|
* @param string $language |
171
|
18 |
|
* @return bool |
172
|
|
|
*/ |
173
|
18 |
|
public function translated($language = '') |
174
|
6 |
|
{ |
175
|
|
|
if (!$this->hasTranslations()) { |
176
|
|
|
return true; |
177
|
12 |
|
} |
178
|
12 |
|
|
179
|
12 |
|
if ($language == '') { |
180
|
|
|
$language = I18N::getCurrent(); |
181
|
12 |
|
} |
182
|
12 |
|
|
183
|
|
|
if (array_key_exists('lang_' . $language, $this->container)) { |
184
|
|
|
return $this->container['lang_' . $language]->translated; |
185
|
3 |
|
} |
186
|
|
|
|
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add one parent to a term |
192
|
|
|
* |
193
|
|
|
* @param int $parent_id |
194
|
15 |
|
* @return mixed |
195
|
|
|
*/ |
196
|
15 |
|
public function addParent($parent_id) |
197
|
|
|
{ |
198
|
|
|
return T::addParent($this->id(), $parent_id); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Add a list of parents to a term |
203
|
|
|
* |
204
|
6 |
|
* @param array<int> $parent_ids |
205
|
|
|
*/ |
206
|
6 |
|
public function addParents(array $parent_ids) |
207
|
|
|
{ |
208
|
|
|
return T::addParents($this->id(), $parent_ids); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Replace the parents on a term by this one |
213
|
|
|
* |
214
|
3 |
|
* @param int $parent_id |
215
|
|
|
*/ |
216
|
3 |
|
public function setParent($parent_id) |
217
|
|
|
{ |
218
|
3 |
|
T::unsetParents($this->id()); |
219
|
3 |
|
|
220
|
|
|
$this->addParent($parent_id); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Replace the parents by this list |
225
|
|
|
* |
226
|
3 |
|
* @param array<int> $parent_ids |
227
|
|
|
*/ |
228
|
3 |
|
public function setParents(array $parent_ids) |
229
|
|
|
{ |
230
|
3 |
|
T::unsetParents($this->id()); |
231
|
3 |
|
|
232
|
|
|
$this->addParents($parent_ids); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Retrieve a language for edition |
237
|
|
|
* |
238
|
|
|
* @param string $language |
239
|
|
|
* @throws UndefinedLanguageException |
240
|
6 |
|
* @return Model\TermData |
241
|
|
|
*/ |
242
|
6 |
|
public function editLanguage($language = '') |
243
|
3 |
|
{ |
244
|
|
|
if (!array_key_exists('lang_' . $language, $this->container)) { |
245
|
|
|
throw new UndefinedLanguageException; |
246
|
3 |
|
} |
247
|
|
|
|
248
|
|
|
return $this->container['lang_' . $language]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Array access |
253
|
|
|
* |
254
|
|
|
* @param mixed $offset |
255
|
3 |
|
* @param mixed $value |
256
|
|
|
*/ |
257
|
3 |
|
public function offsetSet($offset, $value) |
258
|
3 |
|
{ |
259
|
3 |
|
if (!is_null($offset)) { |
260
|
3 |
|
$this->container[$offset] = $value; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Array Access |
266
|
|
|
* |
267
|
|
|
* @param mixed $offset |
268
|
6 |
|
* @return bool |
269
|
|
|
*/ |
270
|
6 |
|
public function offsetExists($offset) |
271
|
|
|
{ |
272
|
|
|
return isset($this->container[$offset]); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Array Access |
277
|
|
|
* |
278
|
3 |
|
* @param mixed $offset |
279
|
|
|
*/ |
280
|
3 |
|
public function offsetUnset($offset) |
281
|
3 |
|
{ |
282
|
|
|
unset($this->container[$offset]); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Array access |
287
|
|
|
* |
288
|
|
|
* @param mixed $offset |
289
|
21 |
|
* @return mixed|null |
290
|
|
|
*/ |
291
|
21 |
|
public function offsetGet($offset) |
292
|
9 |
|
{ |
293
|
|
|
if (!in_array($offset, ['title', 'description', 'translated'])) { |
294
|
|
|
return isset($this->container[$offset]) ? $this->container[$offset] : null; |
295
|
12 |
|
} |
296
|
3 |
|
|
297
|
|
|
if (!$this->hasTranslations()) { |
298
|
|
|
return $this->container['lang'][$offset]; |
299
|
9 |
|
} |
300
|
6 |
|
|
301
|
|
|
if (array_key_exists('lang_' . I18N::getCurrent(), $this->container)) { |
302
|
3 |
|
return $this->container['lang_' . I18N::getCurrent()]->{$offset}; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* echo Term - outputs the term in the current language |
308
|
|
|
* |
309
|
6 |
|
* @return string |
310
|
|
|
*/ |
311
|
6 |
|
public function __toString() |
312
|
|
|
{ |
313
|
|
|
return $this->title(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function toArray() |
317
|
|
|
{ |
318
|
|
|
return array_map( |
319
|
|
|
function ($value) { |
320
|
|
|
return $value instanceof Arrayable ? $value->toArray() : $value; |
321
|
|
|
}, |
322
|
|
|
$this->container |
323
|
|
|
); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|