Attendize/app/Http/Controllers/EventController.php

314 lines
12 KiB
PHP
Raw Normal View History

2016-03-05 00:18:10 +00:00
<?php
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
namespace App\Http\Controllers;
use App\Models\Event;
use App\Models\EventImage;
use App\Models\Organiser;
use Illuminate\Http\Request;
2016-02-29 15:59:36 +00:00
use Auth;
2016-03-05 00:18:10 +00:00
use Carbon\Carbon;
2016-02-29 15:59:36 +00:00
use Image;
2016-03-05 00:18:10 +00:00
use Validator;
2016-06-16 01:36:09 +00:00
use Log;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
class EventController extends MyBaseController
{
/**
* Show the 'Create Event' Modal
*
* @param Request $request
* @return \Illuminate\View\View
*/
public function showCreateEvent(Request $request)
2016-03-05 00:18:10 +00:00
{
2016-02-29 15:59:36 +00:00
$data = [
'modal_id' => $request->get('modal_id'),
2016-03-05 00:18:10 +00:00
'organisers' => Organiser::scope()->lists('name', 'id'),
'organiser_id' => $request->get('organiser_id') ? $request->get('organiser_id') : false,
2016-02-29 15:59:36 +00:00
];
return view('ManageOrganiser.Modals.CreateEvent', $data);
2016-02-29 15:59:36 +00:00
}
/**
* Create an event
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function postCreateEvent(Request $request)
2016-03-05 00:18:10 +00:00
{
2016-02-29 15:59:36 +00:00
$event = Event::createNew();
if (!$event->validate($request->all())) {
return response()->json([
2016-06-16 01:36:09 +00:00
'status' => 'error',
'messages' => $event->errors(),
2016-03-05 00:18:10 +00:00
]);
2016-02-29 15:59:36 +00:00
}
$event->title = $request->get('title');
$event->description = strip_tags($request->get('description'));
$event->start_date = $request->get('start_date') ? Carbon::createFromFormat('d-m-Y H:i', $request->get('start_date')) : null;
2016-02-29 15:59:36 +00:00
/*
* Venue location info (Usually auto-filled from google maps)
2016-02-29 15:59:36 +00:00
*/
$is_auto_address = (trim($request->get('place_id')) !== '');
2016-02-29 15:59:36 +00:00
if ($is_auto_address) { /* Google auto filled */
$event->venue_name = $request->get('name');
$event->venue_name_full = $request->get('venue_name_full');
$event->location_lat = $request->get('lat');
$event->location_long = $request->get('lng');
$event->location_address = $request->get('formatted_address');
$event->location_country = $request->get('country');
$event->location_country_code = $request->get('country_short');
$event->location_state = $request->get('administrative_area_level_1');
$event->location_address_line_1 = $request->get('route');
$event->location_address_line_2 = $request->get('locality');
$event->location_post_code = $request->get('postal_code');
$event->location_street_number = $request->get('street_number');
$event->location_google_place_id = $request->get('place_id');
2016-02-29 15:59:36 +00:00
$event->location_is_manual = 0;
} else { /* Manually entered */
$event->venue_name = $request->get('location_venue_name');
$event->location_address_line_1 = $request->get('location_address_line_1');
$event->location_address_line_2 = $request->get('location_address_line_2');
$event->location_state = $request->get('location_state');
$event->location_post_code = $request->get('location_post_code');
2016-02-29 15:59:36 +00:00
$event->location_is_manual = 1;
}
$event->end_date = $request->get('end_date') ? Carbon::createFromFormat('d-m-Y H:i', $request->get('end_date')) : null;
2016-02-29 15:59:36 +00:00
$event->currency_id = Auth::user()->account->currency_id;
//$event->timezone_id = Auth::user()->account->timezone_id;
if ($request->get('organiser_name')) {
2016-03-05 00:18:10 +00:00
$organiser = Organiser::createNew(false, false, true);
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
$rules = [
'organiser_name' => ['required'],
'organiser_email' => ['required', 'email'],
];
$messages = [
'organiser_name.required' => 'You must give a name for the event organiser.',
];
2016-02-29 15:59:36 +00:00
$validator = Validator::make($request->all(), $rules, $messages);
2016-02-29 15:59:36 +00:00
if ($validator->fails()) {
2016-06-16 01:36:09 +00:00
return response()->json([
2016-06-15 02:31:24 +00:00
'status' => 'error',
'messages' => $validator->messages()->toArray(),
2016-03-05 00:18:10 +00:00
]);
2016-02-29 15:59:36 +00:00
}
$organiser->name = $request->get('organiser_name');
$organiser->about = $request->get('organiser_about');
$organiser->email = $request->get('organiser_email');
$organiser->facebook = $request->get('organiser_facebook');
$organiser->twitter = $request->get('organiser_twitter');
2016-02-29 15:59:36 +00:00
$organiser->save();
$event->organiser_id = $organiser->id;
} elseif ($request->get('organiser_id')) {
$event->organiser_id = $request->get('organiser_id');
2016-03-05 00:18:10 +00:00
} else { /* Somethings gone horribly wrong */
2016-06-16 01:36:09 +00:00
return response()->json([
2016-06-15 02:31:24 +00:00
'status' => 'error',
'messages' => 'There was an issue finding the organiser.',
]);
2016-03-05 00:18:10 +00:00
}
2016-02-29 15:59:36 +00:00
2016-06-16 01:36:09 +00:00
try {
$event->save();
} catch (\Exception $e) {
Log::error($e);
return response()->json([
'status' => 'error',
'messages' => 'Whoops! There was a problem creating your event. Please try again.',
]);
}
2016-02-29 15:59:36 +00:00
if ($request->hasFile('event_image')) {
2016-03-05 00:18:10 +00:00
$path = public_path().'/'.config('attendize.event_images_path');
$filename = 'event_image-'.md5(time().$event->id).'.'.strtolower($request->file('event_image')->getClientOriginalExtension());
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
$file_full_path = $path.'/'.$filename;
2016-02-29 15:59:36 +00:00
$request->file('event_image')->move($path, $filename);
2016-02-29 15:59:36 +00:00
$img = Image::make($file_full_path);
$img->resize(800, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->save($file_full_path);
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
/* Upload to s3 */
\Storage::put(config('attendize.event_images_path').'/'.$filename, file_get_contents($file_full_path));
2016-02-29 15:59:36 +00:00
$eventImage = EventImage::createNew();
2016-03-05 00:18:10 +00:00
$eventImage->image_path = config('attendize.event_images_path').'/'.$filename;
2016-02-29 15:59:36 +00:00
$eventImage->event_id = $event->id;
$eventImage->save();
}
return response()->json([
2016-06-15 02:31:24 +00:00
'status' => 'success',
'id' => $event->id,
'redirectUrl' => route('showEventTickets', [
'event_id' => $event->id,
'first_run' => 'yup',
]),
2016-03-05 00:18:10 +00:00
]);
2016-02-29 15:59:36 +00:00
}
/**
* Edit an event
*
* @param Request $request
* @param $event_id
* @return \Illuminate\Http\JsonResponse
*/
public function postEditEvent(Request $request, $event_id)
2016-03-05 00:18:10 +00:00
{
2016-02-29 15:59:36 +00:00
$event = Event::scope()->findOrFail($event_id);
if (!$event->validate($request->all())) {
return response()->json([
2016-03-05 00:18:10 +00:00
'status' => 'error',
'messages' => $event->errors(),
]);
2016-02-29 15:59:36 +00:00
}
$event->is_live = $request->get('is_live');
$event->title = $request->get('title');
$event->description = strip_tags($request->get('description'));
$event->start_date = $request->get('start_date') ? Carbon::createFromFormat('d-m-Y H:i', $request->get('start_date')) : null;
2016-02-29 15:59:36 +00:00
/*
* If the google place ID is the same as before then don't update the venue
*/
if (($request->get('place_id') !== $event->location_google_place_id) || $event->location_google_place_id == '') {
$is_auto_address = (trim($request->get('place_id')) !== '');
2016-02-29 15:59:36 +00:00
if ($is_auto_address) { /* Google auto filled */
$event->venue_name = $request->get('name');
$event->venue_name_full = $request->get('venue_name_full');
$event->location_lat = $request->get('lat');
$event->location_long = $request->get('lng');
$event->location_address = $request->get('formatted_address');
$event->location_country = $request->get('country');
$event->location_country_code = $request->get('country_short');
$event->location_state = $request->get('administrative_area_level_1');
$event->location_address_line_1 = $request->get('route');
$event->location_address_line_2 = $request->get('locality');
$event->location_post_code = $request->get('postal_code');
$event->location_street_number = $request->get('street_number');
$event->location_google_place_id = $request->get('place_id');
2016-02-29 15:59:36 +00:00
$event->location_is_manual = 0;
} else { /* Manually entered */
$event->venue_name = $request->get('location_venue_name');
$event->location_address_line_1 = $request->get('location_address_line_1');
$event->location_address_line_2 = $request->get('location_address_line_2');
$event->location_state = $request->get('location_state');
$event->location_post_code = $request->get('location_post_code');
2016-02-29 15:59:36 +00:00
$event->location_is_manual = 1;
$event->location_google_place_id = '';
$event->venue_name_full = '';
$event->location_lat = '';
$event->location_long = '';
$event->location_address = '';
$event->location_country = '';
$event->location_country_code = '';
$event->location_street_number = '';
}
}
$event->end_date = $request->get('end_date') ? Carbon::createFromFormat('d-m-Y H:i', $request->get('end_date')) : null;
2016-02-29 15:59:36 +00:00
if ($request->get('remove_current_image') == '1') {
2016-02-29 15:59:36 +00:00
EventImage::where('event_id', '=', $event->id)->delete();
}
$event->save();
if ($request->hasFile('event_image')) {
2016-03-05 00:18:10 +00:00
$path = public_path().'/'.config('attendize.event_images_path');
$filename = 'event_image-'.md5(time().$event->id).'.'.strtolower($request->file('event_image')->getClientOriginalExtension());
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
$file_full_path = $path.'/'.$filename;
2016-02-29 15:59:36 +00:00
$request->file('event_image')->move($path, $filename);
2016-02-29 15:59:36 +00:00
$img = Image::make($file_full_path);
$img->resize(800, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->save($file_full_path);
2016-03-05 00:18:10 +00:00
\Storage::put(config('attendize.event_images_path').'/'.$filename, file_get_contents($file_full_path));
2016-02-29 15:59:36 +00:00
EventImage::where('event_id', '=', $event->id)->delete();
$eventImage = EventImage::createNew();
2016-03-05 00:18:10 +00:00
$eventImage->image_path = config('attendize.event_images_path').'/'.$filename;
2016-02-29 15:59:36 +00:00
$eventImage->event_id = $event->id;
$eventImage->save();
}
return response()->json([
2016-06-15 02:31:24 +00:00
'status' => 'success',
'id' => $event->id,
'message' => 'Event Successfully Updated',
'redirectUrl' => '',
2016-03-05 00:18:10 +00:00
]);
2016-02-29 15:59:36 +00:00
}
/**
* Upload event image
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function postUploadEventImage(Request $request)
2016-03-05 00:18:10 +00:00
{
if ($request->hasFile('event_image')) {
$the_file = \File::get($request->file('event_image')->getRealPath());
$file_name = 'event_details_image-'.md5(microtime()).'.'.strtolower($request->file('event_image')->getClientOriginalExtension());
2016-03-05 00:18:10 +00:00
$relative_path_to_file = config('attendize.event_images_path').'/'.$file_name;
2016-02-29 15:59:36 +00:00
$full_path_to_file = public_path().'/'.$relative_path_to_file;
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$img = Image::make($the_file);
$img->resize(1000, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$img->save($full_path_to_file);
2016-03-05 00:18:10 +00:00
if (\Storage::put($file_name, $the_file)) {
return response()->json([
2016-03-05 00:18:10 +00:00
'link' => '/'.$relative_path_to_file,
2016-02-29 15:59:36 +00:00
]);
}
2016-03-05 00:18:10 +00:00
return response()->json([
2016-03-05 00:18:10 +00:00
'error' => 'There was a problem uploading your image.',
2016-02-29 15:59:36 +00:00
]);
}
}
}