/
CategoryController.php
154 lines (127 loc) · 4.63 KB
/
CategoryController.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Infogue\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Infogue\Category;
use Infogue\Http\Requests;
use Infogue\Subcategory;
class CategoryController extends Controller
{
/*
|--------------------------------------------------------------------------
| Category Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling article showing grouped by
| category and sub category, the categories are given by slug from category
| title and the article list is result of guessing reverse form of slug.
|
*/
/**
* Instance variable of Category.
*
* @var Category
*/
private $category;
/**
* Instance variable of Subcategory.
*
* @var Subcategory
*/
private $subcategory;
/**
* Create a new category controller instance.
*
* @param Category $category
* @param Subcategory $subcategory
*/
public function __construct(Category $category, Subcategory $subcategory)
{
$this->category = $category;
$this->subcategory = $subcategory;
}
/**
* Display a listing of the category.
*
* @param Request $request
* @param $slug
* @return \Illuminate\Http\Response
*/
public function category(Request $request, $slug)
{
/*
* --------------------------------------------------------------------------
* Populating article by category
* --------------------------------------------------------------------------
* Reverse category by slug form into plain name and select article by that
* name, also construct breadcrumb stack, because we implement lazy
* pagination via ajax so return json when 'page' variable exist.
*/
$categoryName = str_replace('-', ' ', $slug);
$category = $this->category->where('category', 'like', $categoryName)->firstOrFail();
$articles = $this->category->categoryArticle($category->id);
$breadcrumb = [
'Archive' => route('article.archive'),
$category->category => route('article.category', [$slug]),
];
$next_ref = '#';
$prev_ref = '#';
if (Input::get('page', false) && $request->ajax()) {
return $articles;
} else {
return view('article.category', compact('breadcrumb', 'next_ref', 'prev_ref'));
}
}
/**
* Display a listing of the subcategory.
*
* @param Request $request
* @param $category_slug
* @param $subcategory_slug
* @return \Illuminate\Http\Response
*/
public function subcategory(Request $request, $category_slug, $subcategory_slug)
{
/*
* --------------------------------------------------------------------------
* Populating article by sub category
* --------------------------------------------------------------------------
* Article is retrieved by category then from category select subcategory,
* because some categories maybe have similar subcategory name and resulting
* same slug, they return view or json depend on 'page' existences.
*/
$category_name = str_replace('-', ' ', $category_slug);
$subcategory_name = str_replace('-', ' ', $subcategory_slug);
$category = $this->category->where('category', 'like', $category_name)->firstOrFail();
$subcategory = $category->subcategories()->where('subcategory', 'like', $subcategory_name)->firstOrFail();
$articles = $this->subcategory->subcategoryArticle($subcategory->id);
$breadcrumb = [
'Archive' => route('article.archive'),
$category->category => route('article.category', [$category_slug]),
$subcategory->subcategory => route('article.subcategory', [$category_slug, $subcategory_slug]),
];
$next_ref = '#';
$prev_ref = '#';
if (Input::get('page', false) && $request->ajax()) {
return $articles;
} else {
return view('article.category', compact('breadcrumb', 'next_ref', 'prev_ref'));
}
}
/**
* Retrieve subcategory by category id request via AJAX.
*
* @param Request $request
* @param $id
* @return json
*/
public function subcategories(Request $request, $id)
{
if ($request->ajax()) {
$category = $this->category->findOrFail($id);
return $category->subcategories;
} else {
abort(403, 'Resources are restricted.');
}
}
}