1 <?php
2
3 /* --------------------------------------------------------------
4 Category.inc.php 2015-12-16
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 MainFactory::load_class('CategoryInterface');
14
15 /**
16 * Class Category
17 *
18 * This class represents a shop category which is not persisted yet.
19 *
20 * @category System
21 * @package Category
22 * @subpackage Entities
23 */
24 class Category implements CategoryInterface
25 {
26 /**
27 * Contains if category is active or not.
28 *
29 * @var bool
30 */
31 protected $active = false;
32
33
34 /**
35 * Parent ID of the Category.
36 *
37 * @var int
38 */
39 protected $parentId = 0;
40
41
42 /**
43 * Sort order value.
44 *
45 * @var int
46 */
47 protected $sortOrder = 0;
48
49
50 /**
51 * Date time when the category was added.
52 *
53 * @var DateTime
54 */
55 protected $dateAdded;
56
57
58 /**
59 * Date time when the category was modified last.
60 *
61 * @var DateTime
62 */
63 protected $lastModified;
64
65
66 /**
67 * Category settings.
68 *
69 * @var CategorySettingsInterface
70 */
71 protected $settings;
72
73
74 /**
75 * Names of the category.
76 *
77 * @var array
78 */
79 protected $names = array();
80
81
82 /**
83 * Heading Titles of the category.
84 *
85 * @var array
86 */
87 protected $headingTitles = array();
88
89
90 /**
91 * Description of the category.
92 *
93 * @var array
94 */
95 protected $descriptions = array();
96
97
98 /**
99 * Meta title of the category.
100 *
101 * @var array
102 */
103 protected $metaTitles = array();
104
105
106 /**
107 * Meta description of the category.
108 *
109 * @var array
110 */
111 protected $metaDescriptions = array();
112
113
114 /**
115 * Meta keywords of the category.
116 *
117 * @var array
118 */
119 protected $metaKeywords = array();
120
121
122 /**
123 * Url keywords of the category.
124 *
125 * @var array
126 */
127 protected $urlKeywords = array();
128
129
130 /**
131 * Path to an image file.
132 *
133 * @var string
134 */
135 protected $image = '';
136
137
138 /**
139 * Alt texts of the image.
140 *
141 * @var array
142 */
143 protected $altTexts = array();
144
145
146 /**
147 * Path to an icon file.
148 *
149 * @var string
150 */
151 protected $icon = '';
152
153
154 /**
155 * Addon values.
156 *
157 * @var EditableKeyValueCollection
158 */
159 protected $addonValues;
160
161
162 /**
163 * Category constructor.
164 *
165 * @param CategorySettingsInterface $settings The category settings.
166 */
167 public function __construct(CategorySettingsInterface $settings)
168 {
169 $this->settings = $settings;
170 $this->dateAdded = new DateTime();
171 $this->lastModified = new DateTime();
172 $this->addonValues = MainFactory::create('EditableKeyValueCollection', array());
173 }
174
175
176 /**
177 * Checks if the category is active or not.
178 *
179 * @return bool
180 */
181 public function isActive()
182 {
183 return $this->active;
184 }
185
186
187 /**
188 * Sets whether category is active or not.
189 *
190 * @param BoolType $status Category active or not?
191 *
192 * @return Category Same instance for chained method calls.
193 */
194 public function setActive(BoolType $status)
195 {
196 $this->active = $status->asBool();
197
198 return $this;
199 }
200
201
202 /**
203 * Returns the ID of the parent category.
204 *
205 * @return int The ID of the parent category
206 */
207 public function getParentId()
208 {
209 return $this->parentId;
210 }
211
212
213 /**
214 * Sets the parent ID of the category.
215 *
216 * @param IdType $categoryId The parent ID.
217 *
218 * @return Category Same instance for chained method calls.
219 */
220 public function setParentId(IdType $categoryId)
221 {
222 $this->parentId = $categoryId->asInt();
223
224 return $this;
225 }
226
227
228 /**
229 * Returns the sort order value.
230 *
231 * @return int Sort order value.
232 */
233 public function getSortOrder()
234 {
235 return $this->sortOrder;
236 }
237
238
239 /**
240 * Sets the sort order to the given value.
241 *
242 * @param IntType $sortOrder Order value.
243 *
244 * @return Category Same instance for chained method calls.
245 */
246 public function setSortOrder(IntType $sortOrder)
247 {
248 $this->sortOrder = $sortOrder->asInt();
249
250 return $this;
251 }
252
253
254 /**
255 * Returns the datetime when the category was added.
256 *
257 * @return DateTime
258 */
259 public function getAddedDateTime()
260 {
261 return $this->dateAdded;
262 }
263
264
265 /**
266 * Sets the datetime when the category was added.
267 *
268 * @param DateTime $added Datetime of when the category has been added.
269 *
270 * @return Category Same instance for chained method calls.
271 */
272 public function setAddedDateTime(DateTime $added)
273 {
274 $this->dateAdded = $added;
275
276 return $this;
277 }
278
279
280 /**
281 * Returns the datetime when the category was modified last.
282 *
283 * @return DateTime
284 */
285 public function getLastModifiedDateTime()
286 {
287 return $this->lastModified;
288 }
289
290
291 /**
292 * Sets the date time when the category was modified last.
293 *
294 * @param DateTime $modified Datetime of when the category has been lastly modified.
295 *
296 * @return Category Same instance for chained method calls.
297 */
298 public function setLastModifiedDateTime(DateTime $modified)
299 {
300 $this->lastModified = $modified;
301
302 return $this;
303 }
304
305
306 /**
307 * Returns the settings of the category.
308 *
309 * @return CategorySettingsInterface
310 */
311 public function getSettings()
312 {
313 return $this->settings;
314 }
315
316
317 /**
318 * Returns the name of the category in the language with the provided language code.
319 *
320 * @param LanguageCode $language Two letter language code.
321 *
322 * @return string
323 */
324 public function getName(LanguageCode $language)
325 {
326 return (string)$this->names[(string)$language];
327 }
328
329
330 /**
331 * Sets the name of the category for the language with the provided language code.
332 *
333 * @param StringType $text Category name.
334 * @param LanguageCode $language Two letter language code.
335 *
336 * @return Category Same instance for chained method calls.
337 */
338 public function setName(StringType $text, LanguageCode $language)
339 {
340 $this->names[(string)$language] = $text->asString();
341
342 return $this;
343 }
344
345
346 /**
347 * Returns the heading title of the category in the language with the provided language code.
348 *
349 * @param LanguageCode $language Two letter language code.
350 *
351 * @return string
352 */
353 public function getHeadingTitle(LanguageCode $language)
354 {
355 return (string)$this->headingTitles[(string)$language];
356 }
357
358
359 /**
360 * Sets the heading title of the category for the language with the provided language code.
361 *
362 * @param StringType $text Heading title of the category.
363 * @param LanguageCode $language Two letter language code.
364 *
365 * @return Category Same instance for chained method calls.
366 */
367 public function setHeadingTitle(StringType $text, LanguageCode $language)
368 {
369 $this->headingTitles[(string)$language] = $text->asString();
370
371 return $this;
372 }
373
374
375 /**
376 * Returns the description of the category in the language with the provided language code.
377 *
378 * @param LanguageCode $language Two letter language code.
379 *
380 * @return string
381 */
382 public function getDescription(LanguageCode $language)
383 {
384 return (string)$this->descriptions[(string)$language];
385 }
386
387
388 /**
389 * Sets the description of the category for the language with the provided language code.
390 *
391 * @param StringType $text Description of the category.
392 * @param LanguageCode $language Two letter language code.
393 *
394 * @return Category Same instance for chained method calls.
395 */
396 public function setDescription(StringType $text, LanguageCode $language)
397 {
398 $this->descriptions[(string)$language] = $text->asString();
399
400 return $this;
401 }
402
403
404 /**
405 * Returns the meta title of the category in the language with the provided language code.
406 *
407 * @param LanguageCode $language Two letter language code.
408 *
409 * @return string
410 */
411 public function getMetaTitle(LanguageCode $language)
412 {
413 return (string)$this->metaTitles[(string)$language];
414 }
415
416
417 /**
418 * Sets the meta title of the category for the language with the provided language code.
419 *
420 * @param StringType $text Meta title for the category.
421 * @param LanguageCode $language Two letter language code.
422 *
423 * @return Category Same instance for chained method calls.
424 */
425 public function setMetaTitle(StringType $text, LanguageCode $language)
426 {
427 $this->metaTitles[(string)$language] = $text->asString();
428
429 return $this;
430 }
431
432
433 /**
434 * Returns the meta title of the category in the language with the provided language code.
435 *
436 * @param LanguageCode $language Two letter language code.
437 *
438 * @return string
439 */
440 public function getMetaDescription(LanguageCode $language)
441 {
442 return (string)$this->metaDescriptions[(string)$language];
443 }
444
445
446 /**
447 * Sets the meta title of the category for the language with the provided language code.
448 *
449 * @param StringType $text Meta title of the category.
450 * @param LanguageCode $language Two letter language code.
451 *
452 * @return Category Same instance for chained method calls.
453 */
454 public function setMetaDescription(StringType $text, LanguageCode $language)
455 {
456 $this->metaDescriptions[(string)$language] = $text->asString();
457
458 return $this;
459 }
460
461
462 /**
463 * Returns the meta keywords of the category in the language with the provided language code.
464 *
465 * @param LanguageCode $language Two letter language code.
466 *
467 * @return string
468 */
469 public function getMetaKeywords(LanguageCode $language)
470 {
471 return (string)$this->metaKeywords[(string)$language];
472 }
473
474
475 /**
476 * Sets the meta keywords of the category for the language with the provided language code.
477 *
478 * @param StringType $text Meta keywords of the category.
479 * @param LanguageCode $language Two letter language code.
480 *
481 * @return Category Same instance for chained method calls.
482 */
483 public function setMetaKeywords(StringType $text, LanguageCode $language)
484 {
485 $this->metaKeywords[(string)$language] = $text->asString();
486
487 return $this;
488 }
489
490
491 /**
492 * Returns the url keywords of the category in the language with the provided language code.
493 *
494 * @param LanguageCode $language Two letter language code.
495 *
496 * @return string
497 */
498 public function getUrlKeywords(LanguageCode $language)
499 {
500 return (string)$this->urlKeywords[(string)$language];
501 }
502
503
504 /**
505 * Sets the url keywords of the category for the language with the provided language code.
506 *
507 * @param StringType $text URL keywords of the category.
508 * @param LanguageCode $language Two letter language code.
509 *
510 * @return Category Same instance for chained method calls.
511 */
512 public function setUrlKeywords(StringType $text, LanguageCode $language)
513 {
514 $this->urlKeywords[(string)$language] = $text->asString();
515
516 return $this;
517 }
518
519
520 /**
521 * Returns the image filename of the category.
522 *
523 * @return string
524 */
525 public function getImage()
526 {
527 return $this->image;
528 }
529
530
531 /**
532 * Sets the image filename of the category.
533 *
534 * @param StringType $imageFile Category image file.
535 *
536 * @return Category Same instance for chained method calls.
537 */
538 public function setImage(StringType $imageFile)
539 {
540 $this->image = $imageFile->asString();
541
542 return $this;
543 }
544
545
546 /**
547 * Returns the alternative text of the image in the language with the provided language code.
548 *
549 * @param LanguageCode $language Two letter language code.
550 *
551 * @return string
552 */
553 public function getImageAltText(LanguageCode $language)
554 {
555 return (string)$this->altTexts[(string)$language];
556 }
557
558
559 /**
560 * Set the alternative text of the image for the language with the provided language code.
561 *
562 * @param StringType $text Alternative text of the category image.
563 * @param LanguageCode $language Two letter language code.
564 *
565 * @return Category Same instance for chained method calls.
566 */
567 public function setImageAltText(StringType $text, LanguageCode $language)
568 {
569 $this->altTexts[(string)$language] = $text->asString();
570
571 return $this;
572 }
573
574
575 /**
576 * Returns the icon filename of the category.
577 *
578 * @return string
579 */
580 public function getIcon()
581 {
582 return $this->icon;
583 }
584
585
586 /**
587 * Sets the icon filename of the category.
588 *
589 * @param StringType $iconFile Category icon file.
590 *
591 * @return Category Same instance for chained method calls.
592 */
593 public function setIcon(StringType $iconFile)
594 {
595 $this->icon = $iconFile->asString();
596
597 return $this;
598 }
599
600 /*
601 * ====================================================
602 * TODO: Implement getIconWidth() and getIconHeight()
603 * ====================================================
604 */
605
606 /**
607 * Returns an addon value by a given key.
608 *
609 * @param StringType $key Identifier key.
610 *
611 * @return string
612 */
613 public function getAddonValue(StringType $key)
614 {
615 return $this->addonValues->getValue($key->asString());
616 }
617
618
619 /**
620 * Sets an addon key and value.
621 *
622 * @param StringType $key Identifier key.
623 * @param StringType $value Value text.
624 *
625 * @return Category Same instance for chained method calls.
626 */
627 public function setAddonValue(StringType $key, StringType $value)
628 {
629 $this->addonValues->setValue($key->asString(), $value->asString());
630
631 return $this;
632 }
633
634
635 /**
636 * Returns a KeyValueCollection of addon values.
637 *
638 * @return KeyValueCollection
639 */
640 public function getAddonValues()
641 {
642 return $this->addonValues;
643 }
644
645
646 /**
647 * Adds a KeyValueCollection of addon values.
648 *
649 * @param KeyValueCollection $collection Key-Value collection.
650 *
651 * @return Category Same instance for chained method calls.
652 */
653 public function addAddonValues(KeyValueCollection $collection)
654 {
655 $this->addonValues->addCollection($collection);
656
657 return $this;
658 }
659
660
661 /**
662 * Deletes an addon value with a given key from the KeyValueCollection.
663 *
664 * @param StringType $key Identifier key.
665 *
666 * @return Category Same instance for chained method calls.
667 */
668 public function deleteAddonValue(StringType $key)
669 {
670 $this->addonValues->deleteValue($key->asString());
671
672 return $this;
673 }
674
675
676 /**
677 * Sets a category setting object
678 *
679 * @param CategorySettingsInterface $settings
680 *
681 * @return Category Same instance for chained method calls.
682 */
683 public function setSettings(CategorySettingsInterface $settings)
684 {
685 $this->settings = $settings;
686
687 return $this;
688 }
689 }