-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTag.php
More file actions
168 lines (142 loc) · 2.97 KB
/
Tag.php
File metadata and controls
168 lines (142 loc) · 2.97 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "tbl_tag".
*
* @property integer $id
* @property string $name
* @property integer $frequency
*/
class Tag extends \yii\db\ActiveRecord
{
const FONT_SIZE_LEVEL=5;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tbl_tag';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['frequency'], 'integer'],
[['name'], 'string', 'max' => 128]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => '标签',
'frequency' => '频率',
];
}
public static function string2array($tags)
{
return preg_split('/\s*,\s*/',trim($tags),-1,PREG_SPLIT_NO_EMPTY);
}
public static function array2string($tags)
{
return implode(', ',$tags);
}
/**
* 返回格式
*["Gii"]=>3
*["GridView"]=>4
*["RESTful Web服务"]=>4
* 值用于<h?>标签,显示不同大小
*/
public static function findTagWeights($limit=20)
{
$models=Tag::find()
->orderBy('frequency DESC')
->limit($limit)
->all();
$total=Tag::find()
->orderBy('frequency DESC')
->limit($limit)
->count();
$stepper=ceil($total/Tag::FONT_SIZE_LEVEL); //把标签按个数,平均分组
$tags=array();
$counter=1;
if($total>0)
{
foreach($models as $model)
{
$weight=ceil($counter/$stepper)+1;
$tags[$model->name]=$weight;
$counter++;
}
ksort($tags);
}
return $tags;
}
public static function updateFrequency($oldTags, $newTags)
{
if( !empty($oldTags) || !empty($newTags) ) {
$oldTags = self::string2array($oldTags);
$newTags = self::string2array($newTags);
self::addTags(array_values(array_diff($newTags, $oldTags)));
self::removeTags(array_values(array_diff($oldTags, $newTags)));
}
}
public static function addTags($tags)
{
if( empty($tags) ) return;
foreach($tags as $name)
{
$aTag = Tag::find()
->where(['name' => $name])
->one();
$aTagCount = Tag::find()
->where(['name' => $name])
->count();
if(!$aTagCount)
{
$tag=new Tag;
$tag->name=$name;
$tag->frequency=1;
$tag->save();
}
else
{
$aTag->frequency+=1;
$aTag->save();
}
}
}
public static function removeTags($tags)
{
if(empty($tags)) return;
foreach($tags as $name)
{
$aTag = Tag::find()
->where(['name' => $name])
->one();
$aTagCount = Tag::find()
->where(['name' => $name])
->count();
if($aTagCount)
{
if($aTagCount && $aTag->frequency<=1)
{
$aTag->delete();
}
else
{
$aTag->frequency-=1;
$aTag->save();
}
}
}
}
}