Exemplo n.º 1
0
 public function actionDo()
 {
     $kw = trim($this->get("kw", ""));
     $p = intval($this->get("p", 1));
     if (!$p) {
         $p = 1;
     }
     $data = [];
     if (!$kw) {
         return $this->redirect("/");
     }
     $this->setTitle($kw);
     $pagesize = 10;
     $offset = ($p - 1) * $pagesize;
     $search_key = ['LIKE', 'search_key', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false];
     $query = IndexSearch::find()->where($search_key);
     $total_count = $query->count();
     $list = $query->orderBy("id desc")->limit($pagesize)->offset($offset)->all();
     if ($list) {
         $book_mapping = DataHelper::getDicByRelateID($list, Book::className(), "book_id", "id", ["subtitle", "summary", "origin_image_url", "tags"]);
         $post_mapping = DataHelper::getDicByRelateID($list, Posts::className(), "post_id", "id", ["title", "content", "tags"]);
         foreach ($list as $_item) {
             if ($_item['book_id']) {
                 $tmp_target = $book_mapping[$_item['book_id']];
                 $tmp_content = mb_substr($tmp_target['summary'], 0, 105, "utf-8");
                 $tmp_title = DataHelper::encode($tmp_target['subtitle']);
                 $tmp_view_url = Url::toRoute("/library/detail/{$_item['book_id']}");
             } else {
                 $tmp_target = $post_mapping[$_item['post_id']];
                 $tmp_content = UtilHelper::blog_summary($tmp_target['content'], 105);
                 $tmp_title = DataHelper::encode($tmp_target['title']);
                 $tmp_view_url = Url::toRoute("/default/{$_item['post_id']}");
             }
             $tags = explode(",", $tmp_target['tags']);
             $data[] = ['title' => $tmp_title, 'content' => nl2br($tmp_content), 'tags' => $tags, 'date' => date("Y年m月d日"), 'view_url' => $tmp_view_url];
         }
     }
     $page_info = DataHelper::ipagination(["total_count" => $total_count, "page_size" => $pagesize, "page" => $p, "display" => 5]);
     return $this->render("result", ["data" => $data, "page_info" => $page_info, "urls" => ["page_base" => Url::toRoute(["/search/do", "kw" => $kw])]]);
 }
Exemplo n.º 2
0
 public function actionIndex()
 {
     $type = intval($this->get("type", 1));
     $type = in_array($type, [1, 2, 3]) ? $type : 1;
     $p = intval($this->get("p", 1));
     if (!$p) {
         $p = 1;
     }
     $data = [];
     $pagesize = 10;
     $offset = ($p - 1) * $pagesize;
     $query = Posts::find()->where(['status' => 1]);
     switch ($type) {
         case 2:
             $query->orderBy(['view_count' => SORT_DESC]);
             break;
         case 3:
             $query->andWhere(['original' => 1]);
             $query->orderBy(['id' => SORT_DESC]);
             break;
         default:
             $query->orderBy(['id' => SORT_DESC]);
             break;
     }
     $total_count = $query->count();
     $posts_info = $query->offset($offset)->limit($pagesize)->all();
     if ($posts_info) {
         $idx = 1;
         $author = Yii::$app->params['author'];
         foreach ($posts_info as $_post) {
             $tmp_content = UtilHelper::blog_summary($_post['content'], 105);
             $tags = explode(",", $_post['tags']);
             $data[] = ['idx' => $idx, 'id' => $_post['id'], 'title' => DataHelper::encode($_post['title']), 'content' => nl2br($tmp_content), 'original' => $_post['original'], 'view_count' => $_post['view_count'], 'author' => $author, 'tags' => $tags, 'date' => date("Y.m.d", strtotime($_post['updated_time'])), 'view_url' => UrlService::buildUrl("/default/info", ["id" => $_post['id']])];
         }
     }
     $page_info = DataHelper::ipagination(["total_count" => $total_count, "page_size" => $pagesize, "page" => $p, "display" => 5]);
     $tags = CacheHelperService::getFrontCache("tag");
     return $this->render("index", ["data" => $data, "page_info" => $page_info, "type" => $type, "hot_kws" => array_slice($tags, 0, 5)]);
 }