<?
include("../../../framework/framework.php");
include('../../../framework/external_lib/oomaker/oomaker.php');

// Define page specific text for template
$PAGE[TXT_TITLE]=EXPORT_MENU_0_7;
$PAGE[PAGE_INTITLE]=EXPORT_MENU_0_7;
$PAGE[PAGE_PICTITLE]="ico_stampa_med.gif";
$module="export";

load_module_language("contact");

$thissearch= load_fwobject("search",$module,8);


template_init();


template_define_elements();
ob_start();


	if ($_GET[form_id]==$thissearch[form][name]){
		$error= check_form($thissearch[form],$_GET,$page);
		if ($error == 1)
		{

                     $filename = get_unique_name();
			$daylist = array(EXPORT_SXW_WEEK6,EXPORT_SXW_WEEK0,EXPORT_SXW_WEEK1,EXPORT_SXW_WEEK2,EXPORT_SXW_WEEK3,
                                         EXPORT_SXW_WEEK4, EXPORT_SXW_WEEK5);

                     $P = $_GET;

                     $codice_from = strlen(trim($P['codice_from'])) ? trim($P['codice_from']) : '';
                     $codice_to = strlen(trim($P['codice_to'])) ? trim($P['codice_to']) : '';
			$operatore= is_numeric($P['operatore'][realval][0]) ? $P['operatore'][realval][0] : '';
			$pr_prof_orig= is_numeric($P['pr_prof_orig'][realval][0]) ? $P['pr_prof_orig'][realval][0] : '';

                  	$iniz_from = is_mysql_date($P['iniz_from']) ? $P['iniz_from'] : '';
                     $iniz_to   = is_mysql_date($P['iniz_to']) ? $P['iniz_to'] : '';
                     $mod_from = is_mysql_date($P['mod_from']) ? $P['mod_from'] : '';
                     $mod_to   = is_mysql_date($P['mod_to']) ? $P['mod_to'] : '';
                     $ins_from = is_mysql_date($P['ins_from']) ? $P['ins_from'] : '';
                     $ins_to   = is_mysql_date($P['ins_to']) ? $P['ins_to'] : '';

			$tipo 	  = strlen($P['pr_tipo'][0]) ? $P['pr_tipo'][0] : '';
			$pr_dept  = strlen($P['pr_dept'][0]) ? $P['pr_dept'][0] : '';
			$pr_uff   = strlen($P['pr_uff'][0]) ? $P['pr_uff'][0] : '';

                     $pr_referral   = strlen($P['pr_referral']) ? $P['pr_referral'] : '';
                    	$pr_uff_orig   = strlen($P['pr_uff_orig']) ? $P['pr_uff_orig'] : '';

                     $title    = strlen(trim($P['title'])) ? trim($P['title']) : '';
                     $note     = strlen(trim($P['note'])) ? trim($P['note']) : '';
                     $dest     = is_numeric($P['dest'][realval][0]) ? $P['dest'][realval][0] : '';
                     if (!is_array($P[print_note])) $P[print_note] = array();
                     $rapida   = in_array(2,$P[print_note]) ? 1 : 0;

                     $wh = array('(1 = 1)');
                     if ($codice_from)  $wh[] = "(pr_codice >= ".$DB->Quote($codice_from).")";
                     if ($codice_to)  $wh[] = "(pr_codice <= ".$DB->Quote($codice_to).")";
                     if ($iniz_from) $wh[] = "(pr_data_inizio >= ".$DB->Quote($iniz_from).")";
                     if ($iniz_to)   $wh[] = "(pr_data_inizio <= ".$DB->Quote($iniz_to).")";
                     if ($mod_from)  $wh[] = "(pr_data_mod >= ".$DB->Quote($mod_from).")";
                     if ($mod_to)    $wh[] = "(pr_data_mod <= ".$DB->Quote($mod_to).")";
                     if ($ins_from)  $wh[] = "(pr_data_ins >= ".$DB->Quote($ins_from).")";
                     if ($ins_to)    $wh[] = "(pr_data_ins <= ".$DB->Quote($ins_to).")";
			if ($tipo)      $wh[] = "(pr_tipo = ".$DB->Quote($tipo).")";
			if ($pr_dept)   $wh[] = "(pr_dept = ".$DB->Quote($pr_dept).")";
			if ($pr_uff)    $wh[] = "(pr_uff = ".$DB->Quote($pr_uff).")";
                     if ($pr_referral) $wh[] = "(pr_referral LIKE ".$DB->Quote("%$pr_referral%").")";
                     if ($pr_uff_orig) $wh[] = "(pr_uff_orig LIKE ".$DB->Quote("%$pr_uff_orig%").")";
                     if ($title)     $wh[] = "(pr_oggetto LIKE ".$DB->Quote("%$title%").")";
                     if ($note)      $wh[] = "(note LIKE ".$DB->Quote("%$note%").")";
			//if ($operatore) $wh[] = "(pr_operatore LIKE ".$DB->Quote("$operatore%")." OR pr_operatore LIKE ".$DB->Quote("%$operatore")." OR pr_operatore LIKE ".$DB->Quote("%,,$operatore,,%").")";
			if ($operatore) $wh[] = "(pr_operatore RLIKE ".$DB->Quote("[[:<:]]".$operatore."[[:>:]]").")";
			if ($pr_prof_orig) $wh[] = "(pr_prof_orig RLIKE ".$DB->Quote("[[:<:]]".$pr_prof_orig."[[:>:]]").")";
			
                        if (is_array($P['pr_ref_idbenefic']) && count($P['pr_ref_idbenefic'])) {
                                $str = "[[:<:]](".implode('|',$P['pr_ref_idbenefic']).")[[:>:]]";
                                $wh[] = "(pr_ref_idbenefic RLIKE ".$DB->Quote($str).")";
                        }
                        if (is_array($P['pr_ref_idavvr']) && count($P['pr_ref_idavvr'])) {
                                $str = "[[:<:]](".implode('|',$P['pr_ref_idavvr']).")[[:>:]]";
                                $wh[] = "(pr_ref_idbenefic RLIKE ".$DB->Quote($str).")";
                        }
                        if (is_array($P['pr_ref_idaltri']) && count($P['pr_ref_idaltri'])) {
                                $str = "[[:<:]](".implode('|',$P['pr_ref_idaltri']).")[[:>:]]";
                                $wh[] = "(pr_ref_idaltri RLIKE ".$DB->Quote($str).")";
                        }

print_r($wh);

                        if (count($wh) > 1) {

                                $PAGE_STYLE[width] = 29.70;
                                $PAGE_STYLE[height] = 21;

                                //intestazione: info cliente

                                if ($dest) {

                                $k = $DB->Execute("SELECT * FROM contact WHERE id=".$DB->Quote($dest));
                                $cliente = $k->FetchRow();
				utf8_encode_array($cliente);

                                $int_font = $oo_def;
                                $int_font[ml] = 23;
                                $int_font[mr] = 2;
                                $int_font[name] = 'Arial12int';
                                $OO_TEXT .= print_text(
                                                "\n\n".EXPORT_SXW_DEST.":\n\n".
                                                "$cliente[nome]\n".
                                                "$cliente[indirizzo]\n".
                                                "$cliente[cap] $cliente[citta]\n",
                                                $int_font);
                                }

                                // operator list
                                $j = $DB->Execute("SELECT id,codice,nome FROM users");
                                while ($k = $j->FetchRow()) $OPER[$k[id]] = array($k[codice],$k[nome]);
				utf8_encode_array($OPER);

				// tipo list
                                $j = $DB->Execute("SELECT codice,ttp_desc FROM INT_pratiche_tipo");
                                while ($k = $j->FetchRow()) $PRTIPO[$k[codice]] = $k[ttp_desc];
				utf8_encode_array($PRTIPO);

                                // create table
                                new_table(get_default_table('Tabella1',array(7,19,3.7)));

                                add_table_header('Tabella1',array(      EXPORT_SXW_COD,
									EXPORT_SXW_OGG,
									EXPORT_SXW_DATE,
                                                                ),make_bold($oo_def_c));

                                $where = implode(' AND ',$wh);

                                $q = $DB->Execute("     SELECT *
                                                        FROM pratiche P
                                                        WHERE $where
                                                        ORDER BY pr_codice ASC");

                                while ($l = $q->FetchRow()) {
					utf8_encode_array($l);

                                        $op = $l[pr_operatore];
                                        $oper = is_array($OPER[$op]) ? $OPER[$op][0]." (".$OPER[$op][1].")" : '';

					//info cliente
					$jq = $DB->Execute("SELECT codice,nome FROM contact WHERE id=".$l[pr_ref_idcliente]);
					$yy = $jq->FetchRow();
					utf8_encode_array($yy);

					$valore = EXPORT_SXW_VAL_IND;
					if ($l[pr_valore] == -2) $valore = EXPORT_SXW_VAL_INDEX;
					if ($l[pr_valore] > 0) $valore = nform($l[pr_valore]);

                                        add_table_row('Tabella1',array( 
						$l[pr_codice]."\n\n".EXPORT_SXW_NUM." ".(int)$l[pr_numero],
                                                EXPORT_SXW_REPN." ".$l[pr_repertorio]."\t\t".EXPORT_SXW_CLI.": ".$yy[codice]." (".$yy[nome].")\n".
						$l[pr_oggetto]."\n".
						EXPORT_SXW_TYPE.": ".$l[pr_tipo]." (".$PRTIPO[$l[pr_tipo]].")\t\t".EXPORT_USER.": ".$oper."\n".
						EXPORT_SXW_VALUE.": $valore\t\t".EXPORT_SXW_CRIT.": ".$l[pr_criterio]."\t\t".EXPORT_SXW_FIDO.":".nform($l[pr_fido])."\n".
						EXPORT_SXW_AUTCOMP.": ".$l[pr_comp_cod]." . ".$l[pr_comp_desc]."\t".EXPORT_SXW_NPROV.": ".$l[pr_nprovv]."\n".
						EXPORT_SXW_GIUD.": ".$l[pr_giudice]."\t\t".EXPORT_SXW_NRUO.": ".$l[pr_nruolo]
						,
                                              	EXPORT_SXW_APE.": ".mysql_to_date($l[pr_data_inizio])."\n".
						EXPORT_SXW_INS.": ".mysql_to_date($l[pr_data_ins])."\n".
						EXPORT_SXW_AGG.": ".mysql_to_date($l[pr_data_mod]),
                                                ),
                                                                array(  $oo_def,$oo_def,$oo_def_e)
                                                        );
                                        // note
                                        if ($l[note] && in_array(1,$P[print_note]) && !$rapida)
                                                add_table_row('Tabella1',array( EXPORT_SXW_NOTE.": ".$l[note]),$oo_def);
                                }

                                // create content
                                $OO_PAGE_STYLE = set_page_style($PAGE_STYLE);
                                $OO_PAGE_HEADER = set_page_header("#DATA#\t\t\t\t".EXPORT_SXW_TIT_PRAT."\t\t".EXPORT_SXW_TIT_PG.": #PAGINA#");
                                $OO_PAGE_FOOTER = ''; // set_page_footer('#DATA# #ORA#');

                                $header_style = array(color => '#000000', size => '12pt', align => 'start', face => 'Verdana');
                                //$footer_style = array(color => '#ffff00', size => '12pt', align => 'end', face => 'Verdana');
                                $OO_HEADER_STYLE = set_header_style($header_style);
                                $OO_FOOTER_STYLE = ''; // set_footer_style($footer_style);

                                $OO_TABLE_STYLES = gen_table_style();

                                $OO_TEXT .= render_table('Tabella1');

                                $OO_FONT_STYLES = gen_font_style();

                                $url = make_sxw($filename);
                                print '<a href="../output_sxw.php?file='.$filename.'">'.EXPORT_SXW_CLICK.'</a><br>';

                        } else print EXPORT_SXW_SEARCH;

		} else print draw_form($thissearch[form],$module,$error,$_GET);
	} else print draw_form($thissearch[form],$module,"",$_GET);


$PAGE[PAGE_CONTENT] = ob_get_contents();
ob_end_clean();

final_render();

?>
