<?php
include_once("db.php");
include_once("utility.php");
include_once("dbutility.php");
include_once("fieldrender.php");
include_once("form.php");
include_once("parserfield.php");
include_once("navbar.php");

class Biblio {
    private $bib;
    private $DATAcat;
    private $controllo;
    private $progetti;
    protected $tipo_ente;
    private $prog_arr;
    private $en_arr;
    private $disc_arr;
    private $nation_arr;
    private $prov_arr;
    private $bi_pwd_2;
    private $biblio_account_fields;
    private $biblio_service_fields;

    function __construct($id = null) {
        $this->bib = $_SESSION['db']->fetch_fields("biblio");
        $this->bi_pwd_2 = clone $this->bib["bi_pwd"];
        $this->bi_pwd_2->fieldname = 'bi_pwd_2';
        if ($id != null) {
            $catlist = "";
            $resc = $_SESSION['db']->execute("SELECT ca_id FROM gruppo_catalogo WHERE gr_id IN (SELECT gr_id from biblio_gruppo WHERE bi_id=" . $id . ")");
            if ($resc && $_SESSION['db']->numrows($resc) > 0) {
                while ($c = $_SESSION['db']->fetch($resc)) {
                    if ($catlist != "")
                        $catlist.=",";
                    $catlist.=$c["ca_id"];
                }
                if ($catlist != "")
                    $catlist = "WHERE ca_id IN ($catlist)";
            }

            $res = $_SESSION['db']->execute("SELECT * FROM biblio WHERE bi_id=" . $id);

            if ($res && $_SESSION['db']->numrows($res) == 1) {
                $dati = $_SESSION['db']->fetch($res);
                foreach ($this->bib as $key => $v) {
                    $this->bib[$key]->value = $dati[$key];
                }
            }
            $tente = $_SESSION['db']->execute("SELECT ti_id FROM ente WHERE en_id=" . $this->bib["en_id"]->value);
            $this->tipo_ente = $_SESSION["db"]->fetch_single($tente);
            $this->progetti = array();
            $res_prog = $_SESSION['db']->execute("SELECT pr_id, pr_nome
FROM progetto
WHERE pr_id IN (SELECT pr_id
FROM biblio_progetto
WHERE bi_id=" . $id . ")");
            while ($row = $_SESSION['db']->fetch($res_prog)) {
                $this->progetti[$row["pr_id"]] = $row["pr_nome"];
            }
            $nameview = "biblioview_" . rand(0, 999999);
            $result_VISTA = $_SESSION['db']->execute("CREATE VIEW biblio_catalogo_" . $nameview . " AS
SELECT ca_id as idcat, bc_attivo
FROM biblio_catalogo
WHERE bi_id =" . $id . "");
            $result_allcat = $_SESSION['db']->execute("SELECT *
FROM catalogo LEFT JOIN biblio_catalogo_" . $nameview . "
ON catalogo.ca_id = biblio_catalogo_" . $nameview . ".idcat $catlist");
            $result_DROPVISTA = $_SESSION['db']->execute("DROP VIEW IF EXISTS biblio_catalogo_" . $nameview);
            while ($row = $_SESSION['db']->fetch($result_allcat)) {
                $key_cat = $row["ca_id"];
                $this->DATAcat[$key_cat]["ca_nome"] = $row["ca_nome"];
                $this->DATAcat[$key_cat]["bc_attivo"] = $row["bc_attivo"];
                if ($this->DATAcat[$key_cat]["bc_attivo"] === NULL)
                    $this->DATAcat[$key_cat]["partecipa"] = FALSE;
                else
                    $this->DATAcat[$key_cat]["partecipa"] = TRUE;

                $this->DATAcat[$key_cat]["ca_tabella"] = $row["ca_tabella"];
                if ($this->DATAcat[$key_cat]["ca_tabella"] != NULL) {
                    $this->DATAcat[$key_cat]["tabella"] = $_SESSION['db']->fetch_fields($row["ca_tabella"]);
                    $res_tab = $_SESSION['db']->execute("SELECT * FROM " . $row["ca_tabella"] . " WHERE bi_id=" . $id);
                    $dati_tab = $_SESSION['db']->fetch($res_tab);
                    foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                        $this->DATAcat[$key_cat]["tabella"][$key_tab]->value = $dati_tab[$key_tab];
                    }
                    if ($key_cat == 2) {
                        if ($this->DATAcat[$key_cat]["tabella"]['sbn_cod']->value != "" && $this->DATAcat[$key_cat]["tabella"]['sbn_cod_polo']->value != "")
                            $this->DATAcat[$key_cat]["partecipa"] = TRUE;
                        else
                            $this->DATAcat[$key_cat]["partecipa"] = FALSE;
                    }
                }
                else
                    $this->DATAcat[$key_cat]["tabella"] = NULL;

                $this->DATAcat[$key_cat]["ca_url"] = $row["ca_url"];
            }

	    $this->en_arr=array();
	    $res_ente=$_SESSION['db']->execute("SELECT en_nome as ente
FROM ente
WHERE en_id=".$this->bib["en_id"]->value);
	    $ente = $_SESSION['db']->fetch_row($res_ente,0);
            $this->en_arr[$this->bib["en_id"]->value] = $ente['ente'];

            $this->nation_arr[$this->bib["na_id"]->value] = $this->bib["na_id"]->value;
            $this->prov_arr = db_elenco("prov_id", "na_id='" . $this->bib["na_id"]->value. "'");
        }
        else {
	    $catlist="";
	    $resc=$_SESSION['db']->execute("SELECT ca_id FROM gruppo_catalogo WHERE gr_id=1");
	    if($resc && $_SESSION['db']->numrows($resc)>0) {
		while ($c=$_SESSION['db']->fetch($resc)) {
		    if($catlist!="") $catlist.=",";
                    $catlist.=$c["ca_id"];
                }
		if($catlist!="") $catlist="WHERE ca_id IN ($catlist)";
            }
            $result_allcat = $_SESSION['db']->execute("SELECT * FROM catalogo $catlist");
            while ($row = $_SESSION['db']->fetch($result_allcat)) {
                $key = $row["ca_id"];
                $nome_cat = $row["ca_nome"];
                $tabella_cat = $row["ca_tabella"];
                $url_cat = $row["ca_url"];

                $this->DATAcat[$key]["ca_nome"] = $nome_cat;
                $this->DATAcat[$key]["bc_attivo"] = 0;
                $this->DATAcat[$key]["partecipa"] = FALSE;
                $this->DATAcat[$key]["ca_tabella"] = $tabella_cat;
                if ($this->DATAcat[$key]["ca_tabella"] != NULL)
                    $this->DATAcat[$key]["tabella"] = $_SESSION['db']->fetch_fields($tabella_cat);
                else
                    $this->DATAcat[$key]["tabella"] = NULL;
            }
            $this->prog_arr = db_elenco("pr_id");
            $this->bib["bi_opac"]->isnull = FALSE;
        }

        $this->biblio_account_fields = array($this->bib["bi_usr"], $this->bib["bi_pwd"], $this->bib["bi_nome"], $this->bib["di_id"], $this->bib["en_id"], $this->bib["bi_indi"], $this->bib["bi_cap"], $this->bib["bi_citta"], $this->bib["na_id"], $this->bib["prov_id"], $this->bib["bi_piva"], $this->bib["bi_cf"], $this->bib["bi_tel"], $this->bib["bi_fax"], $this->bib["bi_url"]);
        $this->biblio_service_fields = array($this->bib["bi_referente_dd"], $this->bib["bi_email_dd"], $this->bib["bi_tel_dd"], $this->bib["bi_costo_ute_dd"], $this->bib["bi_costo_dd"], $this->bib["bi_scompenso"], $this->bib["bi_condforni"], $this->bib["bi_sospini"], $this->bib["bi_sospfine"], $this->bib["bi_preavvisososp"]);
    }

    public function __get($fieldname) {
        return $this->bib[$fieldname]->value;
    }

    public function get_controllo() {
        return $this->controllo;
    }

    public function get_tipoEnte() {
        return $this->tipo_ente;
    }

    public function get_bib() {
        return $this->bib;
    }

    public function reset_controllo() {
        $this->controllo = array();
    }

    public function set_controllo($ctrl) {
        $this->controllo = $ctrl;
    }

    public function set_bib($bib) {
        $this->bib = $bib;
    }

    public function __set($fieldname, $val) {
        $this->bib[$fieldname]->value = $val;
    }

    public function __isset($fieldname) {
        return isset($this->bib[$fieldname]);
    }

    public function __unset($fieldname) {
        unset($this->bib[$fieldname]);
    }

    function __toString() {
        return "Biblioteca: " . $this->bib["bi_id"]->value . " | " . $this->bib["bi_nome"]->value;
    }

    public function registra($step) {
        $this->reset_controllo();
        $res = "";
        switch ($step) {
            case 1:
                $res.="
<div id=\"public_white_area_box_wide\">
<div class=\"b1_t\">
<div class=\"b1_t_r\"></div>
<div class=\"b1_t_l\"></div>
<div class=\"b1_t_c\"></div>
</div>
<div class=\"b1_l\">
<div class=\"b1_r\">
<div id=\"public_white_area\">
<div id=\"public_white_area_content\">
<div id=\"contents_public_menu_intestazione\" class=\"contents_public_menu_intestazione\">";
                $res.=printNavBar("registration_library");
                $res.="					</div>
<div id=\"register_box\">
<div class=\"contents_public_submenu_contenitore\">
<div class=\"contents_public_submenu_corn\"></div>
<div class=\"title_submenu\">";
                $res.=getConstVar("LIBRARYREG");
                $res.="						</div>
</div>
<div class=\"contents_format_align\">
<div class=\"chosen_data_lib_empty smaller2\">
";
                $res.=$this->reg_nav($step);
                $res.="
<br class=\"clear_float\" />
</div>
";
                $res.=$this->reg_passo($step);

                $res.="
</div>
</div> <!-- END: register_box -->
</div> <!-- END: public_white_area_content -->
</div> <!-- END: public_white_area -->
</div> <!-- END: b1_r -->
</div>  <!-- END: b1_l -->
<div class=\"b1_b\">
<div class=\"b1_b_r\"></div>
<div class=\"b1_b_l\"></div>
<div class=\"b1_b_c\"></div>
</div>
</div> <!-- END: public_white_area_box_wide -->
<br class=\"clear_float\" />
";
                break;

            case 6:
                $res.="
<div id=\"public_white_area_box_wide\">
<div class=\"b1_t\">
<div class=\"b1_t_r\"></div>
<div class=\"b1_t_l\"></div>
<div class=\"b1_t_c\"></div>
</div>
<div class=\"b1_l\">
<div class=\"b1_r\">
<div id=\"public_white_area\">
<div id=\"public_white_area_content\">
<div id=\"contents_public_menu_intestazione\" class=\"contents_public_menu_intestazione\">";
                if (!$this instanceof BiblioSubscription)
                    $res.=printNavBar("registration_library");
                else
                    $res.=printNavBar("subscription_library");

                $res.="					</div>
<div id=\"register_box\">
<div class=\"contents_public_submenu_contenitore\">
<div class=\"contents_public_submenu_corn\"></div>
<div class=\"title_submenu\">";
                if (!$this instanceof BiblioSubscription)
                    $res.=getConstVar("LIBRARYREG");
                else
                    $res.=getConstVar("LIBRARYSUB");
                $res.="							</div>
</div>
<div class=\"contents_format_align\">
";
                $res.=$this->reg_passo($step);

                $res.="
<div class=\"chosen_data_lib smaller2 def_lib_data\">
";
                $res.=$this->reg_notifica($step);

                $res.="
</div>
<br class=\"clear_float\" />
<br />
<div id=\"reg_footer\">
<span class=\"bold float_r\">
&gt; <a class=\"viola bold\" href=\"javascript:stampa('reg_bib');\">" . getConstVar("PRINT") . "</a>
</span>
<span class=\"bold\">
< <a class=\"viola bold\" href=\"index.php\">" . getConstVar("RETURN_HOME") . "</a>
</span>
</div>
</div>
</div> <!-- END: register_box -->
</div> <!-- END: public_white_area_content -->
</div> <!-- END: public_white_area -->
</div> <!-- END: b1_r -->
</div>  <!-- END: b1_l -->
<div class=\"b1_b\">
<div class=\"b1_b_r\"></div>
<div class=\"b1_b_l\"></div>
<div class=\"b1_b_c\"></div>
</div>
</div> <!-- END: public_white_area_box_wide -->
<br class=\"clear_float\" />
";
                break;

            default:

                $prev = $step - 1;

                $res.="	<div class=\"chosen_data_lib smaller2\">";
                $res.=$this->reg_nav($step);
                $res.=$this->reg_notifica($step);
                $res.="
</div>

<div id=\"public_white_area_box_medium\">
<div class=\"b1_t\">
    <div class=\"b1_t_r\"></div>
    <div class=\"b1_t_l\"></div>
    <div class=\"b1_t_c\"></div>
</div>
<div class=\"b1_l\">
<div class=\"b1_r\">
<div id=\"public_white_area\">
<div id=\"public_white_area_content\">
<div id=\"contents_public_menu_intestazione\" class=\"contents_public_menu_intestazione\">";
                if (!$this instanceof BiblioSubscription)
                    $res.=printNavBar("registration_library");
                else
                    $res.=printNavBar("subscription_library");
                $res.="					</div>
<div id=\"register_box\">
<div class=\"contents_public_submenu_contenitore\">
<div class=\"contents_public_submenu_corn\"></div>
<div class=\"title_submenu\">";
                if (!$this instanceof BiblioSubscription)
                    $res.=getConstVar("LIBRARYREG");
                else
                    $res.=getConstVar("LIBRARYSUB");

                $res.="							</div>
</div>
<div class=\"contents_format_align\">
";
                $res.=$this->reg_passo($step);

                $res.="
</div>
</div> <!-- END: register_box -->
</div> <!-- END: public_white_area_content -->
</div> <!-- END: public_white_area -->
</div> <!-- END: b1_r -->
</div>  <!-- END: b1_l -->
<div class=\"b1_b\">
    <div class=\"b1_b_r\"></div>
    <div class=\"b1_b_l\"></div>
    <div class=\"b1_b_c\"></div>
</div>
</div> <!-- END: public_white_area_box_medium -->
<br class=\"clear_float\" />
";
                break;
        }

        return $res;
    }

    public function reg_nav($step) {
        $nav = "<div id=\"reg_step_nav\" class=\"biggest4\">";

        for ($i = 1; $i <= 6; $i++) {
            if ($i != 1) {
                if ($i <= $step)
                    $nav.= "<div class=\"reg_step_nav_link\"></div>";
                else
                    $nav.= "<div class=\"reg_step_nav_link_hover\"></div>";
            }
            if ($i <= $step)
                $nav.= "<div class=\"reg_step_nav_item\">" . $i . "</div>";
            else
                $nav.= "<div class=\"reg_step_nav_item_hover\">" . $i . "</div>";
        }
        $nav.= "</div>";
        return $nav;
    }

    public function reg_notifica($step) {
        switch ($step) {
            case 2:
                $ret = "<div id=\"library_identity\">";
                $ret.=$this->fill_library_identity();
                $ret.="</div>";
                break;

            case 3:
                $ret = "<div id=\"library_identity\">";
                $ret.=$this->fill_library_identity();
                $ret.=$this->fill_library_identity2();
                $ret.="</div>";

                $ret.="<div id=\"library_contacts2\">";
                $ret.=$this->fill_library_contacts();
                $ret.="</div>";

                break;

            case 4:
                $ret = "<div id=\"library_identity\">";
                $ret.=$this->fill_library_identity();
                $ret.=$this->fill_library_identity2();
                $ret.="</div>";

                $ret.="<div id=\"library_contacts2\">";
                $ret.=$this->fill_library_contacts();
                $ret.="</div>";

                $ret.="<div id=\"library_cat2\">
<h3 class=\"bigger2\">" . getConstVar("CATALOGHI") . "</h3>
";
                $ret.=$this->fill_library_cat();
                $ret.="</div>";
                break;

            case 5:
                $ret = "<div id=\"library_identity\">";
                $ret.=$this->fill_library_identity();
                $ret.=$this->fill_library_identity2();
                $ret.="</div>";

                $ret.="<div id=\"library_contacts2\">";
                $ret.=$this->fill_library_contacts();
                $ret.="</div>";

                $ret.="<div id=\"library_cat2\">
                        <h3 class=\"bigger2\">" . getConstVar("CATALOGHI") . "</h3>
                    ";
                $ret.=$this->fill_library_cat();
                $ret.="</div>";

                $ret.="<div id=\"library_service2\">
                    <h3 class=\"bigger2\">" . getConstVar("BIBLIO_SERVIZIODD") . "</h3>";
                $ret.=$this->fill_library_service();
                $ret.="</div>";
                break;

            case 6:
                $ret = "<div id=\"library_identity\">";
                $ret.=$this->fill_library_identity();
                $ret.=$this->fill_library_identity2();
                $ret.="</div>";

                $ret.="<div id=\"library_contacts2\">";
                $ret.=$this->fill_library_contacts();
                $ret.="</div>";

                $ret.="<div id=\"library_cat2\">
    <h3 class=\"bigger2\">" . getConstVar("CATALOGHI") . "</h3>
    ";
                $ret.=$this->fill_library_cat();
                $ret.="</div>";

                $ret.="<div id=\"library_service2\">
    <h3 class=\"bigger2\">" . getConstVar("BIBLIO_SERVIZIODD") . "</h3>";
                $ret.=$this->fill_library_service();
                $ret.="</div>";
                    if ($this instanceof BiblioSubscription)
                    {
                    $ret.="<div id=\"library_subscription\">";
                    $ret.=$this->fill_library_subscriptions();
                    $ret.="</div>";
                }

                break;
        }
        return $ret;
    }

    public function reg_passo($step) {
        $this->reset_controllo();
        $passo = "<div id=\"reg_step\">";
        switch ($step) {
            case 1:
                $passo.="<h4>
                            <div class=\"bold\">" . getConstVar("BIBLIO_USRPWD") . "</div>";
                $passo.="<div class=\"bold\">" . getConstVar("BIBLIO_USRPWD_DET") . "</div>";
                $passo.="</h4>
                        <div id=\"registration_feedback\" class=\"red\"></div>";
                $form = new FieldsForm("form_reg_lib");
                $cont_tmp = $form->addField($this->bib["bi_usr"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML("<label>" . getConstVar("BIBLIO_USR_MIN5") . "</label><br />");
                $cont_tmp = $form->addField($this->bib["bi_pwd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML("<label>" . getConstVar("BIBLIO_PWD_MIN8") . "</label><br />");
                $cont_tmp = $form->addField($this->bi_pwd_2);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML($this->reg_buttons($step));
                $form->close();
                $passo.=$form->restituisci();
                break;

            case 2:
                $passo.="<h4>
                            <span class=\"bold\">" . getConstVar("BIBLIO_INSER_DATI") . "</span>
                            <br />
                            <br />
                            " . getConstVar("BIBLIO_CORRECT_DATI") . "
                        </h4>
                        <div id=\"registration_feedback\" class=\"red\"></div>";

                $primo[""] = getConstVar("OPTION_FIRST_LINE");
                $form = new FieldsForm("form_reg_lib");
                $cont_tmp = $form->addField($this->bib["bi_nome"], null, null, "<label>" . getConstVar("BIBLIO_SETNAME") . "</label>");
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_indi"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_cap"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_citta"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $this->disc_arr = db_elenco("di_id");
                $cont_tmp = $form->addFieldSelect($this->bib["di_id"], null, $this->disc_arr, 0, null, null, "<label>" . getConstVar("BIBLIO_SPECIALIZZAZIONE") . "</label>\n");
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML("<br class=\"clear\"/>");
                $this->nation_arr = db_elenco("na_id");
                $this->nation_arr = $primo + $this->nation_arr;
                $cont_tmp = $form->addFieldSelect($this->bib["na_id"], null, $this->nation_arr, 6, "onchange=\"javascript:updateByNation()\"", "<span>", "</span>");
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML("<div id=\"prov_id_box_sel\" class=\"prov_id\">");
                if ($this->bib["na_id"]->value != "")
                    $form->addHTML($this->sel_prov($this->bib["na_id"]->value));
                $this->controllo["prov_id"]["obbligo"] = 1;
                $form->addHTML("</div>");
                $disp = "none";
                if ($this->bib["na_id"]->value != "")
                    $disp = "block";
                $form->addHTML("<div id=\"ti_id_box_sel\" class=\"en_id\" style=\"display:$disp\">");
                if ($this->bib["na_id"]->value != "")
                    $form->addHTML($this->sel_tipoente($this->bib["na_id"]->value));
                $this->controllo["ti_id"]["obbligo"] = 1;
                $form->addHTML("</div>");
                $disp2 = "none";
                if ($this->tipo_ente != "" && $this->bib["na_id"]->value != "")
                    $disp2 = "block";
                $form->addHTML("<div id=\"en_id_box_sel\" class=\"en_id\" style=\"display:$disp2\">");
                if ($this->tipo_ente != "" && $this->bib["na_id"]->value != "")
                    $form->addHTML($this->sel_ente($this->tipo_ente, $this->bib["na_id"]->value));
                $this->controllo["en_id"]["obbligo"] = 1;
                $form->addHTML("</div>");
                $form->addHTML("<div class=\"pr_id\"><div class=\"form_obj_name\">" . getConstVar("BIBLIO_PROGETTO") . "</div>:<div id=\"pr_id_list\">");

                if (get_class($this) != 'Biblio')
                    $this->prog_arr = db_elenco('pr_id');

                if ($this->prog_arr != null) {
                    foreach ($this->prog_arr as $key => $val) {
                        if (isset($this->progetti[$key]))
                            $form->addCheckbox("pr_id_" . $key, $key, TRUE, null, null, $val . "<br />");
                        else
                            $form->addCheckbox("pr_id_" . $key, $key, FALSE, null, null, $val . "<br />");
                        $this->controllo["pr_id_" . $key]["obbligo"] = 0;
                    }
                }

                $form->addHTML("</div><label>" . getConstVar("BIBLIO_CHOOSE_PROGETTO") . "</label></div>");
                $cont_tmp=$form->addField($this->bib["bi_piva"],null,null,"<label>".getConstVar("BIBLIO_INSER_PI_CF")."</label>");
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_cf"], null, null, "<label>" . getConstVar("BIBLIO_INSER_PI_CF") . "</label>");
                $cont_tmp = $form->addField($this->bib["bi_tel"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_fax"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_url"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->close();
                $passo.=$form->restituisci();
                $passo.=$this->reg_buttons($step);
                break;

            case 3:
                $passo.="					<h4>
<span class=\"bold\">" . getConstVar("BIBLIO_INSER_DATICATALOGO") . "</span>
<br />
<br />";
                $passo .= "</h4>
<div id=\"registration_feedback\" class=\"red\"></div>
";
                $form = new FieldsForm("form_reg_lib");

                if ($this->bib["bi_opac"]->value != "") {
                    $checked = TRUE;
                    $disabDiv = "";
                    $disabForm = "";
                } else {
                    $checked = FALSE;
                    $disabDiv = "_disabled";
                    $disabForm = "disabled=\"disabled\"";
                }
                $form->addCheckbox("bi_opac_check", "bi_opac_check", $checked, "onclick=\"javascript:enable_cat('bi_opac')\"");

                $form->addHTML(getConstvar("meta_opac"));

                $form->addHTML("		<div class=\"ca_nome" . $disabDiv . "\" id=\"ca_nome_bi_opac\">");
                $cont_tmp = $form->addField($this->bib["bi_opac"], $disabForm);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                foreach ($this->DATAcat as $key => $v)
                    if ($this->DATAcat[$key]["ca_tabella"] == NULL) {
                        $form->addHTML("<div class=\"" . $this->DATAcat[$key]["ca_nome"] . "\"><div class=\"form_obj_name\">" . getConstvar($this->DATAcat[$key]["ca_nome"]) . "<span class=\"colon\">: </span></div>");
                        $form->addHTML("	<span>");
                        $form->addHTML(getConstVar("NO") . " ");
                        $form->addRadio($this->DATAcat[$key]["ca_nome"], 0, !$this->DATAcat[$key]["partecipa"], $disabForm);
                        $form->addHTML(getConstVar("SI") . " ");
                        $form->addRadio($this->DATAcat[$key]["ca_nome"], 1, $this->DATAcat[$key]["partecipa"], $disabForm);
                        if ($this->DATAcat[$key]["ca_nome"] == "MAI")
                            $form->addHTML("				<label>" . getConstVar("BIBLIO_MAI") . "</label>");

                        $form->addHTML("	</span>");
                        $form->addHTML("</div>");
                        $cont_tmp[$this->DATAcat[$key]["ca_nome"]]["obbligo"] = 0;
                        $this->controllo = array_merge($this->controllo, $cont_tmp);
                    }
                $form->addHTML("				<br />
</div>
");
                foreach ($this->DATAcat as $key => $v) {
                    $tabella = $this->DATAcat[$key]["ca_tabella"];
                    $nome = $this->DATAcat[$key]["ca_nome"];
                    $attivo = strtolower($nome) . "_attivo";                  
                    if ($this->DATAcat[$key]["ca_tabella"] != NULL) {
                        if ($this->DATAcat[$key]["partecipa"]) {
                            $checked = TRUE;
                            $disabDiv = "";
                            $disabForm = "";
                        } else {
                            $checked = FALSE;
                            $disabDiv = "_disabled";
                            $disabForm = "disabled=\"disabled\"";
                        }
                        $form->addCheckbox($nome . "_check", $nome . "_check", $checked, "onclick=\"javascript:enable_cat('" . $nome . "')\"");
                        $form->addHTML($nome);
                        $form->addHTML("		<div class=\"ca_nome" . $disabDiv . "\" id=\"ca_nome_" . $nome . "\">");
                        foreach ($this->DATAcat[$key]["tabella"] as $key_tab => $v_tab)
                            if (strpos($this->DATAcat[$key]["tabella"][$key_tab]->fieldname, "_cod")) {
                                $linkatt = "";
                                $extra = "";
                                if ($this->DATAcat[$key]["tabella"][$key_tab]->fieldname == strtolower($nome) . "_cod") {
                                    if ($this->DATAcat[$key]["tabella"][$attivo]->value == 1) {
                                        $label = strtolower($nome) . "_label_att";
                                        $linkatt = "<label id=\"$label\">" . getConstVar("LINKATTIVO") . "</label>";
                                        $extra = " onchange=\"javascript:reset_link_label('$label');\"";
                                    }
                                    else
                                        $linkatt = "";
                                }
                                $cont_tmp = $form->addField($this->DATAcat[$key]["tabella"][$key_tab], $disabForm . $extra);
                                $form->addHTML($linkatt);
                                $this->controllo = array_merge($this->controllo, $cont_tmp);
                            }
                        $form->addHTML("</div>");
                    }
                }
                $form->addHTML($this->reg_buttons($step));
                $form->close();
                $passo.=$form->restituisci();
                break;

            case 4:
                $passo.="<h4>
                        <span class=\"bold\">" . getConstVar("BIBLIO_INSER_DATISERVIZIO") . "</span>
                        <br />
                        <br />
                        " . getConstVar("BIBLIO_SPEC_MODALITA") . "
                        </h4>
                        <div id=\"registration_feedback\" class=\"red\"></div>";
                $form = new FieldsForm("form_reg_lib");
                $form->addHTML("<h3 class=\"big2\">" . getConstVar("BIBLIO_SERVIZIODD") . "</h3><br />");
                $cont_tmp = $form->addField($this->bib["bi_referente_dd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_email_dd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_tel_dd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_costo_ute_dd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_costo_dd"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $cont_tmp = $form->addField($this->bib["bi_scompenso"]);
                $this->controllo = array_merge($this->controllo, $cont_tmp);
                $form->addHTML("<div class=\"fixed_values\">
                    
                                </div>");
                if ($this->DATAcat[1]["partecipa"] == FALSE) {
                    $cont_tmp = $form->addField($this->bib["bi_condforni"]);
                    $this->controllo = array_merge($this->controllo, $cont_tmp);
                } else {
                    $form->addHTML("<div class=\"bi_condforni\">
                                    <a href=\"" . getConstVar("ACNP_CONDFORNI_URL") . $this->DATAcat[1]["tabella"]["acnp_cod"]->value . "\" target=\"_blank\">" . getConstVar("BIBLIO_PAGINA_ACNP") . "</a>
                                    </div>");
                }
                $form->addHTML(getConstVar("REG_REGOLAMENTO") . "</div>");
                $form->close();
                $passo.=$form->restituisci();
                $passo.=$this->reg_buttons($step);
                break;

            case 5:
                $passo.="<h4>
                        <span class=\"bold\">" . getConstVar("BIBLIO_ACC_CONDIZIONI") . "</span>";
               $passo.="</h4>
                        <div id=\"registration_feedback\" class=\"red\"></div>";
                $year = getConstVar("SUBSCRIPTION_YEAR");
                $tipo_sottoscrizione = getConstVar('LIBRARY');
                $form = new FieldsForm("form_reg_lib");
                $form->addHTML("
                                <div class=\"general_service_conditions\">
                                <span class=\"obj_name\">
                                <h3 class=\"big2\">" . getConstVar("GEN_COND_NILDE") . "</h3>
                                </span>
                                <span>
                                ");
                if ($this->isPublicLibrary()) {
                    $contratto_txt = "contBIBLIO/cond_servizio_biblio" . $year . ".txt";
                } else {
                    $contratto_txt = "contPRIV/cond_servizio_privata" . $year . ".txt";
                }
                $file_contratto = "contents/lang/" . $_SESSION['lang'] . "/contratto/" . $year . "/$contratto_txt";
                $handle = fopen($file_contratto, "r");
                if ($handle)
                    $contratto = fread($handle, filesize($file_contratto));
                else
                    $contratto = "Error !";
                fclose($handle);
                $form->addTextArea("contratto", $contratto, null, null, "readonly=\"readonly\"");
                $form->addHTML("<br /><br />");
                $form->addRadio("radio_accetta", FALSE, TRUE);
                $form->addHTML(getConstVar("NONACCETTO"));
                $form->addHTML("						<br />");
                $form->addRadio("radio_accetta", TRUE, FALSE);
                $form->addHTML(getConstVar("ACCETTO"));
                $cont_tmp["radio_accetta"]["obbligo"] = 1;
                $this->controllo = array_merge($this->controllo, $cont_tmp);                
                $form->addHTML($this->reg_buttons($step));
                $form->close();

                $passo.=$form->restituisci();
                break;

            case 6:
                if ($this->isPublicLibrary()) {
                    $msg_step6 = getConstVar("BIBLIO_REG_COMPLETE");
                } else {
                    $msg_step6 = getConstVar("BIBLIO_REG_COMPLETE_PVT");
                }
                $passo.="<h4><span class=\"bold green\">" . $msg_step6 . "</span></h4>
                        <br />
                        <h4>
                        " . getConstVar("RIEPILOGO_SOTT") . "
                        <br />
                        </h4>";
                break;
        }

        $passo.="<br class=\"clear_float\" />
                 </div> <!-- END: reg_step -->";

        return $passo;
    }

    public function reg_buttons($step) {
        $button = "";
        $button.=getConstVar("REQUIRED_FIELDS");
        if (1 < $step) {
            $prev = $step - 1;
            $button.= "<a id=\"big_left_arrow_btn_v\" class=\"back_btn float_l\" href=\"javascript:prev_step(" . $prev . ")\" title=\"\">
                        <img class=\"big_btn_r\" src=\"images/mynilde/empty1.gif\" alt=\"\" />
                        <span class=\"\">
                        <font>" . getConstVar("INDIETRO") . "</font>
                        </span>
                        <img class=\"big_btn_l\" src=\"images/mynilde/empty1.gif\" alt=\"\" />
                        </a>";
        }
        $next = $step + 1;
        $button.= "<a id=\"big_right_arrow_btn_v\" class=\"float_l btn btn-warning\" href=\"javascript:next_step(" . $next . ")\" title=\"\">
                    <img class=\"big_btn_r\" src=\"images/mynilde/empty1.gif\" alt=\"\" />
                    <span>
                    <font>" . getConstVar("PROSEGUI") . "</font>
                    </span>
                    <img class=\"big_btn_l\" src=\"images/mynilde/empty1.gif\" alt=\"\" />
                    </a>";
        return $button;
    }

    public function set_parse($fieldname, $post) {
        $flds = new ParserField($post);
        $value = $flds->simple_parse($this->bib[$fieldname]);
        $this->bib[$fieldname]->value = $value;
    }

    public function aggiorna($post) {
        $coppie = "";
        $noinsert = array();
        $noinsert["bi_usr"] = TRUE;
        if (isset($_SESSION['from3to4']) && $_SESSION['from3to4'])
            $noinsert["en_id"] = FALSE;
        else
            $noinsert["en_id"] = TRUE;
        $noinsert["pr_id"] = TRUE;
        $noinsert["na_id"] = TRUE;
        $noinsert["ti_id"] = TRUE;
        if (isset($post['bi_nome']))
            $post['bi_nome'] = ucfirst($post['bi_nome']);
        if (isset($post['bi_indi']))
            $post['bi_indi'] = ucfirst(strtolower($post['bi_indi']));
        if (isset($post['bi_citta']))
            $post['bi_citta'] = ucwords(strtolower($post['bi_citta']));

        $dbfields = new ParserField($post);
        if ($post["bi_pwd"] != "" && $post["bi_pwd"] != NULL && $post["bi_pwd"] == $post["bi_pwd_2"])
            $coppie.="bi_pwd=" . $dbfields->parse($this->bib["bi_pwd"]);
        else
            $noinsert["bi_pwd"] = TRUE;

        foreach ($this->biblio_account_fields as $k => $v) {
            $key = $this->biblio_account_fields[$k]->fieldname;
            if (!isset($noinsert[$key]) || !$noinsert[$key]) {
                if ($coppie != "")
                    $coppie.=",";
                $coppie.=$key . "=" . $dbfields->parse($this->bib[$key]);
            }
        }

        if ($coppie != "") {
            $query = "UPDATE biblio SET $coppie WHERE bi_id=" . $this->bib["bi_id"]->value;
            $result = $_SESSION['db']->execute($query);
            if (!$result)
                return false;
        }

        return true;
    }

    public function aggiornaService($post) {
        $noinsert = array();
        $noinsert["bi_costo_dd"] = TRUE;
        $noinsert["bi_scompenso"] = TRUE;
        $coppie = "";
        if (isset($post['bi_referente_dd']))
            $post['bi_referente_dd'] = ucwords(strtolower($post['bi_referente_dd']));
        if (isset($post['bi_referente_ill']))
            $post['bi_referente_ill'] = ucwords(strtolower($post['bi_referente_ill']));
        $dbfields = new ParserField($post);
        foreach ($this->biblio_service_fields as $k => $v) {
            $key = $this->biblio_service_fields[$k]->fieldname;
            $v = $this->biblio_service_fields[$k]->value;
            if (!isset($noinsert[$key]) || !$noinsert[$key]) {
                if ($coppie != "")
                    $coppie.=",";
                $coppie.=$key . "=" . $dbfields->parse($this->bib[$key]);
            }
        }
        if ($coppie != "") {
            $query = "UPDATE biblio SET $coppie WHERE bi_id=" . $this->bib["bi_id"]->value;
            $result = $_SESSION['db']->execute($query);

            if (!$result)
                return false;
        }
        return true;
    }

    public function aggiornaCat($post) {
        $result = null;
        $this->aggiornaObject_cat($post);
        if (isset($post["bi_opac"]) && isset($post["bi_opac_check"])) {
            $dbfields = new ParserField($post);
            $upd_opac = "UPDATE biblio SET bi_opac=" . $dbfields->parse($this->bib["bi_opac"]) . " WHERE bi_id=" . $this->bi_id;
            $result = $_SESSION['db']->execute($upd_opac);
        } else {
            $upd_opac = "UPDATE biblio SET bi_opac='' WHERE bi_id=" . $this->bi_id;
            $result = $_SESSION['db']->execute($upd_opac);
            $del_mai = "DELETE FROM biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=3";
            $result = $_SESSION['db']->execute($del_mai);
        }

        $valori_cat = "";

        foreach ($this->DATAcat as $key_cat => $v_cat) {
            if (isset($this->DATAcat[$key_cat]["partecipa"]) && $this->DATAcat[$key_cat]["partecipa"] == TRUE) {
                if ($this->DATAcat[$key_cat]["tabella"] != NULL) {
                    $attributi_tab = "";
                    $valori_tab = "";
                    $upd = "";
                    foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                        if ($attributi_tab != "")
                            $attributi_tab.=",";
                        if ($valori_tab != "")
                            $valori_tab.=",";
                        if ($upd != "")
                            $upd.=",";

                        $attributi_tab.=$key_tab;

                        if ($key_tab != "bi_id") {
                            $v = ParserField::quote($v_tab);
                            $valori_tab.=$v;
                            $upd.=$key_tab . "=" . $v;
                        }
                    }
                    $query_tab = "INSERT INTO " . $this->DATAcat[$key_cat]["ca_tabella"] . " (" . $attributi_tab . ") VALUES (" . $this->bi_id . "," . $valori_tab . ") ON DUPLICATE KEY UPDATE $upd";
                    $result = $_SESSION['db']->execute($query_tab);
                }

                if (isset($post[$this->DATAcat[$key_cat]["ca_nome"]])) {
                    if ($post[$this->DATAcat[$key_cat]["ca_nome"]] == 1) {
                        $sel = $_SESSION['db']->execute("select bi_id from biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=" . $key_cat);
                        if ($_SESSION['db']->numrows($sel) == 0) {
                            $valori_cat = "(" . $this->bi_id . "," . $key_cat . ")";
                            $query_cat = "INSERT INTO biblio_catalogo (bi_id,ca_id) VALUES " . $valori_cat . "";
                            $result = $_SESSION['db']->execute($query_cat);
                        }
                    } else {
                        if (isset($post[$this->DATAcat[$key_cat]["ca_nome"]]) && $post[$this->DATAcat[$key_cat]["ca_nome"]] == 0) {
                            $queryd_cat = "DELETE FROM biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=" . $key_cat;
                            $result = $_SESSION['db']->execute($queryd_cat);
                        }
                    }
                } else {
                    $sel = $_SESSION['db']->execute("select bi_id from biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=" . $key_cat);
                    if ($_SESSION['db']->numrows($sel) == 0) {
                        $valori_cat = "(" . $this->bi_id . "," . $key_cat . ")";
                        $query_cat = "INSERT INTO biblio_catalogo (bi_id,ca_id) VALUES " . $valori_cat . "";
                        $result = $_SESSION['db']->execute($query_cat);
                    }
                }
            } else {
                $queryd_cat = "DELETE FROM biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=" . $key_cat;
                $result = $_SESSION['db']->execute($queryd_cat);

                if ($this->DATAcat[$key_cat]["ca_tabella"] != "") {
                    $queryd_cat2 = "DELETE FROM " . $this->DATAcat[$key_cat]["ca_tabella"] . " WHERE bi_id=" . $this->bi_id;
                    $result2 = $_SESSION['db']->execute($queryd_cat2);
                }
            }
        }
        return true;
    }

    public function resetCataloghi() {
        $catlist = "";
        $resc = $_SESSION['db']->execute("SELECT ca_id FROM gruppo_catalogo WHERE gr_id=1");
        if ($resc && $_SESSION['db']->numrows($resc) > 0) {
            while ($c = $_SESSION['db']->fetch($resc)) {
                if ($catlist != "")
                    $catlist.=",";
                $catlist.=$c["ca_id"];
            }
            if ($catlist != "")
                $catlist = "WHERE ca_id IN ($catlist)";
        }
        $result_allcat = $_SESSION['db']->execute("SELECT * FROM catalogo $catlist");
        while ($row = $_SESSION['db']->fetch($result_allcat)) {
            $key = $row["ca_id"];
            $nome_cat = $row["ca_nome"];
            $tabella_cat = $row["ca_tabella"];
            $url_cat = $row["ca_url"];
            $this->DATAcat[$key]["ca_nome"] = $nome_cat;
            $this->DATAcat[$key]["bc_attivo"] = 0;
            $this->DATAcat[$key]["partecipa"] = FALSE;
            $this->DATAcat[$key]["ca_tabella"] = $tabella_cat;
            if ($this->DATAcat[$key]["ca_tabella"] != NULL)
                $this->DATAcat[$key]["tabella"] = $_SESSION['db']->fetch_fields($tabella_cat);
            else
                $this->DATAcat[$key]["tabella"] = NULL;
        }
    }

    public function aggiornaDB() {
        $coppie = "";
        $noinsert = array();
        $noinsert["bi_usr"] = TRUE;
        $noinsert["bi_pwd"] = TRUE;
        $biblio_fields = array_merge($this->biblio_account_fields, $this->biblio_service_fields);
        $noinsert["bi_costo_ute_dd"] = TRUE;
        $noinsert["bi_costo_dd"] = TRUE;
        $noinsert["bi_scompenso"] = TRUE;
        $noinsert["bi_condforni"] = TRUE;
        $noinsert["bi_sospini"] = TRUE;
        $noinsert["bi_sospfine"] = TRUE;
        $noinsert["bi_preavvisososp"] = TRUE;
        if ($this->isPublicLibrary())
            $coppie = 'bi_stato=' . getConstVar('BIBLIO_ABI');
        else
            $coppie = 'bi_stato=' . getConstVar('BIBLIO_ABI_ATTESAFAX');
        $coppie.=',bi_opac=\'' . addslashes($this->bi_opac) . '\'';
        foreach ($biblio_fields as $k => $v) {
            $key = $biblio_fields[$k]->fieldname;
            $value = $biblio_fields[$k]->value;
            if (!isset($noinsert[$key]) || !$noinsert[$key]) {
                $coppie.="," . $key . "='" . addslashes($value) . "'";
            }
        }
        if ($coppie != "") {
            $query = "UPDATE biblio SET $coppie WHERE bi_id=" . $this->bib["bi_id"]->value;
            $result = $_SESSION['db']->execute($query);
            if (!$result)
                return false;
        }
        $valori_insertORupdate_cat = null;
        $valori_delete_cat = null;
        foreach ($this->DATAcat as $key_cat => $v_cat) {
            if ($this->DATAcat[$key_cat]["partecipa"] == TRUE) {
                $valori_insertORupdate_cat.="(" . $this->bi_id . "," . $key_cat . "),";
                if ($this->DATAcat[$key_cat]["tabella"] != NULL) {
                    $attributi_tab_insertORupdate = "";
                    $valori_tab_insertORupdate = "";
                    $query_update_cat = "bi_id=" . $this->bi_id . ",";
                    foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                        $attributi_tab_insertORupdate.=$key_tab . ",";
                        if ($key_tab != "bi_id") {
                            $val_tab = ParserField::quote($this->DATAcat[$key_cat]["tabella"][$key_tab]);
                            $valori_tab_insertORupdate.=$val_tab . ",";
                            $query_update_cat.=$key_tab . "=" . $val_tab . ",";
                        }
                    }
                    $attributi_tab_insertORupdate = substr($attributi_tab_insertORupdate, 0, -1);
                    $valori_tab_insertORupdate = substr($valori_tab_insertORupdate, 0, -1);

                    $query_update_cat = substr($query_update_cat, 0, -1);
                    $query_tab = "INSERT INTO " . $this->DATAcat[$key_cat]["ca_tabella"] . " (" . $attributi_tab_insertORupdate . ") VALUES (" . $this->bi_id . "," . $valori_tab_insertORupdate . ") ON DUPLICATE KEY UPDATE $query_update_cat";
                    $result_tab = $_SESSION['db']->execute($query_tab);
                }
            } else {
                $valori_delete_cat.="(bi_id=" . $this->bi_id . " AND ca_id=" . $key_cat . ") OR ";
                if ($this->DATAcat[$key_cat]["tabella"] != NULL) {
                    $query_DELETE_cat = "DELETE FROM " . $this->DATAcat[$key_cat]["ca_tabella"] . " WHERE bi_id=" . $this->bi_id;
                    $result_del_cat = $_SESSION['db']->execute($query_DELETE_cat);
                }
            }
        }
        if ($this->DATAcat[3]["partecipa"] == 1) {
            $query_cat_MAI = "INSERT INTO biblio_catalogo (bi_id,ca_id) VALUES (" . $this->bi_id . ",3) ON DUPLICATE KEY UPDATE bi_id=bi_id, ca_id=ca_id";
        } else {
            $query_cat_MAI = "DELETE FROM biblio_catalogo WHERE bi_id=" . $this->bi_id . " AND ca_id=3";
        }
        $result_cat_MAI = $_SESSION['db']->execute($query_cat_MAI);
        if ($valori_insertORupdate_cat != null) {
            $valori_insertORupdate_cat = substr($valori_insertORupdate_cat, 0, -1);
            $query_cat = "INSERT INTO biblio_catalogo (bi_id,ca_id) VALUES " . $valori_insertORupdate_cat . " ON DUPLICATE KEY UPDATE bi_id=bi_id, ca_id=ca_id";
            $result_cat = $_SESSION['db']->execute($query_cat);
        }
        if ($valori_delete_cat != null) {
            $valori_delete_cat = substr($valori_delete_cat, 0, -3);
            $query_cat = "DELETE FROM biblio_catalogo WHERE $valori_delete_cat";
            $result_cat = $_SESSION['db']->execute($query_cat);
        }
        $coppie_pr = '';
        foreach ($this->progetti as $pr_id => $pr_name) {
            $coppie_pr.="(" . $this->bi_id . ",$pr_id),";
        }
        $coppie_pr = substr($coppie_pr, 0, -1);
        $query_DELETE_prog = "DELETE FROM biblio_progetto WHERE bi_id=" . $this->bi_id;
        $result_pr_delete = $_SESSION['db']->execute($query_DELETE_prog);
        if (sizeof($this->progetti) != 0) {
            $query_INSERT_prog = "INSERT INTO biblio_progetto (bi_id,pr_id) VALUES $coppie_pr";
            $result_pr_insert = $_SESSION['db']->execute($query_INSERT_prog);
        }
        return $this->bib["bi_id"]->value;
    }

    public function aggiornaObject($post) {
        foreach ($this->controllo as $key => $v) {
            if (isset($this->bib[$key])) {
                $this->bib[$key]->value = isset($post[$key]) ? $post[$key] : null;
            }
        }
        if ($this instanceof BiblioSubscription) {
            if (isset($post['anno_fatt']))
                $this->anno_fatt = $post['anno_fatt'];
            if (isset($post['fatt_referente']))
                $this->fatt_referente = $post['fatt_referente'];
            if (isset($post['fatt_referente_mail']))
                $this->fatt_referente_mail = $post['fatt_referente_mail'];
            if (isset($post['fatt_referente_tel']))
                $this->fatt_referente_tel = $post['fatt_referente_tel'];
            if (isset($post['fatt_altrenote']))
                $this->fatt_altrenote = $post['fatt_altrenote'];
            if (isset($post['fatt_data_ordine_gg']) && isset($post['fatt_data_ordine_mm']) && isset($post['fatt_data_ordine_aaaa']))
                $this->fatt_data_ordine=build_date($post['fatt_data_ordine_aaaa'], $post['fatt_data_ordine_mm'], $post['fatt_data_ordine_gg']);
           if (isset($post['fatt_cig']))
                $this->fatt_cig=$post['fatt_cig'];
           if (isset($post['fatt_cartacea']))
                $this->fatt_cartacea=$post['fatt_cartacea'];

        }

        return TRUE;
    }

    public function aggiornaObject_cat($post) {
        foreach ($this->DATAcat as $key => $v) {
            if ($this->DATAcat[$key]["ca_tabella"] == NULL) {
                $this->DATAcat[$key]["partecipa"] = isset($post[$this->DATAcat[$key]["ca_nome"]]) ? $post[$this->DATAcat[$key]["ca_nome"]] : null;
            } else {
                        if (isset($post[$this->DATAcat[$key]["ca_nome"] . "_check"])) {
                    $this->DATAcat[$key]["partecipa"] = TRUE;
                    foreach ($this->DATAcat[$key]["tabella"] as $campo => $v_2) {
                        if (isset($post[$campo]))
                            $this->DATAcat[$key]["tabella"][$campo]->value = $post[$campo];
                    }
                }
                else {
                    $this->DATAcat[$key]["partecipa"] = FALSE;
                }
            }
        }
        return TRUE;
    }

    public function aggiornaObject_progetti($post) {
        $this->tipo_ente = isset($post["ti_id"]) ? $post["ti_id"] : null;
        $this->progetti = array();
        foreach ($this->controllo as $key => $v) {
            if ((strpos($key, "pr_id_") !== FALSE) && isset($post[$key])) {
                $this->progetti[$post[$key]] = $this->prog_arr[$post[$key]];
            }
        }
        return TRUE;
    }

    public function sel_ente($idtipoen,$naz=null) {
		$form= new FieldsForm();

        $primo[""] = getConstVar("OPTION_FIRST_LINE");
        $wh="ti_id=".$idtipoen;
        if($naz!="") $wh.=" AND na_id='$naz' AND en_id<>".getConstVar("ID_AUTHORG");  
		$this->en_arr=db_elenco("en_id",$wh);
        if ($this->en_arr)
            $this->en_arr = $primo + $this->en_arr;
        if (isset($_SESSION['from3to4']) && $_SESSION['from3to4']) {
            $options = "\n";
        }
        else
            $options="<label>".getConstVar("BIBLIO_CHOOSE_ENTE_APPART")."</label>\n";

        $cont_tmp = $form->addFieldSelect($this->bib["en_id"], null, $this->en_arr, 1, null, null, $options);
        if ($this->controllo)
            $this->controllo = array_merge($this->controllo, $cont_tmp);
        else
            $this->controllo = $cont_tmp;

        return $form->restituisci();
    }

    public function sel_tipoente($naid) {
        $form = new FieldsForm();
        $primo[""] = getConstVar("OPTION_FIRST_LINE");
        $this->en_arr = db_elenco("ti_id", "na_id='$naid'");
        $this->en_arr = $primo + $this->en_arr;
        $form->addHTML("<div class=\"ti_id\" ><div class=\"form_obj_name\">" . getConstVar("ti_id") . "<span class=\"colon\">: </span></div>");
        if (isset($_SESSION['from3to4']) && $_SESSION['from3to4']) {
            $options = "<font class=\"redstar\">" . getConstVar("CAMPO_OBBLIGATORIO") . "</font></span>";
        }
        else
            $options = "<font class=\"redstar\">".getConstVar("CAMPO_OBBLIGATORIO")."</font></span><label>".getConstVar("BIBLIO_CHOOSE_ENTE")."</label>";
        $cont_tmp = $form->addSelect("ti_id", null, $this->en_arr, $this->tipo_ente, null, "onchange=\"javascript:updateInst()\"", "<span>", $options);
        $form->addHTML("</div>");
        return $form->restituisci();
    }

    public function sel_prov($idnation) {
        $form = new FieldsForm();
        $primo[""] = getConstVar("OPTION_FIRST_LINE");
        $this->prov_arr = db_elenco("prov_id", "na_id='" . $idnation . "'");
        $this->prov_arr = array_merge($primo, $this->prov_arr);      
        $cont_tmp = $form->addFieldSelect($this->bib["prov_id"], null, $this->prov_arr, 1, null, null);
        $this->controllo = array_merge($this->controllo, $cont_tmp);
        return $form->restituisci();
    }

    public function inserisciDB() {
        if ($this->isPublicLibrary()) 
            $this->bib['bi_stato']->value = getConstVar('BIBLIO_NUOVA');
        else
            $this->bib['bi_stato']->value = getConstVar('BIBLIO_NUOVA_ATTESA_FAX');
        $attributi_bib = "";
        $valori_bib = "";
        foreach ($this->bib as $key => $v)
            if ($key != "bi_id") {
                if ($attributi_bib != "")
                    $attributi_bib.=",";
                if ($valori_bib != "")
                    $valori_bib.=",";

                $attributi_bib.=$key;
                $valori_bib.=ParserField::quote($this->bib[$key]);
            }

        if ($attributi_bib != "") {
            $query = "INSERT INTO biblio ($attributi_bib) VALUES ($valori_bib)";
            $result = $_SESSION['db']->execute($query);
            $idbib = $_SESSION['db']->last_id();
            if ($result) {
                $_SESSION['db']->execute("INSERT INTO biblio_gruppo (bi_id, gr_id) VALUES (" . $idbib . ",1)");
                if ($this->progetti != NULL) {
                    $valori_prog = "";
                    foreach ($this->progetti as $key => $value) {
                        if ($valori_prog != "")
                            $valori_prog.=",";
                        $valori_prog.="(" . $idbib . "," . $key . ")";
                    }
                    $_SESSION['db']->execute("INSERT INTO biblio_progetto (bi_id, pr_id) VALUES $valori_prog");
                }
                $valori_cat = "";
                foreach ($this->DATAcat as $key_cat => $v_cat) {
                    if ($this->DATAcat[$key_cat]["partecipa"] == TRUE) {
                        if ($valori_cat != "")
                            $valori_cat.=",";
                        $valori_cat.="(" . $idbib . "," . $key_cat . ")";
                        if ($this->DATAcat[$key_cat]["tabella"] != NULL) {
                            $attributi_tab = "";
                            $valori_tab = "";
                            foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                                if ($attributi_tab != "")
                                    $attributi_tab.=",";
                                if ($valori_tab != "")
                                    $valori_tab.=",";
                                $attributi_tab.=$key_tab;
                                if ($key_tab != "bi_id")
                                    $valori_tab.=ParserField::quote($this->DATAcat[$key_cat]["tabella"][$key_tab]);
                            }
                            $query_tab = "INSERT INTO " . $this->DATAcat[$key_cat]["ca_tabella"] . " (" . $attributi_tab . ") VALUES (" . $idbib . "," . $valori_tab . ")";
                            $result_tab = $_SESSION['db']->execute($query_tab);
                        }
                    }
                }
                if ($valori_cat != "") {
                    $query_cat = "INSERT INTO biblio_catalogo (bi_id,ca_id) VALUES " . $valori_cat . "";
                    $result_cat = $_SESSION['db']->execute($query_cat);
                }
                return $idbib;
            }
	    else return FALSE;
        }
	else return FALSE;
    }

    function inserisciStoricoSottoscrizioni($id_bib) {
        if ($this instanceof BiblioSubscription) {
            $_importo = $this->importo;
            $_pagato = 0;
            $_opzione_data_fattura = $this->anno_fatt;
            if (!$this->bi_notefatt)
                $_intestazione_fattura = addslashes($this->bi_nome . ",\n" . $this->bi_indi . " - " . $this->bi_citta . " (" . $this->prov_id . ")\n" . $this->bi_cap . " - " . $this->na_id . "\nP.IVA: " . $this->bi_piva . "\nCF: " . $this->bi_cf);
            else {
                $_intestazione_fattura = addslashes($this->bi_notefatt);
            }
            $_nuova_biblio = 0;
        } else {
            $_importo = 0.00;
            $_pagato = 1;
            $_opzione_data_fattura = 'null';
            $_intestazione_fattura = 'null';
            $_nuova_biblio = 1;
        }
        $_anno = getConstVar('SUBSCRIPTION_YEAR');
        $_condforni = isset($this->bi_condforni) ? addslashes($this->bi_condforni) : 'null';
        $_scompenso = isset($this->bi_scompenso) ? $this->bi_scompenso : 'null';
        $_costo_dd = isset($this->bi_costo_dd) ? $this->bi_costo_dd : 'null';
        $_costo_ill = isset($this->bi_costo_ill) ? $this->bi_costo_ill : 'null';
        $tipo_sottoscrizione = $this->kindOfContractLibrary($_anno);
        if ($this instanceof BiblioSubscription)
            $this->tipo_sottoscrizione = $tipo_sottoscrizione;
        switch ($tipo_sottoscrizione) {
            case getConstVar('ADERENTE'):
                if ($this instanceof BiblioSubscription) {
                    $_tipo_contratto = 2;
                    $_nome_contratto = "cond_servizio_aderente";
                } else {
                    if ($this->isPublicLibrary()) {
                        $_tipo_contratto = 1;
                        $_nome_contratto = "cond_servizio_biblio";
                    } else {
                        $_tipo_contratto = 0;
                        $_nome_contratto = "cond_servizio_privata";
                    }
                }
                break;
            case getConstVar('SINGOLA_PUB'):
                $_tipo_contratto = 1;
                $_nome_contratto = "cond_servizio_biblio";
                break;
            case getConstVar('SINGOLA_PVT'):
                $_tipo_contratto = 0;
                $_nome_contratto = "cond_servizio_privata";
                break;
            case getConstVar('ADERENTE_ANNO_PRECEDENTE'):
                if ($this->isPublicLibrary()) {
                    $_tipo_contratto = 1;
                    $_nome_contratto = "cond_servizio_biblio";
                } else {
                    $_tipo_contratto = 0;
                    $_nome_contratto = "cond_servizio_privata";
                }
                break;
        }
        $fatt_referente="";
        $fatt_referente_mail="";
        $fatt_referente_tel="";
        $fatt_altrenote="";
        $fatt_data_ordine="";
        $fatt_cig="";
        $fatt_cartacea=0;
        if($this instanceof BiblioSubscription)
        {
            $this->tipo_sottoscrizione=$tipo_sottoscrizione;

            $fatt_referente=SQL_escape($this->fatt_referente);
            $fatt_referente_mail=$this->fatt_referente_mail;
            $fatt_referente_tel=$this->fatt_referente_tel;
            $fatt_altrenote=SQL_escape($this->fatt_altrenote);
            $fatt_data_ordine= $this->fatt_data_ordine;
            $fatt_cig=$this->fatt_cig;
            $fatt_cartacea=$this->fatt_cartacea;
        }
        $_nome_contratto=$id_bib."-".$_nome_contratto.$_anno;
        $this->nomecontratto=$_nome_contratto;
        $query = "INSERT INTO storico_sottoscrizioni     (bi_id,bi_en,st_pagato,st_importo,st_intestazione_fattura,st_data_opzione_fattura,st_anno_contratto,st_tipo_contratto,st_nome_contratto,st_nuova_biblio,bi_condforni,bi_scompenso,bi_costo_dd,bi_costo_ill,st_fatt_referente,st_fatt_referente_mail,st_fatt_referente_tel,st_fatt_altrenote, st_fatt_data_ordine,st_fatt_cig,st_fatt_cartacea) VALUES
                ($id_bib,".$this->en_id.",$_pagato,$_importo,'$_intestazione_fattura',$_opzione_data_fattura,$_anno,$_tipo_contratto,'$_nome_contratto',$_nuova_biblio,'$_condforni','$_scompenso','$_costo_dd','$_costo_ill','$fatt_referente','$fatt_referente_mail','$fatt_referente_tel','$fatt_altrenote','$fatt_data_ordine','$fatt_cig','$fatt_cartacea')";
        $_SESSION['db']->execute($query);
    }

    function modifica($action = "action_biblio.php") {
        $this->bib["bi_pwd"]->value = "";
        $this->bi_pwd_2->value = "";
        $this->bib["bi_pwd"]->isnull = TRUE;
        $this->bi_pwd_2->isnull = TRUE;
        $this->form($action, FALSE);
    }

    function form($action) {
        $this->reset_controllo();
        $form = new FieldsForm("form_library", "javascript:modificaBiblioAccount('$action')");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value, null, "\n");
        $form->addHidden("op", "update", null, "\n");
        $form->addHTML("<div id=\"library_identity\">");
        $this->edit_library_identity($form);
        $form->addHTML("</div>");
        $form->addHTML("<div id=\"library_contacts\">");
        $this->edit_library_contacts($form);
        $form->addButton("save_btn", getConstVar("SAVE"), "class=\"submit\" onclick=\"javascript:salvaBiblioAccount()\"");
        if (isset($_SESSION['from3to4']) && !$_SESSION['from3to4']) {
            $form->addHTML("&nbsp;&nbsp;");
            $form->addButton("cancel_btn", getConstVar("CANCEL"), "class=\"submit\" onclick=\"javascript:undoModificaBiblio()\"");
        }
        $form->addHTML(getConstVar("REQUIRED_FIELDS"));
       $form->addHTML("</div>");
        $code_controllo = urlencode(serialize($this->controllo));
        $form->addHidden("controllo", $code_controllo);
        $form->close();
        $form->stampa();
    }

    function formService() {
        $this->reset_controllo();

        $form = new FieldsForm("form_library_serv", "javascript:modificaService('action_biblio.php')");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value, null, "\n");
        $form->addHidden("op", "updateServ", null, "\n");
        $form->addHTML("<div id=\"dd_data\">");
        $this->edit_library_service($form);
        $form->addHTML("</div>");
        $form->addHTML("<div id=\"service_suspension\" class=\"big2\">");
        $this->edit_library_suspension($form);
        $form->addHTML("<br/>");
        $form->addButton("save_btn", getConstVar("SAVE"), "class=\"submit\" onclick=\"javascript:salvaService()\"");
        if (isset($_SESSION['from3to4']) && !$_SESSION['from3to4']) {
            $form->addHTML("&nbsp;&nbsp;");
            $form->addButton("cancel_btn", getConstVar("CANCEL"), "class=\"submit\" onclick=\"javascript:undoModificaService()\"");
        }
        $form->addHTML(getConstVar("REQUIRED_FIELDS"));
        $form->addHTML("</div>");
        $code_controllo = urlencode(serialize($this->controllo));
        $form->addHidden("controllo", $code_controllo);
        $form->close();
        $form->stampa();
    }

    public function edit_library_identity($form) {
        $form->addHTML(FieldRender::renderField($this->bib["bi_usr"]));
        $p = $form->addField($this->bib["bi_pwd"]);
        $this->controllo = array_merge($this->controllo, $p);
        $p2 = $form->addField($this->bi_pwd_2);
        $this->controllo = array_merge($this->controllo, $p2);
        $nome = $form->addField($this->bib["bi_nome"]);
        $this->controllo = array_merge($this->controllo, $nome);
        $primo[""] = getConstVar("OPTION_FIRST_LINE");
        $this->disc_arr = db_elenco("di_id");
        $this->disc_arr = $primo + $this->disc_arr;
        $disc = $form->addFieldSelect($this->bib["di_id"], null, $this->disc_arr, 0, null, null, "\n");
        $this->controllo = array_merge($this->controllo, $disc);
        if (isset($_SESSION['from3to4']) && $_SESSION['from3to4']) {
            $form->addHTML("<div id=\"ti_id_box_sel\" class=\"en_id\">");
            if ($this->bib["na_id"]->value != "")
                $form->addHTML($this->sel_tipoente($this->bib["na_id"]->value));
            $this->controllo["ti_id"]["obbligo"] = 1;
            $form->addHTML("</div>");
            $form->addHTML("<div id=\"en_id_box_sel\" class=\"en_id\" style=\"display:block\">");
		if ($this->tipo_ente != "" && $this->bib["na_id"]->value !="") $form->addHTML($this->sel_ente($this->tipo_ente,$this->bib["na_id"]->value));
            $this->controllo["en_id"]["obbligo"] = 1;
            $form->addHTML("</div>");
	} else
	    $form->addHTML(FieldRender::render("en_id",$this->en_arr[$this->bib["en_id"]->value]));

	foreach ($this->progetti as $key=>$value) {
	    $form->addHTML(FieldRender::render("pr_id",$this->progetti[$key]));
        }

        }

    public function edit_library_contacts($form) {
        $ind = $form->addField($this->bib["bi_indi"]);
        $this->controllo = array_merge($this->controllo, $ind);
        $cap = $form->addField($this->bib["bi_cap"]);
        $this->controllo = array_merge($this->controllo, $cap);
        $form->addField($this->bib["bi_citta"]);
        $form->addHTML(FieldRender::render("na_id", $this->nation_arr[$this->bib["na_id"]->value]));
        $form->addHTML("<div id=\"prov_id_box_sel\" class=\"prov_id\">");
	if ($this->bib["na_id"]->value != "") $form->addHTML($this->sel_prov($this->bib["na_id"]->value));
        $this->controllo["prov_id"]["obbligo"] = 1;
        $form->addHTML("</div>");

        $piva = $form->addField($this->bib["bi_piva"]);
        $this->controllo = array_merge($this->controllo, $piva);
        $cf = $form->addField($this->bib["bi_cf"]);
        $this->controllo = array_merge($this->controllo, $cf);
        $tel = $form->addField($this->bib["bi_tel"]);
        $this->controllo = array_merge($this->controllo, $tel);
        $fax = $form->addField($this->bib["bi_fax"]);
        $this->controllo = array_merge($this->controllo, $fax);
        $url = $form->addField($this->bib["bi_url"]);
        $this->controllo = array_merge($this->controllo, $url);
    }

    public function edit_library_service($form) {
        $ref = $form->addField($this->bib["bi_referente_dd"]);
        $this->controllo = array_merge($this->controllo, $ref);
        $email = $form->addField($this->bib["bi_email_dd"]);
        $this->controllo = array_merge($this->controllo, $email);
        $tel = $form->addField($this->bib["bi_tel_dd"]);
        $this->controllo = array_merge($this->controllo, $tel);
    }

    public function edit_library_conditions($form) {
        $form->addHTML("<h3 class=\"big2\">" . getConstVar("LABEL_BIB_SERVIZIOUTENTI") . "</h3>");
        $costoute = $form->addField($this->bib["bi_costo_ute_dd"]);
        $this->controllo = array_merge($this->controllo, $costoute);
        $form->addHTML("<h3 class=\"big2\">" . getConstVar("LABEL_BIB_SERVIZIOBIBLIO") . "</h3>");
        $form->addHTML(FieldRender::renderField($this->bib["bi_costo_dd"]));
        if (isset($_SESSION['from3to4']) && $_SESSION['from3to4']) {
            if ($this->bib["bi_costo_dd"]->value != 0) {
                $scompenso = $form->addField($this->bib["bi_scompenso"]);
                $this->controllo = array_merge($this->controllo, $scompenso);
            }
            else
                $form->addHTML(FieldRender::renderField($this->bib["bi_scompenso"]));
        } else {
            $form->addHTML(FieldRender::renderField($this->bib["bi_scompenso"]));
        }


        if ($this->DATAcat[1]["partecipa"] == FALSE) {
            $cond = $form->addField($this->bib["bi_condforni"]);
            $this->controllo = array_merge($this->controllo, $cond);
        }
        else
            $form->addHTML("	<div class=\"bi_condforni\">
" . getConstVar("LABEL_CONDFORNI_ACNP") . "<a href=\"http://acnp.cib.unibo.it/cgi-ser/start/it/bib/ss-b.tcl?cod=" . $this->DATAcat[1]["tabella"]["acnp_cod"]->value . "&language=ITALIANO\" target=\"_blank\">ACNP</a>
</div>
");
    }

    public function edit_library_suspension($form) {
        $form->addHTML("<h5 class=\"bigger2\">" . getConstVar("LABEL_SOSP_SERVIZIO") . "</h5>");
        $form->addHTML(getConstVar("DAL").":&nbsp");
        $sospini = $form->addField($this->bib["bi_sospini"]);
        $this->controllo = array_merge($this->controllo, $sospini);
        $form->addHTML("&nbsp&nbsp&nbsp".getConstVar("AL_TIME").":&nbsp");        
        $sospfine = $form->addField($this->bib["bi_sospfine"]);
        $this->controllo = array_merge($this->controllo, $sospfine);
        $form->addHTML(getConstVar("GIORNI_PREAVVISO_SOSPENSIONE"));
        $form->addField($this->bib["bi_preavvisososp"]);
    }

    public function edit_library_cat($form) {
        if ($this->bib["bi_opac"]->value != "") {
            $checked = TRUE;
            $disabDiv = "";
            $disabForm = "";
        } else {
            $checked = FALSE;
            $disabDiv = "_disabled";
            $disabForm = "disabled=\"disabled\"";
        }
        $form->addCheckbox("bi_opac_check", "bi_opac_check", $checked, "onclick=\"javascript:enable_cat('bi_opac')\"");
        $form->addHTML(getConstvar("meta_opac"));
        $form->addHTML("		<div class=\"ca_nome" . $disabDiv . "\" id=\"ca_nome_bi_opac\">");

        $opac = $form->addField($this->bib["bi_opac"], $disabForm, null, null, null, 1);
        $this->controllo = array_merge($this->controllo, $opac);
        foreach ($this->DATAcat as $key => $v) {
            if ($this->DATAcat[$key]["ca_tabella"] == NULL) {
                $form->addHTML("<div class=\"" . $this->DATAcat[$key]["ca_nome"] . "\"><div class=\"form_obj_name\">" . getConstvar($this->DATAcat[$key]["ca_nome"]) . "<span class=\"colon\">: </span></div>");
                $form->addHTML(getConstVar("NO") . " ");
                $form->addRadio($this->DATAcat[$key]["ca_nome"], 0, !$this->DATAcat[$key]["partecipa"], $disabForm);
                $form->addHTML(getConstVar("SI") . " ");
                $form->addRadio($this->DATAcat[$key]["ca_nome"], 1, $this->DATAcat[$key]["partecipa"], $disabForm);
                $form->addHTML("</div>");
                if ($this->DATAcat[$key]["ca_nome"] == "MAI")
                    $form->addHTML("				<label>" . getConstVar("BIBLIO_MAI") . "</label>");
                $cont_tmp[$this->DATAcat[$key]["ca_nome"]]["obbligo"] = 0;
                $this->controllo = array_merge($this->controllo, $cont_tmp);
            }
        }
        $form->addHTML("</div>");
        foreach ($this->DATAcat as $key => $v) {
            if ($this->DATAcat[$key]["ca_tabella"] != NULL) {
                $tabella = $this->DATAcat[$key]["ca_tabella"];
                $nome = $this->DATAcat[$key]["ca_nome"];
                $attivo = strtolower($nome) . "_attivo";
                $checked = FALSE;
                $disabDiv = "_disabled";
                $disabForm = "disabled=\"disabled\"";
                if ($this->DATAcat[$key]["partecipa"] == TRUE) {
                    $first = false;
                    $form->addHTML($nome);
                    $form->addHTML("		<div class=\"ca_nome_disabled\" id=\"ca_nome_" . $nome . "\">");
                    $cat_attivo = strtolower($this->DATAcat[$key]["ca_nome"]) . "_attivo";
                    foreach ($this->DATAcat[$key]["tabella"] as $key_tab => $v_tab) {
                        if (strpos($this->DATAcat[$key]["tabella"][$key_tab]->fieldname, "_cod")) {
                            if ($this->DATAcat[$key]["tabella"][$attivo]->value == 1) {
                                $extra = "<label>" . getConstVar("LINKATTIVO") . "</label>";
                                $first = FALSE;
                            }
                            else
                                $extra = "";
                            $form->addCheckbox($nome . "_check", $nome . "_check", TRUE, "style=\"display:none\"");
                            $form->addHTML(FieldRender::renderField($this->DATAcat[$key]["tabella"][$key_tab], null, $extra));
                        }
                    }
                    $form->addHTML("		</div>");
                }
                else {
                    if ($this->DATAcat[$key]["bc_attivo"] == 1) {
                        $checked = TRUE;
                        $disabDiv = "";
                        $disabForm = "";
                    } else {
                        $checked = FALSE;
                        $disabDiv = "_disabled";
                        $disabForm = "disabled=\"disabled\"";
                    }
                    $form->addCheckbox($nome . "_check", $nome . "_check", $checked, "onclick=\"javascript:enable_cat('" . $nome . "')\"");
                    $form->addHTML($nome);
                    $form->addHTML("		<div class=\"ca_nome" . $disabDiv . "\" id=\"ca_nome_" . $nome . "\">");
                    foreach ($this->DATAcat[$key]["tabella"] as $key_tab => $v_tab)
                        if (strpos($this->DATAcat[$key]["tabella"][$key_tab]->fieldname, "_cod")) {
                            $cont_tmp = $form->addField($this->DATAcat[$key]["tabella"][$key_tab], $disabForm);
                            $this->controllo = array_merge($this->controllo, $cont_tmp);
                        }
                    $form->addHTML("</div>");
                }
            }
        }
    }

    function formCat() {
        $this->reset_controllo();
        $form = new FieldsForm("cat_data_form", "javascript:modificaCataloghi('action_biblio.php')");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value, null, "\n");
        $form->addHidden("op", "updateCat", null, "\n");
        $form->addHTML("<div id=\"cat_data\">");
        $this->edit_library_cat($form);
        $form->addHTML("</div>");
        $form->addHTML("<br/>");
        $form->addButton("save_btn", getConstVar("SAVE"), "class=\"submit\" onclick=\"javascript:salvaCataloghi()\"");
        if (isset($_SESSION['from3to4']) && !$_SESSION['from3to4']) {
            $form->addHTML("&nbsp;&nbsp;");
            $form->addButton("cancel_btn", getConstVar("CANCEL"), "class=\"submit\" onclick=\"javascript:undoModificaCataloghi()\"");
        }
        $form->addHTML(getConstVar("REQUIRED_FIELDS"));
        $code_controllo = urlencode(serialize($this->controllo));
        $form->addHidden("controllo", $code_controllo);

        $form->close();
        $form->stampa();
    }

    public function fill_library_identity() {
        $ret = FieldRender::renderField($this->bib["bi_usr"]);
        $ret.="<div class=\"bi_pwd\">
<span class=\"obj_name\">" . getConstVar("bi_pwd") . "</span><span class=\"colon\">: </span><span class=\"obj_value\">" . getConstVar("CHOOSED_PWD") . "</span>

</div>";
        return $ret;
    }

    public function fill_library_identity2() {
        $ret = FieldRender::renderField($this->bib["bi_nome"]);
        $ret.=FieldRender::render("di_id", $this->disc_arr[$this->bib["di_id"]->value]);
        $ret.=FieldRender::render("en_id", $this->en_arr[$this->bib["en_id"]->value]);
        foreach ($this->progetti as $key => $value) {
            $ret.=FieldRender::render("pr_id", $this->progetti[$key]);
        }
        return $ret;
    }

    public function fill_library_contacts() {
        $ret = FieldRender::renderField($this->bib["bi_indi"]);
        $ret.=FieldRender::renderField($this->bib["bi_cap"]);
        $ret.=FieldRender::renderField($this->bib["bi_citta"]);
        $ret.=FieldRender::render("na_id", $this->nation_arr[$this->bib["na_id"]->value]);
        $ret.=FieldRender::render("prov_id", $this->prov_arr[$this->bib["prov_id"]->value]);
        $ret.=FieldRender::renderField($this->bib["bi_piva"]);
        $ret.=FieldRender::renderField($this->bib["bi_cf"]);
        $ret.=FieldRender::renderField($this->bib["bi_tel"]);
        $ret.=FieldRender::renderField($this->bib["bi_fax"]);
        $ret.=FieldRender::renderField($this->bib["bi_url"]);
        return $ret;
    }

    public function fill_library_cat() {
        $ret = FieldRender::renderField($this->bib["bi_opac"]);
        foreach ($this->DATAcat as $key_cat => $v_cat)
            if ($this->DATAcat[$key_cat]["partecipa"] == TRUE && $this->DATAcat[$key_cat]["tabella"] == NULL) {
                if ($this->DATAcat[$key_cat]["bc_attivo"] == 1)
                    $extra = "<label>" . getConstVar("LINKATTIVO") . "</label>";
                else
                    $extra = "";
                $ret.=FieldRender::render($this->DATAcat[$key_cat]["ca_nome"], getConstVar("SI"), $extra);
            }
        foreach ($this->DATAcat as $key_cat => $v_cat) {
            if ($this->DATAcat[$key_cat]["partecipa"] == TRUE && $this->DATAcat[$key_cat]["tabella"] != NULL) {
                $first = TRUE;

                $cat_attivo = strtolower($this->DATAcat[$key_cat]["ca_nome"]) . "_attivo";
                foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {

                    if (strpos($this->DATAcat[$key_cat]["tabella"][$key_tab]->fieldname, "_cod")) {
                        if ($this->DATAcat[$key_cat]["tabella"][$cat_attivo]->value == 1 && $first) {
                            $extra = "<label>" . getConstVar("LINKATTIVO") . "</label>";
                            $first = FALSE;
                        }
                        else
                            $extra = "";

                        $ret.=FieldRender::renderField($this->DATAcat[$key_cat]["tabella"][$key_tab], null, $extra);
                    }
                }
            }
        }
        return $ret;
    }

    public function fill_library_service() {
        $ret = FieldRender::renderField($this->bib["bi_referente_dd"]);
        $ret.=FieldRender::renderField($this->bib["bi_email_dd"]);
        $ret.=FieldRender::renderField($this->bib["bi_tel_dd"]);
        return $ret;
    }

    public function fill_library_conditions() {
        $ret = "<h3 class=\"big2\">" . getConstVar("LABEL_BIB_SERVIZIOUTENTI") . "</h3>";
        $ret.=FieldRender::renderField($this->bib["bi_costo_ute_dd"]);
        if ($this->bib["bi_id"]->value) {
            $ret.="<div class=\"url_reg_ute\">";
            $ret.="	<span class=\"obj_name\">" . getConstVar("URL_REGISTRAZIONE_UTENTI") . "</span><span class=\"colon\">: </span><a class=\"obj_value\" href=\"" . getConstVar("HTTPROOTDIR") . "/register_ute.php?idbib=" . $this->bib["bi_id"]->value . "\" target=\"_blank\">" . getConstVar("HTTPROOTDIR") . "/register_ute.php?idbib=" . $this->bib["bi_id"]->value . "</a>";
            $ret.="</div>";
        }
        $ret.="<h3 class=\"big2\">" . getConstVar("LABEL_BIB_SERVIZIOBIBLIO") . "</h3>";
        $ret.=FieldRender::renderField($this->bib["bi_costo_dd"]);
        $ret.=FieldRender::renderField($this->bib["bi_scompenso"]);
        if ($this->DATAcat[1]["partecipa"] == FALSE)
            $ret.=FieldRender::renderField($this->bib["bi_condforni"]);
        else
            $ret.="	<div class=\"bi_condforni\">
" . getConstVar("LABEL_CONDFORNI_ACNP") . " <a href=\"" . getConstVar("ACNP_CONDFORNI_URL") . $this->DATAcat[1]["tabella"]["acnp_cod"]->value . "\" target=\"_blank\">ACNP</a>
</div>
";
        return $ret;
    }

    public function fill_suspension() {
        $res = "<h5 class=\"bigger2\">" . getConstVar("LABEL_SOSP_SERVIZIO") . "</h5>";
        if ($this->bib["bi_sospini"]->value != NULL) {
            $res.=getConstVar("LABEL_SOSP_SERVIZIO_DATA");
            $res.=FieldRender::renderField($this->bib["bi_sospini"]);
            $res.=" έως ";
            $res.=FieldRender::renderField($this->bib["bi_sospfine"]);
            if ($this->bib["bi_preavvisososp"]->value != 0)
                $res.="<BR/>"  . getConstVar("GIORNI_PREAVVISO_SOSPENSIONE") . "; " . $this->bib["bi_preavvisososp"]->value;
        }
        else
            $res.=getConstVar("LABEL_NO_SOSPENSIONE");

        return $res;
    }

    function visualizza($action = "action_library.php") {
        $form = new FieldsForm("form_library", "");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value);
        $form->addHidden("op", "edit");
        $form->addHTML("<a id=\"edit_account\" class=\"edit viola\" href=\"javascript:modificaBiblioAccount()\" title=\"" . getConstVar("EDIT_ACCOUNT_LINK_TITLE") . "\"><span>" . getConstVar("EDIT_ACCOUNT") . "</span></a>");
        $form->addHTML($this->visualizza_identity());
        $form->addHTML($this->visualizza_contacts());
        $form->close();
        return $form->restituisci();
    }

    function visualizza_popup() {
        $indi = $this->bi_indi != null ? "<br>" . $this->bi_indi . " " : null;
        $citta = $this->bi_citta != null ? " " . $this->bi_citta . " " : null;
        $prov = $this->prov_id != null ? "(" . $this->prov_id . ")" : null;
        $cap = $this->bi_cap != null ? " - " . $this->bi_cap : null;
        $tel = $this->bi_tel != null ? "<br>" . getConstVar("bi_tel") . ": " . $this->bi_tel : null;
        $fax = $this->bi_fax != null ? "<br>" . getConstVar("bi_fax") . ": " . $this->bi_fax : null;
        $piva = $this->bi_piva != null ? "<br>" . getConstVar("bi_piva") . ": " . $this->bi_piva : null;
        $url = $this->bi_url != null ? "<br>" . getConstVar("bi_url") . ": " . $this->bi_url : null;
        $referente = null;
        if ($this->bi_referente_dd != null) {
            $referente = "<br><br><b>" . getConstVar("bi_referente_dd") . "</b><br>" . $this->bi_referente_dd . "<br>" . $this->bi_email_dd . "<br>" . $this->bi_tel_dd;
        }
        $cataloghi = "";
        foreach ($this->DATAcat as $key_cat => $v_cat) {
            if ($this->DATAcat[$key_cat]["partecipa"] == TRUE && $this->DATAcat[$key_cat]["tabella"] != NULL) {

                $cat_attivo = strtolower($this->DATAcat[$key_cat]["ca_nome"]) . "_attivo";
                foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                    if (strpos($this->DATAcat[$key_cat]["tabella"][$key_tab]->fieldname, "_cod")) {
                        if ($this->DATAcat[$key_cat]["tabella"][$cat_attivo]->value == 1) {
                            $extra = "<label>(" . getConstVar("LINKATTIVO") . ")</label>";
                        }
                        else
                            $extra = "";
                        $cataloghi.=FieldRender::renderField($this->DATAcat[$key_cat]["tabella"][$key_tab], null, $extra . "<br/>");
                    }
                }
                if ($key_cat == 1) {
                    $cod = $this->DATAcat[1]["tabella"]["acnp_cod"]->value;
                    $cataloghi.="<a href=\"http://acnp.cib.unibo.it/cgi-ser/start/it/bib/ss-b.tcl?cod=" . $cod . "&language=ITALIANO\" target=\"_blank\">" . getConstVar("BIBLIO_PAGINA_ACNP") . "</a>";
                }
            }
        }

        if ($cataloghi != "")
            $cataloghi = "<div class=\"cat_popup\"><b>" . getConstVar("CATALOGHI") . ":</b>" . $cataloghi . "</div>";

        return "<b>" . $this->bi_nome . "</b>" . $indi . $cap . $citta . $prov . $tel . $fax . $piva . $cf . $url . $referente . $cataloghi;
    }

    public function visualizza_identity() {
        $res = "<div id=\"library_identity\">";
        $res.=$this->fill_library_identity();
        $res.=$this->fill_library_identity2();
        $res.="</div>";
        return $res;
    }

    public function visualizza_contacts() {
        $res = "<div id=\"library_contacts\">";
        $res.=$this->fill_library_contacts();
        $res.="</div>";
        return $res;
    }

    public function visualizza_cat() {
        $form = new FieldsForm("cat_data_form");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value);
        $form->addHidden("op", "editCat");
        $form->addHTML("<a id=\"edit_cat_data\" class=\"edit viola\" href=\"javascript:modificaCataloghi()\" title=\"" . getConstVar("EDIT_CAT_DATA_LINK_TITLE") . "\"><span>" . getConstVar("EDIT_CAT_DATA") . "</span></a>");
        $form->addHTML("<div id=\"cat_data\">");
        $form->addHTML($this->fill_library_cat());
        $form->addHTML("</div>");
        $form->close();
        return $form->restituisci();
    }

    public function visualizza_dd_service() {
        $form = new FieldsForm("form_library_serv");
        $form->addHidden("bi_id", $this->bib["bi_id"]->value);
        $form->addHidden("op", "editServ");
        $form->addHTML("<a id=\"edit_dd_data\" class=\"edit viola\" href=\"javascript:modificaService()\" title=\"" . getConstVar("EDIT_DD_DATA_LINK_TITLE") . "\"><span>" . getConstVar("EDIT_DD_DATA") . "</span></a>");
        $form->addHTML("<div id=\"dd_data\">");
        $form->addHTML($this->fill_library_service());
        $form->addHTML("</div>");
        $form->addHTML("<div id=\"service_suspension\" class=\"big2\">");
        $form->addHTML($this->fill_suspension());
        $form->addHTML("</div>");
        $form->close();
        return $form->restituisci();
    }

    public function totReqLastYear() {
        return $tot_richieste= $_SESSION['db']->fetch_single($_SESSION['db']->execute("SELECT SUM(re_evaso_bw_nilde)+SUM(re_evaso_bw_posta)+SUM(re_evaso_bw_fax)+SUM(re_evaso_bw_altro)+SUM(re_evaso_bw_ariel)+SUM(re_inevaso_bw_riferr)+SUM(re_inevaso_bw_nonposs)+SUM(re_inevaso_bw_nondisp)+SUM(re_inevaso_bw_over15)+SUM(re_inevaso_bw_overquota)+SUM(re_inevaso_bw_altro)
                                                               FROM `report_biblio`
                                                               WHERE bi_idrichie=".$this->bi_id."
                                                                  AND (
                                                                        (re_mese BETWEEN ".getConstVar('MESE_INIZIO_CONTEGGIO')." AND 12 AND re_anno=".getConstVar('ANNO_INIZIO_CONTEGGIO').")
                                                                     OR (re_mese BETWEEN 1 AND ".getConstVar('MESE_FINE_CONTEGGIO')." AND re_anno=".getConstVar('ANNO_FINE_CONTEGGIO').")
                                                                  ) GROUP BY bi_idrichie "));
    }

    public function isOverQuota() {
        if ($this->totReqLastYear() >= getConstVar('NUM_RICHIE_MAX'))
            return true;
        else
            return false;
    }

    public function isPublicLibrary() {
        if ($this->tipo_ente != 5)
            return true;
        else
            return false;
    }

    public function kindOfContractLibrary($year) {
        $lista_pr = $type = null;
        $anno_arr = array();

        foreach ($this->progetti as $key => $value) {
            $lista_pr.=$key . ",";
        }
        if ($lista_pr != null) {
            $lista_pr = substr($lista_pr, 0, -1);
            $lista_pr = "pr_id IN ($lista_pr) OR ";
        }
        $res_aderente = $_SESSION['db']->execute("SELECT DISTINCT st_anno_contratto FROM storico_sottoscrizioni WHERE $lista_pr en_id=" . $this->en_id);
        while ($anno = $_SESSION['db']->fetch($res_aderente)) {
            $anno_arr[] = $anno['st_anno_contratto'];
        }
        if (in_array(($year - 1), $anno_arr))
            $type = getConstVar('ADERENTE_ANNO_PRECEDENTE');
        if (in_array($year, $anno_arr))
            $type = getConstVar('ADERENTE');
        if ($type != null) {
            return $type;
        } else {
            if ($this->isPublicLibrary())
                return $type = getConstVar('SINGOLA_PUB');
            else
                return $type = getConstVar('SINGOLA_PVT');
        }
    }

    function updateOLDCatalogSettings() {
        foreach ($this->DATAcat as $key_cat => $v_cat) {
            if ($this->DATAcat[$key_cat]["partecipa"] == TRUE && $this->DATAcat[$key_cat]["tabella"] != NULL) {
                $cat_attivo = strtolower($this->DATAcat[$key_cat]["ca_nome"]) . "_attivo";

                $tabella = $this->DATAcat[$key_cat]["ca_tabella"];
                $nome = $this->DATAcat[$key_cat]["ca_nome"];
                $attivo = strtolower($nome) . "_attivo";

                foreach ($this->DATAcat[$key_cat]["tabella"] as $key_tab => $v_tab) {
                    if (strpos($this->DATAcat[$key_cat]["tabella"][$key_tab]->fieldname, "_cod")) {
                        if ($this->bi_id > 0) {
                            $res = $_SESSION['db']->execute("SELECT $attivo from $tabella where bi_id=" . $this->bi_id . " AND $key_tab='" . $this->DATAcat[$key_cat]["tabella"][$key_tab]->value . "'");
                            if ($res && $_SESSION['db']->numrows($res) == 1) {
                                $att_val = $_SESSION['db']->fetch_single($res);
                                $this->DATAcat[$key_cat]["tabella"][$cat_attivo]->value = $att_val;
                            }
                        }
                    }
                }
            }
        }
    }

}
?>
