<?php
/**
 * Ce script contient la définition de la classe *layout_jqueryui*.
 *
 * @package framework_openmairie
 * @version SVN : $Id$
 */

/**
 * Définition de la classe *layout_jqueryui*.
 */
class layout_jqueryui extends layout_base {

    /**
     *
     */
    var $layout = "jqueryui";

    /**
     *
     */
    var $html_head_css = array(
        10 => array(
            "../lib/jquery-thirdparty/jquery-minicolors/jquery.minicolors.css",
        ),
        20 => array(
            "../lib/om-assets/css/layout_jqueryui_before.css",
            "../om-theme/jquery-ui-theme/jquery-ui.custom.css",
            "../om-theme/om.css",
        ),
        30 => array(
            "../app/css/app.css",
        ),
    );

    /**
     *
     */
    var $html_head_js = array();

    public function __construct($layout_type = null) {
        $this->html_head_js = array(
            10 => array(
                "../lib/jquery/jquery.min.js",
                "../lib/jquery-thirdparty/jquery.form.js",
                "../lib/jquery-thirdparty/jquery.collapsible.js",
                "../lib/jquery-thirdparty/jquery-minicolors/jquery.minicolors.min.js",
                "../lib/jquery-thirdparty/jquery.autosize-min.js",
            ),
            20 => array(
                "../lib/om-assets/js/layout_jqueryui_before.js",
                "../lib/jquery-ui/jquery.ui.datepicker-fr.min.js",
                "../lib/tinymce/tinymce.min.js",
                "../lib/jquery-ui/jquery-ui.min.js",
                "../lib/om-assets/js/".LOCALE.".js",
                "../lib/om-assets/js/layout_jqueryui_after.js",
            ),
            30 => array(
                "../app/js/script.js",
            )
        );
    }

    // {{{ STRUCTURE GENERALE DE LA PAGE

    /**
     * Permet d'afficher le header, c'est-à-dire le logo, les actions, les
     * raccourcis, le menu, d'ouvrir la section contenu, d'afficher le titre
     * et l'aide si le header HTML a été préalamblement affiche et que le
     * header ne l'a pas été
     */
    public function display_header() {
        // Si le header n'a pas deja ete affiche et si le header HTML a bien
        // ete affiche alors on affiche le header
        if ($this->header_displayed == false
            and $this->html_header_displayed == true) {
            //
            echo "<!-- ########## START HEADER ########## -->\n";
            //
            echo "<div id=\"header\"";
            echo " class=\"ui-widget";
            echo " ".$this->get_parameter("style_header");
            echo "\"";
            echo ">\n";
            // Logo
            $this->display_logo();
            // Actions personnelles
            $this->display_actions();
            // Raccourcis
            $this->display_shortlinks();
            // Fin du header
            echo "</div>\n";
            echo "<!-- ########## END HEADER ########## -->\n";
            // Menu
            $this->display_menu();
            // Content
            $this->display_content_start();
            // Titre
            $this->display_page_title();
            // Marqueur : le header est affiche
            $this->header_displayed = true;
        }
    }

    /**
     * Cette méthode permet d'afficher le début de la section contenu
     */
    public function display_content_start() {
        //
        echo "<!-- ########## START CONTENT ########## -->\n";
        echo "<div id=\"content\"";
        echo " class=\"ui-widget ui-widget-content ui-corner-all";
        // XXX
        if ($this->get_parameter("flag") == "htmlonly"
            || $this->get_parameter("flag") == "htmlonly_nodoctype"
            || $this->get_parameter("flag") == "nohtml"
            || $this->get_parameter("flag") == "login_and_nohtml"
            || count($this->get_parameter("menu")) == 0) {
             echo " nomenu";
        }
        echo "\"";
        echo ">\n\n";
    }

    // }}} STRUCTURE GENERALE DE LA PAGE - END

    public function display_start_fieldset($params = array()) {
        //
        if (!isset($params["fieldset_class"])) {
            $params["fieldset_class"] = "";
        }
        $params["fieldset_class"] .= " cadre ui-corner-all ui-widget-content";
        //
        if (!isset($params["legend_class"])) {
            $params["legend_class"] = "";
        }
        $params["legend_class"] .= " ui-corner-all ui-widget-content ui-state-active";
        //
        parent::display_start_fieldset($params);
    }

    // {{{ NEW

    /**
     *
     */
    function display_list($params) {
        // Ouverture du fieldset
        $this->display_start_fieldset(array(
            "legend_content" => $params["title"],
            "fieldset_class" => (isset($params["class"]) ? $params["class"] : ""),
        ));
        //
        //echo "<ul>";
        foreach($params["list"] as $key => $value) {
            echo "<div class=\"choice ui-corner-all ui-widget-content\">\n";
            //echo "<li>";
            $this->display_link($value);
            //
            if (isset($value["links"]) && is_array($value["links"]) && count($value["links"]) > 0) {
                //
                foreach ($value["links"] as $link) {
                    //
                    $this->display_link($link);
                }
            }
            //echo "</li>";
            echo "</div>";
        }
        //echo "</ul>";
        // Fermeture du fieldset
        $this->display_stop_fieldset();
    }

    // }}}

    /**
     * @ignore
     */
    function display__form_input_submit($params) {
        if (isset($params["id"])
            && ($params["id"] == "adv-search-submit"
                || $params["id"] == "search-submit")) {
            echo "\t\t<button type=\"submit\" id=\"".$params["id"]."\" name=\"".$params["name"]."\">";
            echo __("Recherche");
            echo "</button>\n";
            return;
        }
        parent::display__form_input_submit($params);
    }

    /**
     * @ignore
     */
    function display__form_controls_container__begin($params = null) {
        printf(
            '<div class="formControls%s">',
            (isset($params["controls"]) ? " formControls-".$params["controls"] : "")
        );
    }

    /**
     * Aide à la saisie pour le widget de formulaire *localisation_edition*
     * qui utilise le snippet de formulaire *localisation_edition*.
     */
    public function display__form_helper__localisation_edition($params) {
            printf(
                '<span style="display:none;" data-href="%1$s" class="form-snippet-localisation_edition"><!-- --></span><button class="localisation_edition" type="button" onclick="javascript:localisation_edition(form, \'%2$s\', \'%3$s\', \'%4$s\', \'%5$s\');"><span class="ui-icon ui-icon-pin-s" title="%6$s">%7$s</span></button>',
                $params["snippet_base_url"],
                $params["field_format"],
                $params["field_orientation"],
                $params["field_x"],
                $params["field_y"],
                $params["description"],
                $params["title"]
            );
    }

    /**
     * @ignore
     */
    public function display_table_search_simple($params) {
        echo "<!-- table-search BEGIN -->";
        $this->display__container__begin(array(
            "class" => sprintf('%s-search ui-widget-content ui-corner-all', $params["style"]),
        ));
        $this->display__form_container__begin(array(
            "action" =>  $params["form_action"],
        ));
        // Affichage du champ permettant la saisie du terme a rechercher
        echo "\t\t<input type=\"text\" name=\"recherche\" ";
        echo "value=\"".$params["search"]."\" ";
        echo "class=\"champFormulaire\" />\n";
        // Affichage du champ select permettant de choisir le champ sur lequel
        // doit agir la recherche
        echo "\t\t<select name=\"selectioncol\" class=\"champFormulaire\">\n";
        if ($params["column_search_selected_key"] == "") {
            echo "\t\t\t<option value=\"\" selected=\"selected\">".__("Tous")."</option>\n";
            foreach ($params["column_search"] as $key => $elem) {
                echo "\t\t\t<option value=\"".$key."\">".$elem."</option>\n";
            }
        } else {
            echo "\t\t\t<option value=\"\">".__("Tous")."</option>\n";
            foreach ($params["column_search"] as $key => $elem) {
                if ($params["column_search_selected_key"] == $key) {
                    echo "\t\t\t<option value=\"".$key."\" selected=\"selected\">".$elem."</option>\n";
                } else {
                    echo "\t\t\t<option value=\"".$key."\">".$elem."</option>\n";
                }
            }
        }
        echo "\t\t</select>\n";
        $this->display__form_input_submit(array(
            "id" => "search-submit",
            "name" => "s1",
            "value" => __("Recherche"),
        ));
        $this->display__form_container__end();
        $this->display__container__end();
        echo "<!-- table-search END -->";
    }

    /**
     * @ignore
     */
    public function display_table_advanced_search($params = array()) {
        echo "<!-- table-search BEGIN -->";
        $this->display__form_container__begin(array(
            "action" => $params["form_action"],
            "id" => "advanced-form",
        ));
        $this->display_start_fieldset(array(
            "fieldset_class" => sprintf(
                'table-search-fieldset adv-search-fieldset adv-search-fieldset-%s',
                $params["searchform_type"]
            ),
            "legend_id" => "toggle-advanced-display",
            "legend_content" => __("Recherche"),
        ));
        $this->display__container__begin(array(
            "id" => "adv-search-classic-fields",
            "style" => ($params["searchform_type"] !== "classic" ? "display:none;" : ""),
        ));
        $this->display_link(array(
            "href" => $params["advanced_reset_link_href"],
            "class" => "adv-search-link-to-advanced",
            "title" => __("Afficher la recherche avancée"),
        ));
        printf(
            '%s<div class="visualClear"><!-- --></div><p class="adv-search-helptext">%s</p>',
            $params["classic_form"],
            $params["help_text"]
        );
        $this->display__container__end();
        $this->display__container__begin(array(
            "id" => "adv-search-adv-fields",
            "style" => ($params["searchform_type"] !== "advanced" ? "display:none;" : ""),
        ));
        $this->display_link(array(
            "href" => $params["classic_reset_link_href"],
            "class" => "adv-search-link-to-classic",
            "title" => __("Afficher la recherche simple"),
        ));
        printf(
            '%s<div class="visualClear"><!-- --></div><p class="adv-search-helptext">%s</p>',
            $params["advanced_form"],
            $params["help_text"]
        );
        $this->display__container__end();
        $this->display_stop_fieldset();
        $this->display__form_input_submit(array(
            "id" => "adv-search-submit",
            "name" => sprintf('%s-search-submit', $params["searchform_type"]),
            "value" => __("Recherche"),
        ));
        $this->display_link(array(
            "href" => $params[sprintf('%s_reset_link_href', $params["searchform_type"])],
            "class" => "raz_advs",
            "title" => __("Vider le formulaire"),
        ));
        $this->display__form_container__end();
        echo "<!-- table-search END -->";
    }

    public function display_table_pagination($params) {
        //
        echo "<div class=\"".$params["style"]."-pagination ui-state-default ui-corner-top ui-tabs-selected ui-state-active\">\n";

        echo "<div class=\"pagination-nb\">";

        // Si il y a une page precedente
        if ($params["previous"] != "") {
            // Affichage du lien vers la page precedente
            echo " <a href=\"";
            //
            if ($params["onglet"]) {
                echo "#";
                echo "\" ";
                echo "onclick=\"ajaxIt('".$params["obj"]."','";
            }
            //
            echo $params["previous"];
            //
            if ($params["onglet"]) {
                echo "');";
            }
            echo "\" ";
            echo "title=\"".__("Page precedente")."\" ";
            echo "class=\"pagination-prev\"";
            echo ">";
            // Affichage de l'image representant la page precedente
            echo "<span class=\"ui-icon ui-icon-circle-triangle-w\">".__("Page precedente")."</span>";
            // Fermeture de la balise lien
            echo "</a> ";
        }

        // Affichage du conteneur de message de pagination
        echo "<span class=\"pagination-text\">";
        // Construction du message de pagination
        echo $params["first"]." - ".$params["last"]." ";
        echo __("enregistrement(s) sur")." ".$params["total"];
        // Affichage du message de pagination
        if ($params["search"] != "") {
            echo " = [".$params["search"]."] ";
        }
        // Fermeture de la balise span
        echo "</span>";

        // Si il y a une page suivante
        if ($params["next"] != "") {
            // Affichage du lien vers la page suivante
            echo " <a href=\"";
            //
            if ($params["onglet"]) {
                echo "#";
                echo "\" ";
                echo "onclick=\"ajaxIt('".$params["obj"]."','";
            }
            //
            echo $params["next"];
            //
            if ($params["onglet"]) {
                echo "');";
            }
            echo "\" ";
            echo "title=\"".__("Page suivante")."\" ";
            echo "class=\"pagination-next\"";
            echo ">";
            // Affichage de l'image representant la page suivante
            echo "<span class=\"ui-icon ui-icon-circle-triangle-e\">".__("Page suivante")."</span>";
            // Fermeture de la balise lien
            echo "</a> ";
        }
        //
        echo "</div>";

        //
        if ($params["pagination_select"]["active"] == true) {
            //
            echo "<div class=\"pagination-select\">";
            // Affichage du formulaire
            $this->display__form_container__begin(array(
                "action" => "",
            ));
            // Affichage du mot Page
            echo "&nbsp;".__('Page')."&nbsp;";
            $params["pagination_select"]["link"] = str_replace(
                '<PREMIER>',
                '\'+this.value+\'',
                $params["pagination_select"]["link"]
            );
            // Affichage de la liste de choix
            echo "<select name=\"page\" size='1' ";
            if ($params["onglet"]) {
                echo "onchange=\"ajaxIt('".$params["obj"]."', '";
                echo $params["pagination_select"]["link"];
                echo "');";
                echo "\"";
            } else {
                echo "onchange=\"allerpage('";
                echo $params["pagination_select"]["link"];
                echo "');\"";
            }
            echo " class='champFormulaire' >";
            // Boucle sur le nombre de page pour l'affichage de chaque item de
            // la liste
            for ($i = 1; $i <= $params["pagination_select"]["page_number"]; $i++) {
                // Affichage de l'item selectionne sur la page en cours
                if (($i - 1) * $params["pagination_select"]["serie"] == $params["pagination_select"]["premier"]) {
                    echo "<option value=\"".($i - 1) * $params["pagination_select"]["serie"]."\" selected=\"selected\">";
                    echo $i." / ".$params["pagination_select"]["page_number"];
                    echo "</option>";
                } else {
                    echo "<option value=\"".($i - 1) * $params["pagination_select"]["serie"]."\">";
                    echo $i." / ".$params["pagination_select"]["page_number"];
                    echo "</option>";
                }
            }
            // Fermeture de la balise liste de choix
            echo "</select>";
            // Fonction javascript allerpage()
            echo "<script type=\"text/javascript\">";
            echo "function allerpage(link) { ";
            echo "location=link.replace(/&amp;/g, \"&\")";
            echo "} ";
            echo "</script>";

            // Fermeture de la balise formulaire
            $this->display__form_container__end();

            //
            echo "</div>";
        }

        //
        echo "\t<div class=\"visualClear\"><!-- --></div>\n";
        echo "</div>\n";
    }
}
