1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace nkostadinov\taxonomy\components\terms; |
4
|
|
|
|
5
|
|
|
use insight\core\helpers\ArrayHelper; |
6
|
|
|
use nkostadinov\taxonomy\components\interfaces\IHierarchicalTerm; |
7
|
|
|
use nkostadinov\taxonomy\models\TaxonomyDef; |
8
|
|
|
use nkostadinov\taxonomy\models\TaxonomyTerms; |
9
|
|
|
use Yii; |
10
|
|
|
use yii\db\Query; |
11
|
|
|
|
12
|
|
|
class CategoryTerm extends TagTerm implements IHierarchicalTerm |
13
|
|
|
{ |
14
|
|
|
public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php' ; |
15
|
|
|
|
16
|
|
|
public function getTerms($object_id, $name = []) |
17
|
|
|
{ |
18
|
|
|
$query = (new Query()) |
19
|
|
|
->select(TaxonomyTerms::tableName() . '.term') |
20
|
|
|
->from(TaxonomyTerms::tableName()) |
21
|
|
|
->innerJoin($this->table, $this->table . '.term_id = taxonomy_terms.id', |
|
|
|
|
22
|
|
|
[':object_id' => $object_id]) |
23
|
|
|
->andFilterWhere(['taxonomy_terms.term' => $name]); |
24
|
|
|
|
25
|
|
|
if ($object_id) { |
26
|
|
|
$query->onCondition("$this->table.object_id = $object_id"); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$result = []; |
30
|
|
|
foreach($query->all() as $v) { |
31
|
|
|
$result[] = $v['term']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function addTerm($object_id, $params) |
38
|
|
|
{ |
39
|
|
|
$cachedParents = []; |
40
|
|
|
|
41
|
|
|
$addTerm = function ($parent, $item) use ($object_id, &$cachedParents) { |
42
|
|
|
$term = $this->getTaxonomyTerm($item); |
43
|
|
|
$data['term_id'] = $term->id; |
|
|
|
|
44
|
|
|
$data['object_id'] = $object_id; |
45
|
|
|
|
46
|
|
|
if (array_key_exists($parent, $cachedParents)) { |
47
|
|
|
$term->parent_id = $cachedParents[$parent]->id; |
48
|
|
|
} else if (is_string($parent)) { |
49
|
|
|
$parentTerm = $this->getTaxonomyTerm($parent); |
50
|
|
|
$cachedParents[$parent] = $parentTerm; |
51
|
|
|
$term->parent_id = $parentTerm->id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { |
|
|
|
|
55
|
|
|
Yii::$app->db->transaction(function() use ($data, $term) { |
56
|
|
|
if ($term->getDirtyAttributes(['parent_id'])) { |
57
|
|
|
$term->save(false); |
58
|
|
|
} |
59
|
|
|
CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$term->updateCounters(['total_count' => 1]); |
62
|
|
|
TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
foreach ($params as $parent => $item) { |
68
|
|
|
if (is_array($item)) { |
69
|
|
|
foreach ($item as $child) { |
70
|
|
|
$addTerm($parent, $child); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
$addTerm($parent, $item); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getParent($term) |
79
|
|
|
{ |
80
|
|
|
$childTerm = $this->getTaxonomyTerm($term); |
81
|
|
|
if (!$childTerm || is_null($childTerm->parent_id)) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$parentTerm = (new Query()) |
86
|
|
|
->select('term') |
87
|
|
|
->from(TaxonomyTerms::tableName()) |
88
|
|
|
->where("id = $childTerm->parent_id") |
89
|
|
|
->one(self::getDb()); |
90
|
|
|
|
91
|
|
|
return $parentTerm ? $parentTerm['term'] : null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function hasParent($term) |
95
|
|
|
{ |
96
|
|
|
return $this->getParent($term) != null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getChildren($term) |
100
|
|
|
{ |
101
|
|
|
$parentTerm = $this->getTaxonomyTerm($term); |
102
|
|
|
if (!$parentTerm) { |
103
|
|
|
return []; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$query = (new Query()) |
107
|
|
|
->select('term') |
108
|
|
|
->from(TaxonomyTerms::tableName()) |
109
|
|
|
->where("parent_id = $parentTerm->id"); |
110
|
|
|
|
111
|
|
|
$result = []; |
112
|
|
|
foreach ($query->all(self::getDb()) as $row) { |
113
|
|
|
$result[] = $row['term']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $result; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function hasChildren($term) |
120
|
|
|
{ |
121
|
|
|
$parentTerm = $this->getTaxonomyTerm($term); |
122
|
|
|
if (!$parentTerm) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return (new Query()) |
127
|
|
|
->from(TaxonomyTerms::tableName()) |
128
|
|
|
->where("parent_id = $parentTerm->id") |
129
|
|
|
->exists(self::getDb()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.