/**
 * Validate the event data.
 *
 * @since 1.5.1
 * @package GeoDirectory
 *
 * @param array $gd_post Post array.
 * @return array Event data array.
 */
function geodir_imex_process_event_data($gd_post)
{
    $recurring_pkg = geodir_event_recurring_pkg((object) $gd_post);
    $is_recurring = isset($gd_post['is_recurring_event']) && (int) $gd_post['is_recurring_event'] == 0 ? false : true;
    $event_date = isset($gd_post['event_date']) && $gd_post['event_date'] != '' ? geodir_imex_get_date_ymd($gd_post['event_date']) : '';
    $event_enddate = isset($gd_post['event_enddate']) && $gd_post['event_enddate'] != '' ? geodir_imex_get_date_ymd($gd_post['event_enddate']) : $event_date;
    $all_day = isset($gd_post['is_whole_day_event']) && !empty($gd_post['is_whole_day_event']) ? true : false;
    $starttime = isset($gd_post['starttime']) && !$all_day ? $gd_post['starttime'] : '';
    $endtime = isset($gd_post['endtime']) && !$all_day ? $gd_post['endtime'] : '';
    $repeat_type = '';
    $different_times = '';
    $starttimes = '';
    $endtimes = '';
    $repeat_days = '';
    $repeat_weeks = '';
    $event_recurring_dates = '';
    $repeat_x = '';
    $duration_x = '';
    $repeat_end_type = '';
    $max_repeat = '';
    $repeat_end = '';
    if ($recurring_pkg && $is_recurring) {
        $repeat_type = $gd_post['recurring_type'];
        if ($repeat_type == 'custom') {
            $starttimes = !$all_day && !empty($gd_post['event_starttimes']) ? explode(",", $gd_post['event_starttimes']) : array();
            $endtimes = !$all_day && !empty($gd_post['event_endtimes']) ? explode(",", $gd_post['event_endtimes']) : array();
            if (!empty($starttimes) || !empty($endtimes)) {
                $different_times = true;
            }
            $recurring_dates = isset($gd_post['recurring_dates']) && $gd_post['recurring_dates'] != '' ? explode(",", $gd_post['recurring_dates']) : array();
            if (!empty($recurring_dates)) {
                $event_recurring_dates = array();
                foreach ($recurring_dates as $recurring_date) {
                    $recurring_date = trim($recurring_date);
                    if ($recurring_date != '') {
                        $event_recurring_dates[] = geodir_imex_get_date_ymd($recurring_date);
                    }
                }
                $event_recurring_dates = array_unique($event_recurring_dates);
                $event_recurring_dates = implode(",", $event_recurring_dates);
            }
        } else {
            $duration_x = !empty($gd_post['event_duration_days']) ? (int) $gd_post['event_duration_days'] : 1;
            $repeat_x = !empty($gd_post['recurring_interval']) ? (int) $gd_post['recurring_interval'] : 1;
            $max_repeat = !empty($gd_post['max_recurring_count']) ? (int) $gd_post['max_recurring_count'] : 1;
            $repeat_end = !empty($gd_post['recurring_end_date']) ? geodir_imex_get_date_ymd($gd_post['recurring_end_date']) : '';
            $repeat_end_type = $repeat_end != '' ? 1 : 0;
            $max_repeat = $repeat_end != '' ? '' : $max_repeat;
            $week_days = array_flip(array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'));
            $a_repeat_days = isset($gd_post['recurring_week_days']) && trim($gd_post['recurring_week_days']) != '' ? explode(',', trim($gd_post['recurring_week_days'])) : array();
            $repeat_days = array();
            if (!empty($a_repeat_days)) {
                foreach ($a_repeat_days as $repeat_day) {
                    $repeat_day = strtolower(trim($repeat_day));
                    if ($repeat_day != '' && isset($week_days[$repeat_day])) {
                        $repeat_days[] = $week_days[$repeat_day];
                    }
                }
                $repeat_days = array_unique($repeat_days);
            }
            $a_repeat_weeks = isset($gd_post['recurring_week_nos']) && trim($gd_post['recurring_week_nos']) != '' ? explode(",", trim($gd_post['recurring_week_nos'])) : array();
            $repeat_weeks = array();
            if (!empty($a_repeat_weeks)) {
                foreach ($a_repeat_weeks as $repeat_week) {
                    $repeat_weeks[] = (int) $repeat_week;
                }
                $repeat_weeks = array_unique($repeat_weeks);
            }
        }
    }
    if (isset($gd_post['recurring_dates'])) {
        unset($gd_post['recurring_dates']);
    }
    $gd_post['is_recurring'] = $is_recurring;
    $gd_post['event_date'] = $event_date;
    $gd_post['event_start'] = $event_date;
    $gd_post['event_end'] = $event_enddate;
    $gd_post['all_day'] = $all_day;
    $gd_post['starttime'] = $starttime;
    $gd_post['endtime'] = $endtime;
    $gd_post['repeat_type'] = $repeat_type;
    $gd_post['different_times'] = $different_times;
    $gd_post['starttimes'] = $starttimes;
    $gd_post['endtimes'] = $endtimes;
    $gd_post['repeat_days'] = $repeat_days;
    $gd_post['repeat_weeks'] = $repeat_weeks;
    $gd_post['event_recurring_dates'] = $event_recurring_dates;
    $gd_post['repeat_x'] = $repeat_x;
    $gd_post['duration_x'] = $duration_x;
    $gd_post['repeat_end_type'] = $repeat_end_type;
    $gd_post['max_repeat'] = $max_repeat;
    $gd_post['repeat_end'] = $repeat_end;
    return $gd_post;
}
/**
 * Retrive markers data to use in map
 *
 * @since 1.0.0
 * @since 1.5.7 Fixed non recurring events markers.
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global array  $geodir_cat_icons Array of the category icon urls.
 * 
 * @return string
 */
function get_markers()
{
    global $wpdb, $plugin_prefix, $geodir_cat_icons;
    $search = '';
    $main_query_array;
    $srcharr = array("'", "/", "-", '"', '\\');
    $replarr = array("′", "⁄", "–", "“", '');
    $post_type = isset($_REQUEST['gd_posttype']) ? $_REQUEST['gd_posttype'] : 'gd_place';
    $map_cat_ids_array = array('0');
    $cat_find_array = array(" FIND_IN_SET(%d, pd." . $post_type . "category)");
    $field_default_cat = '';
    if (isset($_REQUEST['cat_id']) && $_REQUEST['cat_id'] != '') {
        $map_cat_arr = trim($_REQUEST['cat_id'], ',');
        if (!empty($map_cat_arr)) {
            $field_default_cat .= "WHEN (default_category IN (" . $map_cat_arr . ")) THEN default_category ";
            $map_cat_ids_array = explode(',', $map_cat_arr);
            $cat_find_array = array();
            foreach ($map_cat_ids_array as $cat_id) {
                $field_default_cat .= "WHEN (FIND_IN_SET({$cat_id}, `" . $post_type . "category`) > 0) THEN {$cat_id} ";
                $cat_find_array[] = " FIND_IN_SET(%d, pd." . $post_type . "category)";
                $main_query_array[] = $cat_id;
            }
        }
    }
    if (!empty($field_default_cat)) {
        $field_default_cat = '';
    }
    if (!empty($cat_find_array)) {
        $search .= "AND (" . implode(' OR ', $cat_find_array) . ")";
    }
    $main_query_array = $map_cat_ids_array;
    if (isset($_REQUEST['search']) && !empty($_REQUEST['search']) && $_REQUEST['search'] != __('Title', 'geodirectory')) {
        $search .= " AND p.post_title LIKE %s";
        $main_query_array[] = "%" . $_REQUEST['search'] . "%";
    }
    /**
     * Filter the marker query search SQL, values are replaces with %s or %d.
     *
     * @since 1.5.3
     *
     * @param string $search The SQL query for search/where.
     */
    $search = apply_filters('geodir_marker_search', $search);
    /**
     * Filter the marker query search SQL values %s and %d, this is an array of values.
     *
     * @since 1.5.3
     *
     * @param array $main_query_array The SQL query values for search/where.
     */
    $main_query_array = apply_filters('geodir_marker_main_query_array', $main_query_array);
    $gd_posttype = '';
    if (isset($_REQUEST['gd_posttype']) && $_REQUEST['gd_posttype'] != '') {
        $table = $plugin_prefix . $_REQUEST['gd_posttype'] . '_detail';
        $gd_posttype = " AND p.post_type = %s";
        $main_query_array[] = $_REQUEST['gd_posttype'];
    } else {
        $table = $plugin_prefix . 'gd_place_detail';
    }
    $join = ", " . $table . " AS pd ";
    /**
     * Filter the SQL JOIN clause for the markers data
     *
     * @since 1.0.0
     *
     * @param string $join Row of SQL JOIN clause to join table.
     */
    $join = apply_filters('geodir_home_map_listing_join', $join);
    /**
     * Filter the searched fields for the markers data
     *
     * @since 1.0.0
     *
     * @param string $search Row of searched fields to use in WHERE clause.
     */
    $search = apply_filters('geodir_home_map_listing_where', $search);
    $search = str_replace(array("'%", "%'"), array("'%%", "%%'"), $search);
    $cat_type = $post_type . 'category';
    if ($post_type == 'gd_event') {
        $event_select = ", pd.recurring_dates, pd.is_recurring";
    } else {
        $event_select = "";
    }
    $sql_select = 'SELECT pd.default_category, pd.' . $cat_type . ', pd.post_title, pd.post_id, pd.post_latitude, pd.post_longitude' . $event_select;
    /**
     * Filter the SQL SELECT clause to retrive fields data
     *
     * @since 1.0.0
     *
     * @param string $sql_select Row of SQL SELECT clause.
     */
    $select = apply_filters('geodir_home_map_listing_select', $sql_select);
    $groupby = " GROUP BY pd.post_id";
    /**
     * Filter the SQL GROUP BY clause to retrive map markers data.
     *
     * @since 1.5.7
     *
     * @param string $groupby Row of SQL GROUP BY clause.
     */
    $groupby = apply_filters('geodir_home_map_listing_groupby', $groupby);
    $catsql = $wpdb->prepare("{$select} {$field_default_cat} FROM " . $wpdb->posts . " as p" . $join . " WHERE p.ID = pd.post_id AND p.post_status = 'publish' " . $search . $gd_posttype . $groupby, $main_query_array);
    /**
     * Filter the SQL query to retrive markers data
     *
     * @since 1.0.0
     *
     * @param string $catsql Row of SQL query.
     * @param string $search Row of searched fields to use in WHERE clause.
     */
    $catsql = apply_filters('geodir_home_map_listing_query', $catsql, $search);
    $catinfo = $wpdb->get_results($catsql);
    $cat_content_info = array();
    $content_data = array();
    $post_ids = array();
    /**
     * Called before marker data is processed into JSON.
     *
     * Called before marker data is processed into JSON, this action can be used to change the format or add/remove markers.
     *
     * @since 1.5.3
     * @param object $catinfo The posts object containing all marker data.
     * @see 'geodir_after_marker_post_process'
     */
    $catinfo = apply_filters('geodir_before_marker_post_process', $catinfo);
    /**
     * Called before marker data is processed into JSON.
     *
     * Called before marker data is processed into JSON, this action can be used to change the format or add/remove markers.
     *
     * @since 1.4.9
     * @param object $catinfo The posts object containing all marker data.
     * @see 'geodir_after_marker_post_process'
     */
    do_action('geodir_before_marker_post_process_action', $catinfo);
    // Sort any posts into a ajax array
    if (!empty($catinfo)) {
        $geodir_cat_icons = geodir_get_term_icon();
        global $geodir_date_format;
        $today = strtotime(date_i18n('Y-m-d'));
        foreach ($catinfo as $catinfo_obj) {
            $post_title = $catinfo_obj->post_title;
            if ($post_type == 'gd_event' && !empty($catinfo_obj->recurring_dates)) {
                $event_dates = '';
                $recurring_data = isset($catinfo_obj->recurring_dates) ? maybe_unserialize($catinfo_obj->recurring_dates) : array();
                $post_info = geodir_get_post_info($catinfo_obj->post_id);
                if (!empty($catinfo_obj->is_recurring) && !empty($recurring_data) && !empty($recurring_data['is_recurring']) && geodir_event_recurring_pkg($post_info)) {
                    $recurring_dates = explode(',', $recurring_data['event_recurring_dates']);
                    if (!empty($recurring_dates)) {
                        $e = 0;
                        foreach ($recurring_dates as $date) {
                            if (strtotime($date) >= $today) {
                                $event_dates .= ' :: ' . date_i18n($geodir_date_format, strtotime($date));
                                $e++;
                                if ($e == 3) {
                                    // only show 3 event dates
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    $start_date = !empty($recurring_data['event_start']) && $recurring_data['event_start'] != '0000-00-00 00:00:00' ? $recurring_data['event_start'] : '';
                    $end_date = !empty($recurring_data['event_end']) && $recurring_data['event_end'] != '0000-00-00 00:00:00' ? $recurring_data['event_end'] : $start_date;
                    if ($end_date != '' && strtotime($end_date) >= $today) {
                        $event_dates .= ' :: ' . date_i18n($geodir_date_format, strtotime($start_date)) . ' -> ' . date_i18n($geodir_date_format, strtotime($end_date));
                    }
                }
                if (empty($event_dates)) {
                    continue;
                }
                $post_title .= $event_dates;
            }
            $icon = !empty($geodir_cat_icons) && isset($geodir_cat_icons[$catinfo_obj->default_category]) ? $geodir_cat_icons[$catinfo_obj->default_category] : '';
            $mark_extra = isset($catinfo_obj->marker_extra) ? $catinfo_obj->marker_extra : '';
            $title = str_replace($srcharr, $replarr, $post_title);
            $content_data[] = '{"id":"' . $catinfo_obj->post_id . '","t": "' . $title . '","lt": "' . $catinfo_obj->post_latitude . '","ln": "' . $catinfo_obj->post_longitude . '","mk_id":"' . $catinfo_obj->post_id . '_' . $catinfo_obj->default_category . '","i":"' . $icon . '"' . $mark_extra . '}';
            $post_ids[] = $catinfo_obj->post_id;
        }
    }
    /**
     * Called after marker data is processed into JSON.
     *
     * Called after marker data is processed into JSON, this action can be used to change the format or add/remove markers.
     *
     * @since 1.4.9
     * @param array $content_data The array containing all markers in JSON format.
     * @param object $catinfo The posts object containing all marker data.
     * @see 'geodir_before_marker_post_process'
     */
    do_action('geodir_after_marker_post_process', $content_data, $catinfo);
    if (!empty($content_data)) {
        $cat_content_info[] = implode(',', $content_data);
    }
    $totalcount = count(array_unique($post_ids));
    if (!empty($cat_content_info)) {
        return '[{"totalcount":"' . $totalcount . '",' . substr(implode(',', $cat_content_info), 1) . ']';
    } else {
        return '[{"totalcount":"0"}]';
    }
}