1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 03.02.2015 |
4
|
|
|
* Time: 09:25 ч. |
5
|
|
|
* @author Nikola Kostadinov |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace nkostadinov\taxonomy\components\terms; |
9
|
|
|
|
10
|
|
|
use nkostadinov\taxonomy\models\TaxonomyDef; |
11
|
|
|
use nkostadinov\taxonomy\models\TaxonomyTerms; |
12
|
|
|
use yii\db\Exception; |
13
|
|
|
use yii\db\Migration; |
14
|
|
|
use yii\db\Query; |
15
|
|
|
use yii\db\Schema; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
class PropertyTerm extends BaseTerm |
19
|
|
|
{ |
20
|
|
|
public $templateFile = '@nkostadinov/taxonomy/migrations/template/properties.php' ; |
21
|
|
|
|
22
|
|
|
public $updateOnExist = true; |
23
|
|
|
|
24
|
|
|
public function install() |
25
|
|
|
{ |
26
|
|
|
parent::install(); |
27
|
|
|
|
28
|
|
|
$migration = new Migration(); |
29
|
|
|
$migration->createTable($this->getTable(), [ |
30
|
|
|
'id' => Schema::TYPE_PK, |
31
|
|
|
'object_id' => Schema::TYPE_INTEGER, |
32
|
|
|
'term_id' => Schema::TYPE_BIGINT, |
33
|
|
|
'value' => Schema::TYPE_STRING, |
34
|
|
|
]); |
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'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function addTerm($object_id, $params) |
42
|
|
|
{ |
43
|
|
|
foreach($params as $item => $value) { |
44
|
|
|
$term = TaxonomyTerms::findOne(['term' => $item, 'taxonomy_id' => $this->id]); |
45
|
|
View Code Duplication |
if (!isset($term)) { |
|
|
|
|
46
|
|
|
$term = new TaxonomyTerms(); |
47
|
|
|
$term->taxonomy_id = $this->id; |
48
|
|
|
$term->term = $item; |
49
|
|
|
$term->total_count = 0; |
50
|
|
|
$term->save(); |
51
|
|
|
} |
52
|
|
|
$data['term_id'] = $term->id; |
|
|
|
|
53
|
|
|
$data['object_id'] = $object_id; |
|
|
|
|
54
|
|
|
|
55
|
|
|
$query = new Query(); |
56
|
|
|
if (!$query->from($this->getTable())->where($data)->exists($this->getDb())) { |
57
|
|
|
$transaction = $this->getDb()->beginTransaction(); |
58
|
|
|
try { |
59
|
|
|
$data['value'] = $value; |
60
|
|
|
$this->getDb()->createCommand()->insert($this->getTable(), $data)->execute(); |
61
|
|
|
|
62
|
|
|
$term->updateCounters(['total_count' => 1]); |
|
|
|
|
63
|
|
|
TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); |
64
|
|
|
|
65
|
|
|
$transaction->commit(); |
66
|
|
|
} catch (Exception $e) { |
67
|
|
|
$transaction->rollBack(); |
68
|
|
|
} |
69
|
|
|
} elseif($this->updateOnExist) { |
70
|
|
|
$this->getDb()->createCommand()->update($this->getTable(), ['value' => $value], $data)->execute(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function removeTerm($object_id, $params = []) |
76
|
|
|
{ |
77
|
|
|
$terms = $this->getTerms($object_id, $params); |
78
|
|
View Code Duplication |
foreach($terms as $term => $value) { |
|
|
|
|
79
|
|
|
$term = $this->getTaxonomyTerm($term); |
80
|
|
|
$data['term_id'] = $term->id; |
|
|
|
|
81
|
|
|
$data['object_id'] = $object_id; |
|
|
|
|
82
|
|
|
|
83
|
|
|
$query = new Query(); |
84
|
|
|
if ($query->from($this->getTable())->where($data)->exists($this->getDb())) { |
85
|
|
|
$this->getDb()->createCommand()->delete($this->getTable(), $data)->execute(); |
86
|
|
|
|
87
|
|
|
$term->updateCounters(['total_count' => -1]); |
|
|
|
|
88
|
|
|
TaxonomyDef::updateAllCounters(['total_count' => -1], [ 'id' => $this->id ]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getTerms($object_id = null, $name = []) |
94
|
|
|
{ |
95
|
|
|
$query = (new Query()) |
96
|
|
|
->select(TaxonomyTerms::tableName() . '.term, ' . $this->getTable() . '.value') |
97
|
|
|
->from(TaxonomyTerms::tableName()) |
98
|
|
|
->innerJoin($this->getTable(), $this->getTable() . '.term_id = taxonomy_terms.id and ' . $this->getTable() . '.object_id=:object_id', |
99
|
|
|
[':object_id' => $object_id]) |
100
|
|
|
->andFilterWhere([TaxonomyTerms::tableName() . '.term' => $name]); |
101
|
|
|
|
102
|
|
|
return ArrayHelper::map($query->all(), 'term', 'value'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.