Skip to content

Commit 524c5b5

Browse files
committed
V1.12
完成前台首页标签云的实现
1 parent 00bb888 commit 524c5b5

File tree

5 files changed

+92
-11
lines changed

5 files changed

+92
-11
lines changed

common/models/Tag.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ public static function updateFrequency($oldTags,$newTags)
115115
}
116116
}
117117

118-
118+
119+
public static function findTagWeights($limit=20)
120+
{
121+
$tag_size_level = 5;
122+
123+
$models=Tag::find()->orderBy('frequency desc')->limit($limit)->all();
124+
$total=Tag::find()->limit($limit)->count();
125+
126+
$stepper=ceil($total/$tag_size_level);
127+
128+
$tags=array();
129+
$counter=1;
130+
131+
if($total>0)
132+
{
133+
foreach ($models as $model)
134+
{
135+
$weight=ceil($counter/$stepper)+1;
136+
$tags[$model->name]=$weight;
137+
$counter++;
138+
}
139+
ksort($tags);
140+
}
141+
return $tags;
142+
}
143+
119144

120145
}

database/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222

2323
源码版本:V1.10 ————— SQL文件版本:V1.4
2424

25-
源码版本:V1.11 ————— 同上
25+
源码版本:V1.11 ————— 同上
26+
27+
源码版本:V1.12 ————— 同上
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace frontend\components;
3+
4+
use yii\base\Widget;
5+
use yii\helpers\Html;
6+
7+
class TagsCloudWidget extends Widget
8+
{
9+
public $tags;
10+
11+
public function init()
12+
{
13+
parent::init();
14+
}
15+
16+
public function run()
17+
{
18+
$tagString='';
19+
$fontStyle=array("6"=>"danger",
20+
"5"=>"info",
21+
"4"=>"warning",
22+
"3"=>"primary",
23+
"2"=>"success",
24+
);
25+
26+
foreach ($this->tags as $tag=>$weight)
27+
{
28+
$tagString.='<a href="'.\Yii::$app->homeUrl.'?r=post/index&PostSearch[tags]='
29+
.$tag.'">'.
30+
' <h'.$weight.' style="display:inline-block;"><span class="label label-'
31+
.$fontStyle[$weight].'">'.$tag.'</span></h'.$weight.'></a>';
32+
}
33+
return $tagString;
34+
35+
}
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
}

frontend/controllers/PostController.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use yii\web\NotFoundHttpException;
1010
use yii\filters\VerbFilter;
1111

12+
use common\models\Tag;
13+
1214
/**
1315
* PostController implements the CRUD actions for Post model.
1416
*/
@@ -35,15 +37,19 @@ public function behaviors()
3537
*/
3638
public function actionIndex()
3739
{
38-
$searchModel = new PostSearch();
39-
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
40-
41-
return $this->render('index', [
42-
'searchModel' => $searchModel,
43-
'dataProvider' => $dataProvider,
44-
]);
40+
$tags=Tag::findTagWeights();
41+
42+
$searchModel = new PostSearch();
43+
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
44+
45+
return $this->render('index', [
46+
'searchModel' => $searchModel,
47+
'dataProvider' => $dataProvider,
48+
'tags'=>$tags,
49+
]);
4550
}
46-
51+
52+
4753
/**
4854
* Displays a single Post model.
4955
* @param integer $id

frontend/views/post/index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use yii\helpers\Html;
44
use yii\grid\GridView;
55
use yii\widgets\ListView;
6+
use frontend\components\TagsCloudWidget;
67

78
/* @var $this yii\web\View */
89
/* @var $searchModel common\models\PostSearch */
@@ -60,7 +61,9 @@
6061
<li class="list-group-item">
6162
<span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 标签云
6263
</li>
63-
<li class="list-group-item">标签云</li>
64+
<li class="list-group-item">
65+
<?= TagsCloudWidget::widget(['tags'=>$tags])?>
66+
</li>
6467
</ul>
6568
</div>
6669

0 commit comments

Comments
 (0)