Completed
Push — 16470-hide-recipients-on-bulk-... ( 9688d4 )
by Shawn
02:27
created

NewsController::emailNews()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Gate;
7
use Illuminate\Support\Facades\Storage;
8
use Krucas\Notification\Facades\Notification;
9
use SET\Attachment;
10
use SET\Http\Requests\NewsRequest;
11
use SET\News;
12
13
/**
14
 * Description of NewsController.
15
 *
16
 * @author sketa
17
 */
18
class NewsController extends Controller
19
{
20
    /**
21
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
22
     */
23
    public function index()
24
    {
25
        if (Gate::denies('view')) {
26
            $allNews = News::publishedNews()->orderBy('publish_date', 'desc')->get();
27
        } else {
28
            $allNews = News::orderBy('publish_date', 'desc')->get();
29
        }
30
31
        return view('news.index', compact('allNews'));
32
    }
33
34
    public function create()
35
    {
36
        $this->authorize('edit');
37
38
        return view('news.create', compact('users'));
39
    }
40
41
    /**
42
     * Store a newly created resource in storage.
43
     *
44
     * @param NewsRequest $request
45
     *
46
     * @return \Illuminate\Http\RedirectResponse
47
     */
48
    public function store(NewsRequest $request)
49
    {
50
        $this->authorize('edit');
51
        $data = $request->all();
52
        $data['author_id'] = Auth::user()->id;
53
        $news = News::create($data);
54
55
        if ($request->hasFile('files')) {
56
            Attachment::upload($news, $request->file('files'));
57
        }
58
59
        $news->emailNews();
60
        Notification::container()->success('News Created');
61
62
        return redirect()->action('NewsController@index');
63
    }
64
65
    /**
66
     * Show the individual news article.
67
     *
68
     * @param News $news
69
     *
70
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
71
     */
72
    public function show(News $news)
73
    {
74
        $this->authorize('show_published_news', $news);
75
76
        return view('news.show', compact('news'));
77
    }
78
79
    /**
80
     * Show the news article to be edited.
81
     *
82
     * @param $newsId
83
     *
84
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
85
     */
86
    public function edit($newsId)
87
    {
88
        $this->authorize('edit');
89
        $news = News::findOrFail($newsId);
90
91
        return view('news.edit', compact('news'));
92
    }
93
94
    /**
95
     * Update the selected news object with the request.
96
     *
97
     * @param NewsRequest $request
98
     * @param News        $news
99
     *
100
     * @return \Illuminate\Http\RedirectResponse
101
     */
102
    public function update(NewsRequest $request, News $news)
103
    {
104
        $this->authorize('edit');
105
106
        $data = $request->all();
107
        $news->update($data);
108
        if ($request->hasFile('files')) {
109
            Attachment::upload($news, $request->file('files'));
110
        }
111
        $news->emailNews();
112
113
        Notification::container()->success('News Updated');
114
115
        return redirect()->action('NewsController@index');
116
    }
117
118
    /**
119
     * Delete the specified news article.
120
     *
121
     * @param News $news
122
     */
123
    public function destroy(News $news)
124
    {
125
        $this->authorize('edit');
126
127
        Storage::deleteDirectory('news_'.$news->id);
128
        $news->delete();
129
    }
130
}
131