<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */


/**
 * Search give forms results
 *
 * @since Campoal 1.4.0
 */
if( !function_exists('conikal_search_give_forms') ): 
    function conikal_search_give_forms($post_id = false, $author_id = false, $posts_per_page = false, $country_selected = false) {
        global $campoal_options;
        global $paged;
        global $current_user;

        $give_prefix                = '_give_';
        $sortby_default             = isset($campoal_options['conikal_sortby_default_field']) ? $campoal_options['conikal_sortby_default_field'] : 'date';
        $order_default              = isset($campoal_options['conikal_order_default_field']) ? $campoal_options['conikal_order_default_field'] : 'DESC';
        $fundraising_with_petition  = isset($campoal_options['conikal_fundraising_only_field']) ? $campoal_options['conikal_fundraising_only_field'] : 1;
        $country_selection_mode     = isset($campoal_options['conikal_country_selection_mode_field']) ? $campoal_options['conikal_country_selection_mode_field'] : 0;
        $filter_by_country          = isset($campoal_options['conikal_filter_by_country_field']) ? $campoal_options['conikal_filter_by_country_field'] : 0;
        
        // if no country select param will use user selection instead
        if (empty($country_selected)) {
            $country_selected       = conikal_get_country_selected($current_user);
        }

        if (!$posts_per_page) {
            $posts_per_page_setting = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : '';
            $posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 10;
        }
        
        if (!$post_id) {
            $post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : '';
        }
        $search_key = isset($_GET['search_key']) ? sanitize_text_field($_GET['search_key']) : '';
        $search_category = isset($_GET['search_category']) && $_GET['search_category'] != '' ? sanitize_text_field($_GET['search_category']) : '0';
        $search_topics = (isset($_GET['search_topics']) && $_GET['search_topics'] != '' ? sanitize_text_field($_GET['search_topics']) : '0');
        $search_topics = $search_topics != '0' ? explode(',', $search_topics) : '0';
        $search_address = isset($_GET['search_address']) ? sanitize_text_field($_GET['search_address']) : '';
        $search_country = isset($_GET['search_country']) && $_GET['search_country'] != '' ? sanitize_text_field($_GET['search_country']) : '';
        $search_neighborhood = isset($_GET['search_neighborhood']) && $_GET['search_neighborhood'] != '' ? sanitize_text_field($_GET['search_neighborhood']) : '';      
        $search_state = isset($_GET['search_state']) && $_GET['search_state'] != '' ? sanitize_text_field($_GET['search_state']) : '';
        $search_city = isset($_GET['search_city']) && $_GET['search_city'] != '' ? sanitize_text_field($_GET['search_city']) : '';
        $search_save = isset($_GET['search_save']) ? sanitize_text_field($_GET['search_save']) : 'off';
        $search_hide = isset($_GET['search_hide']) ? sanitize_text_field($_GET['search_hide']) : 'off';
        $search_invert = isset($_GET['search_invert']) ? sanitize_text_field($_GET['search_invert']) : 'off';

        $order      = isset($_GET['order']) && !empty($_GET['order']) ? sanitize_text_field($_GET['order']) : $order_default;
        $orderby    = isset($_GET['orderby']) && !empty($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : $sortby_default;

        if (!isset($_GET['search_key']) && !isset($_GET['search_address']) && !isset($_GET['search_neighborhood']) && !isset($_GET['search_city']) && !isset($_GET['search_state']) && !isset($_GET['search_country']) && !isset($_GET['search_category']) && !isset($_GET['search_topics'])) {
            $search_key = get_post_meta($post_id, 'search_key', true);
            $search_key = $search_key ? $search_key : '';
            $search_address = get_post_meta($post_id, 'search_address', true);
            $search_address = $search_address ? $search_address : '';
            $search_neighborhood = get_post_meta($post_id, 'search_neighborhood', true);
            $search_neighborhood = $search_neighborhood ? $search_neighborhood : '';
            $search_city = get_post_meta($post_id, 'search_city', true);
            $search_city = $search_city ? $search_city : '';
            $search_state = get_post_meta($post_id, 'search_state', true);
            $search_state = $search_state ? $search_state : '';
            $search_country = get_post_meta($post_id, 'search_country', true);
            $search_country = $search_country ? $search_country : '';
            $search_category = get_post_meta($post_id, 'search_category', true);
            $search_category = $search_category ? $search_category : '0';
            $search_topics = get_post_meta($post_id, 'search_topics', true);
            $search_topics = $search_topics ? $search_topics : '0';
            $search_invert = get_post_meta($post_id, 'search_invert', true);
            $search_invert = $search_invert ? $search_invert : 'off';
        } else {
            // save search page
            $search_fields = array(
                'search_key' => $search_key,
                'search_address' => $search_address,
                'search_neighborhood' => $search_neighborhood,
                'search_city' => $search_city,
                'search_state' => $search_state,
                'search_country' => $search_country,
                'search_category' => $search_category,
                'search_topics' => $search_topics
            );
            conikal_save_search($post_id, $search_save, $search_hide, $search_invert, $search_fields);
        }

        $search_compare = 'IN';
        if ($search_invert == 'on') {
            $search_compare = 'NOT IN';
        }

        if ($author_id) {
            $post_status = array('publish', 'pending', 'draft');
        } else {
            $post_status = 'publish';
        }

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'posts_per_page'    => $posts_per_page,
            'paged'             => $paged,
            'post_type'         => 'give_forms',
            'post_status'       => $post_status,
            's'                 => $search_key,
            'order'             => $order,
            'orderby'           => conikal_meta_key_ordering_filter($orderby),
        );

        if ($author_id) {
            $args['author'] = $author_id;
        }

        // add meta key or supporters ordering
        if ($orderby == 'views') {
            $args['meta_key'] = 'post_views_count';
        }

        if($search_category != '0' && $search_topics != '0') {
            $args['tax_query'] = array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'give_forms_category',
                    'field'    => 'id',
                    'terms'    => $search_category,
                    'operator' => $search_compare
                ),
                array(
                    'taxonomy' => 'give_forms_tag',
                    'field'    => 'name',
                    'terms'    => $search_topics,
                    'operator' => $search_compare
                ),
            );
        } elseif ($search_category != '0') {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'give_forms_category',
                    'field'    => 'id',
                    'terms'    => $search_category,
                    'operator' => $search_compare
                ),
            );
        } elseif ($search_topics != '0') {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'give_forms_tag',
                    'field'    => 'name',
                    'terms'    => $search_topics,
                    'operator' => $search_compare
                ),
            );
        }

        $args['meta_query'] = array('relation' => 'AND');

        if($search_country != '') {
            array_push($args['meta_query'], array(
                'key'     => $give_prefix . 'country',
                'value'   => $search_country,
                'compare' => $search_compare
            ));
        } elseif ($country_selection_mode && $filter_by_country && $country_selected != 'WORLD') {
            array_push($args['meta_query'], array(
                'key'     =>  $give_prefix . 'country',
                'value'   => $country_selected,
                'compare' => 'IN'
            ));
        }

        if($search_state != '') {
            array_push($args['meta_query'], array(
                'key'     => $give_prefix . 'state',
                'value'   => $search_state,
                'compare' => $search_compare
            ));
        }

        if($search_city != '') {
            array_push($args['meta_query'], array(
                'key'     => $give_prefix . 'city',
                'value'   => $search_city,
                'compare' => $search_compare
            ));
        }

        if($search_neighborhood != '') {
            array_push($args['meta_query'], array(
                'key'     => $give_prefix . 'neighborhood',
                'value'   => $search_neighborhood,
                'compare' => $search_compare
            ));
        }

        if ($fundraising_with_petition == 1) {
             array_push($args['meta_query'], array(
                'key'     => $give_prefix . 'petition_id',
                'compare' => 'NOT EXISTS'
            ));
        }
       
        $query = new WP_Query($args);

        wp_reset_query();
        wp_reset_postdata();

        return $query;
    }
endif;


?>