/** * Action: foursquare proxy */ public function action_foursquare() { $foursquare = Arr::get_once($_REQUEST, 'method'); $url = 'https://api.foursquare.com/v2'; $method = 'GET'; $required = $optional = array(); switch ($foursquare) { // Venue info case 'venue': $url .= '/venue.json'; $required = array('vid'); break; // Venue search // Venue search case 'venues': $url .= '/venues/search'; $required = array('ll', 'query'); $optional = array('limit', 'intent'); break; default: return; } $params = array_filter(Arr::intersect($_REQUEST, $required)); if (!empty($params)) { $params += array_filter(Arr::intersect($_REQUEST, $optional)); try { if ($method == 'GET') { // Send GET request if (!empty($params)) { $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params, '', '&'); } $options = array(); } else { // Send POST request $options = array(CURLOPT_POST => true, CURLOPT_FOLLOWLOCATION => true); if (!empty($params)) { $options[CURLOPT_POSTFIELDS] = http_build_query($params); } } /** @var Request $request */ $request = Request::factory($url); $request->get_client()->options($options); $response = $request->execute(); var_dump($response); if ($response->status() == 200) { $this->data[$foursquare] = json_decode($response->body()); } } catch (Kohana_Exception $e) { } } }
/** * Action: edit */ public function action_edit() { $this->history = false; $this->tabs = null; $group_id = (int) $this->request->param('id', null); // Load group if ($group_id) { $group = Model_Forum_Group::factory((int) $group_id); if (!$group->loaded()) { throw new Model_Exception($group, (int) $group_id); } } else { $group = Model_Forum_Group::factory(); $group->created = time(); $group->author_id = self::$user->id; } Permission::required($group, $group->loaded() ? Model_Forum_Group::PERMISSION_UPDATE : Model_Forum_Group::PERMISSION_CREATE, self::$user); // Handle post $errors = array(); if ($_POST) { $group->set_fields(Arr::intersect($_POST, array('name', 'description', 'sort'))); try { $group->save(); $this->request->redirect(Route::model($group)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validate'); } } // Build page $this->view = new View_Page(__('Forum group') . ($group->name ? ': ' . HTML::chars($group->name) : '')); $this->view->tab = 'areas'; // Set actions if ($group->loaded() && Permission::has($group, Model_Forum_Group::PERMISSION_DELETE, self::$user)) { $this->view->actions[] = array('link' => Route::model($group, 'delete'), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete group'), 'class' => 'btn btn-danger group-delete'); } $this->view->add(View_Page::COLUMN_MAIN, $this->section_edit($group, $errors)); }
/** * Action: foursquare proxy */ public function action_foursquare() { $foursquare = Arr::get_once($_REQUEST, 'method'); $url = 'https://api.foursquare.com/v2'; $required = $optional = array(); switch ($foursquare) { // Venue info case 'venue': $url .= '/venue.json'; $required = array('vid'); break; // Venue search // Venue search case 'venues': $url .= '/venues/search'; $required = array('ll', 'query'); $optional = array('limit', 'intent'); break; default: return; } $params = array_filter(Arr::intersect($_REQUEST, $required)); if (!empty($params)) { $params += array_filter(Arr::intersect($_REQUEST, $optional)); // Client keys $client_id = Kohana::$config->load('site.foursquare_client_id'); $client_secret = Kohana::$config->load('site.foursquare_client_secret'); if (!$client_id || !$client_secret) { throw new Anqh_Controller_API_Exception('Client id or secret missing'); } $params['client_id'] = $client_id; $params['client_secret'] = $client_secret; $params['v'] = '20130815'; try { if (!empty($params)) { $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params, '', '&'); } /** @var Request $request */ $request = Request::factory($url); Kohana::$log->add(Log::DEBUG, 'Foursquare API call: :url ', array(':url' => $url)); $response = $request->execute(); switch ($response->status()) { // Success case 200: $result = json_decode($response->body(), true); $this->data[$foursquare] = self::_prepare_foursquare($foursquare, $result['response']); Kohana::$log->add(Log::DEBUG, 'Foursquare API response: :response ', array(':response' => $response->body())); break; // Error in request // Error in request case 400: $this->data['error'] = json_decode($response->body(), true); throw new Controller_API_Exception('Error in request: :response', array(':response' => $response->body())); break; default: $this->data['error'] = json_decode($response->body(), true); throw new Controller_API_Exception('Unknown response: :response', array(':response' => $response->body())); } } catch (Controller_API_Exception $e) { Kohana::$log->add(Log::DEBUG, 'Foursquare API error: :error ', array(':error' => $e->getMessage())); } catch (Kohana_Exception $e) { } } }
/** * Edit gallery * * @param integer $gallery_id * @param integer $event_id */ protected function _edit_gallery($gallery_id = null, $event_id = null) { $this->history = false; if ($gallery_id) { // Editing old $gallery = Model_Gallery::factory($gallery_id); if (!$gallery->loaded()) { throw new Model_Exception($gallery, $gallery_id); } Permission::required($gallery, Model_Gallery::PERMISSION_UPDATE, self::$user); $cancel = Route::model($gallery); $save = null; $upload = false; } else { // Creating new $gallery = Model_Gallery::factory(); Permission::required($gallery, Model_Gallery::PERMISSION_CREATE, self::$user); $cancel = Request::back(Route::url('galleries'), true); $save = __('Continue'); $upload = true; if ($event_id) { /** @var Model_Event $event */ $event = Model_Event::factory($event_id); } } // Handle post $errors = array(); if ($_POST || isset($_GET['from'])) { $event_id = $_POST ? (int) Arr::get($_POST, 'event') : (int) Arr::get($_GET, 'from'); $event = Model_Event::factory($event_id); if (!$gallery->loaded() && $event->loaded()) { // Redirect to existing gallery if trying to create duplicate $old = Model_Gallery::factory()->find_by_event($event_id); if ($old->loaded()) { $this->request->redirect(Route::model($old, 'upload')); } // Fill gallery info from event when creating new $gallery->name = $event->name; $gallery->date = $event->stamp_begin; $gallery->event_id = $event->id; } else { if ($gallery->loaded()) { // Editing old $gallery->set_fields(Arr::intersect($_POST, Model_Gallery::$editable_fields)); } } try { $gallery->save(); $this->request->redirect(Route::model($gallery, $upload ? 'upload' : null)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); } } // Build page $this->view = View_Page::factory(__('Upload images')); // Gallery edit form $section = $this->section_gallery_edit(isset($event) ? $event : null); $section->errors = $errors; $this->view->add(View_Page::COLUMN_MAIN, $section); // Gallery event info if (isset($event) && $event->loaded()) { $this->view->add(View_Page::COLUMN_MAIN, $this->section_event_info($event)); } $this->view->add(View_Page::COLUMN_SIDE, $this->section_upload_help()); }
/** * Edit forum topic * * @param integer $area_id * @param integer $topic_id * * @throws Model_Exception invalid area, invalid topic * @throws InvalidArgumentException missing area and topic */ protected function _edit_topic($area_id = null, $topic_id = null) { $this->history = false; $this->view = new View_Page(); if ($area_id && !$topic_id) { // Start new topic $mode = View_Forum_PostEdit::NEW_TOPIC; /** @var Model_Forum_Private_Area|Model_Forum_Area $area */ $area = $this->private ? Model_Forum_Private_Area::factory($area_id) : Model_Forum_Area::factory($area_id); if (!$area->loaded()) { throw new Model_Exception($area, $area_id); } Permission::required($area, Model_Forum_Area::PERMISSION_POST, self::$user); $this->view->title = HTML::chars($area->name); if ($this->private) { $topic = new Model_Forum_Private_Topic(); $post = new Model_Forum_Private_Post(); $cancel = Route::url('forum_area', array('id' => 'private', 'action' => '')); $recipients = array(); } else { $topic = new Model_Forum_Topic(); $post = new Model_Forum_Post(); $cancel = Route::model($area); } } else { if ($topic_id) { // Edit old topic $mode = View_Forum_PostEdit::EDIT_TOPIC; /** @var Model_Forum_Private_Topic|Model_Forum_Topic $topic */ $topic = $this->private ? Model_Forum_Private_Topic::factory($topic_id) : Model_Forum_Topic::factory($topic_id); if (!$topic->loaded()) { throw new Model_Exception($topic, $topic_id); } Permission::required($topic, Model_Forum_Topic::PERMISSION_UPDATE, self::$user); // Build recipients list if ($this->private) { $recipients = $topic->find_recipient_names(); } $this->view->title_html = Forum::topic($topic); $cancel = Route::model($topic); // Set actions if (Permission::has($topic, Model_Forum_Topic::PERMISSION_DELETE, self::$user)) { $this->view->actions[] = array('link' => Route::model($topic, 'delete') . '?' . Security::csrf_query(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete topic'), 'class' => 'btn btn-danger topic-delete'); } } else { throw new InvalidArgumentException('Topic and area missing'); } } $errors = array(); if ($_POST && Security::csrf_valid()) { // Get recipients if ($this->private) { $post_recipients = array(); foreach (explode(',', Arr::get_once($_POST, 'recipients')) as $recipient) { if ($user = Model_User::find_user_light(trim($recipient))) { $post_recipients[$user['id']] = $user['username']; } } // Make sure author is included $post_recipients[self::$user->id] = self::$user->username; } if (isset($post)) { // New topic $post->post = $_POST['post']; $post->forum_area_id = $area->id; $post->author_id = self::$user->id; $post->author_name = self::$user->username; $post->author_ip = Request::$client_ip; $post->author_host = Request::host_name(); $post->created = time(); try { $post->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } $topic->author_id = self::$user->id; $topic->author_name = self::$user->username; $topic->name = $_POST['name']; $topic->forum_area_id = $area->id; $topic->created = time(); try { $topic->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } // If no errors found, save models if (empty($errors)) { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } // Post $post->forum_topic_id = $topic->id; $post->save(); // Topic $topic->first_post_id = $topic->last_post_id = $post->id; $topic->last_poster = self::$user->username; $topic->last_posted = time(); $topic->post_count = 1; $topic->save(); // Area, only public forums if (!$this->private) { $area->last_topic_id = $topic->id; $area->post_count++; $area->topic_count++; $area->save(); } // User self::$user->post_count++; self::$user->save(); // News feed if (!$this->private) { NewsfeedItem_Forum::topic(self::$user, $topic); } $this->request->redirect(Route::model($topic)); } isset($post_recipients) and $recipients = $post_recipients; } else { // Old topic $topic->set_fields(Arr::intersect($_POST, array('name', 'status', 'sticky'))); try { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } $this->request->redirect(Route::model($topic)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validate'); } } } $form['errors'] = $errors; $section = $this->section_post_edit($mode, isset($post) ? $post : null); $section->forum_topic = $topic; $section->errors = $errors; $section->cancel = $cancel; $section->recipients = isset($recipients) ? implode(', ', $recipients) : null; $this->view->add(View_Page::COLUMN_MAIN, $section); }
/** * Action: settings */ public function action_settings() { $this->history = false; $user = $this->_get_user(); Permission::required($user, Model_User::PERMISSION_UPDATE, self::$user); // Handle post $errors = array(); if ($_POST && Security::csrf_valid()) { $user->set_fields(Arr::intersect($_POST, Model_User::$editable_fields)); $user->modified = time(); try { $user->save(); $this->request->redirect(URL::user($user)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); } } // Build page $this->_set_page($user); $this->view->tab = 'settings'; $this->view->add(View_Page::COLUMN_MAIN, $this->section_settings($user, $errors)); }
/** * Generates the full URL for a certain page. * * @param string|boolean $filter * @return string */ public function url($filter) { if ($this->type === self::TYPE_URL) { return URL::site($this->base_url . '/' . $filter); } $query = $_GET; if ($filter === false) { // Remove filters unset($query[$this->parameter]); } elseif ($this->multiple_query) { // Add filter $filters = explode(',', Arr::get($query, $this->parameter)); $filters[] = $filter; $query[$this->parameter] = implode(',', $filters); } else { // Replace filter $query[$this->parameter] = $filter; } // Clear other parameters? if ($this->clear_query) { $query = Arr::intersect($query, array($this->parameter)); } list($uri) = $this->base_url ? array($this->base_url) : explode('?', Request::current()->current_uri()); return URL::site($uri) . URL::query($query); }
/** * Action: settings */ public function action_settings() { $this->history = false; $user = $this->_get_user(); Permission::required($user, Model_User::PERMISSION_UPDATE); // Handle post $errors = array(); if ($_POST && Security::csrf_valid()) { // Login information if ($user->username !== Arr::get($_POST, 'username') || $user->email !== UTF8::strtolower(Arr::get($_POST, 'email')) || Arr::get($_POST, 'password')) { $visitor = Visitor::instance(); $old_password = Arr::get($_POST, 'current_password'); $new_password = Arr::get($_POST, 'password'); $validation = Validation::factory(array('current_password' => $old_password)); $validation->rule('current_password', 'not_empty'); if (!$visitor->check_password($old_password) && Visitor::generate_password($user->password_kohana) !== $old_password) { $validation->rule('current_password', 'equals', array(':validation', __('current password'))); } else { if ($new_password) { // Change password only if correct old one given $user->password_kohana = $new_password; } } $user->username = Arr::get($_POST, 'username'); $user->email = Arr::get($_POST, 'email'); } $user->set_fields(Arr::intersect($_POST, Model_User::$editable_fields)); // Settings $name_visiblity = Arr::get($_POST, 'name_visibility'); if (in_array($name_visiblity, array(Model_User::NAME_HIDDEN, Model_User::NAME_VISIBLE))) { $user->setting('user.name', $name_visiblity); } $dob_visiblity = Arr::get($_POST, 'dob_visibility'); if (in_array($dob_visiblity, array(Model_User::DOB_DATEONLY, Model_User::DOB_HIDDEN, Model_User::DOB_VISIBLE))) { $user->setting('user.dob', $dob_visiblity); } // Clear default image id if Facebook image is set if (Arr::get($_POST, 'picture')) { $user->default_image_id = null; } $user->modified = time(); try { $user->save(isset($validation) ? $validation : null); $this->request->redirect(URL::user($user)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); } } // Build page $this->view = self::_set_page($user); $this->view->add(View_Page::COLUMN_TOP, $this->section_settings($user, $errors)); }
/** * 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, self::$user); $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, self::$user)) { $this->view->actions[] = array('link' => Route::model($event, 'delete') . '?token=' . Security::csrf(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete event'), 'class' => 'btn-danger event-delete'); } $edit = true; } else { // Creating new $event = new Model_Event(); Permission::required($event, Model_Event::PERMISSION_CREATE, self::$user); $cancel = Request::back(Route::get('events')->uri(), true); $this->view = View_Page::factory(__('New event')); $event->author_id = self::$user->id; $event->created = time(); $edit = false; } // Handle post if ($_POST && Security::csrf_valid()) { // 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'])) { $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->event_host = true; $venue->author_id = self::$user->id; $venue->city_name = $event->city_name; try { $venue->save(); $event->venue_id = $venue->id; } catch (Validation_Exception $venue_validation) { } } } } // Validate event try { $event->is_valid(); } catch (Validation_Exception $event_validation) { } // If no errors found, save if (!isset($venue_validation) && !isset($event_validation)) { // 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(); // Set tags $event->set_tags(Arr::get($_POST, 'tag')); $edit ? NewsfeedItem_Events::event_edit(self::$user, $event) : NewsfeedItem_Events::event(self::$user, $event); $this->request->redirect(Route::model($event)); } } // Fill the required information to view $this->view->event = $event; $this->view->event_errors = isset($event_validation) ? $event_validation->array->errors('validation') : null; $this->view->venue = isset($venue) ? $venue : null; $this->view->venue_errors = isset($venue_validation) ? $venue_validation->array->errors('validation') : null; // 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->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); }
/** * Edit track. * * @param integer $track_id * * @throws Model_Exception */ protected function _edit_track($track_id = null) { $this->history = false; if ($track_id) { // Editing old $track = new Model_Music_Track($track_id); if (!$track->loaded()) { throw new Model_Exception($track, $track_id); } Permission::required($track, Model_Music_Track::PERMISSION_UPDATE); $cancel = Route::model($track); $this->view = new View_Page(HTML::chars($track->name)); // Set actions if (Permission::has($track, Model_Music_Track::PERMISSION_DELETE)) { $this->view->actions[] = array('link' => Route::model($track, 'delete') . '?token=' . Security::csrf(), 'text' => '<i class="fa fa-trash-o"></i> ' . __('Delete'), 'class' => 'btn-danger music-delete'); } } else { // Creating new $track = new Model_Music_Track(); Permission::required($track, Model_Music_Track::PERMISSION_CREATE); $cancel = Request::back(Route::url('charts'), true); $newsfeed = true; $this->view = new View_Page($this->request->param('music') === 'mixtape' ? __('New mixtape') : __('New track')); $track->author_id = Visitor::$user->id; $track->type = $this->request->param('music') === 'mixtape' ? Model_Music_Track::TYPE_MIX : Model_Music_Track::TYPE_TRACK; $track->created = time(); } // Handle post $errors = array(); if ($_POST && Security::csrf_valid()) { try { $track->set_fields(Arr::intersect($_POST, Model_Music_Track::$editable_fields)); $track->save(); // Set tags $track->set_tags(Arr::get($_POST, 'tag')); // Newsfeed if (isset($newsfeed) && $newsfeed) { NewsfeedItem_Music::track(Visitor::$user, $track); // Create forum topic if ($track->add_forum_topic()) { Visitor::$user->post_count++; Visitor::$user->save(); } } $this->request->redirect(Route::model($track)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); } } // Form $section = $this->section_track_edit($track); $section->cancel = $cancel; $section->errors = $errors; $this->view->add(View_Page::COLUMN_TOP, $section); }
/** * Edit venue * * @param integer $venue_id */ protected function _edit_venue($venue_id = null) { $this->history = false; $edit = true; if ($venue_id) { // Editing old $venue = Model_Venue::factory($venue_id); if (!$venue->loaded()) { throw new Model_Exception($venue, $venue_id); } Permission::required($venue, Model_Venue::PERMISSION_UPDATE); $cancel = Route::model($venue); $this->view = View_Page::factory($venue->name); // Modified timestamp $venue->modified = time(); // Set actions if (Permission::has($venue, Model_Venue::PERMISSION_DELETE)) { $this->view->actions[] = array('link' => Route::model($venue, 'delete') . '?' . Security::csrf_query(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete venue'), 'class' => 'btn btn-danger venue-delete'); } } else { // Creating new $edit = false; $venue = Model_Venue::factory(); $venue->author_id = Visitor::$user->id; $cancel = Route::url('venues'); $this->view = View_Page::factory(__('New venue')); } // Handle post $errors = array(); if ($_POST && Security::csrf_valid()) { $venue->set_fields(Arr::intersect($_POST, Model_Venue::$editable_fields)); try { $venue->save(); $edit ? NewsfeedItem_Venues::venue_edit(Visitor::$user, $venue) : NewsfeedItem_Venues::venue(Visitor::$user, $venue); $this->request->redirect(Route::model($venue)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); } } $section = $this->section_venue_edit($venue); $section->errors = $errors; $section->cancel = $cancel; $this->view->add(View_Page::COLUMN_TOP, $section); }
/** * 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); }