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

185 lines
4.7 KiB
PHP
Raw Normal View History

2020-08-06 10:21:19 +00:00
<?php
namespace App\Http\Controllers;
2020-08-31 11:20:49 +00:00
use App\Event;
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;
date_default_timezone_set('Europe/Copenhagen');
2020-08-06 10:21:19 +00:00
class NewsController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
2020-08-31 06:16:26 +00:00
$this->middleware([ "lang" ]);
2020-08-06 10:21:19 +00:00
2020-08-31 07:36:10 +00:00
$this->middleware([ "check.auth:news.show" ])->only("show", "index");
2020-08-06 10:21:19 +00:00
$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()->where('news_expiration_date', '<=', date('Y-m-d').'T'.date('H:i') )->get();
foreach ($news as $new) {
$new->delete();
}
2020-09-18 08:04:33 +00:00
$news = News::query()->orderBy('id', 'desc')->get();
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",
"content" => "required",
"news_expiration_date" => ""
2020-08-06 10:21:19 +00:00
]);
$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
{
$OldNews = News::query()->where('arrangement_id', "=", $news->arrangement_id);
$OldNews->delete();
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",
"content" => "required",
"news_expiration_date" => ""
2020-08-06 10:21:19 +00:00
]);
$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
}