コード例 #1
0
ファイル: PoolController.php プロジェクト: JCQS04/myimouto
 public function index()
 {
     $this->set_title('Pools');
     $sql_query = Pool::none()->page($this->page_number())->perPage(CONFIG()->pool_index_default_limit);
     $order = $this->params()->order ?: 'id';
     $search_tokens = array();
     if ($this->params()->query) {
         $this->set_title($this->params()->query . " - Pools");
         // $query = array_map(function($v){return addslashes($v);}, explode($this->params()->query);
         // $query = Tokenize.tokenize_with_quotes($this->params[:query] || "")
         $query = explode(' ', addslashes($this->params()->query));
         foreach ($query as &$token) {
             if (preg_match('/^(order|limit|posts):(.+)$/', $token, $m)) {
                 if ($m[1] == "order") {
                     $order = $m[2];
                 } elseif ($m[1] == "limit") {
                     $sql_query->perPage(min((int) $m[2], 100));
                 } elseif ($m[1] == "posts") {
                     Post::generate_sql_range_helper(Tag::parse_helper($m[2]), "post_count", $sql_query);
                 }
             } else {
                 // # TODO: removing ^\w- from token.
                 // $token = preg_replace('~[^\w-]~', '', $token);
                 $search_tokens[] = $token;
             }
         }
     }
     if (!empty($search_tokens)) {
         // $value_index_query = QueryParser.escape_for_tsquery($search_tokens);
         $value_index_query = implode('_', $search_tokens);
         if ($value_index_query) {
             # If a search keyword contains spaces, then it was quoted in the search query
             # and we should only match adjacent words.    tsquery won't do this for us; we need
             # to filter results where the words aren't adjacent.
             #
             # This has a side-effect: any stopwords, stemming, parsing, etc. rules performed
             # by to_tsquery won't be done here.    We need to perform the same processing as
             # is used to generate search_index.    We don't perform all of the stemming rules, so
             # although "jump" may match "jumping", "jump beans" won't match "jumping beans" because
             # we'll filter it out.
             #
             # This also doesn't perform tokenization, so some obscure cases won't match perfectly;
             # for example, "abc def" will match "xxxabc def abc" when it probably shouldn't.    Doing
             # this more correctly requires Postgresql support that doesn't exist right now.
             foreach ($query as $q) {
                 # Don't do this if there are no spaces in the query, so we don't turn off tsquery
                 # parsing when we don't need to.
                 // if (!strstr($q, ' ')) continue;
                 $sql_query->where("(position(LOWER(?) IN LOWER(REPLACE(name, '_', ' '))) > 0 OR position(LOWER(?) IN LOWER(description)) > 0)", $q, $q);
             }
         }
     }
     if (empty($order)) {
         $order = empty($search_tokens) ? 'date' : 'name';
     }
     switch ($order) {
         case "name":
             $sql_query->order("name asc");
             break;
         case "date":
             $sql_query->order("created_at desc");
         case "updated":
             $sql_query->order("updated_at desc");
             break;
         case "id":
             $sql_query->order("id desc");
             break;
         default:
             $sql_query->order("created_at desc");
             break;
     }
     $this->pools = $sql_query->paginate();
     $samples = [];
     foreach ($this->pools as $p) {
         if (!($post = $p->get_sample())) {
             continue;
         }
         $p_id = (string) $p->id;
         $samples[$p_id] = $post;
     }
     $this->samples = $samples;
     $this->respond_to_list('pools');
 }