phpDocumentor VFront
Function-Libraries
[ class tree: VFront ] [ index: VFront ] [ all elements ]

Source for file func.export.php

Documentation is available at func.export.php

  1. <?php
  2. /**
  3. * Libreria di funzioni legate all'esportazione dei dati.
  4. @package VFront
  5. @subpackage Function-Libraries
  6. @author Mario Marcello Verona <marcelloverona@gmail.com>
  7. @copyright 2007 Mario Marcello Verona
  8. @version 0.90
  9. @license http://www.gnu.org/licenses/gpl.html GNU Public License
  10. */
  11.  
  12.  
  13. /**
  14.  * Funzione per l'esportazione dei dati
  15.  *
  16.  * @param int $oid 
  17.  * @param bool $only_visibile 
  18.  * @return resource 
  19.  */
  20. function __tabella_elaborata($oid,$only_visibile=true){
  21.     
  22.     global $link,$db1;
  23.     
  24.     $nome_tabella oid2name($oid);
  25.     
  26.     $clausola_visibile ($only_visibile"AND c.in_visibile=1" "";
  27.     
  28.     // PRENDI INFO Colonne della TABELLA
  29.     $query2 vmsql_query("SELECT c.column_name , c.data_type , c.character_maximum_length as maxsizec.is_nullable
  30.                             c.in_tipoc.in_defaultt.orderbyt.orderby_sort
  31.                             FROM {$db1['frontend']}.registro_col c, {$db1['frontend']}.registro_tab t
  32.                             WHERE c.id_table=$oid 
  33.                             AND c.id_table=t.id_table
  34.                             $clausola_visibile
  35.                             ORDER BY c.ordinal_position",$link);
  36.     
  37.     $matrice_info vmsql_fetch_assoc_all($query2);
  38.     
  39.     foreach($matrice_info as $k=>$info){
  40.         
  41.         if($k==0){
  42.             $ORDERBY ($info['orderby']!=''"ORDER BY ".$info['orderby']." ".$info['orderby_sort'"";
  43.         }
  44.         
  45.         if($info['in_tipo']=="select_from"){    
  46.             
  47.             $test preg_match("'SELECT ([\w]+) ?, ?([\w]+) +FROM +([\w]+).*?'i",$info['in_default'],$campo_k);
  48.         
  49.             $campi.=" (SELECT t$k.{$campo_k[2]} FROM {$campo_k[3]} t$k WHERE t$k.{$campo_k[1]}=t.{$info['column_name']}as ".$info['column_name'].",";
  50.         }
  51.         else{
  52.             
  53.             $campi.=" t.".$info['column_name'].",";
  54.         }
  55.     }
  56.     
  57.     $campi=substr($campi,0,-1);
  58.     
  59.     $query_elab=vmsql_query("SELECT $campi FROM {$db1['dbname']}.$nome_tabella t $ORDERBY",$link);
  60.     
  61.     return $query_elab;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /**
  75.  * Funzione di esportazione generica tabella in formato CSV
  76.  *
  77.  * @param int $oid_tabella 
  78.  * @param bool $raw 
  79.  * @param string $sep 
  80.  */
  81. function tabella_csv($oid_tabella,$raw=true,$sep=','){
  82.     
  83.     global $link,$db1;
  84.     
  85.     $oid_tabella = (int) $oid_tabella;
  86.     
  87.     $nome_tabella oid2name($oid_tabella);
  88.     
  89.     $nome_file_tmp=FRONT_ROOT."/files/tmp/".md5($oid_tabella).".csv";
  90.     
  91.     
  92.     // Prende la tabella in XML
  93.     if($raw){
  94.         
  95.         $q=vmsql_query("SELECT * FROM ".$db1['dbname'].".$nome_tabella",$link);
  96.     
  97.     }
  98.     else{
  99.         $q=__tabella_elaborata($oid_tabella);
  100.     }
  101.     
  102.     $RIGHE="";
  103.     
  104.     $i=0;
  105.     
  106.     while($RS=vmsql_fetch_assoc($q)){
  107.         
  108.         if($i==0){
  109.             
  110.             $campi=array_keys($RS);
  111.             $fp=fopen($nome_file_tmp,'w');
  112.             fwrite($fp,"\"".implode('"'.$sep.'"',$campi)."\"\n");
  113.             fclose($fp);
  114.             
  115.         }
  116.         
  117.         $RIGA='';
  118.         
  119.         foreach($RS as $val){
  120.             $RIGA.="\"".addslashes($val)."\"$sep";
  121.         }
  122.         
  123.         $RIGA=stripslashes($RIGA);
  124.         $RIGA=str_replace(array("\r\n","\n\r","\n","\r")," ",$RIGA);
  125.         
  126.         $fp=fopen($nome_file_tmp,'a');
  127.         fwrite($fp,substr($RIGA,0,-1)."\n");
  128.         fclose($fp);
  129.         
  130.         $i++;
  131.     }
  132.     
  133.     
  134.     $nome_tabella oid2name($oid_tabella);
  135.     
  136.           //Begin writing headers
  137.     header("Content-Type: application/csv-tab-delimited-table");
  138.     header("Content-dispositionfilename=$nome_tabella-".date("Ymd").".csv");
  139.     print join('',file($nome_file_tmp));
  140.     unlink($nome_file_tmp);
  141.     exit;
  142.     
  143. }
  144.  
  145.  
  146. /**
  147.  * Funzione di trasfomazione di tabella in HTML
  148.  *
  149.  * @param int $oid_tabella 
  150.  * @param bool $raw 
  151.  * @param string $colore_th 
  152.  */
  153. function tabella_html($oid_tabella,$raw=true,$colore_th=''){
  154.     
  155.       //Begin writing headers
  156.     print _table2html($oid_tabella,$raw,$colore_th);
  157.     exit;
  158. }
  159.  
  160.  
  161.  
  162. /**
  163.  * Funzione interna di generazione della tabella HTML.
  164.  * Viene poi utilizzata da altre funzioni di questa libreria per generare l'output.
  165.  *
  166.  * @param int $oid_tabella 
  167.  * @param bool $raw 
  168.  * @param string $colore_th Espresso in RGB esadecimale. Ad es. #FF0000
  169.  * @return string 
  170.  */
  171. function _table2html($oid_tabella,$raw=true,$colore_th=''){
  172.     
  173.     global $link,$db1;
  174.     
  175.     $oid_tabella= (int) $oid_tabella;
  176.             
  177.     $nome_tabella oid2name($oid_tabella);
  178.     
  179.     $nome_file_tmp=FRONT_ROOT."/files/tmp/".md5($oid_tabella).".htm";
  180.     
  181.     // PREN DE LA QUERY
  182.     if($raw){
  183.         
  184.         $q=vmsql_query("SELECT * FROM ".$db1['dbname'].".$nome_tabella",$link);
  185.     }
  186.     else{
  187.         $q=__tabella_elaborata($oid_tabella);
  188.     }
  189.         
  190.  
  191.     
  192.     
  193.     $HTML=<<<HTML
  194. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  195. <html>
  196.   <head>
  197.   <meta http-equiv="Content-typecontent="text/htmlcharset=iso-8859-1">
  198.   <title></title>
  199.   </head>
  200.   <body>
  201. HTML;
  202.     $i=0;
  203.     $info_colore($colore_th==''" bgcolor=\"#FF9900\"" " bgcolor=\"$colore_th\"";
  204.     $TR_TD="";
  205.     
  206.     $fp=fopen($nome_file_tmp,'w');
  207.     
  208.     while($RS=vmsql_fetch_assoc($q)){
  209.         
  210.         if($i==0){
  211.             
  212.             $campi=array_keys($RS);
  213.             
  214.             $TR_TH="<tr><th$info_colore>".implode("</th><th$info_colore>",$campi)."</th></tr>\n";
  215.             
  216.         }
  217.         
  218.         $TR_TD="<tr><td>".implode("</td><td>",$RS)."</td></tr>\n";
  219.         
  220.         fwrite($fp,$TR_TD);
  221.         
  222.         
  223.         
  224.         
  225.         $i++;
  226.     }
  227.     
  228.     fclose($fp);
  229.     
  230. return "
  231. $HTML
  232. <table border=\"1\" summary=\"$nome_tabella\">
  233.         $TR_TH
  234.         ".join('',file($nome_file_tmp))."
  235. </table>
  236. </body>
  237. </html>";
  238. }
  239.  
  240.  
  241. #################################################################
  242. #
  243. #    EXCEL (html letto da excel)
  244. #
  245.  
  246.  
  247. /**
  248.  * Genera una tabella XLS creando tabella HTML e mandando un header di excel
  249.  *
  250.  * @param int $oid_tabella 
  251.  * @param bool $raw 
  252.  * @param string $colore_th 
  253.  */
  254. function tabella_xls($oid_tabella,$raw=true,$colore_th=''){
  255.     
  256.     global $link,$db1;
  257.     
  258.     // Prende la tabella in XML
  259.     $html=_table2html($oid_tabella,$raw,$colore_th);
  260.     
  261.     $nome_tabella oid2name($oid_tabella);
  262.     $ctype="application/vnd.ms-excel";
  263.     
  264.       //Begin writing headers
  265.     header("Pragma: public");
  266.     header("Expires: 0");
  267.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  268.     header("Cache-Control: public");
  269.     header("Content-Description: File Transfer");
  270.   
  271.     //Use the switch-generated Content-Type
  272.     header("Content-Type$ctype");
  273.     
  274.     //Force the download
  275.     $header="Content-Disposition: attachment; filename=".$nome_tabella."-".date("Ymd").".xls;";
  276.     header($header );
  277.     print $html;
  278.     exit;
  279.     
  280. }
  281.  
  282.  
  283.  
  284. /**
  285.  * Genera una tabella nello standard Open Document Spreadsheet (ad es. openOffice)
  286.  *
  287.  * @param int $oid_tabella 
  288.  * @param bool $raw 
  289.  * @param string $colore_th 
  290.  */
  291. function tabella_ods($oid_tabella,$raw=true,$colore_th=''){
  292.     
  293.     global $link,$db1;
  294.     
  295.     $id_tabella = (int) $oid_tabella;
  296.     
  297.     // PRENDI LE IMPOSTAZIONI DEI CAMPI DELLA TABELLA
  298.     
  299.     $q0=vmsql_query("SELECT column_name,data_type FROM {$db1['frontend']}.registro_col WHERE id_table=$id_tabella ORDER BY ordinal_position",$link);
  300.     
  301.     while($RS0=vmsql_fetch_row($q0)){
  302.         $tipo_campo[$RS0[0]]=$RS0[1];
  303.     }
  304.     
  305.     $nome_tabella oid2name($id_tabella);
  306.     
  307.     if($raw){
  308.         $q=vmsql_query("SELECT * FROM ".$db1['dbname'].".$nome_tabella",$link);
  309.     }
  310.     else{
  311.         $q=__tabella_elaborata($id_tabella);
  312.     }
  313.     
  314.     
  315.     
  316.     
  317.     
  318.     // Cancella se esiste già il file
  319.     @unlink(FRONT_ROOT.'/files/tmp/content.xml');
  320.     
  321.     
  322.     // prendi il file content.xml
  323.     $cont=join('',file(FRONT_REALPATH."/plugins/ods/content.xml"));
  324.     
  325.     // array contenuto;
  326.     $ar_cont=explode('<!-- contenuto -->',$cont);
  327.     
  328.     
  329.     
  330.     $XML_INT='';
  331.     $XML_CONT='';
  332.     
  333.     $i=0;
  334.     $ultima_scrittura=0;
  335.     
  336.     
  337.     while($RS=vmsql_fetch_assoc($q)){
  338.         
  339.         // prima riga, scrive le intestazioni
  340.         
  341.         $XML_CONT.='<table:table-row table:style-name="ro1">';
  342.         
  343.         foreach($RS as $lab=>$val){
  344.             
  345.             if($i==0){
  346.                 
  347.                 $XML_INT.="
  348.                     <table:table-cell table:style-name=\"ce1\" office:value-type=\"string\">
  349.                         <text:p>{$lab}</text:p>
  350.                     </table:table-cell>";
  351.                 
  352.                     
  353.  
  354.                 
  355.             }
  356.                 
  357.             $XML_CONT.=_ods_type($tipo_campo,$lab,$val);
  358.             
  359.         }
  360.         
  361.         
  362.             
  363.         
  364.         $XML_CONT.='</table:table-row>';
  365.         
  366.         
  367.         // scrittura prima volta:
  368.         
  369.         if($i==0){
  370.             
  371.             
  372.             $apertura_XML='
  373.                     <table:table table:name="'.$nome_tabella.'" table:style-name="ta1" table:print="false">
  374.                         <table:table-column table:style-name="co1" table:number-columns-repeated="'.($i+1).'" table:default-cell-style-name="Default"/>
  375.                         <table:table-row table:style-name="ro1">
  376.                         '.$XML_INT.'
  377.                         </table:table-row>
  378.                         ';
  379.             
  380.             // Scrittura inizio di tabella
  381.             if($fp=@fopen(FRONT_ROOT.'/files/tmp/content.xml','a')){
  382.                 fwrite($fp,$ar_cont[0]);
  383.                 fwrite($fp,$apertura_XML);
  384.             }
  385.             
  386.         }
  387.         
  388.         // scrivo la roba in ciclo ogni 10 record
  389.         if($i%20==0){
  390.             if($fp=@fopen(FRONT_ROOT.'/files/tmp/content.xml','a')){
  391.                 fwrite($fp,$XML_CONT);
  392.                 fclose($fp);
  393.                 
  394.                 $ultima_scrittura=$i;
  395.                 
  396.                 unset($XML_CONT);
  397.             }
  398.         }
  399.         
  400.         
  401.         $i++;
  402.     }
  403.     
  404.     // ultima scrittura fuori dal ciclo (per i record esclusi dal multiplo...)
  405.     
  406.     if($i>$ultima_scrittura){
  407.             if($fp=@fopen(FRONT_ROOT.'/files/tmp/content.xml','a')){
  408.                 fwrite($fp,$XML_CONT);
  409.                 fclose($fp);
  410.                 
  411.                 unset($XML_CONT);
  412.             }
  413.     }
  414.     
  415.         
  416.     
  417.     
  418.     $chiusura_XML=    '\t</table:table>';
  419.     
  420.         
  421.         
  422.     
  423.         
  424.     
  425.     
  426.     // chiudi il nuovo file
  427.     if($fp=@fopen(FRONT_ROOT.'/files/tmp/content.xml','a')){
  428.         fwrite($fp,$chiusura_XML);
  429.         fwrite($fp,$ar_cont[1]);
  430.         fclose($fp);
  431.     }
  432.     else {
  433.         openErrorGenerico("Errore: impossibile scrivere sul filesystem.",false,'Non &egrave; stato possibile generare il file per impostazioni di diritti di scrittura del server.<br />Se il problema si verificasse sistematicamente contattare l\'amministratore di sistema');
  434.         exit;
  435.     }
  436.     
  437.     
  438.     // METODO PHP
  439.     // CREA IL PACCHETTO
  440.     
  441.     require_once(FRONT_REALPATH."/inc/EasyZIP.class.php");
  442.     
  443.     $zip new EasyZIP();
  444.     
  445.     $zip->addFile("content.xml",FRONT_ROOT."/files/tmp/");
  446.     $zip->addDir(FRONT_REALPATH."/plugins/ods","Configurations2");
  447.     $zip->addDir(FRONT_REALPATH."/plugins/ods","META-INF");
  448.     $zip->addDir(FRONT_REALPATH."/plugins/ods","Pictures");
  449.     $zip->addFile("meta.xml",FRONT_REALPATH."/plugins/ods/");
  450.     $zip->addFile("settings.xml",FRONT_REALPATH."/plugins/ods/");
  451.     $zip->addFile("styles.xml",FRONT_REALPATH."/plugins/ods/");
  452.     $zip->addFile("mimetype",FRONT_REALPATH."/plugins/ods/");
  453.  
  454.     
  455.     $tmp_ods=FRONT_ROOT.'/files/tmp/'.md5(time()).".ods";
  456.     
  457.     $zip->zipFile($tmp_ods);
  458.     
  459.     
  460.     
  461.     
  462.     $ctype="application/vnd.oasis.opendocument.spreadsheet";
  463.     
  464.       
  465.     if(is_file($tmp_ods)){
  466.         
  467.         
  468.         //Begin writing headers
  469.         header("Pragma: public");
  470.         header("Expires: 0");
  471.         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  472.         header("Cache-Control: public");
  473.         header("Content-Description: File Transfer");
  474.       
  475.         //Use the switch-generated Content-Type
  476.         header("Content-Type$ctype");
  477.         
  478.         //Force the download
  479.         $header="Content-Disposition: attachment; filename=".$nome_tabella."-".date("Ymd").".ods;";
  480.         header($header);
  481.         
  482.         print join('',file($tmp_ods));
  483.     
  484.     }
  485.     else{
  486.         
  487.         openErrorGenerico("Errore: impossibile scrivere sul filesystem.",false,'Non &egrave; stato possibile generare il file per impostazioni di diritti di scrittura del server.<br />Se il problema si verificasse sistematicamente contattare l\'amministratore di sistema');
  488.         exit;
  489.         
  490.     }
  491.     
  492.     unlink(FRONT_ROOT."/files/tmp/content.xml");
  493.     unlink($tmp_ods);
  494.     exit;
  495.     
  496.     
  497. }
  498.  
  499.  
  500.  
  501.  
  502.  
  503. /**
  504.  * Funzione interna usata nella generazione di foglio di calcolo OpenDocument.
  505.  * Questa funzione è richiamata dalla funzione {@see tabella_ods}
  506.  *
  507.  * @param array $array_tipo_campo 
  508.  * @param string $nome_campo 
  509.  * @param mixed $value 
  510.  * @see function tabella_ods
  511.  * @return string Il frammento di XML per il campo scelto
  512.  */
  513. function _ods_type($array_tipo_campo,$nome_campo,$value){
  514.     
  515.     switch($array_tipo_campo[$nome_campo]){
  516.         
  517.         case 'int':
  518.         case 'float':
  519.         case 'double'
  520.         return '
  521.                     <table:table-cell office:value-type="float" office:value="'.$value.'">
  522.                         <text:p>'.$value.'</text:p>
  523.                     </table:table-cell>';
  524.         break;
  525.         
  526.         
  527.         
  528.         
  529.         case 'date' :
  530.         return '
  531.                     <table:table-cell office:value-type="date" office:date-value="'.$value.'">
  532.                         <text:p>'.dataITA($value,false).'</text:p>
  533.                     </table:table-cell>';
  534.         break;
  535.         
  536.         
  537.         
  538.         
  539.         
  540.         case 'timestamp':
  541.         case 'datetime':
  542.         return '
  543.                     <table:table-cell office:value-type="date" office:date-value="'.$value.'">
  544.                         <text:p>'.dataITA($value,true).'</text:p>
  545.                     </table:table-cell>';
  546.         
  547.         
  548.         
  549.         
  550.         default // varie string
  551.                     $value=str_replace(array("\n","\r"),"",$value);
  552.                     $value=htmlspecialchars(utf8_encode($value));
  553.                     
  554.         return '
  555.                     <table:table-cell office:value-type="string">
  556.                         <text:p>'.$value.'</text:p>
  557.                     </table:table-cell>';
  558.         
  559.         
  560.     }
  561.     
  562. }
  563.  
  564. ?>

Documentation generated on Sat, 22 Sep 2007 11:50:12 +0200 by phpDocumentor 1.4.0a2