Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

class_document_modele.php

Go to the documentation of this file.
00001 <?
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision$ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00028 class Document_modele {
00029   var $cn;              
00030   var $md_id;           
00031   var $md_name;         
00032   var $md_type;         
00033   var $md_lob;          
00034   var $md_sequence;     
00035   var $sequence;        
00036   //Constructor parameter = database connexion
00037   function Document_modele($p_cn,$p_id=-1) {
00038     $this->cn=$p_cn;    
00039     $this->md_id=$p_id;
00040   }
00041   
00049   function myList() { 
00050     $sql="select md_id,md_name,dt_value from document_modele join document_type on(dt_id=md_type)";
00051     $Res=ExecSql($this->cn,$sql);
00052     $all=pg_fetch_all($Res);
00053     if ( pg_NumRows($Res) == 0 ) return "";
00054     $r='<p><form method="post">';
00055     $r.="<table>";
00056     $r.="<tr> <th> Nom </th> <th>Type</Th><th>Fichier</th><th> Effacer</th>"; 
00057     $r.="</tr>";
00058     foreach ( $all as $row) {
00059       $r.="<tr>";
00060       $r.="<td>";
00061       $r.=$row['md_name'];
00062       $r.="</td>";
00063       $r.="<td>";
00064       $r.=$row['dt_value'];
00065       $r.="</td>";
00066       $r.="<td>";
00067       $r.='<A HREF="show_document_modele.php?md_id='.$row['md_id'].'">Document</a>';
00068       $r.="</td>";
00069       $r.="<TD>";
00070       $c=new widget("checkbox");
00071       $c->name="dm_remove_".$row['md_id'];
00072       $r.=$c->IOValue();
00073       $r.="</td>";
00074       $r.="</tr>";
00075     } 
00076     $r.="</table>";
00077     
00078     // need hidden parameter for subaction
00079     $a=new widget("hidden");
00080     $a->name="sa";
00081     $a->value="rm_template";
00082     $r.=$a->IOValue();
00083     $r.=$a->Submit("rm_template","Effacer la sélection");
00084     $r.="</form></p>";
00085     return $r;
00086   }
00103   function Save() 
00104     {
00105       // if name is empty return immediately
00106       if ( trim(strlen($this->md_name))==0) 
00107         return;
00108       // Start transaction
00109       StartSql($this->cn);
00110       // Save data into the table invoice
00111       // if $this->md_id == -1 it means it is a new document model
00112       // so first we have to insert it
00113       // the name and the type must be set before calling save
00114       if ( $this->md_id == -1) 
00115         {
00116 
00117           // insert into the table document_modele
00118           $this->name=FormatString($this->md_name);
00119           $this->md_id=NextSequence($this->cn,'document_modele_md_id_seq');
00120           $sql=sprintf("insert into document_modele(md_id,md_name,md_type) 
00121                               values (%d,'%s',%d)",
00122                              $this->md_id,$this->name,$this->md_type);
00123           $Ret=ExecSql($this->cn,$sql);
00124           // create the sequence for this modele of document
00125           $this->md_sequence="document_".NextSequence($this->cn,"document_seq");
00126           // if start is not equal to 0 and he's a number than the user
00127           // request a number change
00128           if ( $this->start != 0 && isNumber($this->start) == 1 )
00129             {
00130               $sql="alter sequence seq_doc_type_".$this->md_type." start with ".$this->start;
00131               ExecSql($this->cn,$sql);
00132             }
00133           
00134         }
00135       // Save the file
00136       $new_name=tempnam('/tmp','document_');
00137       echo_debug('class_document_modele.php',__LINE__,"new name=".$new_name);
00138       if ( strlen ($_FILES['doc']['tmp_name']) != 0 ) 
00139         {
00140           if (move_uploaded_file($_FILES['doc']['tmp_name'],
00141                                  $new_name)) 
00142             {
00143                     // echo "Image saved";
00144               $oid= pg_lo_import($this->cn,$new_name);
00145               if ( $oid == false ) 
00146                 {
00147                   echo_error('class_document_modele.php',__LINE__,"cannot upload document");
00148                   Rollback($cn);
00149                   return;
00150                 }
00151               echo_debug('class_document_modele.php',__LINE__,"Loading document");
00152               // Remove old document
00153               $ret=ExecSql($this->cn,"select md_lob from document_modele where md_id=".$this->md_id);
00154               if (pg_num_rows($ret) != 0) 
00155                 {
00156                   $r=pg_fetch_array($ret,0);
00157                   $old_oid=$r['md_lob'];
00158                   if (strlen($old_oid) != 0) 
00159                     pg_lo_unlink($this->cn,$old_oid);
00160                 }
00161               // Load new document
00162               var_dump($_FILES);
00163               ExecSql($this->cn,"update document_modele set md_lob=".$oid.", md_mimetype='".$_FILES['doc']['type']."' ,md_filename='".$_FILES['doc']['name']."' where md_id=".$this->md_id);
00164               Commit($this->cn);
00165             }
00166           else 
00167             {
00168               echo "<H1>Error</H1>";
00169               Rollback($this->cn);
00170               exit;
00171             }
00172         }
00173     }
00178   function Delete() 
00179     {
00180       StartSql($this->cn);
00181       // first we unlink the document
00182       $sql="select md_lob from document_modele where md_id=".$this->md_id;
00183       $res=ExecSql($this->cn,$sql);
00184       $r=pg_fetch_array($res,0);
00185       // if a lob is found
00186       if ( strlen ($r['md_lob']) != 0 )
00187         {
00188           // we remove it first
00189           pg_lo_unlink($r['md_lob']);
00190         }
00191       // now we can delete the row
00192       $sql="delete from document_modele where md_id =".$this->md_id;
00193       $sql=ExecSql($this->cn,$sql);
00194       Commit($this->cn);
00195     }
00196   
00203   function Get($p_id,$p_internal) 
00204     {
00205       // retrieve info from the table invoice
00206       
00207       // retrieve the template and generate document
00208       StartSql($this->cn);
00209       $ret=ExecSql($cn,"select iv_name,iv_file from invoice where iv_id".$this->iv_id);
00210       if ( pg_num_rows ($ret) == 0 )
00211         return;
00212       $row=pg_fetch_array($ret,0);
00213       //the template is saved into file $tmp
00214       $tmp=tempnam('/tmp/','invoice_');
00215       pg_lo_export($cn,$row['iv_file'],$tmp);
00216       // Parse the file 
00217       
00218       
00219       
00220       
00221       // send it to stdout
00222       ini_set('zlib.output_compression','Off');
00223       header("Pragma: public");
00224       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00225       header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00226       header("Cache-Control: must-revalidate");
00227       header('Content-type: rtf/x-application');
00228       header('Content-Disposition: attachment;filename="invoice'.$p_internal.'.rtf"',FALSE);
00229       header("Accept-Ranges: bytes");
00230       $file=fopen($tmp,'r');
00231       while ( !feof ($file) )
00232         {
00233           echo fread($file,8192);
00234         }
00235       fclose($file);
00236       
00237       unlink ($tmp);
00238       
00239       Commit($cn);
00240       
00241     }
00251   function form($p_action) 
00252     {
00253       $r="<p>";
00254       $r.='<form enctype="multipart/form-data"  action="'.$p_action.'" method="post">';
00255       $t=new widget("text");
00256       $t->name="md_name";
00257       $r.=" Nom ".$t->IOValue()."";
00258       $r.="Type de document ";
00259       $w=new widget("select");
00260       $w->name="md_type";
00261       $w->value=make_array($this->cn,'select dt_id,dt_value from document_type');
00262       $r.=$w->IOValue();
00263       $r.="</p><p>";
00264       $f=new widget("file");
00265       $f->name="doc";
00266       $r.="fichier ".$f->IOValue()."";
00267       // we need to add the sub action as hidden
00268       $h=new widget("hidden");
00269       $h->name="sa";
00270       $h->value="add_document";
00271       $r.=$h->IOValue();
00272       $start=new widget("text");
00273       $start->name="start_seq";
00274       $start->size=9;
00275       $start->value="0";
00276       $r.=" Numerotation commence a ".$start->IOValue()."";
00277       $r.=$w->Submit('add_document','Ajout');
00278       $r.="</form></p>";
00279       return $r;
00280     }
00281 
00282 }
00283 ?>

Generated on Fri Jun 23 00:12:45 2006 for phpcompta by  doxygen 1.4.4