273 lines
10 KiB
PHP
273 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Guide;
|
|
use App\News;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Http\Request;
|
|
use tidy;
|
|
|
|
class GuideController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware([ "auth" ]);
|
|
$this->middleware([ "lang" ]);
|
|
|
|
$this->middleware([ "check.auth:guides.show" ])->only("show", "index");
|
|
$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");
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$guides = Guide::query()->orderBy("created_at", "desc")->get();
|
|
|
|
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)
|
|
{
|
|
$requestGuide = $request->validate([
|
|
"name" => "required|max:255",
|
|
"guide_articles" => "required",
|
|
"guide_category_id" => "required",
|
|
]);
|
|
|
|
$guide = new Guide($requestGuide);
|
|
|
|
//If an image has been uploaded, store the file
|
|
if($request->file("resource")) {
|
|
$guide->resource_id = ResourceController::store($request)->id;
|
|
}
|
|
|
|
$saved = $guide->save();
|
|
|
|
if(!$saved) {
|
|
return redirect()->route("guides.store");
|
|
} else {
|
|
$guides = Guide::query()->get();
|
|
|
|
if($request->newsoption == true){
|
|
$news = new News();
|
|
|
|
$news->name = "Ny vejledning";
|
|
$news->subname = $guide->name;
|
|
$news->arrangement_id = $guide->id;
|
|
$news->type_id = '4';
|
|
$news->news_expiration_date = $request->input("news_expiration_date");
|
|
|
|
$news->content = $guide->guide_articles;
|
|
|
|
if($guide->resource_id !== null)
|
|
$news->resource_id = $guide->resource_id;
|
|
|
|
NewsController::storeAndGet($news);
|
|
}
|
|
|
|
return redirect()->route("guides.index", ['guides' => $guides]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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",
|
|
"guide_articles" => "required",
|
|
"guide_category_id" => "required",
|
|
]);
|
|
|
|
$guidee = Guide::query()->where("id", "=", $guide->id)->first();
|
|
$guidee->update($data);
|
|
$saved = $guidee->save();
|
|
|
|
if(!$saved){
|
|
return redirect()->route("guides.update", [ "guide" => $guide ]);
|
|
}else{
|
|
$guides = Guide::query()->get();
|
|
|
|
if($request->newsoption == true){
|
|
$news = new News();
|
|
|
|
$news->name = "Vejledning opdateret";
|
|
$news->subname = $guidee->name;
|
|
$news->arrangement_id = $guide->id;
|
|
$news->type_id = '4';
|
|
|
|
$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>';
|
|
|
|
NewsController::storeAndGet($news);
|
|
}
|
|
|
|
return redirect()->route("guides.index", ['guides' => $guides]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param Guide $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
// Find the guide you want to remove
|
|
$guide = Guide::find($id);
|
|
// Find the news the you made when creating the guide (if there is a news)
|
|
$news = News::query()->join('news_types', 'news_types.id', '=', 'news.type_id')->where("type", "=", "Guide")->where("arrangement_id", "=", $id);
|
|
|
|
//Delete the news or/and guide
|
|
$news->delete();
|
|
$guide->delete();
|
|
|
|
|
|
return redirect()->route("guides.index");
|
|
}
|
|
|
|
//Used to fix html, that have been substringed
|
|
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;
|
|
}
|
|
|
|
//Used for checking if the currently typed guide name is unique. Create version
|
|
public function nameCheck(Request $request) {
|
|
$guide = Guide::query()->where('name', 'LIKE',$request->nameCheck)->get();
|
|
if(count($guide) > 0 && $request->nameCheck !== NULL){
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
//Used for checking if the currently typed guide name is unique. Edit version
|
|
public function nameCheckUpdate(Request $request) {
|
|
$guide = Guide::query()->where('name', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
|
|
if(count($guide) > 0 && $request->nameCheck !== NULL){
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Runs the ajax call when changing the select field in guides.index (app)
|
|
public function showCategory(Request $request){
|
|
if($request->ajax()) {
|
|
// Get all guides where the category is what you want
|
|
if ($request->category != 'All')
|
|
$guides = Guide::query()->orderBy("created_at", "desc")->where('guide_category_id', '=', $request->category)->get();
|
|
else
|
|
$guides = Guide::query()->orderBy("created_at", "desc")->get();
|
|
|
|
$output = '';
|
|
|
|
// Begin showing all guides with the given category
|
|
if(!$guides->isEmpty()) {
|
|
foreach ($guides as $guide) {
|
|
$output .= '<div class="card">';
|
|
if ($guide->resource_id !== null) {
|
|
$output .= '<div class="header" style="background-size: cover; background-position: center; background-image: url(' . asset(\App\Resource::query()->where("id", "=", $guide->resource_id)->first()->filename) . ');">' .
|
|
'<h3 style="text-shadow: 2px 2px 2px #00788A;">' . $guide->name . '</h3>' .
|
|
'</div>';
|
|
} else {
|
|
$output .= '<div class="header">' .
|
|
'<h3>' . $guide->name . '</h3>' .
|
|
'</div>';
|
|
}
|
|
|
|
$output .= '<div class="container">';
|
|
$tags = ['<p>', '<b>', '<em>', '<a>', '<u>', '<s>', '<sub>', '<ul>', '<li>', '<sup>', '<div>', '<blockquote>', '<ol>', '<strong>', '<br>', '<h1>', '<h2>', '<h3>', '<h4>', '<h5>', '<h6>', '<h7>', '<span>'];
|
|
$output .= '' . \App\Helpers::closetags(substr(strip_tags($guide->guide_articles, $tags), 0, 300)) . '' .
|
|
'<div class="row" style="justify-content: center;">';
|
|
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
$output .= '<a style="margin: 0; padding: 0; text-align: center; font-weight: 700;" class="sde-blue" href="'. route("guides.show", ["guide" => $guide->id ]) .'">Læs mere</a>';
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
$output .= '<a style="margin: 0; padding: 0; text-align: center; font-weight: 700;" class="sde-blue" href="'. route("guides.show", ["guide" => $guide->id ]) .'">Read more</a>';
|
|
else
|
|
$output .= '<a style="margin: 0; padding: 0; text-align: center; font-weight: 700;" class="sde-blue" href="'. route("guides.show", ["guide" => $guide->id ]) .'">Læs mere</a>';
|
|
|
|
$output .= '</div>'.
|
|
'</div>'.
|
|
'</div>';
|
|
}
|
|
} else { // If there are no guides with the given category, then display error message
|
|
if (request()->cookie('languagesSetting') == "dk")
|
|
$output .= '<p style="margin-bottom: auto; text-align: center">Der er ingen vejledninger af denne kategori</p>';
|
|
elseif (request()->cookie('languagesSetting') == "en")
|
|
$output .= '<p style="margin-bottom: auto; text-align: center">There are no guides of this category</p>';
|
|
else
|
|
$output .= '<p style="margin-bottom: auto; text-align: center">Der er ingen vejledninger af denne kategori</p>';
|
|
}
|
|
|
|
return Response($output);
|
|
}
|
|
}
|
|
}
|