2020-08-04 11:21:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Guide;
|
2020-08-07 07:47:23 +00:00
|
|
|
use App\News;
|
2020-08-04 11:21:23 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Http\Request;
|
2020-08-10 08:21:09 +00:00
|
|
|
use tidy;
|
2020-08-04 11:21:23 +00:00
|
|
|
|
|
|
|
class GuideController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware([ "auth" ]);
|
2020-08-06 13:31:38 +00:00
|
|
|
$this->middleware([ "lang" ]);
|
2020-08-04 11:21:23 +00:00
|
|
|
|
2020-08-06 06:44:21 +00:00
|
|
|
$this->middleware([ "check.auth:guides.list" ])->only("index");
|
|
|
|
$this->middleware([ "check.auth:guides.show" ])->only("show");
|
|
|
|
$this->middleware([ "check.auth:guides.create" ])->only("create", "store");
|
|
|
|
$this->middleware([ "check.auth:guides.edit" ])->only("edit", "update");
|
|
|
|
$this->middleware([ "check.auth:guides.delete" ])->only("delete");
|
2020-08-04 11:21:23 +00:00
|
|
|
}
|
2020-08-06 06:44:21 +00:00
|
|
|
|
2020-08-04 11:21:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$guides = Guide::query()->paginate($request->input("limit", 20));
|
|
|
|
|
|
|
|
return Response::detect("guides.index", [ "guides" => $guides]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Response::detect("guides.create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
//Me no sure what to store mester big smoke :)
|
|
|
|
$requestGuide = $request->validate([
|
|
|
|
"name" => "required|max:255",
|
2020-08-04 11:37:06 +00:00
|
|
|
"guide_articles" => "required",
|
2020-08-04 11:21:23 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$guide = new Guide($requestGuide);
|
2020-08-13 05:38:14 +00:00
|
|
|
|
2020-08-12 10:49:05 +00:00
|
|
|
if($request->file("resource")) {
|
|
|
|
$guide->resource_id = ResourceController::store($request)->id;
|
|
|
|
}
|
2020-08-13 05:38:14 +00:00
|
|
|
|
2020-08-04 11:21:23 +00:00
|
|
|
$saved = $guide->save();
|
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
if(!$saved) {
|
2020-08-05 07:23:57 +00:00
|
|
|
return redirect()->route("guides.store");
|
2020-08-13 05:38:14 +00:00
|
|
|
} else {
|
2020-08-07 07:47:23 +00:00
|
|
|
$guides = Guide::query()->paginate($request->input("limit", 20));
|
|
|
|
|
|
|
|
if($request->newsoption == true){
|
|
|
|
$news = new News();
|
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
$news->name = "Ny vejledning";
|
2020-08-10 07:37:19 +00:00
|
|
|
$news->subname = $guide->name;
|
2020-08-12 13:17:48 +00:00
|
|
|
$news->arrangement_id = $guide->id;
|
|
|
|
$news->type_id = '4';
|
2020-08-10 08:21:09 +00:00
|
|
|
|
2020-08-12 13:17:48 +00:00
|
|
|
$news->content = $guide->guide_articles;
|
2020-08-07 07:47:23 +00:00
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
if($guide->resource_id !== null)
|
|
|
|
$news->resource_id = $guide->resource_id;
|
|
|
|
|
|
|
|
NewsController::storeAndGet($news);
|
2020-08-07 07:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route("guides.index", ['guides' => $guides]);
|
2020-08-04 11:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Guide $guide
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show(Guide $guide)
|
|
|
|
{
|
|
|
|
return Response::detect("guides.show", [ "guide" => $guide]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param \App\Guide $guide
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit(Guide $guide)
|
|
|
|
{
|
|
|
|
$guidee = Guide::query()->where("id", "=", $guide->id)->first();
|
|
|
|
return Response::detect("guides.edit", ["guide" => $guidee]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \App\Guide $guide
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, Guide $guide)
|
|
|
|
{
|
|
|
|
$data = $request->validate([
|
|
|
|
"name" => "required|max:255",
|
2020-08-04 11:37:06 +00:00
|
|
|
"guide_articles" => "required",
|
2020-08-04 11:21:23 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$guidee = Guide::query()->where("id", "=", $guide->id)->first();
|
|
|
|
$guidee->update($data);
|
|
|
|
$saved = $guidee->save();
|
|
|
|
|
|
|
|
if(!$saved){
|
2020-08-05 07:43:53 +00:00
|
|
|
return redirect()->route("guides.update", [ "guide" => $guide ]);
|
2020-08-04 11:21:23 +00:00
|
|
|
}else{
|
2020-08-13 05:38:14 +00:00
|
|
|
$guides = Guide::query()->paginate($request->input("limit", 20));
|
2020-08-07 07:47:23 +00:00
|
|
|
|
|
|
|
if($request->newsoption == true){
|
|
|
|
$news = new News();
|
|
|
|
|
|
|
|
$news->name = "Vejledning opdateret";
|
2020-08-10 07:37:19 +00:00
|
|
|
$news->subname = $guidee->name;
|
2020-08-12 13:17:48 +00:00
|
|
|
$news->arrangement_id = $guide->id;
|
|
|
|
$news->type_id = '4';
|
2020-08-10 08:21:09 +00:00
|
|
|
|
2020-08-10 10:14:09 +00:00
|
|
|
$news->content = $this->closetags(substr($guidee->guide_articles, 0, 300)). '<br><a style="font-weight: 700;" href="' . route("guides.show", ["guide" => $guide]). '" class="sde-blue ">Læs mere</a>';
|
2020-08-07 07:47:23 +00:00
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
NewsController::storeAndGet($news);
|
2020-08-07 07:47:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 05:38:14 +00:00
|
|
|
return redirect()->route("guides.index", ['guides' => $guides]);
|
2020-08-04 11:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-08-06 12:47:22 +00:00
|
|
|
* @param Guide $guide
|
2020-08-04 11:21:23 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-08-06 12:47:22 +00:00
|
|
|
public function destroy($guide)
|
2020-08-04 11:21:23 +00:00
|
|
|
{
|
2020-08-06 12:47:22 +00:00
|
|
|
$guide = Guide::find($guide);
|
2020-08-04 11:21:23 +00:00
|
|
|
$guide->delete();
|
2020-08-06 12:47:22 +00:00
|
|
|
|
2020-08-04 11:21:23 +00:00
|
|
|
return redirect()->route("guides.index");
|
|
|
|
}
|
2020-08-10 08:21:09 +00:00
|
|
|
|
|
|
|
public function closetags($html) {
|
|
|
|
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
|
|
|
|
$openedtags = $result[1];
|
|
|
|
preg_match_all('#</([a-z]+)>#iU', $html, $result);
|
|
|
|
$closedtags = $result[1];
|
|
|
|
$len_opened = count($openedtags);
|
|
|
|
if (count($closedtags) == $len_opened) {
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
$openedtags = array_reverse($openedtags);
|
|
|
|
for ($i=0; $i < $len_opened; $i++) {
|
|
|
|
if (!in_array($openedtags[$i], $closedtags)) {
|
|
|
|
$html .= '</'.$openedtags[$i].'>';
|
|
|
|
} else {
|
|
|
|
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:21:23 +00:00
|
|
|
}
|