Esempio 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])]]);
 }