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 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
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
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
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
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
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
00187 if ( strlen ($r['md_lob']) != 0 )
00188 {
00189
00190 pg_lo_unlink($r['md_lob']);
00191 }
00192
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
00207
00208
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
00215 $tmp=tempnam('/tmp/','invoice_');
00216 pg_lo_export($cn,$row['iv_file'],$tmp);
00217
00218
00219
00220
00221
00222
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
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 ?>