<?php
/*
Template Name: Petitions Search Results
*/

/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;

get_header();

$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;
$decisionmakers_display = isset($campoal_options['conikal_decisionmakers_display_field']) ? $campoal_options['conikal_decisionmakers_display_field'] : 1;
$show_type_label        = isset($campoal_options['conikal_type_label_field']) ? $campoal_options['conikal_type_label_field'] : '';

$filter_category        = isset($campoal_options['conikal_f_category_field']) ? $campoal_options['conikal_f_category_field'] : 1;
$filter_topic           = isset($campoal_options['conikal_f_topic_field']) ? $campoal_options['conikal_f_topic_field'] : 1;
$filter_country         = isset($campoal_options['conikal_f_country_field']) ? $campoal_options['conikal_f_country_field'] : 1;
$filter_state           = isset($campoal_options['conikal_f_state_field']) ? $campoal_options['conikal_f_state_field'] : 1;
$filter_city            = isset($campoal_options['conikal_f_city_field']) ? $campoal_options['conikal_f_city_field'] : 1;
$filter_neighborhood    = isset($campoal_options['conikal_f_neighborhood_field']) ? $campoal_options['conikal_f_neighborhood_field'] : 1;

$search_result_page     = isset($campoal_options['conikal_page_petitions_search_results_field']) ? $campoal_options['conikal_page_petitions_search_results_field'] : 0;
$search_result_page     = ($search_result_page ? get_permalink($search_result_page) : '');

$sort                   = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'newest';
$search_key             = isset($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
$searched_posts         = conikal_search_petitions($post->ID, 'WORLD');
$total                  = $searched_posts->found_posts;

$search_cities          = array();
$search_states          = array();
$search_neighborhoods   = array();
$search_country         = array();
$search_categories      = array();
$search_topics          = array();

$order_options          = conikal_sort_petition_options('order');
$orderby_options        = conikal_sort_petition_options('orderby');

$lazy_placeholder 	        = conikal_lazy_load_placeholder('thumbnail-small');
$lazy_placeholder_avatar    = conikal_lazy_load_placeholder('avatar');
?>
<div class="wrapper petition ui vertical white color very padded segment">
    <div class="ui container search-form-container">
        <div class="ui centered grid">
            <div class="sixteen wide mobile sixteen wide computer column">
                <div class="ui secondary menu">
                    <a href="<?php echo esc_url(add_query_arg(array('s' => $search_key), home_url('/'))) ?>" class="item"><?php esc_html_e('All', 'campoal') ?></a>
                    <a href="<?php echo esc_url(add_query_arg(array('q' => $search_key), $search_result_page)) ?>" class="item active"><?php esc_html_e('Campaigns', 'campoal') ?></a>
                </div>
                <!-- start search input -->
                <div class="ui fluid category search page-search">
                    <div class="ui icon fluid large input">
                      <input class="prompt search-input" type="text" value="<?php echo esc_attr($search_key ? $search_key : '') ?>" placeholder="<?php esc_attr_e('Search...', 'campoal') ?>">
                      <i class="search link icon"></i>
                    </div>
                    <div class="results"></div>
                </div>
                <!-- end search input -->

                <div class="ui hidden divider"></div>

                <!-- start order button -->
                <div class="ui floating dropdown small basic circular button search-order">
                    <input type="hidden" name="search_order" id="search_order">
                    <span class="text"><?php esc_html_e('Order', 'campoal') ?></span>
                    <div class="menu">
                        <?php foreach ($order_options as $key => $value) { ?>
                            <div class="item" data-value="<?php echo esc_attr($key) ?>"><?php echo esc_html($value) ?></div>
                        <?php } ?>
                    </div>
                </div>

                <div class="ui floating dropdown small basic circular button search-orderby">
                    <input type="hidden" name="search_orderby" id="search_orderby">
                    <span class="text"><?php esc_html_e('Sort by', 'campoal') ?></span>
                    <div class="menu">
                        <?php foreach ($orderby_options as $key => $value) { ?>
                            <div class="item" data-value="<?php echo esc_attr($key) ?>"><?php echo esc_html($value) ?></div>
                        <?php } ?>
                    </div>
                </div>
                <!-- end order button -->

                <div class="ui right floated header">
                    <button class="ui primary small button search-filter-btn" id="open-search-filter"><i class="filter icon"></i><?php esc_html_e('Filter', 'campoal') ?></button>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="wrapper" class="wrapper">
<div class="ui container mobile">
    <div class="page-content">
    <div class="ui hidden section divider"></div>
        <div class="ui relaxed grid">
            <div class="sixteen wide mobile twelve wide computer column">

            <div class="ui header">
                <span class="results-number"><?php echo esc_html($total) ?></span> 
                <?php echo ' ' . _n('result for', 'results for', $total, 'campoal') . ' '; ?> 
                <?php esc_html_e('“', 'campoal') ?>
                <span class="search-keywords"><?php echo esc_html($search_key); ?></span>
                <?php esc_html_e('”', 'campoal') ?>
            </div>

            <?php if($searched_posts != false) : ?>
                <div class="ui divided very relaxed link items search-results" id="search-results">
                    <?php while ( $searched_posts->have_posts() ) : $searched_posts->the_post();
                        $id                 = get_the_ID();
                        $link               = get_permalink($id);
                        $title              = get_the_title($id);
                        $category           = wp_get_post_terms($id, 'petition_category', true);
                        $topics             = wp_get_post_terms($id, 'petition_topics', true);
                        $excerpt            = conikal_get_excerpt_by_id($id, 12);
                        $comments           = wp_count_comments($id);
                        $gallery            = get_post_meta($id, 'petition_gallery', true);
                        $images             = conikal_get_gallery_images($gallery);
                        $address            = get_post_meta($id, 'petition_address', true);
                        $city               = get_post_meta($id, 'petition_city', true);
                        $state              = get_post_meta($id, 'petition_state', true);
                        $neighborhood       = get_post_meta($id, 'petition_neighborhood', true);
                        $zip                = get_post_meta($id, 'petition_zip', true);
                        $country            = get_post_meta($id, 'petition_country', true);
                        $lat                = get_post_meta($id, 'petition_lat', true);
                        $lng                = get_post_meta($id, 'petition_lng', true);
                        $receiver           = conikal_get_decistion_makers($id, true, $decisionmakers_display);
                        $supproter_number   = conikal_get_supporter_donors_number( $id, 'supporter', wp_is_mobile() );
                        $goal               = $supproter_number->goal;
                        $sign               = $supproter_number->sign;
                        $updates            = get_post_meta($id, 'petition_update', true);
                        $status             = get_post_meta($id, 'petition_status', true);

                        $author_name        = conikal_get_author_name(get_the_author_meta('ID'));
                        $author_url         = get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ));
                        $avatar             = conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 64) );
                        $avatar_retina      = conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 128) );

                        $user_city          = get_user_meta( get_the_author_meta('ID'), 'user_city', true);
                        $user_state         = get_user_meta( get_the_author_meta('ID'), 'user_state', true);
                        $user_country       = get_user_meta( get_the_author_meta('ID'), 'user_country', true);

                        // get post thumbnail
                        $thumbnail 			= conikal_get_petition_thumbnail($id, 'conikal-petition-thumbnail', false);
                        $thumbnail_retina 	= conikal_get_petition_thumbnail($id, 'conikal-petition-thumbnail-retina', false);

                        if ( !in_array($city, $search_cities) && $city != '') {
                            array_push($search_cities, $city);
                        }

                        if ( !in_array($state, $search_states) && $state != '') {
                            array_push($search_states, $state);
                        }

                        if ( !in_array($neighborhood, $search_neighborhoods) && $neighborhood != '') {
                            array_push($search_neighborhoods, $neighborhood);
                        }

                        if ( !in_array($country, $search_country) && $country != '') {
                            array_push($search_country, $country);
                        }

                        if ($category) {
                            if ( !in_array($category[0]->name, $search_categories)) {
                                $search_categories[$category[0]->term_id] = $category[0]->name;
                            }
                        }

                        if ($topics) {
                            foreach ($topics as $topic) {
                                if ( !in_array($topic->name, $search_topics) && $topic) {
                                    $search_topics[$topic->term_id] = $topic->name;
                                }
                            }
                        }
                    ?>
                        <div class="item search-results-item">
                            <a href="<?php echo esc_url($link) ?>" class="image" data-bjax>
                                <?php if ($sign >= $goal || $status == '1') { ?>
                                    <span class="ui primary left corner label victory-label">
                                            <?php echo conikal_custom_icon('victory') ?>
                                    </span>
                                <?php } ?>
                                <img class="ui fluid rounded image lazy" data-src="<?php echo esc_url( $thumbnail ) ?>" data-retina="<?php echo esc_url( $thumbnail_retina ) ?>" src="<?php echo esc_url( $lazy_placeholder ) ?>" alt="<?php echo esc_attr($title) ?>">
                            </a>
                            <div class="middle aligned content">
                                <a class="header list-petition-title" href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
                                <div class="meta">
                                    <span class="receiver truncate petition-to"><?php echo conikal_custom_icon('send') ?> <?php echo sprintf(esc_html__('Petition to %s', 'campoal'), esc_html($receiver)) ?></span>
                                </div>
                                <div class="description">
                                    <p class="text grey"><?php echo esc_html($excerpt) ?></p>
                                </div>
                                <div class="extra display-computer only display-tablet only">
                                    <div class="ui right floated tiny header">
                                        <a href="<?php echo esc_url($author_url) ?>" data-bjax>
                                            <img class="ui avatar image supporter-avatar lazy" 
                                                data-src="<?php echo esc_url($avatar) ?>" 
                                                data-retina="<?php echo esc_url($avatar_retina) ?>" 
                                                src="<?php echo esc_url($lazy_placeholder_avatar) ?>" 
                                                alt="<?php echo esc_attr($author_name) ?>" 
                                                data-title="<?php echo esc_attr($author_name) ?>" 
                                                data-content="<?php if ($user_country || $user_state || $user_city) { ?>
                                                <?php echo esc_attr($user_city ? $user_city . ', ' : '') . esc_attr($user_state ? $user_state . ', ' : '') . esc_attr($user_country ? $user_country : '') ?>
                                                <?php } ?>">
                                        </a>
                                    </div>
                                    <span class="ui primary label"><?php echo conikal_custom_icon('supporter') . esc_html($supproter_number->label); ?></span>
                                    <span class="ui label">
                                        <i class="comments icon"></i>
                                        <?php echo esc_html( sprintf( _n('%s comment', '%s comments', $comments->approved, 'campoal'), conikal_format_number('%!,0i', $comments->approved, true) ) ); ?>
                                    </span>
                                    <?php if($category) { ?>
                                        <a class="ui label" href="<?php echo get_term_link($category[0]->term_id) ?>" data-bjax>
                                            <i class="tag icon"></i><?php echo esc_html($category[0]->name); ?>
                                        </a>
                                    <?php } ?>
                                </div>
                                <div class="extra display-mobile only">
                                    <div class="ui right floated tiny header">
                                        <a href="<?php echo esc_url($author_url) ?>" data-bjax>
                                            <img class="ui avatar image lazy" data-src="<?php echo esc_url($avatar) ?>" data-retina="<?php echo esc_url($avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>" alt="<?php echo esc_attr($author_name) ?>" />
                                        </a>
                                    </div>
                                    <span class="ui primary label"><?php echo conikal_custom_icon('supporter') . esc_html($supproter_number->label); ?></span>
                                    <span class="ui label"><i class="comments icon"></i><?php echo conikal_format_number('%!,0i', $comments->approved, true) ?></span>
                            </div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
                <br/>

                <!-- PAGINATION -->
                <?php conikal_pagination_buttons(_n('Campaign', 'Campaigns', $total, 'campoal'), $searched_posts->max_num_pages, $total, $posts_per_page) ?>
            <?php else :
                // display not found post notice
                conikal_not_found_post(__('No campaigns found.', 'campoal'), __("We're sorry, but no campaigns matched your criteria.", 'campoal'));
            endif; ?>
            </div>
            <?php if (!wp_is_mobile()) : ?>
            <div class="sixteen wide mobile four wide computer column">
                <div class="ui dividing medium header"><i class="filter icon"></i><?php esc_html_e('Filter', 'campoal') ?></div>
                <form class="ui form" id="form-filter">
                <?php if ($search_topics && $filter_topic == 1) { ?>
                    <div class="field">
                        <label><?php esc_html_e('Topics', 'campoal') ?></label>
                        <div class="ui fluid multiple search selection dropdown" id="search_topics">
                            <input type="hidden" name="search_topics[]">
                            <i class="dropdown icon"></i>
                            <div class="default text"><?php esc_html_e('Select Topics', 'campoal') ?></div>
                            <div class="menu">
                            <?php foreach ($search_topics as $id => $topic) { ?>
                                <div class="item" data-value="<?php echo esc_attr($id) ?>"><?php echo esc_html($topic) ?></div>
                            <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($search_categories && $filter_category == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('Category', 'campoal') ?></label>
                        <?php foreach ($search_categories as $id => $category) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_category[]" value="<?php echo esc_attr($id) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($category) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_neighborhoods && $filter_neighborhood == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('Neighborhood', 'campoal') ?></label>
                        <?php foreach ($search_neighborhoods as $neighborhood) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_neighborhood[]" value="<?php echo esc_attr($neighborhood) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($neighborhood) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_cities && $filter_city == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('City', 'campoal') ?></label>
                        <?php foreach ($search_cities as $city) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_city[]" value="<?php echo esc_attr($city) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($city) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_states && $filter_state == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('State', 'campoal') ?></label>
                        <?php foreach ($search_states as $state) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_state[]" value="<?php echo esc_attr($state) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($state) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($filter_country == 1) { ?>
                    <div class="field">
                        <label><?php esc_html_e('Country', 'campoal') ?></label>
                        <select id="search_country" name="search_country" class="ui search dropdown">
                            <option value=""><?php esc_html_e('Select Country', 'campoal') ?></option>
                            <?php foreach ($search_country as $country) { ?>
                                <option value="<?php echo esc_attr($country) ?>"><?php echo esc_html($country) ?></option>
                            <?php } ?>
                        </select>
                    </div>
                <?php } ?>

                <div class="field right aligned">
                    <a href="javascript:void(0)" class="ui tiny button" id="reset-filter"><?php esc_html_e('Reset Filter', 'campoal') ?></a>
                </div>
                <?php wp_nonce_field('load_petitions_ajax_nonce', 'securitySearch', true); ?>

                </form>
            </div>
            <?php endif; ?>
        </div>
    </div>  

</div>
</div>

<?php if (wp_is_mobile()) : ?>
    <div class="ui right sidebar vertical menu" id="search-filter-sidebar">
        <div class="ui basic segment">
        <div class="ui dividing medium header"><i class="filter icon"></i><?php esc_html_e('Filter', 'campoal') ?></div>
                <form class="ui form" id="form-filter">
                <?php if ($search_topics && $filter_topic == 1) { ?>
                    <div class="field">
                        <label><?php esc_html_e('Topics', 'campoal') ?></label>
                        <div class="ui fluid multiple search selection dropdown" id="search_topics">
                            <input type="hidden" name="search_topics[]">
                            <i class="dropdown icon"></i>
                            <div class="default text"><?php esc_html_e('Select Topics', 'campoal') ?></div>
                            <div class="menu">
                            <?php foreach ($search_topics as $id => $topic) { ?>
                                <div class="item" data-value="<?php echo esc_attr($id) ?>"><?php echo esc_html($topic) ?></div>
                            <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($search_categories && $filter_category == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('Category', 'campoal') ?></label>
                        <?php foreach ($search_categories as $id => $category) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_category[]" value="<?php echo esc_attr($id) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($category) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_neighborhoods && $filter_neighborhood == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('Neighborhood', 'campoal') ?></label>
                        <?php foreach ($search_neighborhoods as $neighborhood) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_neighborhood[]" value="<?php echo esc_attr($neighborhood) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($neighborhood) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_cities && $filter_city == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('City', 'campoal') ?></label>
                        <?php foreach ($search_cities as $city) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_city[]" value="<?php echo esc_attr($city) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($city) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($search_states && $filter_state == 1) { ?>
                    <div class="grouped fields">
                        <label><?php esc_html_e('State', 'campoal') ?></label>
                        <?php foreach ($search_states as $state) { ?>
                            <div class="field">
                                <div class="ui checkbox">
                                    <input type="checkbox" name="search_state[]" value="<?php echo esc_attr($state) ?>" tabindex="0" class="hidden">
                                    <label><?php echo esc_html($state) ?></label>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } ?>
                <?php if ($filter_country == 1) { ?>
                    <div class="field">
                        <label><?php esc_html_e('Country', 'campoal') ?></label>
                        <select id="search_country" name="search_country" class="ui search dropdown">
                            <option value=""><?php esc_html_e('Select Country', 'campoal') ?></option>
                            <?php foreach ($search_country as $country) { ?>
                                <option value="<?php echo esc_attr($country) ?>"><?php echo esc_html($country) ?></option>
                            <?php } ?>
                        </select>
                    </div>
                <?php } ?>

                <div class="field right aligned">
                    <a href="javascript:void(0)" class="ui tiny primary button search-filter-btn"><?php esc_html_e('Apply', 'campoal') ?></a>
                    <a href="javascript:void(0)" class="ui tiny button" id="reset-filter"><?php esc_html_e('Reset Filter', 'campoal') ?></a>
                </div>
                <?php wp_nonce_field('load_petitions_ajax_nonce', 'securitySearch', true); ?>

                </form>
                <div class="ui hidden divider"></div>
                <div class="ui hidden divider"></div>
                <div class="ui hidden divider"></div>
                <div class="ui hidden divider"></div>
                <div class="ui hidden divider"></div>
                <div class="ui hidden divider"></div>
        </div>
    </div>
<?php endif; ?>

<?php get_footer(); ?>