Esempio n. 1
0
 /**
  * Edit event
  *
  * @param  int|string  $event_id
  */
 public function _event_edit($event_id = false)
 {
     $this->history = false;
     $event = new Event_Model((int) $event_id);
     // For authenticated users only
     if (!$this->user || !$event->is_author() && !$this->visitor->logged_in(array('admin', 'event moderator'))) {
         url::back('/events');
     }
     $errors = $form_errors = array();
     $form_values = $event->as_array();
     $form_values['start_date'] = '';
     $form_values['start_hour'] = '';
     $form_values['end_hour'] = '';
     /***** CHECK POST *****/
     if (request::method() == 'post') {
         $post = array_merge($this->input->post(), $_FILES);
         $extra = array('start_time' => date::unix2sql(strtotime($post['start_date'] . ' ' . date::time_24h($post['start_hour']))));
         if (!empty($post['end_hour'])) {
             $end_time = strtotime($post['start_date']);
             // end hour is earlier than start hour = event ends the next day
             if ($post['end_hour'] < $post['start_hour']) {
                 $end_time = strtotime('+1 day', $end_time);
             }
             $extra['end_time'] = date('Y-m-d', $end_time) . ' ' . date::time_24h($post['end_hour']) . ':00';
         }
         // update
         $editing = (bool) $event->id;
         if ($editing) {
             $extra['modified'] = date::unix2sql(time());
             $extra['modifies'] = (int) $event->modifies + 1;
         } else {
             $extra['author_id'] = $this->user->id;
         }
         $city = ORM::factory('city', $post['city_id']);
         if ($city) {
             $extra['country_id'] = $city->country_id;
         }
         if (csrf::valid() && $event->validate($post, true, $extra)) {
             // Update tags
             $event->remove(ORM::factory('tag'));
             if (!empty($post['tags'])) {
                 foreach ($post['tags'] as $tag_id => $tag) {
                     $event->add(ORM::factory('tag', $tag_id));
                 }
                 $event->save();
             }
             // Handle flyer uploads
             foreach (array('flyer_front_image_id' => $post->flyer_front, 'flyer_back_image_id' => $post->flyer_back) as $image_id => $flyer) {
                 if (isset($flyer) && empty($flyer['error'])) {
                     $image = Image_Model::factory('events.flyer', $flyer, $this->user->id);
                     if ($image->id) {
                         $event->add($image);
                         $event->{$image_id} = $image->id;
                         $event->save();
                     }
                 }
             }
             if (!$editing) {
                 newsfeeditem_events::event($this->user, $event);
             }
             url::redirect(url::model($event));
         } else {
             $form_errors = $post->errors();
         }
         $form_values = arr::overwrite($form_values, $post->as_array());
     }
     /***** /CHECK POST *****/
     /***** SHOW FORM *****/
     if ($event->id) {
         $this->page_actions[] = array('link' => url::model($event) . '/delete/?token=' . csrf::token(), 'text' => __('Delete event'), 'class' => 'event-delete');
         $this->page_title = text::title($event->name);
         $this->page_subtitle = __('Edit event');
         list($form_values['start_date'], $form_values['start_hour']) = explode(' ', date('Y-m-d H', strtotime($event->start_time)));
         if (!empty($event->end_time)) {
             list($temp, $form_values['end_hour']) = explode(' ', date('Y-m-d H', strtotime($event->end_time)));
         }
     } else {
         $this->page_title = __('New event');
     }
     $form = $event->get_form();
     // Tags
     if ($tag_group = Kohana::config('events.tag_group')) {
         $form['tags'] = $form_values['tags'] = array();
         $tags = ORM::factory('tag_group', $tag_group);
         foreach ($tags->tags as $tag) {
             $form['tags'][$tag->id] = $tag->name;
             if ($event->has($tag)) {
                 $form_values['tags'][$tag->id] = $tag->name;
             }
         }
     }
     // City autocomplete
     $this->_autocomplete_city('city_name');
     // Venue autocomplete
     $venues = ORM::factory('venue')->where('event_host', '=', 1)->find_all();
     $hosts = array();
     foreach ($venues as $venue) {
         $hosts[] = "{ id: '" . $venue->id . "', text: '" . html::chars($venue->name) . "' }";
     }
     widget::add('foot', html::script_source('var venues = [' . implode(', ', $hosts) . "];\n\$('input[name=venue_name]').autocomplete(venues, {\n\tformatItem: function(item) {\n\t\treturn item.text;\n\t}\n}).result(function(event, item) {\n\t\$('input[name=venue_id]').val(item.id);\n});"));
     // Date pickers
     widget::add('foot', html::script_source("\$('input#start_date').datepicker({ dateFormat: 'd.m.yy', firstDay: 1, changeFirstDay: false, showOtherMonths: true, showWeeks: true, showStatus: true, showOn: 'both' });"));
     if (empty($errors)) {
         widget::add('main', View::factory('events/event_edit', array('form' => $form, 'values' => $form_values, 'errors' => $form_errors)));
     } else {
         //			$this->_error(Kohana::lang('generic.error'), $errors);
     }
     /***** /SHOW FORM *****/
     $this->_side_views();
 }
Esempio n. 2
0
 /**
  * Edit basic info
  */
 public function _edit()
 {
     $this->tab_id = 'profile';
     $this->history = false;
     // only owner or admin
     if ($this->member->id !== $this->user->id && !$this->visitor->logged_in('admin')) {
         url::back('members');
     }
     $form_errors = array();
     $form_values = $this->member->as_array();
     // check post
     if (request::method() == 'post') {
         $post = array_merge($this->input->post(), $_FILES);
         $extra = array();
         // location
         if (empty($post['address_street']) && empty($post['address_city'])) {
             // empty address, clear location
             $extra['latitude'] = 0;
             $extra['longitude'] = 0;
         } else {
             if ($post['address_street'] != $this->member->address_street || $post['address_city'] != $this->member->address_city) {
                 // update location
                 list($extra['latitude'], $extra['longitude']) = Gmap::address_to_ll(implode(', ', array($post['address_street'], $post['address_zip'], $post['address_city'])));
             }
         }
         if ($this->member->validate($post, true, $extra)) {
             // handle picture upload
             if (isset($post->image) && empty($post->image['error'])) {
                 $image = Image_Model::factory('members.image', $post->image, $this->member->id);
                 if ($image->id) {
                     $this->member->add($image);
                     $this->member->default_image_id = $image->id;
                     $this->member->save();
                 }
             }
             url::redirect(url::user($this->member));
         } else {
             $form_errors = $post->errors();
         }
         $form_values = arr::overwrite($form_values, $post->as_array());
     }
     // City autocomplete
     $this->_autocomplete_city('address_city');
     // Date pickers
     widget::add('footer', html::script_source("\$('input#dob').datepicker({ dateFormat: 'd.m.yy', firstDay: 1, changeFirstDay: false, showOtherMonths: true, showStatus: true, showOn: 'both', minDate: '-60Y', maxDate: 0 });"));
     if (empty($errors)) {
         widget::add('main', View::factory('member/info_edit', array('values' => $form_values, 'errors' => $form_errors)));
     } else {
         $this->_error(Kohana::lang('generic.error'), $errors);
     }
     $this->_side_views();
 }
Esempio n. 3
0
 /**
  * Edit venue
  *
  * @param  integer|string  $venue_id
  * @param  integer|string  $category_id
  */
 public function _venue_edit($venue_id = false, $category_id = false)
 {
     $this->history = false;
     $venue = new Venue_Model((int) $venue_id);
     // Check access
     if (!($venue->loaded() && $venue->has_access(Venue_Model::ACCESS_EDIT)) && !(!$venue->loaded() && $this->visitor->logged_in(array('admin', 'venue moderator', 'venue')))) {
         url::back('venues');
     }
     $errors = $form_errors = array();
     $form_values = $venue->as_array();
     // check post
     if (request::method() == 'post') {
         $post = array_merge($this->input->post(), $_FILES);
         $extra = array('author_id' => $this->user->id);
         // got address, get geocode
         if (!empty($post['address']) && !empty($post['city_name'])) {
             list($extra['latitude'], $extra['longitude']) = Gmap::address_to_ll(implode(', ', array($post['address'], $post['zip'], $post['city_name'])));
         }
         if (csrf::valid() && $venue->validate($post, true, $extra)) {
             // handle logo upload
             if (isset($post->logo) && empty($post->logo['error'])) {
                 $logo = Image_Model::factory('venues.logo', $post->logo, $this->user->id);
                 if ($logo->id) {
                     $venue->add($logo);
                     $venue->default_image_id = $logo->id;
                     $venue->save();
                 }
             }
             // handle picture uploads
             foreach (array($post->picture1, $post->picture2) as $picture) {
                 if (isset($picture) && empty($picture['error'])) {
                     $image = Image_Model::factory('venues.image', $picture, $this->user->id);
                     if ($image->id) {
                         $venue->add($image);
                         $venue->save();
                     }
                 }
             }
             // update tags
             $venue->remove(ORM::factory('tag'));
             if (!empty($post->tags)) {
                 foreach ($post->tags as $tag_id => $tag) {
                     $venue->add(ORM::factory('tag', $tag_id));
                 }
             }
             url::redirect(url::model($venue));
         } else {
             $form_errors = $post->errors();
         }
         $form_values = arr::overwrite($form_values, $post->as_array());
     }
     // editing old?
     if ($venue_id) {
         if ($venue->has_access(Venue_Model::ACCESS_DELETE)) {
             $this->page_actions[] = array('link' => 'venue/' . url::title($venue->id, $venue->name) . '/delete/?token=' . csrf::token(), 'text' => __('Delete venue'), 'class' => 'venue-delete');
         }
         $this->page_subtitle = __('Edit venue');
         if (!$venue->id) {
             $errors = array('venues.error_venue_not_found');
         } else {
             $venue_category = $venue->venue_category;
         }
     } else {
         $this->page_subtitle = __('Add venue');
         if ($category_id) {
             $venue_category = new Venue_Category_Model((int) $category_id);
             if ($venue_category->id) {
                 $form_values['venue_category_id'] = $venue_category->id;
             } else {
                 $errors = array('venues.error_venue_category_not_found');
             }
         }
     }
     $this->page_actions[] = array('link' => 'venue/' . url::title($venue->id, $venue->name), 'text' => __('Cancel'), 'class' => 'cancel');
     $this->breadcrumb[] = html::anchor('/venues/' . url::title($venue_category->id, $venue_category->name), $venue_category->name);
     if ($venue->id) {
         $this->breadcrumb[] = html::anchor('/venue/' . url::title($venue->id, $venue->name), $venue->name);
     }
     // show form
     if (empty($errors)) {
         $form = array();
         // tags
         if ($venue_category->tag_group_id) {
             $form['tags'] = $form_values['tags'] = array();
             foreach ($venue_category->tag_group->tags as $tag) {
                 $form['tags'][$tag->id] = $tag->name;
                 if ($venue->has($tag)) {
                     $form_values['tags'][$tag->id] = $tag->name;
                 }
             }
         }
         $venue_categories = ORM::factory('venue_category')->find_all()->select_list('id', 'name');
         $form['venue_category_id'] = $venue_categories;
         widget::add('main', View_Mod::factory('venues/venue_edit', array('form' => $form, 'values' => $form_values, 'errors' => $form_errors)));
         // city autocomplete
         $this->_autocomplete_city();
     } else {
         $this->_error(Kohana::lang('generic.error'), $errors);
     }
     $this->_side_views();
 }