00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
00106 if ( trim(strlen($this->md_name))==0)
00107 return;
00108
00109 StartSql($this->cn);
00110
00111
00112
00113
00114 if ( $this->md_id == -1)
00115 {
00116
00117
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
00125 $this->md_sequence="document_".NextSequence($this->cn,"document_seq");
00126
00127
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
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
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
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
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
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
00186 if ( strlen ($r['md_lob']) != 0 )
00187 {
00188
00189 pg_lo_unlink($r['md_lob']);
00190 }
00191
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
00206
00207
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
00214 $tmp=tempnam('/tmp/','invoice_');
00215 pg_lo_export($cn,$row['iv_file'],$tmp);
00216
00217
00218
00219
00220
00221
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
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 ?>