public function run() { $tags = BlogTag::findTagWeights(); $str = ''; foreach ($tags as $tag => $weight) { $link = Html::a(Html::encode($tag), Yii::$app->getUrlManager()->createUrl(['blog/default/index', 'tag' => $tag])); $str .= Html::tag('span', $link, ['class' => 'tag', 'style' => "font-size:{$weight}pt"]) . ",\n"; } return $this->render('portal', ['title' => $this->title, 'content' => $str]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = BlogTag::find(); $query->orderBy(['frequency' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'frequency' => $this->frequency]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Finds the Tag model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Tag the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BlogTag::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function findTagWeights($limit = 20) { $query = BlogTag::find()->orderBy(['frequency' => SORT_DESC]); $cacheId = Yii::$app->params["domain"] . '_blog_tag_' . md5(serialize($query)); $models = Yii::$app->cache->get($cacheId); if ($models === false) { $models = $query->all(); Yii::$app->cache->set($cacheId, $models, 3600); } $total = 0; foreach ($models as $model) { $total += $model->frequency; } $tags = []; if ($total > 0) { foreach ($models as $model) { $tags[$model->name] = 8 + (int) (16 * $model->frequency / ($total + 10)); } ksort($tags); } return $tags; }
/** * */ public function getTagLinks() { $links = []; foreach (BlogTag::string2array($this->tags) as $tag) { $links[] = Html::a($tag, Yii::$app->getUrlManager()->createUrl(['blog/default/index', 'tag' => $tag])); } return $links; }