Ejemplo n.º 1
0
    /**
     * perform a search against all the posts and
     * return some results
     * @todo - ignore word order
     * @todo - remove stop words
     * @todo - deal with exact phrases
     */
    public function find($text)
    {
        $phraseSearch = false;
        $displaySearchResults = true;
        // if text is ^"(.*)"$ or ^'(.*)'$ then we are searching for a phrase
        $pattern = <<<'pattern'
/^['|"](.*)['|"]$/
pattern;
        $matches = false;
        preg_match($pattern, $text, $matches);
        if (!$matches) {
            // set initial results as nothing
            $results = false;
            // look for all the words
            $rawSearchTerms = explode(' ', $text);
            $searchTerms = array();
            // remove stop words here
            foreach ($rawSearchTerms as $word) {
                if (!in_array(strtolower($word), self::$stopWords)) {
                    $searchTerms[] = $word;
                }
            }
            // dd($searchTerms);
            foreach ($searchTerms as $word) {
                // remove unwanted characters from the start and end
                // @todo this does not remove multiple unwanted commas etc
                $word = trim($word);
                if (strlen($word) !== 0) {
                    if ($results) {
                        $results = $results->where('text', 'like', "%{$word}%");
                    } else {
                        // first where
                        $results = \Nexus\Post::where('text', 'like', "%{$word}%");
                    }
                }
            }
        } else {
            $phrase = trim($matches[1]);
            $results = \Nexus\Post::where('text', 'like', "%{$phrase}%");
        }
        if ($results) {
            $results->orderBy('time', 'desc');
        }
        \Nexus\Helpers\ActivityHelper::updateActivity(\Auth::user()->id, "Searching", action('Nexus\\SearchController@index'));
        $breadcrumbs = \Nexus\Helpers\BreadcrumbHelper::breadcumbForUtility('Search');
        return view('search.results', compact('results', 'breadcrumbs', 'text', 'displaySearchResults'));
    }
Ejemplo n.º 2
0
 public function test_deleting_topic_soft_deletes_its_posts()
 {
     // GIVEN we have a topic with post
     $topic = factory(Topic::class, 1)->create();
     factory(Post::class, 20)->create(['topic_id' => $topic->id]);
     // we have 1 topic with 20 posts
     $this->assertEquals(Topic::all()->count(), 1);
     $this->assertEquals(Post::where('topic_id', $topic->id)->count(), 20);
     // WHEN we archive the topic
     $topic->delete();
     // THEN we have no topics and no posts
     $this->assertEquals(Topic::all()->count(), 0);
     $this->assertEquals(Post::all()->count(), 0);
     // BUT we have 1 trashed topic and 20 trashed posts
     $this->assertEquals(Topic::withTrashed()->count(), 1);
     $this->assertEquals(Post::withTrashed()->where('topic_id', $topic->id)->count(), 20);
 }