-
Notifications
You must be signed in to change notification settings - Fork 2
/
Taxonomy.php
89 lines (79 loc) · 2.45 KB
/
Taxonomy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* @author Nikola Kostadinov<[email protected]>
* Date: 19.10.2014
* Time: 10:46 ч.
*/
namespace nkostadinov\taxonomy;
use nkostadinov\taxonomy\components\exceptions\TermNotDefinedException;
use nkostadinov\taxonomy\components\terms\BaseTerm;
use nkostadinov\taxonomy\components\terms\CategoryTerm;
use nkostadinov\taxonomy\components\terms\PropertyTerm;
use nkostadinov\taxonomy\components\terms\TagTerm;
use nkostadinov\taxonomy\models\TaxonomyDef;
use nkostadinov\taxonomy\models\TaxonomyTerms;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Migration;
use yii\db\Schema;
use yii\log\Logger;
class Taxonomy extends Component
{
/* @var Connection The db connection component */
public $db = 'db';
public $table = 'taxonomy';
//cache array of initialized terms
private $_taxonomy = [];
//
public $definitions = [];
public function isTermInstalled($termName)
{
$term = $this->getTerm($termName);
return $term->isInstalled();
}
public function addTerm($term, $object_id, $params)
{
$term = $this->getTerm($term);
$term->addTerm($object_id, $params);
}
public function removeTerm($term, $object_id, $params = [])
{
$term = $this->getTerm($term);
return $term->removeTerm($object_id, $params);
}
public function getTerms($term, $object_id, $name = null)
{
$term = $this->getTerm($term);
return $term->getTerms($object_id, $name);
}
/**
* @param $termName
* @return BaseTerm
* @throws InvalidConfigException
* @throws TermNotDefinedException
*/
public function getTerm($termName, $reload = false)
{
if(!isset($this->_taxonomy[$termName]) || $reload) {
$tax = TaxonomyDef::findOne(['name' => $termName]);
\Yii::getLogger()->log("Initialising term $termName", Logger::LEVEL_INFO, 'nkostadinov.taxonomy.terms');
$this->_taxonomy[$termName] = \Yii::createObject($tax->attributes);
}
return $this->_taxonomy[$termName];
}
public function isInstalled()
{
return \Yii::$app->db->getTableSchema(TaxonomyDef::tableName(), true) !== null;
}
/**
* @return array
*/
public function getDefinitions()
{
return array_merge(
[TagTerm::className(), PropertyTerm::className(), CategoryTerm::className()],
$this->definitions
);
}
}