Example #1
0
 public static function buildTags($post_id)
 {
     $post_info = Posts::findOne(['id' => $post_id]);
     if (!$post_info) {
         return;
     }
     $tags_arr = $post_info['tags'] ? explode(",", $post_info['tags']) : [];
     foreach ($tags_arr as $_tag) {
         $has_in = PostsTags::findOne(['posts_id' => $post_id, "tag" => $_tag]);
         if ($has_in) {
             continue;
         }
         $model_posts_tag = new PostsTags();
         $model_posts_tag->posts_id = $post_id;
         $model_posts_tag->tag = $_tag;
         $model_posts_tag->save(0);
     }
 }
Example #2
0
 public static function buildFront($refresh = false)
 {
     $cache = new FileCache();
     $cache_key = "tag_post";
     $root_path = UtilHelper::getRootPath();
     $cache->cachePath = $root_path . '/common/logs/cache';
     $data = $cache[$cache_key];
     if (!$data || $refresh) {
         $data = ["tag" => [], "post_hot" => [], "post_origin" => [], "post_latest" => []];
         $tags = PostsTags::find()->select("tag,count(*) as num ")->groupBy("tag")->orderBy("num desc")->limit(20)->all();
         if ($tags) {
             foreach ($tags as $_tag) {
                 $data['tag'][] = $_tag['tag'];
             }
         }
         $post_hot = Posts::find()->where(['status' => 1])->orderBy(['view_count' => SORT_DESC])->limit(10)->all();
         if ($post_hot) {
             foreach ($post_hot as $_post_info) {
                 $data['post_hot'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $post_origin = Posts::find()->where(['status' => 1, 'original' => 1])->orderBy(['id' => SORT_DESC])->limit(10)->all();
         if ($post_origin) {
             foreach ($post_origin as $_post_info) {
                 $data['post_origin'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $post_latest = Posts::find()->where(['status' => 1])->orderBy(['id' => SORT_DESC])->limit(10)->all();
         if ($post_latest) {
             foreach ($post_latest as $_post_info) {
                 $data['post_latest'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $data = json_encode($data);
         $cache[$cache_key] = $data;
     }
     return $data;
 }
Example #3
0
 private function afterDel($blog_id)
 {
     PostsTags::deleteAll(["posts_id" => $blog_id]);
     PostsRecommend::deleteAll(['OR', ['blog_id' => $blog_id], ["relation_blog_id" => $blog_id]]);
 }