Completed
Push — master ( 4b9a6b...702352 )
by ARCANEDEV
04:47
created

CategoriesController::restore()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 8.8571
cc 2
eloc 16
nc 4
nop 1
crap 6
1
<?php namespace Arcanesoft\Blog\Http\Controllers\Foundation;
2
3
use Arcanesoft\Blog\Bases\FoundationController;
4
use Arcanesoft\Blog\Http\Requests\Backend\Categories\CreateCategoryRequest;
5
use Arcanesoft\Blog\Http\Requests\Backend\Categories\UpdateCategoryRequest;
6
use Arcanesoft\Blog\Models\Category;
7
use Illuminate\Support\Facades\Log;
8
9
/**
10
 * Class     CategoriesController
11
 *
12
 * @package  Arcanesoft\Blog\Http\Controllers\Foundation
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class CategoriesController extends FoundationController
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The category model.
23
     *
24
     * @var \Arcanesoft\Blog\Models\Category
25
     */
26
    private $category;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Constructor
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * Instantiate the controller.
34
     *
35
     * @param  \Arcanesoft\Blog\Models\Category  $category
36
     */
37
    public function __construct(Category $category)
38
    {
39
        parent::__construct();
40
41
        $this->category = $category;
42
43
        $this->setCurrentPage('blog-categories');
44
        $this->addBreadcrumbRoute('Categories', 'blog::foundation.categories.index');
45
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Main Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    public function index($trashed = false)
52
    {
53
        $this->authorize('blog.categories.list');
54
55
        $categories = $this->category->with(['posts']);
56
        $categories = $trashed
57
            ? $categories->onlyTrashed()->paginate(30)
58
            : $categories->paginate(30);
59
60
        $title = 'Blog - Categories';
61
        $this->setTitle($title);
62
        $this->addBreadcrumb('List all categories');
63
64
        return $this->view('foundation.categories.list', compact('categories', 'trashed'));
65
    }
66
67
    public function trash()
68
    {
69
        return $this->index(true);
70
    }
71
72
    public function create()
73
    {
74
        $this->authorize('blog.categories.create');
75
76
        $title = 'Blog - Categories';
77
        $this->setTitle($title);
78
        $this->addBreadcrumb('Create category');
79
80
        return $this->view('foundation.categories.create');
81
    }
82
83
    public function store(CreateCategoryRequest $request, Category $category)
84
    {
85
        $this->authorize('blog.categories.create');
86
87
        $category->fill($request->only(['name']));
88
        $category->save();
89
90
        $message = "The category {$category->name} was created successfully !";
91
        Log::info($message, $category->toArray());
92
        $this->notifySuccess($message, 'Category created !');
93
94
        return redirect()->route('blog::foundation.categories.index');
95
    }
96
97
    public function show(Category $category)
98
    {
99
        $this->authorize('blog.categories.show');
100
101
        $category->load(['posts']);
102
103
        $title = 'Blog - Categories';
104
        $this->setTitle($title);
105
        $this->addBreadcrumb('Category - ' . $category->name);
106
107
        return $this->view('foundation.categories.show', compact('category'));
108
    }
109
110
    public function edit(Category $category)
111
    {
112
        $this->authorize('blog.categories.update');
113
114
        $title = 'Blog - Categories';
115
        $this->setTitle($title);
116
        $this->addBreadcrumb('Update category');
117
118
        return $this->view('foundation.categories.edit', compact('category'));
119
    }
120
121
    public function update(UpdateCategoryRequest $request, Category $category)
122
    {
123
        $this->authorize('blog.categories.update');
124
125
        $category->update($request->only(['name']));
126
127
        $message = "The category {$category->name} was updated successfully !";
128
        Log::info($message, $category->toArray());
129
        $this->notifySuccess($message, 'Category updated !');
130
131
        return redirect()->route('blog::foundation.categories.show', [$category->id]);
132
    }
133
134
    public function restore(Category $category)
135
    {
136
        self::onlyAjax();
137
        $this->authorize('blog.categories.update');
138
139
        try {
140
            $category->restore();
141
142
            $message = "The category {$category->name} has been successfully restored !";
143
            Log::info($message, $category->toArray());
144
            $this->notifySuccess($message, 'Category restored !');
145
146
            $ajax = [
147
                'status'  => 'success',
148
                'message' => $message,
149
            ];
150
        }
151
        catch (\Exception $e) {
152
            $ajax = [
153
                'status'  => 'error',
154
                'message' => $e->getMessage(),
155
            ];
156
        }
157
158
        return response()->json($ajax);
159
    }
160
161
    public function delete(Category $category)
162
    {
163
        self::onlyAjax();
164
        $this->authorize('blog.categories.delete');
165
166
        try {
167
            if ($category->trashed()) {
168
                $category->forceDelete();
169
            }
170
            else {
171
                $category->delete();
172
            }
173
174
            $message = "The category {$category->name} has been successfully deleted !";
175
            Log::info($message, $category->toArray());
176
            $this->notifySuccess($message, 'Category deleted !');
177
178
            $ajax = [
179
                'status'  => 'success',
180
                'message' => $message,
181
            ];
182
        }
183
        catch(\Exception $e) {
184
            $ajax = [
185
                'status'  => 'error',
186
                'message' => $e->getMessage(),
187
            ];
188
        }
189
190
        return response()->json($ajax);
191
    }
192
}
193