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: 1.2 $ */
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           echo_debug('class_document_modele',__LINE__, "this->start ".$this->start." a number ".isNumber($this->start));
00129 
00130           if ( $this->start != 0 && isNumber($this->start) == 1 )
00131             {
00132               $sql="alter sequence seq_doc_type_".$this->md_type." restart ".$this->start;
00133               ExecSql($this->cn,$sql);
00134             }
00135           
00136         }
00137       // Save the file
00138       $new_name=tempnam('/tmp','document_');
00139       echo_debug('class_document_modele.php',__LINE__,"new name=".$new_name);
00140       if ( strlen ($_FILES['doc']['tmp_name']) != 0 ) 
00141         {
00142           if (move_uploaded_file($_FILES['doc']['tmp_name'],
00143                                  $new_name)) 
00144             {
00145                     // echo "Image saved";
00146               $oid= pg_lo_import($this->cn,$new_name);
00147               if ( $oid == false ) 
00148                 {
00149                   echo_error('class_document_modele.php',__LINE__,"cannot upload document");
00150                   Rollback($cn);
00151                   return;
00152                 }
00153               echo_debug('class_document_modele.php',__LINE__,"Loading document");
00154               // Remove old document
00155               $ret=ExecSql($this->cn,"select md_lob from document_modele where md_id=".$this->md_id);
00156               if (pg_num_rows($ret) != 0) 
00157                 {
00158                   $r=pg_fetch_array($ret,0);
00159                   $old_oid=$r['md_lob'];
00160                   if (strlen($old_oid) != 0) 
00161                     pg_lo_unlink($this->cn,$old_oid);
00162                 }
00163               // Load new document
00164               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);
00165               Commit($this->cn);
00166             }
00167           else 
00168             {
00169               echo "<H1>Error</H1>";
00170               Rollback($this->cn);
00171               exit;
00172             }
00173         }
00174     }
00179   function Delete() 
00180     {
00181       StartSql($this->cn);
00182       // first we unlink the document
00183       $sql="select md_lob from document_modele where md_id=".$this->md_id;
00184       $res=ExecSql($this->cn,$sql);
00185       $r=pg_fetch_array($res,0);
00186       // if a lob is found
00187       if ( strlen ($r['md_lob']) != 0 )
00188         {
00189           // we remove it first
00190           pg_lo_unlink($r['md_lob']);
00191         }
00192       // now we can delete the row
00193       $sql="delete from document_modele where md_id =".$this->md_id;
00194       $sql=ExecSql($this->cn,$sql);
00195       Commit($this->cn);
00196     }
00197   
00204   function Get($p_id,$p_internal) 
00205     {
00206       // retrieve info from the table invoice
00207       
00208       // retrieve the template and generate document
00209       StartSql($this->cn);
00210       $ret=ExecSql($cn,"select iv_name,iv_file from invoice where iv_id".$this->iv_id);
00211       if ( pg_num_rows ($ret) == 0 )
00212         return;
00213       $row=pg_fetch_array($ret,0);
00214       //the template is saved into file $tmp
00215       $tmp=tempnam('/tmp/','invoice_');
00216       pg_lo_export($cn,$row['iv_file'],$tmp);
00217       // Parse the file 
00218       
00219       
00220       
00221       
00222       // send it to stdout
00223       ini_set('zlib.output_compression','Off');
00224       header("Pragma: public");
00225       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00226       header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00227       header("Cache-Control: must-revalidate");
00228       header('Content-type: rtf/x-application');
00229       header('Content-Disposition: attachment;filename="invoice'.$p_internal.'.rtf"',FALSE);
00230       header("Accept-Ranges: bytes");
00231       $file=fopen($tmp,'r');
00232       while ( !feof ($file) )
00233         {
00234           echo fread($file,8192);
00235         }
00236       fclose($file);
00237       
00238       unlink ($tmp);
00239       
00240       Commit($cn);
00241       
00242     }
00252   function form($p_action) 
00253     {
00254       $r="<p>";
00255       $r.='<form enctype="multipart/form-data"  action="'.$p_action.'" method="post">';
00256       $t=new widget("text");
00257       $t->name="md_name";
00258       $r.=" Nom ".$t->IOValue()."";
00259       $r.="Type de document ";
00260       $w=new widget("select");
00261       $w->name="md_type";
00262       $w->value=make_array($this->cn,'select dt_id,dt_value from document_type');
00263       $r.=$w->IOValue();
00264       $r.="</p><p>";
00265       $f=new widget("file");
00266       $f->name="doc";
00267       $r.="fichier ".$f->IOValue()."";
00268       // we need to add the sub action as hidden
00269       $h=new widget("hidden");
00270       $h->name="sa";
00271       $h->value="add_document";
00272       $r.=$h->IOValue();
00273       $start=new widget("text");
00274       $start->name="start_seq";
00275       $start->size=9;
00276       $start->value="0";
00277       $r.=" Numerotation commence a ".$start->IOValue()."";
00278       $r.=$w->Submit('add_document','Ajout');
00279       $r.="</form></p>";
00280       return $r;
00281     }
00282 
00283 }
00284 ?>