-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tag.php
105 lines (87 loc) · 2.59 KB
/
Tag.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class Model_Tag extends Model_Tree implements Countable
{
protected $_table = 'tags';
protected $_parent_field = 'syn_id';
protected $_attributes = array(
'name' => 'name',
'parent' => 'syn_id',
);
protected $_fields = array(
'id' => Model::TYPE_INTEGER,
'name' => Model::TYPE_STRING,
'syn_id' => Model::TYPE_INTEGER,
);
private $_total = null;
private static $_auto_create = false;
protected $_list_class_name = 'Model_List_Tag';
public function setAutoCreate($value = true) { self::$_auto_create = (bool)$value; }
public function __construct(Storage_Db $db, $id = null)
{
if (is_string($id) && !is_numeric($id)) $id = array( 'name' => $id );
parent::__construct($db, $id);
if (self::$_auto_create && !$this->getId() && is_array($id) && isset($id['name']))
{
$this->name = $id['name'];
$this->save();
}
}
private function getModelsList($objname, $page = 0)
{
$model = Model_List::create($this->_db, $objname);
$table = $model->getTable();
$result = $this->_db->select(array('tag_relations', $table), "{$table}.*", "tag_relations.obj_type = '$table' and tag_relations.obj_id = {$table}.id and tag_relations.tag_id = ".$this->getId());
$model->find($result);
return $model;
}
public function getArticles($page = 0)
{
return $this->getModelsList('article', $page);
}
public function getTopics($page = 0)
{
return $this->getModelsList('topic', $page);
}
public function __toString()
{
return (string)$this->name;
}
public function parseData(array $values)
{
if (isset($values['total'])) $this->_total = (int)$values['total'];
return parent::parseData($values);
}
public function count()
{
if ($this->_total === null)
{
$result = $this->_db->select('tag_relations', 'COUNT(*) AS total', array( 'tag_id' => $this->getId() ))->fetchFirst();
$this->_total = (int)$result['total'];
}
return $this->_total;
}
public function tagModel(Model_Tagged $model)
{
$this->_db->insert('tag_relations', array( 'tag_id' => $this->getId(), 'obj_type' => $model->getTable(), 'obj_id' => $model->getId() ));
}
public function untagModel(Model_Tagged $model, $alltags = false)
{
$cond = array( 'obj_type' => $model->getTable(), 'obj_id' => $model->getId() );
if (!$alltags) $cond['tag_id'] = $this->getId();
$this->_db->delete('tag_relations', $cond);
}
public function regenerate($parent = 0, $state = 0)
{
return;
}
public function remove()
{
$id = $this->getId();
if ($id)
{
parent::remove();
$this->_db->delete('tag_relations', array( 'tag_id' => $id ));
}
}
}
?>