@@ -2,7 +2,6 @@ |
||
2 | 2 | |
3 | 3 | namespace nkostadinov\taxonomy\components\terms; |
4 | 4 | |
5 | -use insight\core\helpers\ArrayHelper; |
|
6 | 5 | use nkostadinov\taxonomy\components\interfaces\IHierarchicalTerm; |
7 | 6 | use nkostadinov\taxonomy\models\TaxonomyDef; |
8 | 7 | use nkostadinov\taxonomy\models\TaxonomyTerms; |
@@ -11,14 +11,14 @@ discard block |
||
11 | 11 | |
12 | 12 | class CategoryTerm extends TagTerm implements IHierarchicalTerm |
13 | 13 | { |
14 | - public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php' ; |
|
14 | + public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php'; |
|
15 | 15 | |
16 | 16 | public function getTerms($object_id, $name = []) |
17 | 17 | { |
18 | 18 | $query = (new Query()) |
19 | - ->select(TaxonomyTerms::tableName() . '.term') |
|
19 | + ->select(TaxonomyTerms::tableName().'.term') |
|
20 | 20 | ->from(TaxonomyTerms::tableName()) |
21 | - ->innerJoin($this->table, $this->table . '.term_id = taxonomy_terms.id', |
|
21 | + ->innerJoin($this->table, $this->table.'.term_id = taxonomy_terms.id', |
|
22 | 22 | [':object_id' => $object_id]) |
23 | 23 | ->andFilterWhere(['taxonomy_terms.term' => $name]); |
24 | 24 | |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | } |
28 | 28 | |
29 | 29 | $result = []; |
30 | - foreach($query->all() as $v) { |
|
30 | + foreach ($query->all() as $v) { |
|
31 | 31 | $result[] = $v['term']; |
32 | 32 | } |
33 | 33 | |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | { |
39 | 39 | $cachedParents = []; |
40 | 40 | |
41 | - $addTerm = function ($parent, $item) use ($object_id, &$cachedParents) { |
|
41 | + $addTerm = function($parent, $item) use ($object_id, &$cachedParents) { |
|
42 | 42 | $term = $this->getTaxonomyTerm($item); |
43 | 43 | $data['term_id'] = $term->id; |
44 | 44 | $data['object_id'] = $object_id; |
@@ -13,11 +13,11 @@ discard block |
||
13 | 13 | |
14 | 14 | class TagTerm extends BaseTerm |
15 | 15 | { |
16 | - public $templateFile = '@nkostadinov/taxonomy/migrations/template/tag.php' ; |
|
16 | + public $templateFile = '@nkostadinov/taxonomy/migrations/template/tag.php'; |
|
17 | 17 | |
18 | 18 | public function addTerm($object_id, $params) |
19 | 19 | { |
20 | - foreach($params as $item) { |
|
20 | + foreach ($params as $item) { |
|
21 | 21 | $term = $this->getTaxonomyTerm($item); |
22 | 22 | $data['term_id'] = $term->id; |
23 | 23 | $data['object_id'] = $object_id; |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | $this->getDb()->createCommand()->insert($this->table, $data)->execute(); |
30 | 30 | |
31 | 31 | $term->updateCounters(['total_count' => 1]); |
32 | - TaxonomyDef::updateAllCounters(['total_count' => 1], [ 'id' => $this->id ]); |
|
32 | + TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); |
|
33 | 33 | |
34 | 34 | $transaction->commit(); |
35 | 35 | } catch (Exception $e) { |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | class PropertyTerm extends BaseTerm { |
19 | 19 | |
20 | - public $templateFile = '@nkostadinov/taxonomy/migrations/template/properties.php' ; |
|
20 | + public $templateFile = '@nkostadinov/taxonomy/migrations/template/properties.php'; |
|
21 | 21 | |
22 | 22 | public $updateOnExist = true; |
23 | 23 | |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | 'value' => Schema::TYPE_STRING, |
34 | 34 | ]); |
35 | 35 | if ($migration->db->driverName === 'mysql') { |
36 | - $migration->addForeignKey('fk_' . $this->getTable() . '_' . $this->getRefTableName(), $this->getTable(), 'object_id', $this->getRefTableName(), 'id', 'CASCADE'); |
|
37 | - $migration->addForeignKey('fk_' . $this->getTable() . '_' . TaxonomyTerms::tableName(), $this->getTable(), 'term_id', TaxonomyTerms::tableName(), 'id', 'CASCADE'); |
|
36 | + $migration->addForeignKey('fk_'.$this->getTable().'_'.$this->getRefTableName(), $this->getTable(), 'object_id', $this->getRefTableName(), 'id', 'CASCADE'); |
|
37 | + $migration->addForeignKey('fk_'.$this->getTable().'_'.TaxonomyTerms::tableName(), $this->getTable(), 'term_id', TaxonomyTerms::tableName(), 'id', 'CASCADE'); |
|
38 | 38 | } |
39 | 39 | } |
40 | 40 | |
41 | 41 | public function addTerm($object_id, $params) |
42 | 42 | { |
43 | - foreach($params as $item => $value) { |
|
43 | + foreach ($params as $item => $value) { |
|
44 | 44 | $term = TaxonomyTerms::findOne(['term' => $item, 'taxonomy_id' => $this->id]); |
45 | 45 | if (!isset($term)) { |
46 | 46 | $term = new TaxonomyTerms(); |
@@ -66,8 +66,8 @@ discard block |
||
66 | 66 | } catch (Exception $e) { |
67 | 67 | $transaction->rollBack(); |
68 | 68 | } |
69 | - } elseif($this->updateOnExist) { |
|
70 | - $this->getDb()->createCommand()->update($this->getTable(), [ 'value' => $value ], $data)->execute(); |
|
69 | + } elseif ($this->updateOnExist) { |
|
70 | + $this->getDb()->createCommand()->update($this->getTable(), ['value' => $value], $data)->execute(); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | public function removeTerm($object_id, $params = []) |
76 | 76 | { |
77 | 77 | $terms = $this->getTerms($object_id, isset($params['name']) ? $params['name'] : []); |
78 | - foreach($terms as $term => $value) { |
|
78 | + foreach ($terms as $term => $value) { |
|
79 | 79 | $term = $this->getTaxonomyTerm($term); |
80 | 80 | $data['term_id'] = $term->id; |
81 | 81 | $data['object_id'] = $object_id; |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | $this->getDb()->createCommand()->delete($this->getTable(), $data)->execute(); |
86 | 86 | |
87 | 87 | $term->updateCounters(['total_count' => -1]); |
88 | - TaxonomyDef::updateAllCounters(['total_count' => -1], [ 'id' => $this->id ]); |
|
88 | + TaxonomyDef::updateAllCounters(['total_count' => -1], ['id' => $this->id]); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | } |
@@ -93,12 +93,12 @@ discard block |
||
93 | 93 | public function getTerms($object_id, $name = []) |
94 | 94 | { |
95 | 95 | $query = (new Query()) |
96 | - ->select(TaxonomyTerms::tableName() . '.term, ' . $this->getTable() . '.value') |
|
96 | + ->select(TaxonomyTerms::tableName().'.term, '.$this->getTable().'.value') |
|
97 | 97 | ->from(TaxonomyTerms::tableName()) |
98 | - ->innerJoin($this->getTable(), $this->getTable() . '.term_id = taxonomy_terms.id and ' . $this->getTable() . '.object_id=:object_id', |
|
98 | + ->innerJoin($this->getTable(), $this->getTable().'.term_id = taxonomy_terms.id and '.$this->getTable().'.object_id=:object_id', |
|
99 | 99 | [':object_id' => $object_id]) |
100 | - ->andFilterWhere([TaxonomyTerms::tableName() . '.term' => $name]); |
|
101 | - foreach($query->all() as $v) |
|
100 | + ->andFilterWhere([TaxonomyTerms::tableName().'.term' => $name]); |
|
101 | + foreach ($query->all() as $v) |
|
102 | 102 | $result[$v['term']] = $v['value']; |
103 | 103 | return isset($result) ? $result : []; |
104 | 104 | } |
@@ -35,11 +35,11 @@ discard block |
||
35 | 35 | |
36 | 36 | public function removeTerm($object_id, $params = []) |
37 | 37 | { |
38 | - if(empty($params)) { |
|
38 | + if (empty($params)) { |
|
39 | 39 | $params = $this->getTerms($object_id); |
40 | 40 | } |
41 | 41 | |
42 | - foreach($params as $item) { |
|
42 | + foreach ($params as $item) { |
|
43 | 43 | $term = $this->getTaxonomyTerm($item); |
44 | 44 | $data['term_id'] = $term->id; |
45 | 45 | $data['object_id'] = $object_id; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | $this->getDb()->createCommand()->delete($this->table, $data)->execute(); |
50 | 50 | |
51 | 51 | $term->updateCounters(['total_count' => -1]); |
52 | - Taxonomydef::updateAllCounters(['total_count' => -1], [ 'id' => $this->id ]); |
|
52 | + Taxonomydef::updateAllCounters(['total_count' => -1], ['id' => $this->id]); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | } |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | public function getTerms($object_id, $name = []) |
58 | 58 | { |
59 | 59 | $query = (new Query()) |
60 | - ->select(TaxonomyTerms::tableName() . '.term') |
|
60 | + ->select(TaxonomyTerms::tableName().'.term') |
|
61 | 61 | ->from(TaxonomyTerms::tableName()) |
62 | - ->innerJoin($this->table, $this->table . '.term_id = taxonomy_terms.id and ' . $this->table . '.object_id=:object_id', |
|
62 | + ->innerJoin($this->table, $this->table.'.term_id = taxonomy_terms.id and '.$this->table.'.object_id=:object_id', |
|
63 | 63 | [':object_id' => $object_id]) |
64 | 64 | ->andFilterWhere(['taxonomy_terms.term' => $name]); |
65 | 65 | |
66 | 66 | $result = []; |
67 | - foreach($query->all() as $v) |
|
67 | + foreach ($query->all() as $v) |
|
68 | 68 | $result[] = $v['term']; |
69 | 69 | return $result; |
70 | 70 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | } |
111 | 111 | |
112 | 112 | public function canInstall() { |
113 | - if(!$this->getTable()) |
|
113 | + if (!$this->getTable()) |
|
114 | 114 | return 'Missing "table" property'; |
115 | 115 | return true; |
116 | 116 | } |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | public function getTaxonomyTerm($name, $create = true) |
119 | 119 | { |
120 | 120 | $term = TaxonomyTerms::findOne(['term'=>$name, 'taxonomy_id' => $this->id]); |
121 | - if($create and !isset($term)) |
|
121 | + if ($create and !isset($term)) |
|
122 | 122 | { |
123 | 123 | $term = new TaxonomyTerms(); |
124 | 124 | $term->taxonomy_id = $this->id; |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | |
132 | 132 | public function getRefTableName() |
133 | 133 | { |
134 | - if(strpos($this->refTable, '\\') === FALSE) //not an AR class but a table name |
|
134 | + if (strpos($this->refTable, '\\') === FALSE) //not an AR class but a table name |
|
135 | 135 | return $this->refTable; |
136 | 136 | else |
137 | 137 | return call_user_func([$this->refTable, 'tableName']); |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | if (!preg_match('/^\w+$/', $this->name)) { |
153 | 153 | throw new Exception('The migration name should contain letters, digits and/or underscore characters only.'); |
154 | 154 | } |
155 | - $name = 'm' . gmdate('ymd_His') . '_' . $this->name; |
|
155 | + $name = 'm'.gmdate('ymd_His').'_'.$this->name; |
|
156 | 156 | return $name; |
157 | 157 | } |
158 | 158 | |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | { |
161 | 161 | |
162 | 162 | $name = $this->getMigrationFile(); |
163 | - $file = Yii::getAlias($this->migrationPath . DIRECTORY_SEPARATOR . $name . '.php'); |
|
163 | + $file = Yii::getAlias($this->migrationPath.DIRECTORY_SEPARATOR.$name.'.php'); |
|
164 | 164 | |
165 | 165 | //$data = get_object_vars($this); |
166 | 166 | $data = []; |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | $data['migration'] = $name; |
172 | 172 | |
173 | 173 | $this->migration = $name; |
174 | - $content = Yii::$app->getView()->renderFile(Yii::getAlias($this->templateFile), [ 'data' => $data ]); |
|
174 | + $content = Yii::$app->getView()->renderFile(Yii::getAlias($this->templateFile), ['data' => $data]); |
|
175 | 175 | file_put_contents($file, $content); |
176 | 176 | return $name; |
177 | 177 | } |
@@ -64,8 +64,9 @@ discard block |
||
64 | 64 | ->andFilterWhere(['taxonomy_terms.term' => $name]); |
65 | 65 | |
66 | 66 | $result = []; |
67 | - foreach($query->all() as $v) |
|
68 | - $result[] = $v['term']; |
|
67 | + foreach($query->all() as $v) { |
|
68 | + $result[] = $v['term']; |
|
69 | + } |
|
69 | 70 | return $result; |
70 | 71 | } |
71 | 72 | |
@@ -110,8 +111,9 @@ discard block |
||
110 | 111 | } |
111 | 112 | |
112 | 113 | public function canInstall() { |
113 | - if(!$this->getTable()) |
|
114 | - return 'Missing "table" property'; |
|
114 | + if(!$this->getTable()) { |
|
115 | + return 'Missing "table" property'; |
|
116 | + } |
|
115 | 117 | return true; |
116 | 118 | } |
117 | 119 | |
@@ -131,10 +133,12 @@ discard block |
||
131 | 133 | |
132 | 134 | public function getRefTableName() |
133 | 135 | { |
134 | - if(strpos($this->refTable, '\\') === FALSE) //not an AR class but a table name |
|
136 | + if(strpos($this->refTable, '\\') === FALSE) { |
|
137 | + //not an AR class but a table name |
|
135 | 138 | return $this->refTable; |
136 | - else |
|
137 | - return call_user_func([$this->refTable, 'tableName']); |
|
139 | + } else { |
|
140 | + return call_user_func([$this->refTable, 'tableName']); |
|
141 | + } |
|
138 | 142 | } |
139 | 143 | |
140 | 144 | public function getTable() |