middleware([ "auth" ]);
$this->middleware([ "lang" ]);
$this->middleware([ "check.auth:event.list" ])->only("index");
$this->middleware([ "check.auth:event.show" ])->only("show");
$this->middleware([ "check.auth:event.create" ])->only("create", "store");
$this->middleware([ "check.auth:event.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:event.delete" ])->only("delete");
}
/**
* Display a listing of the resource..
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
$events = Event::query()->orderBY('date' , 'asc')->paginate($request->input("limit", 20));
//returns the function with events index page and a parameter of events.
//also Response::detect checks screen size to determine if user is on a computer or mobile.
return Response::detect("events.index", [ "events" => $events]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
//returns "create event" blade
return Response::detect("events.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function store(Request $request)
{
$requestBody = $request->validate([
"name" => "required|max:255",
"accountable" => "required|max:255",
"description" => "required",
"date" => "required"
]);
//creates a new Event model with the given parameter
$event = new Event($requestBody);
$allEvents = Event::query()->where('name', '=', $request->name)->get();
if(count($allEvents) > 0) {
return redirect()->route("events.index", ['events' => $event])->with('EventNameExists', '
Der findes allerede et event med det navn!
');
} else {
if($request->file("resource")) {
$event->resource_id = ResourceController::store($request)->id;
}
$event->save();
$events = Event::query()->paginate($request->input("limit", 20));
if($request->newsoption == true){
$news = new News();
$news->name = "Ny aktivitet";
$news->subname = $event->name;
$news->content = "" . $this->closetags(substr($event->description, 0, 300)). "
" .
'';
$news->resource_id = $event->resource_id;
$news->save();
}
return redirect()->route('events.index', ['events' => $events]);
}
}
/**
* Display the specified resource.
*
* @param Event $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show($id)
{
$event = Event::query()->where("id", "=", $id)->first();
return Response::detect("events.show", [ "event" => $event ]);
}
/**
* Display signups for event.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function signups(Request $request)
{
// Find every event you have clicked on. And find all users to that event, and the event name itself.
$events = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('events', 'events.id', '=', 'user_events.event_id')->where('event_id', $request->event)->get();
if (count($events) == 0)
$events = Event::where('id', $request->event)->get();
return Response::detect("events.signups", [ "events" => $events ]);
}
/**
* Display signups for event.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function accountsignups(Request $request)
{
// Find every event you have clicked on. And find all users to that event, and the event name itself. - This is only being showed on App
$events = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('events', 'events.id', '=', 'user_events.event_id')->where('event_id', $request->event)->get();
return Response::detect("events.signups", [ "events" => $events ]);
}
/**
* Show the form for editing the specified resource.-
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id)
{
$event = Event::find($id);
return Response::detect("events.edit", [ "event" => $event ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function update(Request $request, $id)
{
$data = $request->all();
$event = Event::find($id);
$allEvents = Event::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get();
if(count($allEvents) > 0){
return redirect()->route("events.index", ['events' => $event])->with('EventNameExists', 'Der findes allerede et event med det navn!
');
}else{
$event->update($data);
$event->save();
$events = Event::query()->paginate($request->input("limit", 20));
if($request->newsoption == true){
$news = new News();
$news->name = "Aktivitet opdateret";
$news->subname = $event->name;
$news->content = "" . $this->closetags(substr($event->description, 0, 300)). "
" .
'';
$news->save();
}
return redirect()->route("events.index", ['events' => $events]);
}
}
/**
* Remove the specified resource from storage.
*
* @param Event $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Exception
*/
public function destroy(Request $request, $id)
{
if ($request->signup != null) { // If input signup is not empty, which has been set when you look at the individuel signup, then delete the user who have signed up for the event
$UserEvent = UserEvent::where('user_id', $request->signup)->where('event_id', $id);
$UserEvent->delete();
return redirect()->route("events.signups", [ "event" => $id ]);
} else { // Else if you are deleting an event. Then delete all the sign ups AND the event
$UserEvent = UserEvent::where('event_id', $id);
$UserEvent->delete();
$event = Event::find($id);
$event->delete();
return redirect()->route("events.index");
}
}
public function search(Request $request){
if($request->ajax()){
$output = "".
"Event Navn | ".
"Event Ansvarlig | ".
"Event Dato | ".
" | ".
" | ".
" | ".
"
";
$events = DB::table('events')->orderBY('date' , 'asc')->where('name', 'LIKE',$request->search.'%')
->orWhere('date','LIKE', $request->search.'%')
->get();//Job is here dude :9
if(count($events) !== 0){
foreach ($events as $key => $event){
$output.=''.
'' . $event->name . ' | '.
'' . $event->accountable .' | '.
'' . \Illuminate\Support\Facades\Date::createFromTimeStamp(strtotime($event->date))->format('d/m/Y \k\l\. H:i') .' | '.
' $event->id ]).'"> | '.
' $event->id ]) . '"> | '.
''.
' | '.
'
';
}
}else{
$output.=''.
'Intet match | '.
' | '.
' | '.
' | '.
' | '.
' | '.
'
';
}
return Response($output);
}
}
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;
}
}