<?php
/*
    VC deprecated search module
*/
namespace vc;

if( ! defined( 'ABSPATH' ) ) exit;

new search_deprecated;
class search_deprecated {

    /*
        General constructor
    */
    public function __construct() {
        
        // exit if vc is not installed  
        // if (!helper::is_active_plugin('js_composer/js_composer.php')) return;
        if (!is_admin()) {        
            add_shortcode( 'vc_search_form', array($this, 'search_form_sc') );
        }

        // exit if vc is not installed  
        if (is_admin() && helper::is_active_plugin('js_composer/js_composer.php')) {    
            add_action( 'vc_before_init', array($this, 'search_form_vc') );
        }
    }
    
    /* visual composer */
    public function search_form_vc() {
        vc_map(array(
            "name"          => __('Suche', 'core'),
            "description"   => __('Konfigurierbares Suchfeld', 'core'),
            "base"          => "vc_search_form",
            "category"      => __("Inhalt", 'core'),
            "icon"          => get_template_directory_uri()."/assets/icon/search.svg",
            "deprecated"    => "0.8.1",
            "params"        => array_merge(array(
                array(
                    "type"        => 'sub_seperator',
                    "heading"     => __('Suche in...', 'core'),
                    "param_name"  => 'seperator_search',
                    "weight"        => 10,
                ),
                // search in
                array(
                    'type' => 'dropdown',
                    'heading' => __('Post Type', 'core'),
                    'param_name' => 'search_posttype',
                    'admin_label' => true,
                    'edit_field_class' => 'vc_col-xs-4',
                    'value' => array(),
                    "weight"        => 10,
                ),
                array(
                    'type' => 'dropdown',
                    'heading' => __('Kategorie', 'core'),
                    'param_name' => 'search_category',
                    'edit_field_class' => 'vc_col-xs-4',
                    'value' => array(
                        _x('automatisch', 'small', 'core') => '',
                        _x('alle Kategorien', 'small', 'core') => 0,
                        // _x('Auswahl anzeigen', 'small', 'core') => 'show_choice',
                    ),
                    'dependency'  => array(
                        'element'   => 'search_posttype',
                        'value' => array( '', 'show_choice', 'any' )
                    ),
                    "weight"        => 10,
                ),
                array(
                    'type'        => 'checkbox',
                    'heading'     => __('Beitragsanzahl', 'core'),
                    'param_name'  => 'search_count',
                    'edit_field_class' => 'vc_col-xs-4',
                    'value' => array(
                        __('anzeigen', 'core') => 'count',
                    ),
                    'dependency'  => array(
                        'element'   => 'search_posttype',
                        'value' => array('show_choice')
                    ),
                ),


                array(
                    "type"        => 'seperator',
                    "heading"     => __('Layout', 'core'),
                    "param_name"  => 'seperator_search_layout',
                ),
                // layout
                array(
                    'type' => 'dropdown',
                    'heading'   => __('Anordnung der Suchfeldelemente', 'core'),
                    'description' => __('Suchfeldelemente: Eingabefeld, Button und ggfs. Auswahlfelder.', 'core'),
                    'param_name' => 'search_layout',
                    'admin_label' => true,
                    'edit_field_class' => 'vc_col-xs-6',
                    'value' => array(
                        _x('alle nebeneinander', 'small', 'core') => 'horizontal',
                        _x('Eingabefeld oben, Auswahl & Button darunter', 'small', 'core') => 'horizontal trd',
                        _x('Eingabe & Auswahl nebeneinander, Button darunter', 'small', 'core') => 'horizontal sec',
                        _x('alle untereinander', 'small', 'core') => 'vertical',
                    ),
                ),
                array(
                    'type'        => 'checkbox',
                    'heading'     => __('Responsive', 'core'),
                    'description'       => __('Vertikaler Umbruch ab 767px.', 'core'),
                    'param_name'  => 'search_responsive',
                    'edit_field_class' => 'vc_col-xs-3',
                    'value' => array(
                        __('Ja', 'core') => 'responsive',
                    ),
                ),
                array(
                    'type'          => 'textfield',
                    'heading'       => __('Abstand', 'core'),
                    'description'   => __('Zwischen den Suchfeldelementen. (Einheit: in px)', 'core'),
                    'param_name'    => 'search_fields_padding',
                    'value'         => '10px',
                    'edit_field_class' => 'vc_col-xs-3',
                ),


                array(
                    "type"        => 'seperator',
                    "heading"     => __('Eingabefeld', 'core'),
                    "param_name"  => 'seperator_search_field',
                ),
                // dynamic searchfield
                array(
                    'type'          => 'dynamic',
                    'param_name'    => 'dynamic_holder',
                    'heading'       => __( 'Typ', 'core' ),
                    'text'          => __('Indem du Inhalte von Modulen in Templates dynamisch machst, kannst du diese beim späteren Einbinden in eine Seite befüllen. Dadurch kannst du das Template an verschiedenen Stellen mit unterschiedlichen Inhalten verwenden.', 'core'),
                    'value'         => 'static',
                    'params'        => array(
                        'type' => 'textfield',
                        'field' => 'holder',
                    ),
                    'edit_field_class' => 'vc_col-xs-12',
                ),
                // searchfield
                array(
                    'type'        => 'textfield',
                    'heading'     => __('Platzhalter', 'core'),
                    'description'       => __('Platzhaltertext im Eingabefeld.', 'core'),
                    'param_name'  => 'holder',
                    'edit_field_class' => 'vc_col-xs-6',
                    'admin_label' => true
                ),
                array(
                    'type'          => 'textfield',
                    'heading'       => __('Breite', 'core'),
                    'description'   => __('in px oder %', 'core'),
                    'param_name'    => 'search_field_width',
                    'value'         => '100%',
                    'edit_field_class' => 'vc_col-xs-3',
                ),
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Ausrichtung', 'core'),
                    "param_name"    => "search_field_align",
                    'edit_field_class' => 'vc_col-xs-3',
                    "value"         => array(
                        _x('linksbündig/oben', 'small', 'core') => 'start',
                        _x('zentriert', 'small', 'core') => 'center',
                        _x('rechtsbündig/unten', 'small', 'core') => "end",
                    ),
                ),
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Darstellung', 'core'),
                    'description'       => __('Wie soll das Eingabefeld aussehen?', 'core'),
                    "param_name"    => "searchfield_style",
                    'edit_field_class' => 'vc_col-xs-4 vc_clearleft',
                    "value" => array(
                        _x('primäres Feld', 'small', 'core') => '_input',
                        _x('sekundäres Feld', 'small', 'core') => "_input _sec",
                        _x('individuell überschreiben', 'small', 'core') => "custom",
                    ),
                ),
            ),
            // custom searchfield
            helper::custom_element_params_vc('searchfield_', 'searchfield_style', 'custom'),

            array(
                array(
                    "type"        => 'seperator',
                    "heading"     => __('Auswahlfelder', 'core'),
                    "param_name"  => 'seperator_search_dropdown',
                    'dependency'  => array(
                        'element' => 'search_posttype',
                        'value' => array('show_choice'),
                    ),
                ),
                // dropdown
                array(
                    'type' => 'dropdown',
                    'heading'   => __('Anordnung der Auswahl für Post Type und/oder Kategorie', 'core'),
                    'param_name' => 'search_dropdown_layout',
                    'admin_label' => true,
                    'edit_field_class' => 'vc_col-xs-6',
                    'value' => array(
                        _x('nebeneinander', 'small', 'core') => 'horizontal',
                        _x('untereinander', 'small', 'core') => 'vertical',
                    ),
                    'dependency'  => array(
                        'element' => 'search_posttype',
                        'value' => array('show_choice'),
                    ),
                ),
                array(
                    'type'          => 'textfield',
                    'heading'       => __('Breite', 'core'),
                    'description'   => __('in px oder %', 'core'),
                    'param_name'    => 'search_dropdown_width',
                    'value'         => '100%',
                    'edit_field_class' => 'vc_col-xs-3',
                    'dependency'  => array(
                        'element' => 'search_posttype',
                        'value' => array('show_choice'),
                    ),
                ),
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Ausrichtung', 'core'),
                    "param_name"    => "search_dropdown_align",
                    'edit_field_class' => 'vc_col-xs-3',
                    "value"         => array(
                        _x('linksbündig/oben', 'small', 'core') => 'start',
                        _x('zentriert', 'small', 'core') => 'center',
                        _x('rechtsbündig/unten', 'small', 'core') => "end",
                    ),
                    'dependency'  => array(
                        'element' => 'search_posttype',
                        'value' => array('show_choice'),
                    ),
                ),
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Darstellung', 'core'),
                    "description"   => __('Wie sollen die Auswahlfelder aussehen?', 'core'),
                    "param_name"    => "dropdown_style",
                    'edit_field_class' => 'vc_col-xs-4 vc_clearleft',
                    "value" => array(
                        _x('primäres Dropdown', 'small', 'core') => '_dropdown',
                        _x('sekundäres Dropdown', 'small', 'core') => "_dropdown _sec",
                        _x('individuell überschreiben', 'small', 'core') => "custom",
                    ),
                    'dependency'  => array(
                        'element' => 'search_posttype',
                        'value' => array('show_choice'),
                    ),
                ),
            ),
            // custom dropdown
            helper::custom_element_params_vc('dropdown_', 'dropdown_style', 'custom'),

            array(
                array(
                    "type"        => 'seperator',
                    "heading"     => __('Suchen Button', 'core'),
                    "param_name"  => 'seperator_search_btn',
                ),
                // dynamic submit button
                array(
                    'type'          => 'dynamic',
                    'param_name'    => 'dynamic_btn',
                    'heading'       => __( 'Typ', 'core' ),
                    'text'          => __('Indem du Inhalte von Modulen in Templates dynamisch machst, kannst du diese beim späteren Einbinden in eine Seite befüllen. Dadurch kannst du das Template an verschiedenen Stellen mit unterschiedlichen Inhalten verwenden.', 'core'),
                    'value'         => 'static',
                    'params'        => array(
                        'type' => 'textarea',
                        'field' => 'btn',
                    ),
                    'edit_field_class' => 'vc_col-xs-12',
                ),
                // submit button
                array(
                    'type'        => 'textarea',
                    'heading'     => __('Text', 'core'),
                    'description'       => __('Welcher Text soll auf dem Button stehen?', 'core'),
                    'param_name'  => 'btn',
                    'edit_field_class' => 'vc_col-xs-6',
                    'admin_label' => true
                ), 
                array(
                    'type'          => 'textfield',
                    'heading'       => __('Breite', 'core'),
                    'description'   => __('in px oder %', 'core'),
                    'param_name'    => 'search_btn_width',
                    'value'         => '100%',
                    'edit_field_class' => 'vc_col-xs-3',
                ),
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Ausrichtung', 'core'),
                    "param_name"    => "search_btn_align",
                    'edit_field_class' => 'vc_col-xs-3',
                    "value"         => array(
                        _x('linksbündig/oben', 'small', 'core') => 'start',
                        _x('zentriert', 'small', 'core') => 'center',
                        _x('rechtsbündig/unten', 'small', 'core') => "end",
                    ),
                ),
            ),

            array(
                // submit button style
                array(
                    "type"          => "dropdown",
                    "heading"       => __('Darstellung', 'core'),
                    "description"   => __('Wie soll der Suchen-Button aussehen?', 'core'),
                    "param_name"    => "btn_style",
                    'edit_field_class' => 'vc_col-xs-4 vc_clearleft',
                    "value" => array(
                        _x('primärer Link', 'small', 'core') => '_link',
                        _x('sekundärer Link', 'small', 'core') => '_link _sec',
                        _x('primärer Button', 'small', 'core') => '_button',
                        _x('sekundärer Button', 'small', 'core') => "_button _sec",
                        _x('alternativer Button', 'small', 'core') => "_button _trd",
                        _x('individuell überschreiben', 'small', 'core') => "custom",
                    ),
                ), 
            ),
            // custom button
            helper::custom_element_params_vc('btn_', 'btn_style', 'custom'),
                                           
            // Icon
            helper::custom_icon_params_vc('btn_')

            )
        ));
    }

    /* rendering */
    public function search_form_sc($atts=[], $content='') {
        //debug($atts);

        //
        // get layout and style
        $search_layout = isset($atts['search_layout']) ? explode(" ", esc_attr($atts['search_layout'])) : array("horizontal");
        $search_layout[0] = ($search_layout[0] == 'horizontal') ? 'row' : 'column';
        $search_layout[1] = (isset($search_layout[1])) ? $search_layout[1] : '';
        $search_dropdown_layout = isset($atts['search_dropdown_layout']) ? esc_attr($atts['search_dropdown_layout']) : 'horizontal';
        $search_dropdown_layout = ($search_dropdown_layout == 'horizontal') ? 'row' : 'column';
        $search_responsive = isset($atts['search_responsive']) ? true : false;
        if (!$search_responsive) {
            if ($search_layout[0] == 'row') $search_layout[0] .= '-fixed';
            if ($search_dropdown_layout == 'row') $search_dropdown_layout .= '-fixed';
        }

        $search_fields_padding = isset($atts['search_fields_padding']) ? helper::force_units(esc_attr($atts['search_fields_padding'])) : '10px';
        $half = str_replace('px', '', $search_fields_padding) / 2;
        $style_wrap = "margin-left:-".$half."px;margin-right:-".$half."px; ";
        $style_search = "margin-left:".$half."px;margin-right:".$half."px; ";
        $style_search_input = "margin-bottom:".$search_fields_padding."; ";
        $style_dropdown = "margin-left:".$half."px;margin-right:".$half."px; ";
        $style_dropdown_input = "margin-bottom:".$search_fields_padding."; ";
        $style_submit = "margin-left:".$half."px;margin-right:".$half."px; ";
        $style_submit_input = "margin-bottom:".$search_fields_padding."; ";

        $search_field_width = isset($atts['search_field_width']) ? helper::force_units(esc_attr($atts['search_field_width']), ['%','px'], '%') : '100%';
        $search_field_align = isset($atts['search_field_align']) ? esc_attr($atts['search_field_align']) : 'start';
        if ($search_field_align != "center") $search_field_align = "flex-".$search_field_align;
        $style_search .= "width:calc(".$search_field_width." - ".$search_fields_padding.");align-self:".$search_field_align."; ";
        if (strpos($search_field_width, 'px') !== false) $style_search .= "flex-shrink:0; ";
        $searchfield_style = isset($atts['searchfield_style']) ? esc_attr($atts['searchfield_style']) : '_input';
        $searchfield_style = str_replace(array("_input", "_"), "", $searchfield_style);
        if ($searchfield_style == 'custom') $style_search_input = helper::make_custom_element($atts, 'searchfield', $style_search_input);
        else {
            $search_field_color = isset($atts['search_field_color']) ? esc_attr($atts['search_field_color']) : '';
            if ($search_field_color != "") $style_search_input .= "color:".$search_field_color."; ";
            $search_field_background = isset($atts['search_field_background']) ? esc_attr($atts['search_field_background']) : '';
            if ($search_field_background != "") $style_search_input .= "background:".$search_field_background."; ";
        }

        $search_dropdown_width = isset($atts['search_dropdown_width']) ? helper::force_units(esc_attr($atts['search_dropdown_width']), ['%','px'], '%') : '100%';
        $search_dropdown_align = isset($atts['search_dropdown_align']) ? esc_attr($atts['search_dropdown_align']) : 'start';
        if ($search_dropdown_align != "center") $search_dropdown_align = "flex-".$search_dropdown_align;
        $style_dropdown .= "width:calc(".$search_dropdown_width." - ".$search_fields_padding.");align-self:".$search_dropdown_align."; ";
        if (strpos($search_dropdown_width, 'px') !== false) $style_dropdown .= "flex-shrink:0; ";
        $dropdown_style = isset($atts['dropdown_style']) ? esc_attr($atts['dropdown_style']) : '_dropdown';
        $dropdown_style = str_replace(array("_dropdown", "_"), "", $dropdown_style);
        if ($dropdown_style == 'custom') $style_dropdown_input = helper::make_custom_element($atts, 'dropdown', $style_dropdown_input);

        $search_btn_width = isset($atts['search_btn_width']) ? helper::force_units(esc_attr($atts['search_btn_width']), ['%','px'], '%') : '100%';
        $search_btn_align = isset($atts['search_btn_align']) ? esc_attr($atts['search_btn_align']) : 'start';
        if ($search_btn_align != "center") $search_btn_align = "flex-".$search_btn_align;
        $style_submit .= "width:calc(".$search_btn_width." - ".$search_fields_padding.");align-self:".$search_btn_align."; ";
        if (strpos($search_btn_width, 'px') !== false) $style_submit .= "flex-shrink:0; ";
        if ($search_btn_align == "flex-start") $style_submit .= "text-align:left; ";
        if ($search_btn_align == "center") $style_submit .= "text-align:center; ";
        if ($search_btn_align == "flex-end") $style_submit .= "text-align:right; ";

        //
        // get button
        $btn_style = isset($atts['btn_style']) ? esc_attr($atts['btn_style']) : '_button';
        $btn_style = str_replace(array("_link", "_"), "", $btn_style);
        if ($btn_style == 'custom') $style_submit_input = helper::make_custom_element($atts, 'btn', $style_submit_input);
        // make link content & icon
        $btn_text = isset($atts['btn']) ? nl2br(str_replace(array('<br />','<br>'), '', $atts['btn'])) : '';
        if ($btn_text == "" && (!isset($atts['btn_icon']) || empty($atts['btn_icon']) || $atts['btn_icon'] == "")) $atts['btn_icon'] = "icon_search";
        $btn_content = helper::make_custom_icon($atts, 'btn_', $btn_text);

        //
        // get search
        global $wp_query;
        $holder = isset($atts['holder']) ? esc_attr($atts['holder']) : '';
        $search_pt = isset($atts['search_posttype']) ? esc_attr($atts['search_posttype']) : '';
        if ($search_pt == "" || $search_pt == "show_choice" || $search_pt == "any")
            $search_cat = isset($atts['search_category']) ? esc_attr($atts['search_category']) : '';
        else $search_cat = isset($atts['search_'.$search_pt.'_category']) ? esc_attr($atts['search_'.$search_pt.'_category']) : '';
        $search_count = isset($atts['search_count']) ? true : false;
        $posttype = $wp_query->query_vars['post_type'];
        $value  = get_search_query();

        //
        // make search form elements
        //
        // make input
        $search_input = "<input class='".$searchfield_style."' style='".$style_search_input."' type='search' name='s' placeholder='".$holder."' title='".$holder."' value='".$value."'/>";
        // make posttype input
        $posttype_input = "";
        if ($search_pt == "") {
            // auto post search when placed in archive template
            if ($posttype == "") $posttype = get_post_type();
            if (is_archive()) $posttype_input = "<input type='hidden' name='post_type' value='".$posttype."' />"; 
        }
        else if ($search_pt == "show_choice") {
            // give user a choice
            $pts = get_post_types( array(
                'exclude_from_search' => false
            ), 'objects' );
            $posttype_input = "<div class='custom-select ".$dropdown_style."'><select style='".$style_dropdown_input."' name='post_type' id='post_type'>
                                    <option value='any'>".__("Alle Post Types", "core")."</option>";
            foreach ($pts as $item) {
                $counto = wp_count_posts($item->name);
                if ($counto->publish > 0) {
                    $count = ($search_count) ? " (".$counto->publish.")" : "";
                    $selected = ($item->name == $posttype) ? "selected='selected'" : "";
                    $posttype_input .=  "<option value='".$item->name."' ".$selected.">
                                            ".$item->label.$count."
                                        </option>";
                }
            }
            $posttype_input .=  "</select></div>";

        }
        else {
            // search only selected post_types
            $posttype_input = "<input type='hidden' name='post_type' value='".$search_pt."' />";
        }
        // make category input
        $category_input = "";
        if ($search_cat == "") {
            // auto category search when placed in archive-{category} template
            if (is_category() || is_search()) { 
                if ($posttype == "") $posttype = get_post_type();
                $name = 'cat';
                $val = is_object(get_queried_object()) ? get_queried_object()->term_id : 0;
                if ($posttype != 'post') {
                    $name = $posttype.'_category';
                    $term = get_term($val);
                    $val = !is_wp_error($val) && is_object($val) ? $val->slug : '';
                }
                $category_input = "<input type='hidden' name='".$name."' value='".$val."' />";
            }
        }
        else if ($search_cat == "show_choice") {
            // make category dropdown
            $category_input_dropdown = wp_dropdown_categories(array(
                'show_option_none' => 'Alle Kategorien',
                'option_none_value' => 0,
                'show_count'       => $search_count,
                'orderby'          => 'name',
                'echo'             => 0,
            ));
            if ($category_input_dropdown)
                $category_input = "<div class='custom-select ".$dropdown_style."'>".str_replace("<select", "<select style='".$style_dropdown_input."'", $category_input_dropdown)."</div>";
        }
        else {
            // search only selected categories
            $name = 'cat';
            $val = $search_cat;
            if ($search_pt != 'post') {
                $name = $search_pt.'_category';
                $term = get_term($search_cat);
                $val = (!is_wp_error($term)) ? $term->slug : '';
            }
            $category_input = "<input type='hidden' name='".$name."' value='".$val."' />";
        }
        // make submit button
        $form_id = "form".substr(md5(microtime()),0,10);
        $submit_input = "<a class='submit ".$btn_style."' style='".$style_submit_input."' onclick='document.getElementById(\"".$form_id."\").submit()'>".$btn_content."</a>";

        //
        // render
        ob_start();

        echo "<form id='".$form_id."' role='search' method='get' class='search-form' action='".home_url( '/' )."'>
                <div class='wps-form'>
                    <div class='wps-form-".$search_layout[0]."' style='".$style_wrap."'>
                        <div class='wps-form-column' style='".$style_search."'>
                            ".$search_input."
                        </div>";
        if ($search_layout[1] == 'trd') {
            echo   "</div>
                    <div class='wps-form-".$search_layout[0]."' style='".$style_wrap."justify-content:".$search_dropdown_align.";'>";
        }
        if ($search_pt == "show_choice") {
            echo        "<div class='wps-form-column' style='".$style_dropdown."'>
                            <div class='wps-form-".$search_dropdown_layout."' style='margin-left:0px;margin-right:0px;'>";
            if ($search_cat == "show_choice") {
                echo           "<div style='min-width:0px;width:100%;flex-shrink:1;margin-right:".$search_fields_padding."'>".$posttype_input."</div>
                                <div style='min-width:0px;width:100%;flex-shrink:1'>".$category_input."</div>";
            }
            else echo          "<div style='min-width:0px;width:100%;flex-shrink:1;'>".$posttype_input."</div>";
            echo            "</div>
                        </div>";
        }
        if ($search_layout[1] == 'sec') {
            echo   "</div>
                    <div class='wps-form-".$search_layout[0]."' style='".$style_wrap."justify-content:".$search_btn_align.";'>";
        }
        if ($search_pt != "show_choice") echo $posttype_input;
        if ($search_cat != "show_choice") echo $category_input;
        echo            "<div class='wps-form-column' style='".$style_submit."'>
                            ".$submit_input."
                        </div>
                    </div>
                </div>
            </form>";

        $return = ob_get_contents();
        ob_end_clean();
        return $return;
    }
}