<?php
/**
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2012
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 * @package xmldb
 * 
 */
//TODO: to complete
class xmldb_searchform
{
    function xmldb_searchform($databasename, $tablename, $path = "misc", $lang = "en", $languages = "en,it", $params = false)
    {
        $this->xmldb_frm = xmldb_frm($databasename, $tablename, $path, $lang, $languages, $params);
    }
    function HtmlSearchForm($oldvalues = false, $isadmin = false)
    {
        $html = "";
        $lang = $this->xmldb_frm->lang;
        foreach ($this->xmldb_frm->formvals as $k => $fv)
        {
            if (isset($fv['primarykey']) && $fv['primarykey'] == "1")
                $primarykey = $k;
        }
        $extravalues = null;
        foreach ($this->xmldb_frm->formvals as $extravaluesk => $extravalues)
        {

            if (isset($extravalues['fk_filter_field']) && $extravalues['fk_filter_field'] != "")
                $extravalues['options'] = $this->xmldb_frm->LoadOptions($extravalues, $oldvalues);
            // --------multilanguage -------------->
            if (isset($extravalues['frm_multilanguage']) && $extravalues['frm_multilanguage'] == 1)
            {
                continue;
            }
            $extravalues['realname'] = $extravalues['name'];
            // --------multilanguage --------------<
            //------------gestione visualizzazione----------->
            $showfield = true;
            if (empty($extravalues['frm_search']))
               $showfield = false;
            //------------gestione visualizzazione-----------<
            $oldval = isset($oldvalues[$extravaluesk]) ? $oldvalues[$extravaluesk] : "";
            $extravalues['messages'] = $this->xmldb_frm->messages;
            $extravalues['value'] = $oldval;
            $extravalues['fieldform'] = $this->xmldb_frm;
            $extravalues['oldvalues'] = $oldvalues;
            $extravalues['oldvalues_primarikey'] = $primarykey;
            $extravalues['multilanguage'] = false;
            $extravalues['lang'] = $lang;
            $extravalues['lang_noprefix'] = $lang;
            $extravalues['languagesfield'] = $lang;
            $extravalues['frm_help'] = isset($extravalues['frm_help']) ? $extravalues['frm_help'] : "";
            if ($showfield)
            {
                $html .="<div>";
                $html .= $extravalues['title'] . " ";
                $str = "";
                $str .= "<select name=\"_search_{$extravalues['name']}\" >";
                $str .= "<option  value=\"\" >ignora campo</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "_x_") ? "selected=\"selected\"" : "";
                $str .= "<option $s value=\"_x_\" >contiene</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "x") ? "selected=\"selected\"" : "";
                $str .= "<option $s value=\"x\">uguale</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "x_") ? "selected=\"selected\"" : "";
                $str .= "<option  $s value=\"x_\" >inizia per</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "_x") ? "selected=\"selected\"" : "";
                $str .= "<option  $s value=\"_x\">finisce per</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "<") ? "selected=\"selected\"" : "";
                $str .= "<option  $s value=\"<\">&egrave minore di</option>";
                $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == ">") ? "selected=\"selected\"" : "";
                $str .= "<option  $s value=\">\">&egrave maggiore di</option>";
                if (!empty($extravalues['frm_search_ranges']))
                {
                    $s = (isset($oldvalues["_search_{$extravalues['name']}"]) && $oldvalues["_search_{$extravalues['name']}"] == "range") ? "selected=\"selected\"" : "";
                    $str .= "<option $s value=\"range\">&egrave compreso tra</option>";
                }$str .= "</select>";
                $html.= "<br />" . $str;
                if (!empty($extravalues['frm_search_ranges']))
                {
                    $extravalues2 = $extravalues;
                    $extravalues2['strhiddenfield'] = "";
                    $extravalues2['frm_starttagvalue'] = "";
                    $extravalues2['frm_endtagvalue'] = "";
                    $extravalues2['frm_starttagtitle'] = "";
                    $extravalues2['frm_endtagtitle'] = "";
                    $extravalues2['title'] = "";
                    $extravalues2['value'] = (!empty($oldvalues["_search_range_end_" . $extravalues2['name']])) ? $oldvalues["_search_range_end_" . $extravalues2['name']] : "";
                    $extravalues2['name'] = "_search_range_end_" . $extravalues2['name'];
                    $html.= $this->xmldb_frm->formclass[$extravaluesk]->show($extravalues2);
                }
                $html.=$this->xmldb_frm->formclass[$extravaluesk]->show($extravalues);
                $html.= "</div>";
            }
        }
        return $html;
    }
    function GetQueryByPost()
    {
        $post = $this->GetSearchByPost();
        //dprint_r($post);
        $query = "SELECT * FROM {$this->table}  ";
        $sep = "";
        $query2 = "";
        foreach ($this->formvals as $key => $value)
        {
            $mode = isset($post['_search_' . $key]) ? $post['_search_' . $key] : "";
            if (!empty($value['frm_search']) && $mode != "")
            {
                if ($mode == "x_")
                {
                    $query2 .= " $key LIKE '{$post[$key]}%'";
                    $sep = " AND ";
                }
                if ($mode == "_x_")
                {
                    $query2 .= "$sep $key LIKE '%{$post[$key]}%'";
                    $sep = " AND ";
                }
                if ($mode == "_x")
                {
                    $query2 .= "$sep $key LIKE '%{$post[$key]}'";
                    $sep = " AND ";
                }
                if ($mode == "x")
                {
                    $query2 .= "$sep $key LIKE '{$post[$key]}'";
                    $sep = " AND ";
                }
                if ($mode == ">")
                {
                    $query2 .= "$sep $key > '{$post[$key]}'";
                    $sep = " AND ";
                }
                if ($mode == "<")
                {
                    $query2 .= "$sep $key < '{$post[$key]}'";
                    $sep = " AND ";
                }

                if (!empty($post[$key]) && !empty($post['_search_range_end_' . $key]) && $mode == "range")
                {
                    $query2 .= "$sep ($key >= '{$post[$key]}' AND $key <= '" . $post['_search_range_end_' . $key] . "')";
                    $sep = " AND ";
                }
            }
        }
        if ($query2 != "")
            $query = "WHERE $query2";
        return $query;
    }
    /**
     * getbypost
     *
     * riempe un array con i valori ricevuti tramite post
     * @param array $oldvalues i valori che non sono in post vanno mantenuti
     */
    function GetSearchByPost()
    {

        $newvalues = array();
        foreach ($_POST as $key => $value)
        {
            $newvalues[$key] = $value;
        }
        foreach ($this->xmldb_frm->formvals as $key => $value)
        {
            if (isset($value['type']) && ($value['type'] == 'check'))
            {
                if (isset($_POST["__check__$key"]) && !isset($_POST["$key"]))
                {
                    $newvalues[$key] = "";
                }
                if (isset($_POST["__check__$key"]) && isset($_POST[$key]))
                {
                    $newvalues[$key] = $_POST[$key];
                }
            }
            else
            {
                if (isset($_POST[$key]))
                {
                    $newvalues[$key] = $_POST[$key];
                    if (ini_get('magic_quotes_gpc') == 1)
                    {
                        $newvalues[$key] = stripslashes($newvalues[$key]);
                    }
                    if (isset($value['frm_allowhtml']) && $value['frm_allowhtml'] != "true")
                    {
                        $newvalues[$key] = htmlentities($_POST[$key]);
                    }
                }
            }
        }
        return $newvalues;
    }
}
?>
