Attendize/app/Http/Controllers/EventController.php

276 lines
11 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;
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 Input;
use Response;
use Validator;
2016-02-29 15:59:36 +00:00
use View;
2016-03-05 00:18:10 +00:00
class EventController extends MyBaseController
{
public function showCreateEvent()
{
2016-02-29 15:59:36 +00:00
$data = [
2016-03-05 00:18:10 +00:00
'modal_id' => Input::get('modal_id'),
'organisers' => Organiser::scope()->lists('name', 'id'),
'organiser_id' => Input::get('organiser_id') ? Input::get('organiser_id') : false,
2016-02-29 15:59:36 +00:00
];
return View::make('ManageOrganiser.Modals.CreateEvent', $data);
}
2016-03-05 00:18:10 +00:00
public function postCreateEvent()
{
2016-02-29 15:59:36 +00:00
$event = Event::createNew();
if (!$event->validate(Input::all())) {
2016-03-05 00:18:10 +00:00
return Response::json([
'status' => 'error',
'messages' => $event->errors(),
]);
2016-02-29 15:59:36 +00:00
}
$event->title = Input::get('title');
$event->description = strip_tags(Input::get('description'));
2016-03-05 00:18:10 +00:00
$event->start_date = Input::get('start_date') ? Carbon::createFromFormat('d-m-Y H:i', Input::get('start_date')) : null;
2016-02-29 15:59:36 +00:00
/*
* Venue location info (Usually autofilled from google maps)
*/
$is_auto_address = (trim(Input::get('place_id')) !== '');
if ($is_auto_address) { /* Google auto filled */
$event->venue_name = Input::get('name');
$event->venue_name_full = Input::get('venue_name_full');
$event->location_lat = Input::get('lat');
$event->location_long = Input::get('lng');
$event->location_address = Input::get('formatted_address');
$event->location_country = Input::get('country');
$event->location_country_code = Input::get('country_short');
$event->location_state = Input::get('administrative_area_level_1');
$event->location_address_line_1 = Input::get('route');
$event->location_address_line_2 = Input::get('locality');
$event->location_post_code = Input::get('postal_code');
$event->location_street_number = Input::get('street_number');
$event->location_google_place_id = Input::get('place_id');
$event->location_is_manual = 0;
} else { /* Manually entered */
$event->venue_name = Input::get('location_venue_name');
$event->location_address_line_1 = Input::get('location_address_line_1');
$event->location_address_line_2 = Input::get('location_address_line_2');
$event->location_state = Input::get('location_state');
$event->location_post_code = Input::get('location_post_code');
$event->location_is_manual = 1;
}
2016-03-05 00:18:10 +00:00
$event->end_date = Input::get('end_date') ? Carbon::createFromFormat('d-m-Y H:i', Input::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 (Input::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(Input::all(), $rules, $messages);
if ($validator->fails()) {
2016-03-05 00:18:10 +00:00
return Response::json([
'status' => 'error',
'messages' => $validator->messages()->toArray(),
]);
2016-02-29 15:59:36 +00:00
}
$organiser->name = Input::get('organiser_name');
$organiser->about = Input::get('organiser_about');
$organiser->email = Input::get('organiser_email');
$organiser->facebook = Input::get('organiser_facebook');
$organiser->twitter = Input::get('organiser_twitter');
$organiser->save();
$event->organiser_id = $organiser->id;
} elseif (Input::get('organiser_id')) {
$event->organiser_id = Input::get('organiser_id');
2016-03-05 00:18:10 +00:00
} else { /* Somethings gone horribly wrong */
}
2016-02-29 15:59:36 +00:00
$event->save();
if (Input::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(Input::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
Input::file('event_image')->move($path, $filename);
$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();
}
2016-03-05 00:18:10 +00:00
return Response::json([
'status' => 'success',
'id' => $event->id,
'redirectUrl' => route('showEventTickets', [
'event_id' => $event->id,
'first_run' => 'yup',
]),
]);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
public function postEditEvent($event_id)
{
2016-02-29 15:59:36 +00:00
$event = Event::scope()->findOrFail($event_id);
if (!$event->validate(Input::all())) {
2016-03-05 00:18:10 +00:00
return Response::json([
'status' => 'error',
'messages' => $event->errors(),
]);
2016-02-29 15:59:36 +00:00
}
$event->is_live = Input::get('is_live');
$event->title = Input::get('title');
$event->description = strip_tags(Input::get('description'));
2016-03-05 00:18:10 +00:00
$event->start_date = Input::get('start_date') ? Carbon::createFromFormat('d-m-Y H:i', Input::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 ((Input::get('place_id') !== $event->location_google_place_id) || $event->location_google_place_id == '') {
$is_auto_address = (trim(Input::get('place_id')) !== '');
if ($is_auto_address) { /* Google auto filled */
$event->venue_name = Input::get('name');
$event->venue_name_full = Input::get('venue_name_full');
$event->location_lat = Input::get('lat');
$event->location_long = Input::get('lng');
$event->location_address = Input::get('formatted_address');
$event->location_country = Input::get('country');
$event->location_country_code = Input::get('country_short');
$event->location_state = Input::get('administrative_area_level_1');
$event->location_address_line_1 = Input::get('route');
$event->location_address_line_2 = Input::get('locality');
$event->location_post_code = Input::get('postal_code');
$event->location_street_number = Input::get('street_number');
$event->location_google_place_id = Input::get('place_id');
$event->location_is_manual = 0;
} else { /* Manually entered */
$event->venue_name = Input::get('location_venue_name');
$event->location_address_line_1 = Input::get('location_address_line_1');
$event->location_address_line_2 = Input::get('location_address_line_2');
$event->location_state = Input::get('location_state');
$event->location_post_code = Input::get('location_post_code');
$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 = '';
}
}
2016-03-05 00:18:10 +00:00
$event->end_date = Input::get('end_date') ? Carbon::createFromFormat('d-m-Y H:i', Input::get('end_date')) : null;
2016-02-29 15:59:36 +00:00
if (Input::get('remove_current_image') == '1') {
EventImage::where('event_id', '=', $event->id)->delete();
}
$event->save();
if (Input::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(Input::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
Input::file('event_image')->move($path, $filename);
$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();
}
2016-03-05 00:18:10 +00:00
return Response::json([
'status' => 'success',
'id' => $event->id,
'message' => 'Event Successfully Updated',
'redirectUrl' => '',
]);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
public function postUploadEventImage()
{
2016-02-29 15:59:36 +00:00
if (Input::hasFile('event_image')) {
$the_file = \File::get(Input::file('event_image')->getRealPath());
2016-03-05 00:18:10 +00:00
$file_name = 'event_details_image-'.md5(microtime()).'.'.strtolower(Input::file('event_image')->getClientOriginalExtension());
$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)) {
2016-02-29 15:59:36 +00:00
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
2016-02-29 15:59:36 +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
]);
}
}
}