Beispiel #1
0
 /**
  * Edit event
  *
  * @param  integer  $event_id
  */
 protected function _edit_event($event_id = null)
 {
     $this->history = false;
     if ($event_id) {
         // Editing old
         $event = Model_Event::factory($event_id);
         if (!$event->loaded()) {
             throw new Model_Exception($event, $event_id);
         }
         Permission::required($event, Model_Event::PERMISSION_UPDATE);
         $cancel = Request::back(Route::model($event), true);
         $this->view = View_Page::factory(HTML::chars($event->name));
         // Set actions
         if (Permission::has($event, Model_Event::PERMISSION_DELETE)) {
             $this->view->actions[] = array('link' => Route::model($event, 'delete') . '?token=' . Security::csrf(), 'text' => '<i class="fa fa-trash-o"></i> ' . __('Delete event'), 'class' => 'btn-danger event-delete');
         }
         $edit = true;
         $event->update_count++;
         $event->modified = time();
     } else {
         // Creating new
         $event = new Model_Event();
         Permission::required($event, Model_Event::PERMISSION_CREATE);
         $cancel = Request::back(Route::get('events')->uri(), true);
         $this->view = View_Page::factory(__('New event'));
         $event->author_id = Visitor::$user->id;
         $event->created = time();
         $edit = false;
     }
     // Handle post
     if ($_POST && Security::csrf_valid()) {
         $preview = isset($_POST['preview']);
         // Handle venue
         if ($venue_hidden = Arr::get($_POST, 'venue_hidden')) {
             // Hidden events require only city
         } else {
             if ($venue_id = (int) Arr::get_once($_POST, 'venue_id')) {
                 // Old venue
                 $venue = Model_Venue::factory($venue_id);
             } else {
                 if ($venue_name = Arr::get($_POST, 'venue_name')) {
                     // Check for duplicate venue
                     $venues = Model_Venue::factory()->find_by_name($venue_name);
                     if ($venues->count()) {
                         $city_name = strtolower(Arr::get($_POST, 'city_name'));
                         foreach ($venues as $venue_old) {
                             if (strtolower($venue_old->city_name) == $city_name) {
                                 $venue = $venue_old;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         $post = Arr::intersect($_POST, Model_Event::$editable_fields);
         if (isset($post['stamp_begin']['date']) && isset($post['stamp_end']['time']) && !isset($post['stamp_end']['date'])) {
             $post['stamp_end']['date'] = $post['stamp_begin']['date'];
         }
         $event->set_fields($post);
         if (Arr::get($_POST, 'free')) {
             $event->price = 0;
         }
         // Venue/location
         $event->venue_hidden = (bool) $venue_hidden;
         if ($venue_hidden) {
             // Hidden events don't have a venue
             $event->venue_id = null;
             $event->venue_name = null;
         } else {
             if (isset($venue)) {
                 // Venue loaded
                 $event->venue_id = $venue->id;
                 $event->city_name = $venue->city_name;
             } else {
                 if (!empty($venue_name)) {
                     // Create new venue
                     $venue = Model_Venue::factory();
                     $venue->name = Arr::get($_POST, 'venue_name');
                     $venue->address = Arr::get($_POST, 'address');
                     $venue->latitude = Arr::get($_POST, 'latitude');
                     $venue->longitude = Arr::get($_POST, 'longitude');
                     $venue->foursquare_id = Arr::get($_POST, 'foursquare_id');
                     $venue->event_host = true;
                     $venue->author_id = Visitor::$user->id;
                     $venue->city_name = $event->city_name;
                     if (!$preview) {
                         try {
                             $venue->save();
                             $event->venue_id = $venue->id;
                         } catch (Validation_Exception $venue_validation) {
                         }
                     }
                 }
             }
         }
         // Validate event
         try {
             $event->is_valid();
         } catch (Validation_Exception $event_validation) {
         }
         // Handle preview request
         if ($preview) {
             if ($this->ajax) {
                 $preview = '<p>' . self::_event_subtitle($event) . '</p>';
                 $preview .= '<div id="main" class="col-md-8">';
                 $preview .= $this->section_event_main($event);
                 $preview .= '<hr></div>';
                 $this->response->body($preview);
             }
             return;
         }
         // Flyer
         if ($flyer_url = Arr::get($_POST, 'flyer')) {
             $event->flyer_url = $flyer_url;
             $image = new Model_Image();
             $image->remote = $flyer_url;
             $image->created = time();
             $image->author_id = Visitor::$user->id;
             try {
                 $image->save();
                 try {
                     $flyer = new Model_Flyer();
                     $flyer->set_fields(array('image_id' => $image->id, 'name' => $event->name, 'stamp_begin' => $event->stamp_begin));
                     $flyer->save();
                 } catch (Validation_Exception $flyer_validation) {
                     $flyer_error = print_r($flyer_validation->array->errors('validation'), true);
                 }
             } catch (Validation_Exception $image_validation) {
                 $flyer_error = print_r($image_validation->array->errors('validation'), true);
             } catch (Kohana_Exception $e) {
                 $flyer_error = $e->getMessage();
             }
         }
         // If no errors found, save
         if (!isset($venue_validation) && !isset($event_validation) && !isset($flyer_error)) {
             // Make sure end time is after start time, i.e. the next day
             if ($event->stamp_end < $event->stamp_begin) {
                 $event->stamp_end += Date::DAY;
             }
             $event->save();
             // Handle flyer
             if (isset($image) && isset($flyer) && $flyer->loaded()) {
                 $flyer->event_id = $event->id;
                 $flyer->save();
                 $event->set_flyer($flyer);
                 $event->save();
             }
             // Set tags
             $event->set_tags(Arr::get($_POST, 'tag'));
             if ($edit) {
                 // Don't flood edits right after save
                 if (time() - $event->created > 60 * 30) {
                     NewsfeedItem_Events::event_edit(Visitor::$user, $event);
                 }
             } else {
                 NewsfeedItem_Events::event(Visitor::$user, $event);
                 // Add to favorites
                 $event->add_favorite(Visitor::$user);
                 // Create forum topic
                 if ($event->add_forum_topic()) {
                     Visitor::$user->post_count++;
                     Visitor::$user->save();
                 }
             }
             $this->request->redirect(Route::model($event));
         }
     }
     // Remove orphan flyer on all errors
     if (isset($flyer)) {
         $flyer->delete();
     } else {
         if (isset($image)) {
             $image->delete();
         }
     }
     // Tags
     $tags = array();
     $tag_group = new Model_Tag_Group('Music');
     if ($tag_group->loaded() && count($tag_group->tags())) {
         foreach ($tag_group->tags() as $tag) {
             $tags[$tag->id()] = $tag->name();
         }
     }
     // Form
     $section = $this->section_event_edit($event);
     $section->event_errors = isset($event_validation) ? $event_validation->array->errors('validation') : null;
     $section->flyer_error = isset($flyer_error) ? $flyer_error : null;
     $section->venue = isset($venue) ? $venue : $event->venue;
     $section->venue_errors = isset($venue_validation) ? $venue_validation->array->errors('validation') : null;
     $section->cancel = $cancel;
     $this->view->add(View_Page::COLUMN_TOP, $section);
 }