Example #1
0
 public static function getThreadInfo($sort = '', $order = '')
 {
     //ソートの種類によってデータベースアクセス方法を分岐
     switch ($sort) {
         case 'thread':
             $threads = ThreadView::select('id', 'amount', 'thread', 'user')->orderBy('created_at', $order)->get();
             break;
         case 'user':
             $threads = ThreadView::select('id', 'amount', 'thread', 'user')->orderBy($sort, $order)->get();
             break;
         case 'amount':
             $threads = ThreadView::select('id', 'amount', 'thread', 'user')->orderBy($sort, $order)->get();
             break;
         default:
             $threads = ThreadView::all();
     }
     return $threads;
 }
 public function index()
 {
     if (!Request::all()) {
         return view('search.index');
     }
     //バリデーションの設定
     $validation = Validator::make(['search' => Input::get('search'), 'user' => Input::get('user'), 'thread' => Input::get('thread'), 'keyword' => Input::get('keyword'), 'content' => Input::get('content')], ['search' => 'required', 'user' => 'required_with:search|accepted', 'thread' => 'required_with:search|accepted', 'keyword' => 'required_with:search|accepted', 'content' => 'required_with:search|accepted'], ['required' => '検索語句を入力してください', 'required_with' => '検索条件が設定されていません', 'accepted' => '検索フォームから検索してください']);
     //バリデーション失敗時
     if ($validation->fails()) {
         return redirect()->back()->withErrors($validation->errors());
     }
     $scope = array('keyword' => 0, 'thread' => 0, 'user' => 0, 'content' => 0);
     foreach ($scope as $key => $val) {
         $scope[$key] = Input::has($key) ? 1 : $val;
     }
     $words = Input::get('search');
     $words = trim($words);
     $words = str_replace(" ", " ", $words);
     if (stristr($words, " ")) {
         $words = explode(" ", $words);
     } else {
         $words = array($words);
     }
     //検索結果格納配列の初期化
     $result = array();
     //検索方法ごとに検索ワードを検索
     foreach ($scope as $key => $val) {
         if (!$val) {
             continue;
         }
         switch ($key) {
             case 'keyword':
                 foreach ($words as $word) {
                     $result[$key][$word] = Comment::select('thread_view.id', 'thread', 'content', 'comments.user as commentator', 'amount', 'thread_view.user')->join('thread_view', 'thread_view.id', '=', 'comments.thread_id')->where('thread', 'Like', "%{$word}%")->orWhere('content', 'Like', "%{$word}%")->orWhere('comments.user', 'Like', "%{$word}%")->groupBy('thread')->groupBy('content')->groupBy('comments.user')->get();
                     if (!isset($result[$key][$word][0])) {
                         continue;
                     }
                     //ヒット個所の強調
                     foreach ($result[$key][$word] as $row) {
                         $row['thread'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['thread']);
                         $row['commentator'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['commentator']);
                         $row['content'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['content']);
                     }
                 }
                 break;
             case 'thread':
                 foreach ($words as $word) {
                     $result[$key][$word] = ThreadView::select('id', 'thread', 'thread_view.user', 'amount')->where('thread', 'like', "%{$word}%")->groupBy('thread')->get();
                     if (!isset($result[$key][$word][0])) {
                         continue;
                     }
                     //ヒット個所の強調
                     foreach ($result[$key][$word] as $row) {
                         //ヒット個所の強調
                         $row['thread'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['thread']);
                     }
                 }
                 break;
             case 'user':
                 foreach ($words as $word) {
                     $result[$key][$word] = Comment::select('thread_view.id', 'thread', 'comments.user as commentator', 'thread_view.user', 'amount')->join('thread_view', 'thread_view.id', '=', 'comments.thread_id')->where('comments.user', 'Like', "%{$word}%")->groupBy('comments.user')->get();
                     if (!isset($result[$key][$word][0])) {
                         continue;
                     }
                     //ヒット個所の強調
                     foreach ($result[$key][$word] as $row) {
                         $row['commentator'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['commentator']);
                     }
                 }
                 break;
             case 'content':
                 foreach ($words as $word) {
                     $result[$key][$word] = Comment::select('thread_view.id', 'thread', 'content', 'thread_view.user', 'amount')->join('thread_view', 'thread_view.id', '=', 'comments.thread_id')->where('content', 'Like', "%{$word}%")->groupBy('content')->get();
                     if (!isset($result[$key][$word][0])) {
                         continue;
                     }
                     //ヒット個所の強調
                     foreach ($result[$key][$word] as $row) {
                         $row['content'] = preg_replace('/' . $word . '/', '~~~~' . $word . '####', $row['content']);
                     }
                 }
                 break;
         }
     }
     return view('search.index')->with('threads', $result);
 }