2020-06-09 07:05:07 +00:00
< ? php
namespace App\Http\Controllers ;
2020-06-18 06:01:42 +00:00
use App\Event ;
2020-08-07 07:47:23 +00:00
use App\News ;
2020-08-13 11:50:00 +00:00
use App\Notification ;
2021-04-19 12:42:03 +00:00
use App\Resource ;
2020-07-28 12:54:19 +00:00
use App\UserEvent ;
2020-06-10 06:33:06 +00:00
use App\User ;
2020-06-09 07:05:07 +00:00
use Illuminate\Http\Request ;
2020-06-24 06:23:15 +00:00
use Illuminate\Http\Response ;
2020-07-27 14:03:49 +00:00
use Illuminate\Support\Facades\DB ;
2020-06-24 06:23:15 +00:00
2020-08-31 12:04:02 +00:00
date_default_timezone_set ( 'Europe/Copenhagen' );
2020-06-09 07:05:07 +00:00
class EventController extends Controller
{
2020-06-30 07:21:21 +00:00
public function __construct ()
{
$this -> middleware ([ " auth " ]);
2020-08-06 13:31:38 +00:00
$this -> middleware ([ " lang " ]);
2020-06-30 07:21:21 +00:00
2020-08-31 07:36:10 +00:00
$this -> middleware ([ " check.auth:event.show " ]) -> only ( " show " , " index " );
2020-06-30 07:21:21 +00:00
$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 " );
}
2020-06-09 07:05:07 +00:00
/**
2020-06-10 06:33:06 +00:00
* Display a listing of the resource ..
2020-06-09 07:05:07 +00:00
*
2020-06-18 06:01:42 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-09 07:05:07 +00:00
*/
2020-06-10 06:33:06 +00:00
public function index ( Request $request )
2020-06-09 07:05:07 +00:00
{
2020-09-18 08:04:33 +00:00
$events = Event :: query () -> orderBY ( 'date' , 'asc' ) -> get ();
2020-06-10 06:33:06 +00:00
2020-07-01 09:29:45 +00:00
//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.
2020-06-26 06:56:18 +00:00
return Response :: detect ( " events.index " , [ " events " => $events ]);
2020-06-09 07:05:07 +00:00
}
/**
* Show the form for creating a new resource .
*
2020-06-18 06:01:42 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-09 07:05:07 +00:00
*/
public function create ()
{
2020-07-01 09:29:45 +00:00
//returns "create event" blade
2020-06-26 06:56:18 +00:00
return Response :: detect ( " events.create " );
2020-06-09 07:05:07 +00:00
}
/**
* Store a newly created resource in storage .
*
* @ param \Illuminate\Http\Request $request
2020-08-31 11:20:49 +00:00
* @ return \Illuminate\Http\RedirectResponse
2020-06-09 07:05:07 +00:00
*/
public function store ( Request $request )
{
2020-06-18 06:01:42 +00:00
$requestBody = $request -> validate ([
2020-08-04 07:34:56 +00:00
" name " => " required|max:255 " ,
2020-08-06 12:47:22 +00:00
" accountable " => " required|max:255 " ,
2020-08-07 10:59:21 +00:00
" description " => " required " ,
2020-06-29 10:53:08 +00:00
" date " => " required "
2020-06-18 06:01:42 +00:00
]);
2020-07-01 09:29:45 +00:00
//creates a new Event model with the given parameter
2020-06-18 06:01:42 +00:00
$event = new Event ( $requestBody );
2020-08-04 07:34:56 +00:00
$allEvents = Event :: query () -> where ( 'name' , '=' , $request -> name ) -> get ();
2020-07-01 07:43:11 +00:00
2020-08-07 08:51:10 +00:00
if ( count ( $allEvents ) > 0 ) {
2020-08-17 08:21:02 +00:00
return redirect () -> route ( " events.index " , [ 'events' => $event ]);
2020-08-07 08:51:10 +00:00
} else {
2020-08-12 10:28:05 +00:00
if ( $request -> file ( " resource " )) {
$event -> resource_id = ResourceController :: store ( $request ) -> id ;
}
2020-08-04 07:34:56 +00:00
$event -> save ();
2020-09-22 08:45:27 +00:00
$events = Event :: query () -> get ();
2020-08-07 07:47:23 +00:00
if ( $request -> newsoption == true ){
$news = new News ();
$news -> name = " Ny aktivitet " ;
2020-08-10 07:37:19 +00:00
$news -> subname = $event -> name ;
2020-08-12 13:17:48 +00:00
$news -> arrangement_id = $event -> id ;
$news -> type_id = '3' ;
$news -> content = $event -> description ;
2020-08-12 10:28:05 +00:00
$news -> resource_id = $event -> resource_id ;
2020-08-13 05:38:14 +00:00
NewsController :: storeAndGet ( $news );
2020-08-07 07:47:23 +00:00
}
return redirect () -> route ( 'events.index' , [ 'events' => $events ]);
2020-07-01 07:43:11 +00:00
}
2020-06-09 07:05:07 +00:00
}
/**
* Display the specified resource .
*
2020-06-18 06:01:42 +00:00
* @ param Event $id
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-09 07:05:07 +00:00
*/
2020-08-10 08:16:05 +00:00
public function show ( $id )
2020-06-09 07:05:07 +00:00
{
2020-08-10 08:16:05 +00:00
$event = Event :: query () -> where ( " id " , " = " , $id ) -> first ();
return Response :: detect ( " events.show " , [ " event " => $event ]);
2020-06-09 07:05:07 +00:00
}
2020-07-28 12:54:19 +00:00
/**
* 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.
2021-05-04 12:28:14 +00:00
$events = UserEvent :: join ( 'users' , 'users.id' , '=' , 'user_events.user_id' ) -> join ( 'events' , 'events.id' , '=' , 'user_events.event_id' ) -> where ( 'event_id' , $request -> event ) -> select ( 'user_events.*' , 'users.name_first' , 'users.name_last' , 'users.phone' ) -> get ();
2020-07-28 12:54:19 +00:00
if ( count ( $events ) == 0 )
$events = Event :: where ( 'id' , $request -> event ) -> get ();
return Response :: detect ( " events.signups " , [ " events " => $events ]);
}
2020-08-07 12:11:13 +00:00
/**
* 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 ]);
}
2020-06-09 07:05:07 +00:00
/**
2020-06-09 08:45:16 +00:00
* Show the form for editing the specified resource .-
2020-06-09 07:05:07 +00:00
*
* @ param int $id
2020-06-18 06:01:42 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-09 07:05:07 +00:00
*/
2020-06-30 06:52:11 +00:00
public function edit ( $id )
2020-06-09 07:05:07 +00:00
{
2020-06-30 06:52:11 +00:00
$event = Event :: find ( $id );
return Response :: detect ( " events.edit " , [ " event " => $event ]);
2020-06-09 07:05:07 +00:00
}
/**
* Update the specified resource in storage .
*
* @ param \Illuminate\Http\Request $request
* @ param int $id
2020-06-19 06:15:24 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-09 07:05:07 +00:00
*/
2020-06-30 06:52:11 +00:00
public function update ( Request $request , $id )
2020-06-09 07:05:07 +00:00
{
2020-11-26 14:22:59 +00:00
2020-06-30 06:52:11 +00:00
$data = $request -> all ();
2020-06-19 06:15:24 +00:00
2020-06-30 06:52:11 +00:00
$event = Event :: find ( $id );
2020-07-01 07:43:11 +00:00
2020-08-04 07:34:56 +00:00
$allEvents = Event :: query () -> where ( 'name' , '=' , $request -> name ) -> where ( 'id' , '!=' , $id ) -> get ();
if ( count ( $allEvents ) > 0 ){
2020-08-17 08:21:02 +00:00
return redirect () -> route ( " events.index " , [ 'events' => $event ]);
2020-07-01 07:43:11 +00:00
} else {
2020-08-04 07:34:56 +00:00
$event -> update ( $data );
2020-11-26 14:22:59 +00:00
if ( $request -> file ( " resource " )) {
$event -> resource_id = ResourceController :: store ( $request ) -> id ;
}
2020-08-04 07:34:56 +00:00
$event -> save ();
2020-09-22 08:45:27 +00:00
$events = Event :: query () -> get ();
2020-08-07 07:47:23 +00:00
if ( $request -> newsoption == true ){
$news = new News ();
$news -> name = " Aktivitet opdateret " ;
2020-08-10 07:37:19 +00:00
$news -> subname = $event -> name ;
2020-08-12 13:17:48 +00:00
$news -> arrangement_id = $event -> id ;
$news -> type_id = '3' ;
2020-10-26 13:59:16 +00:00
$news -> resource_id = $event -> resource_id ;
2020-10-26 13:10:39 +00:00
$news -> content = " <p> " . $this -> closetags ( substr ( $event -> description , 0 , 300 ));
2020-08-13 05:38:14 +00:00
NewsController :: storeAndGet ( $news );
2020-08-07 07:47:23 +00:00
}
return redirect () -> route ( " events.index " , [ 'events' => $events ]);
2020-07-01 07:43:11 +00:00
}
2020-06-19 06:15:24 +00:00
2020-06-09 07:05:07 +00:00
}
/**
* Remove the specified resource from storage .
*
2020-06-18 06:01:42 +00:00
* @ param Event $id
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
* @ throws \Exception
2020-06-09 07:05:07 +00:00
*/
2020-07-28 12:54:19 +00:00
public function destroy ( Request $request , $id )
2020-06-09 07:05:07 +00:00
{
2020-07-28 12:54:19 +00:00
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
2021-05-04 12:28:14 +00:00
$UserEvent = UserEvent :: find ( $id );
2020-08-14 05:36:42 +00:00
2020-07-28 12:54:19 +00:00
$UserEvent -> delete ();
2020-08-14 05:36:42 +00:00
2021-05-04 12:28:14 +00:00
return 1 ;
2020-07-28 12:54:19 +00:00
} else { // Else if you are deleting an event. Then delete all the sign ups AND the event
2020-08-14 05:36:42 +00:00
$userEvents = UserEvent :: query () -> where ( 'event_id' , " = " , $id ) -> get ();
$event = Event :: query () -> find ( $id );
2020-08-13 11:50:00 +00:00
2020-08-14 05:36:42 +00:00
foreach ( $userEvents as $userEvent ) {
2020-08-13 11:50:00 +00:00
$notification = new Notification ();
$notification -> user_id = $userEvent -> user_id ;
$notification -> message = $event -> name . " - " ;
2020-08-14 05:36:42 +00:00
$notification -> save ();
$userEvent -> delete ();
2020-08-13 11:50:00 +00:00
}
2020-08-31 10:48:59 +00:00
$news = News :: query () -> join ( 'news_types' , 'news_types.id' , '=' , 'news.type_id' ) -> where ( " type " , " = " , " Activity " ) -> where ( " arrangement_id " , " = " , $id );
$news -> delete ();
2020-08-14 09:22:18 +00:00
2020-07-28 12:54:19 +00:00
$event -> delete ();
2020-08-13 11:50:00 +00:00
2020-07-28 12:54:19 +00:00
return redirect () -> route ( " events.index " );
}
2020-06-09 07:05:07 +00:00
}
2020-07-27 14:03:49 +00:00
2020-09-21 06:32:05 +00:00
//Fixes tags that have been substringed
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-14 05:36:42 +00:00
public function deleteNotifications ( Request $request ){
if ( $request -> ajax ()){
foreach ( Notification :: query () -> where ( " user_id " , " = " , $request -> user_id ) -> get () as $notification ) {
$notification -> delete ();
}
return " Done " ;
} else {
return " ERROR " ;
}
}
2020-08-13 12:41:10 +00:00
2020-09-22 06:56:03 +00:00
//Used for checking if the currently typed event name is unique. Create version
2020-08-13 12:41:10 +00:00
public function nameCheck ( Request $request ){
2020-08-14 09:22:51 +00:00
$event = Event :: query () -> where ( 'name' , 'LIKE' , $request -> nameCheck ) -> get ();
if ( count ( $event ) > 0 && $request -> nameCheck !== NULL ){
return 1 ;
}
2020-08-13 12:41:10 +00:00
}
2020-09-22 06:56:03 +00:00
//Used for checking if the currently typed event name is unique. Edit version
2020-08-17 08:21:02 +00:00
public function nameCheckUpdate ( Request $request ){
$event = Event :: query () -> where ( 'name' , 'LIKE' , $request -> nameCheck ) -> where ( 'id' , '!=' , $request -> id ) -> get ();
if ( count ( $event ) > 0 && $request -> nameCheck !== NULL ){
return 1 ;
}
}
2021-04-09 08:32:45 +00:00
public function previewPages ( Request $request ){
2021-04-27 13:15:23 +00:00
//if(!Resource::find($request->preview)){
// $event = Event::find($request->preview);
//}else{
$event = Event :: where ( 'events.id' , '=' , $request -> preview ) -> leftJoin ( 'resources' , 'resources.id' , '=' , 'events.resource_id' ) -> first ();
//}
2021-04-09 08:32:45 +00:00
$convertToJsonArray = json_encode ( $event );
return $convertToJsonArray ;
}
2021-04-09 23:43:05 +00:00
2020-06-09 07:05:07 +00:00
}
2020-07-27 14:03:49 +00:00