summaryrefslogtreecommitdiff
path: root/app/Http/Controllers/IndexController.php
blob: 50f2bf45ed3b874e12debcd401fb6cf0c936ce3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Article;
use App\Discussion;
use App\Category;
use App\Libraries\Helper;

class IndexController extends Controller
{

    public function index()
    {
            return view('index');
    }

    public function about()
    {
            return view('about');
    }

    public function topic( $topic )
    {
	    $articles = new Category;
	    $articles = $articles->orWhere('name', $topic);
	    $articles = $articles->get()->first();
	    if ( is_null($articles) ) {
		    abort(404);
	    }
	    $articles = $articles->getArticles();
	    $articles = $articles->orderBy('impact', 'desc');
	    $count = $articles->count();
	    $articles = $articles->simplePaginate(10);

            return view('list', ["articles" => $articles, "count" => $count]);
    }

    public function new()
    {
	    $articles = Article::orderBy('created_at', 'desc');
	    $count = $articles->count();
	    $articles = $articles->simplePaginate(10);

            return view('list', ["articles" => $articles, "count" => $count]);
    }

    public function search(Request $request) {
	    $search_unsafe = $request->input("q");

	    if ( "" == $search_unsafe ) {
		    $search_unsafe = "";
	    }

	    $search_unsafe = explode(",", $request->input("q"));

	    $articles = new Article;

	    if ( "on" == $request->input("onlypopular") ) {
		$articles = $articles->setTable('view_popular');
	    }

	    foreach($search_unsafe as $q) {
		    $q = Helper::escapeLike($q);
		    $q = "%".$q."%";
		    $articles = $articles->where(function ($query) use ($q) {
		    		$query->whereHas('getCategories', function ($query) use ($q){
		    			$query->where('name', 'like', $q);
	    			})
		    		->orWhere('title', 'like', $q)
				->orWhere('excerpt_html', 'like', $q);
				});
	    }
	    $count = $articles->count();

	    if ( "on" == $request->input("onlypopular") ) {
		$articles = $articles->orderBy('impact', 'desc');
	    }
	    $articles = $articles->orderBy('created_at', 'desc');
	    $articles = $articles->simplePaginate(10);

            return view('list', ["articles" => $articles, "count" => $count]);
    }

    public function popular()
    {
	    $articles = new Article;
	    $articles = $articles->setTable('view_popular');
	    $count = $articles->count();
	    $articles = $articles->simplePaginate(10);

            return view('list', ["articles" => $articles, "count" => $count]);
    }

    function topicindex() {
	    $categories = Category::orderBy('name');
	    $letters = DB::select("SELECT DISTINCT LEFT(name, 1) as name FROM category ORDER BY left(name, 1);");

	    return view('topicindex', ['topics' => $categories, 'letters' => $letters]);
    }

    public function random() {
	    $articles = new Article;
	    $articles = $articles->inRandomOrder();
	    $count = $articles->count();
	    $articles = $articles->simplePaginate(10);

            return view('list', ["articles" => $articles, "count" => $count]);
    }

    public function populartopics() {
	    $topics = DB::select("
			SELECT
			    c.name,
			    count(c.name) AS count
			FROM category AS c
			JOIN
				article_category AS ac ON c.id = ac.category_id
			GROUP BY
				c.name
			ORDER BY count(c.name) DESC;");
#echo "<pre>"; var_dump($topics);exit;

	    return view('topicpopular', ['topics' => $topics ]);
    }
}