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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-08-24 09:27:17 +00:00
|
|
|
$news = News::query()->orderBy('id', 'desc')->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([
|
2020-08-12 06:42:58 +00:00
|
|
|
"subname" => "required",
|
2020-08-06 10:21:19 +00:00
|
|
|
"content" => "required"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$news = new News($data);
|
2020-08-12 06:42:58 +00:00
|
|
|
$news->name = "";
|
2020-08-12 13:17:48 +00:00
|
|
|
$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
|
|
|
|
*/
|
2020-08-13 05:38:14 +00:00
|
|
|
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([
|
2020-08-12 13:17:48 +00:00
|
|
|
"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-13 05:38:14 +00:00
|
|
|
|
2020-08-06 10:21:19 +00:00
|
|
|
return redirect()->route("news.index");
|
|
|
|
}
|
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
/**
|
|
|
|
* 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());
|
2020-08-13 05:38:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|