Ekapp/skolehjem/app/Http/Controllers/NewsController.php

172 lines
4.3 KiB
PHP
Raw Normal View History

2020-08-06 10:21:19 +00:00
<?php
namespace App\Http\Controllers;
2020-08-12 13:26:32 +00:00
use App\Helpers;
2020-08-06 10:21:19 +00:00
use App\News;
2020-08-12 13:26:32 +00:00
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse;
2020-08-06 10:21:19 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class NewsController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
2020-08-18 10:33:04 +00:00
$this->middleware(["lang"]);
2020-08-06 10:21:19 +00:00
$this->middleware([ "check.auth:news.list" ])->only("index");
$this->middleware([ "check.auth:news.show" ])->only("show");
$this->middleware([ "check.auth:news.create" ])->only("create", "store");
$this->middleware([ "check.auth:news.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:news.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$news = News::query()->orderBy('id', 'asc')->paginate(20);
2020-08-06 10:21:19 +00:00
return Response::detect("news.index", [ "news" => $news ]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("news.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
*/
public function store(Request $request)
{
$data = $request->validate([
"subname" => "required",
2020-08-06 10:21:19 +00:00
"content" => "required"
]);
$news = new News($data);
$news->name = "";
$news->type_id = '1';
2020-08-06 10:21:19 +00:00
$news->save();
2020-08-13 11:50:00 +00:00
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
2020-08-12 13:26:32 +00:00
2020-08-06 10:21:19 +00:00
return redirect()->route("news.index");
}
2020-08-12 13:26:32 +00:00
/**
* Store a newly created resource in storage.
*
* @return News
*/
public static function storeAndGet(News $news)
2020-08-12 13:26:32 +00:00
{
$news->save();
2020-08-13 11:50:00 +00:00
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
2020-08-12 13:26:32 +00:00
return $news;
}
2020-08-06 10:21:19 +00:00
/**
* Display the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function show(News $news)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function edit(News $news)
{
return Response::detect("news.edit", [ "news" => $news ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
*/
public function update(Request $request, News $news)
{
$data = $request->validate([
"subname" => "required",
2020-08-06 10:21:19 +00:00
"content" => "required"
]);
$news->update($data);
2020-08-13 11:50:00 +00:00
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
2020-08-06 10:21:19 +00:00
return redirect()->route("news.index");
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
* @return RedirectResponse
*/
public static function updateAndGet(News $news)
{
$news->update();
2020-08-13 11:50:00 +00:00
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
return $news;
}
2020-08-06 10:21:19 +00:00
/**
* Remove the specified resource from storage.
*
* @param \App\News $news
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
* @throws \Exception
*/
public function destroy(News $news)
{
$news->delete();
return redirect()->route("news.index");
}
2020-08-17 07:21:47 +00:00
public function nameCheck(Request $request){
$news = News::query()->where('subname', 'LIKE',$request->nameCheck)->get();
if(count($news) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
2020-08-17 08:21:02 +00:00
public function nameCheckUpdate(Request $request){
$news = News::query()->where('subname', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
if(count($news) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
2020-08-06 10:21:19 +00:00
}