/**
  * save function
  *
  * Saves the current event data to the database. If $this->post_id exists,
  * but $update is false, creates a new record in the ai1ec_events table of
  * this event data, but does not try to create a new post. Else if $update
  * is true, updates existing event record. If $this->post_id is empty,
  * creates a new post AND record in the ai1ec_events table for this event.
  *
  * @param  bool  $update  Whether to update an existing event or create a
  *                        new one
  * @return int            The post_id of the new or existing event.
  **/
 function save($update = false)
 {
     global $wpdb, $ai1ec_events_helper, $ai1ec_exporter_controller;
     // ===========================
     // = Insert events meta data =
     // ===========================
     // Set facebook user and eid to 0 if they are not set, otherwise they
     // will be set to '' since we use %s for big ints
     $facebook_eid = isset($this->facebook_eid) ? $this->facebook_eid : 0;
     $facebook_user = isset($this->facebook_user) ? $this->facebook_user : 0;
     $columns = array('post_id' => $this->post_id, 'start' => $this->start, 'end' => $this->end, 'allday' => $this->allday, 'instant_event' => $this->instant_event, 'recurrence_rules' => $this->recurrence_rules, 'exception_rules' => $this->exception_rules, 'recurrence_dates' => $this->recurrence_dates, 'exception_dates' => $this->exception_dates, 'venue' => $this->venue, 'country' => $this->country, 'address' => $this->address, 'city' => $this->city, 'province' => $this->province, 'postal_code' => $this->postal_code, 'show_map' => $this->show_map, 'contact_name' => $this->contact_name, 'contact_phone' => $this->contact_phone, 'contact_email' => $this->contact_email, 'contact_url' => $this->_handle_property_destruct('contact_url'), 'cost' => $this->_handle_property_destruct('cost'), 'ticket_url' => $this->_handle_property_destruct('ticket_url'), 'ical_feed_url' => $this->ical_feed_url, 'ical_source_url' => $this->ical_source_url, 'ical_uid' => $this->ical_uid, 'show_coordinates' => $this->show_coordinates, 'latitude' => $this->latitude, 'longitude' => $this->longitude, 'facebook_eid' => $facebook_eid, 'facebook_user' => $facebook_user, 'facebook_status' => $this->facebook_status);
     $format = array('%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%f', '%f', '%s', '%s', '%s');
     $table_name = $wpdb->prefix . 'ai1ec_events';
     if ($this->post_id) {
         if (!$update) {
             // =========================
             // = Insert new event data =
             // =========================
             $wpdb->query($wpdb->prepare("INSERT INTO {$table_name} ( " . join(', ', array_keys($columns)) . " ) VALUES ( " . join(', ', $format) . " )", $columns));
             $ai1ec_exporter_controller->export_location($columns, false);
         } else {
             // ==============================
             // = Update existing event data =
             // ==============================
             $where = array('post_id' => $this->post_id);
             $where_escape = array('%d');
             $wpdb->update($table_name, $columns, $where, $format, $where_escape);
             $ai1ec_exporter_controller->export_location($columns, true);
         }
     } else {
         // ===================
         // = Insert new post =
         // ===================
         $this->post_id = wp_insert_post($this->post);
         $columns['post_id'] = $this->post_id;
         wp_set_post_terms($this->post_id, $this->categories, 'events_categories');
         wp_set_post_terms($this->post_id, $this->tags, 'events_tags');
         if (isset($this->feed) && isset($this->feed->feed_id)) {
             $feed_name = $this->feed->feed_url;
             // If the feed is not from an imported file, parse the url.
             if (!isset($this->feed->feed_imported_file)) {
                 $url_components = parse_url($this->feed->feed_url);
                 $feed_name = $url_components["host"];
             }
             $term = term_exists($feed_name, 'events_feeds');
             if (!$term) {
                 // term doesn't exist, create it
                 $term = wp_insert_term($feed_name, 'events_feeds', array('description' => $this->feed->feed_url));
             }
             // term_exists returns object, wp_insert_term returns array
             $term = (object) $term;
             if (isset($term->term_id)) {
                 // associate the event with the feed only if we have term id set
                 $a = wp_set_object_terms($this->post_id, (int) $term->term_id, 'events_feeds', false);
             }
         }
         // =========================
         // = Insert new event data =
         // =========================
         $wpdb->query($wpdb->prepare("INSERT INTO {$table_name} ( " . join(', ', array_keys($columns)) . " ) VALUES ( " . join(', ', $format) . " )", $columns));
         $ai1ec_exporter_controller->export_location($columns, false);
     }
     Ai1ec_Author::get_instance()->get_all(true);
     return $this->post_id;
 }
 /**
  * Generates the HTML for a author selector.
  *
  * @param array $view_args        Arguments to the parent view
  *
  * @return string                 HTML for authors selector
  */
 public function get_html_for_authors($view_args)
 {
     global $ai1ec_events_helper, $ai1ec_view_helper;
     $authors = Ai1ec_Author::get_instance()->get_all();
     if (!is_array($authors) || count($authors) < 2) {
         return '';
     }
     foreach ($authors as &$author) {
         $href = Ai1ec_View_Factory::create_href_helper_instance($view_args, 'author');
         $href->set_term_id($author->user_id);
         $author->href = $href->generate_href();
     }
     $href_for_clearing_filter = $this->generate_href_without_arguments($view_args, array('auth_ids'));
     $args = array('authors' => $authors, 'selected_auth_ids' => $view_args['auth_ids'], 'data_type' => $view_args['data_type'], 'clear_filter' => $href_for_clearing_filter);
     return $ai1ec_view_helper->get_theme_view('authors.php', $args);
 }
 /**
  * Returns rendered calendar page.
  *
  * @return string Rendered calendar page
  */
 public function get_calendar_page(Ai1ec_Abstract_Query $request)
 {
     global $ai1ec_view_helper, $ai1ec_settings, $ai1ec_events_helper, $ai1ec_themes_controller;
     $ai1ec_calendar_helper = Ai1ec_Calendar_Helper::get_instance();
     if ($notice = $ai1ec_themes_controller->frontend_outdated_themes_notice(false)) {
         return $notice;
     }
     // Queue any styles, scripts
     $this->load_css();
     // Are we loading a shortcode?
     $shortcode = $request->get('shortcode');
     $view_args = $this->get_view_args_for_view($request);
     $action = $view_args['action'];
     $type = $request->get('request_type');
     $exact_date = $this->get_exact_date($request);
     $get_view = 'get_' . $action . '_view';
     $page_hash = $this->get_cache_key($request);
     $cache = NULL;
     if (false !== $page_hash) {
         $cache = Ai1ec_Strategies_Factory::create_blob_persistence_context($page_hash, AI1EC_CACHE_PATH);
     }
     $generated = false;
     if (false !== $page_hash) {
         try {
             $generated = $cache->get_data_from_persistence();
         } catch (Ai1ec_Cache_Not_Set_Exception $excpt) {
             // discard
             $generated = false;
         }
     }
     if (false === $generated) {
         $view = $this->{$get_view}($view_args);
         $content = '';
         $categories = '';
         $tags = '';
         $authors = '';
         $args_for_filter = array();
         if (true === $ai1ec_settings->use_select2_widgets) {
             $categories = Ai1ec_View_Factory::create_select2_multiselect(array('name' => 'ai1ec_filter_categories', 'id' => 'ai1ec_filter_categories', 'use_id' => true, 'placeholder' => __('Filter by category', AI1EC_PLUGIN_NAME), 'type' => 'category'), get_terms('events_categories'), $view_args);
             $categories = $categories->render_as_html();
             $tags = Ai1ec_View_Factory::create_select2_multiselect(array('name' => 'ai1ec_filter_tags', 'id' => 'ai1ec_filter_tags', 'use_id' => true, 'placeholder' => __('Filter by tag', AI1EC_PLUGIN_NAME), 'type' => 'tag'), get_terms('events_tags'), $view_args);
             $tags = $tags->render_as_html();
             $authors = Ai1ec_Author::get_instance()->get_all_for_select2();
             $authors = Ai1ec_View_Factory::create_select2_multiselect(array('name' => 'ai1ec_filter_authors', 'id' => 'ai1ec_filter_authors', 'use_id' => true, 'placeholder' => __('Filter by author', AI1EC_PLUGIN_NAME), 'type' => 'author'), $authors, $view_args);
             $authors = $authors->render_as_html();
         } else {
             $categories = $ai1ec_calendar_helper->get_html_for_categories($view_args);
             $authors = $ai1ec_calendar_helper->get_html_for_authors($view_args);
             $tags = $ai1ec_calendar_helper->get_html_for_tags($view_args);
         }
         $dropdown_args = $view_args;
         if (isset($dropdown_args['time_limit']) && false !== $exact_date) {
             $dropdown_args['exact_date'] = $exact_date;
         }
         $views_dropdown = $ai1ec_calendar_helper->get_html_for_views_dropdown($dropdown_args);
         $subscribe_buttons = '';
         if (!$ai1ec_settings->turn_off_subscription_buttons) {
             $subscribe_buttons = $ai1ec_calendar_helper->get_html_for_subscribe_buttons($view_args);
         }
         $save_view_btngroup = Ai1ec_View_Factory::create_save_filtered_view_buttongroup($view_args, $shortcode);
         if (($view_args['no_navigation'] || $type !== 'standard') && 'true' !== $shortcode) {
             $args_for_filter = $view_args;
             $are_filters_set = Ai1ec_Router::is_at_least_one_filter_set_in_request($view_args);
             // send data both for json and jsonp as shortcodes are jsonp
             $content = array('html' => $view, 'categories' => $categories, 'authors' => $authors, 'tags' => $tags, 'views_dropdown' => $views_dropdown, 'subscribe_buttons' => $subscribe_buttons, 'save_view_btngroup' => $save_view_btngroup->render_as_html(), 'are_filters_set' => $are_filters_set);
         } else {
             // Determine whether to display "Post your event" button on front-end.
             $contribution_buttons = $ai1ec_calendar_helper->get_html_for_contribution_buttons();
             $show_dropdowns = true;
             $show_select2 = false;
             $span_for_select2 = '';
             // if we use select2, calculate the span settings
             if (true === $ai1ec_settings->use_select2_widgets) {
                 $show_dropdowns = false;
                 $show_select2 = true;
                 $count = 0;
                 if (!empty($categories)) {
                     $count += 1;
                 }
                 if (!empty($authors)) {
                     $count += 1;
                 }
                 if (!empty($tags)) {
                     $count += 1;
                 }
                 if (0 === $count) {
                     $show_select2 = false;
                 } else {
                     $span_for_select2 = 'span' . 12 / $count;
                 }
             }
             // Define new arguments for overall calendar view
             $page_args = array('current_view' => $action, 'views_dropdown' => $views_dropdown, 'view' => $view, 'contribution_buttons' => $contribution_buttons, 'categories' => $categories, 'authors' => $authors, 'tags' => $tags, 'subscribe_buttons' => $subscribe_buttons, 'data_type' => $view_args['data_type'], 'save_view_btngroup' => $save_view_btngroup, 'disable_standard_filter_menu' => $ai1ec_settings->disable_standard_filter_menu, 'show_dropdowns' => $show_dropdowns, 'show_select2' => $show_select2, 'span_for_select2' => $span_for_select2);
             $filter_menu = Ai1ec_Render_Entity_Utility::get_instance('Filter_Menu')->set($page_args);
             $page_args['filter_menu'] = $filter_menu;
             $content = $ai1ec_view_helper->get_theme_view('calendar.php', $page_args);
             $args_for_filter = $page_args;
         }
         $generated = apply_filters('ai1ec_view', $content, $args_for_filter);
         if ($page_hash) {
             try {
                 $cache->write_data_to_persistence($generated);
             } catch (Ai1ec_Cache_Not_Set_Exception $exception) {
                 // ignore write failures
             }
         }
     }
     return $generated;
 }