1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Taxonomy\Vocabulary;
13
14 use ICanBoogie\ActiveRecord;
15
16 class Vocabulary extends ActiveRecord
17 {
18 const VID = 'vid';
19 const SITEID = 'siteid';
20 const VOCABULARY = 'vocabulary';
21 const VOCABULARYSLUG = 'vocabularyslug';
22 const IS_TAGS = 'is_tags';
23 const IS_MULTIPLE = 'is_multiple';
24 const IS_REQUIRED = 'is_required';
25 const WEIGHT = 'weight';
26 const SCOPE = 'scope';
27
28 /**
29 * Identifier of the vocabulary
30 *
31 * @var int
32 */
33 public $vid;
34
35 /**
36 * Identifier of the site the vocabulary is attached to.
37 *
38 * This value maybe 0, indicating that the vocabulary is not attached to any site.
39 *
40 * @var int
41 */
42 public $siteid;
43
44 /**
45 * Name of the vocabulary.
46 *
47 * @var string
48 */
49 public $vocabulary;
50
51 /**
52 * Version of the {@vocabulary} property that can be used in URLs. Written in lowercase, it
53 * contains only unaccentuated letters, numbers and hyphens.
54 *
55 * @var string
56 */
57 public $vocabularyslug;
58
59 /**
60 * Can terms be defined as coma-separated values ?
61 *
62 * @var bool
63 */
64 public $is_tags;
65
66 /**
67 * Can multiple terms be associated to a node ?
68 *
69 * @var bool
70 */
71 public $is_multiple;
72
73 /**
74 * Is the vocabulary required for the associated scope ?
75 *
76 * @var bool
77 */
78 public $is_required;
79
80 /**
81 * Weight of the vocabulary relative to other vocabulary.
82 *
83 * @var int
84 */
85 public $weight;
86
87 /**
88 * Removes the `scope` and `terms` properties.
89 */
90 public function __sleep()
91 {
92 $properties = parent::__sleep();
93
94 $properties = array_flip($properties);
95
96 unset($properties['scope']);
97 unset($properties['terms']);
98
99 return array_flip($properties);
100 }
101
102 /**
103 * Returns the scope of the vocabulary, that is the constructors to which the vocabulary is
104 * associated.
105 *
106 * @return array[]string
107 */
108 protected function lazy_get_scope()
109 {
110 global $core;
111
112 return $core->models['taxonomy.vocabulary/scopes']->select('constructor')
113 ->filter_by_vid($this->vid)->all(\PDO::FETCH_COLUMN);
114 }
115
116 /**
117 * Returns the terms associated to this vocabulary, ordered by weight.
118 *
119 * @return array[]Term
120 */
121 protected function lazy_get_terms()
122 {
123 global $core;
124
125 $model = $core->models['taxonomy.terms'];
126
127 return $model->select('term.*')->filter_by_vid($this->vid)
128 ->order('weight')->all(\PDO::FETCH_CLASS, 'Icybee\Modules\Taxonomy\Terms\Term', array($model));
129 }
130 }