2021-04-22 13:31:42 +00:00
< ? php
namespace App\Http\Controllers ;
use App\MultipleEvents ;
2021-04-27 09:23:25 +00:00
use App\MultipleEventsParent ;
2021-04-22 13:31:42 +00:00
use Illuminate\Http\Request ;
use Illuminate\Http\Response ;
2021-04-29 08:12:59 +00:00
use Barryvdh\DomPDF\Facade as PDF ;
2021-04-22 13:31:42 +00:00
//Not in use yet
use App\Event ;
use App\News ;
use App\Notification ;
use App\Resource ;
use App\UserEvent ;
class MultipleEventsController extends Controller
{
public function __construct ()
{
$this -> middleware ([ " auth " ]);
$this -> middleware ([ " lang " ]);
$this -> middleware ([ " check.auth:event.show " ]) -> only ( " show " , " index " );
$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\Http\Response
*/
2021-05-04 09:24:56 +00:00
public function index ( Request $request )
2021-04-22 13:31:42 +00:00
{
2021-05-04 09:24:56 +00:00
$multiEvents = MultipleEventsParent :: query () -> join ( " multiple_events " , " multiple_events.multiple_events_parent_id " , " = " , " multiple_events_parent.id " ) -> where ( 'multiple_events_parent.id' , '=' , $request -> multiEvent ) -> orderBY ( 'multiple_events_parent.id' , 'desc' ) -> get ();
2021-04-27 13:23:01 +00:00
return Response :: detect ( " multiple-events.index " , [ " multiEvents " => $multiEvents ]);
2021-04-22 13:31:42 +00:00
}
2021-04-27 13:23:01 +00:00
2021-04-22 13:31:42 +00:00
/**
* Show the form for creating a new resource .
*
* @ return \Illuminate\Http\Response
*/
public function create ()
{
//returns create multiple events blade file from the view folder
return Response :: detect ( " multiple-events.create " );
}
/**
* Store a newly created resource in storage .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\Response
*/
public function store ( Request $request )
{
$requestBody = $request -> validate ([
2021-04-27 09:23:25 +00:00
" name " => " array " ,
" description " => " array " ,
2021-04-22 13:31:42 +00:00
" accountable " => " array " ,
" date " => " array " ,
]);
2021-04-27 09:23:25 +00:00
$multipleParent = new MultipleEventsParent ();
2021-04-22 13:31:42 +00:00
2021-04-27 09:23:25 +00:00
$multipleParent -> week = $request -> week ;
if ( $request -> file ( " resource " )) {
$multipleParent -> resource_id = ResourceController :: store ( $request ) -> id ;
}
$multipleParent -> save ();
for ( $i = 0 ; $i < count ( $requestBody [ " name " ]); $i ++ ) {
2021-04-22 13:31:42 +00:00
$multi_event = new MultipleEvents ();
$multi_event -> date = $requestBody [ 'date' ][ $i ];
2021-04-27 09:23:25 +00:00
$multi_event -> description = $requestBody [ 'description' ][ $i ];
2021-04-22 13:31:42 +00:00
$multi_event -> accountable = $requestBody [ 'accountable' ][ $i ];
2021-04-27 09:23:25 +00:00
$multi_event -> name = $requestBody [ 'name' ][ $i ];
$multi_event -> multiple_events_parent_id = $multipleParent -> id ;
2021-04-22 13:31:42 +00:00
$multi_event -> save ();
}
//creates a new Event model with the given parameter
2021-04-27 09:23:25 +00:00
$event = new MultipleEventsParent ( $requestBody );
2021-04-22 13:31:42 +00:00
2021-04-27 09:23:25 +00:00
$multiEvents = MultipleEventsParent :: query () -> get ();
2021-04-22 13:31:42 +00:00
if ( $request -> newsoption == true ){
$news = new News ();
2021-04-28 13:14:31 +00:00
$news -> name = " Nye ugentlig aktivitets " ;
2021-05-04 09:24:56 +00:00
$news -> subname = $request -> week ;
2021-04-27 09:23:25 +00:00
$news -> arrangement_id = $multipleParent -> id ;
$news -> type_id = '5' ;
2021-04-28 13:14:31 +00:00
$news -> content = " Den ugentlige aktivitets plan er landet " ;
2021-05-04 09:24:56 +00:00
$news -> resource_id = $request -> resource_id ;
2021-04-22 13:31:42 +00:00
NewsController :: storeAndGet ( $news );
2021-04-27 09:23:25 +00:00
2021-04-22 13:31:42 +00:00
}
2021-04-28 13:14:31 +00:00
return redirect () -> route ( 'events.index' , [ 'multiEvents' => $multiEvents ]);
2021-04-22 13:31:42 +00:00
}
/**
* Display the specified resource .
*
* @ param \App\MultipleEvents $multipleeventsController
* @ return \Illuminate\Http\Response
*/
public function show ( MultipleeventsController $multipleeventsController )
{
//
}
/**
2021-04-29 08:12:59 +00:00
* Show the form for editing the specified resource .-
2021-04-22 13:31:42 +00:00
*
2021-04-29 08:12:59 +00:00
* @ param int $id
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2021-04-22 13:31:42 +00:00
*/
2021-04-29 08:12:59 +00:00
public function edit ( $id )
2021-04-22 13:31:42 +00:00
{
2021-05-04 10:32:24 +00:00
$multiEvents = MultipleEvents :: where ( 'multiple_events_parent_id' , '=' , $id ) -> get ();
return Response :: detect ( " multiple-events.edit " , [ " multiEvents " => $multiEvents , " parentEvent " => $id ]);
2021-04-22 13:31:42 +00:00
}
/**
* Update the specified resource in storage .
*
* @ param \Illuminate\Http\Request $request
2021-05-04 10:32:24 +00:00
* @ param int $id
2021-04-22 13:31:42 +00:00
* @ return \Illuminate\Http\Response
*/
2021-05-04 10:32:24 +00:00
public function update ( Request $request , $id )
2021-04-22 13:31:42 +00:00
{
2021-05-04 10:32:24 +00:00
dd ( $request );
2021-04-22 13:31:42 +00:00
}
2021-04-28 13:14:31 +00:00
public function accountsignups ( Request $request )
{
// Find every event you have clicked on. And find all users to that event, and the event name itself.
$multiEvents = UserEvent :: join ( 'users' , 'users.id' , '=' , 'user_events.user_id' ) -> join ( 'multiple_events' , 'multiple_events.id' , '=' , 'user_events.multiple_event_id' ) -> where ( 'multiple_event_id' , $request -> multiEvent ) -> get ();
if ( count ( $multiEvents ) == 0 )
$multiEvents = Event :: where ( 'id' , $request -> event ) -> get ();
return Response :: detect ( " multiple-events.signups " , [ " multiEvent " => $multiEvents ]);
}
2021-04-22 13:31:42 +00:00
/**
* Remove the specified resource from storage .
*
* @ param \App\MultipleEvents $multipleeventsController
* @ return \Illuminate\Http\Response
*/
2021-05-04 10:32:24 +00:00
public function destroy ( Request $request , $id )
2021-04-22 13:31:42 +00:00
{
2021-04-27 10:27:46 +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 10:32:24 +00:00
$UserEvent = UserEvent :: find ( $id );
2021-04-27 10:27:46 +00:00
$UserEvent -> delete ();
2021-05-04 10:32:24 +00:00
return 1 ;
2021-04-27 10:27:46 +00:00
} else { // Else if you are deleting an event. Then delete all the sign ups AND the event
2021-05-04 10:32:24 +00:00
$userEvents = UserEvent :: where ( 'multiple_event_id' , " = " , $id ) -> get ();
$multiparent = MultipleEventsParent :: find ( $id );
$multievent = MultipleEvents :: where ( 'multiple_events_parent_id' , '=' , $id ) -> get ();
2021-04-27 10:27:46 +00:00
foreach ( $userEvents as $userEvent ) {
$userEvent -> delete ();
}
foreach ( $multievent as $me ) {
$me -> delete ();
}
2021-05-04 10:32:24 +00:00
$news = News :: query () -> join ( 'news_types' , 'news_types.id' , '=' , 'news.type_id' ) -> where ( " type " , " = " , " MultipleEvent " ) -> where ( " arrangement_id " , " = " , $id );
2021-04-27 10:27:46 +00:00
$news -> delete ();
$multiparent -> delete ();
2021-05-04 10:32:24 +00:00
return 1 ;
2021-04-27 10:27:46 +00:00
}
}
public function previewPages ( Request $request ){
/* if ( ! Resource :: find ( $request -> preview )){
$multipleEventsParent = MultipleEventsParent :: find ( $request -> preview );
$multipleEvents = MultipleEvents :: where ( 'multiple_events_parent_id' , '=' , $multipleEventsParent -> id ) -> get ();
} else { */
//$multipleEventsParent = MultipleEventsParent::find($request->preview);
2021-05-04 10:32:24 +00:00
$multipleEventsParent = MultipleEventsParent :: leftJoin ( 'resources' , 'resources.id' , '=' , 'multiple_events_parent.resource_id' ) -> where ( 'multiple_events_parent.id' , '=' , $request -> preview ) -> select ( 'multiple_events_parent.*' , 'resources.filename' ) -> first ();
2021-04-27 10:27:46 +00:00
$multipleEvents = MultipleEvents :: where ( 'multiple_events_parent_id' , '=' , $multipleEventsParent -> id ) -> get ();
//}
return [ $multipleEventsParent , $multipleEvents ];
2021-04-22 13:31:42 +00:00
}
2021-04-29 06:23:09 +00:00
public function genPDF ( $Id ){
2021-04-29 08:12:59 +00:00
$multiEvent = MultipleEventsParent :: query () -> join ( " multiple_events " , " multiple_events.multiple_events_parent_id " , " = " , " multiple_events_parent.id " ) -> where ( " multiple_events_parent.id " , " = " , $Id ) -> first ();
2021-04-29 06:23:09 +00:00
$var = 149 ;
$multiEvent -> name = substr ( $multiEvent -> name , 0 , $var );
$multiEvent -> accountable = substr ( $multiEvent -> accountable , 0 , $var );
$multiEvent -> description = substr ( $multiEvent -> description , 0 , $var );
$multiEvent -> date = substr ( $multiEvent -> date , 0 , $var );
2021-04-29 07:16:08 +00:00
$pdf = PDF :: loadView ( 'multipdf.multipleevent' , [ " multiEvent " => $multiEvent ]);
2021-04-29 06:23:09 +00:00
$pdf -> setPaper ( 'A4' , 'portrait' );
return $pdf -> stream ( " ugentligt_aktivitet_uge_ " . $multiEvent -> week . " .pdf " );
}
2021-04-29 08:43:35 +00:00
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 10:32:24 +00:00
//$multiEvents = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('multiple_events', 'multiple_events.id', '=', 'user_events.multiple_event_id')->where('multiple_event_id', '=', $request->multipleEvent)->get();
2021-04-29 08:43:35 +00:00
2021-05-04 10:32:24 +00:00
$multiEvents = MultipleEvents :: where ( 'multiple_events_parent_id' , '=' , $request -> multipleEvent ) -> get ();
//if (count($multiEvents) == 0)
// $multiEvents = MultipleEvents::where('id', '=', $request->multipleEvent)->get();
2021-04-29 08:43:35 +00:00
return Response :: detect ( " multiple-events.signups " , [ " multiEvent " => $multiEvents ]);
}
2021-04-22 13:31:42 +00:00
}
2021-04-29 06:23:09 +00:00