00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 require_once('class_icheckbox.php');
00027 require_once('class_ihidden.php');
00028 require_once('class_ifile.php');
00029 class Document_modele
00030 {
00031 var $cn;
00032 var $md_id;
00033 var $md_name;
00034 var $md_type;
00035 var $md_lob;
00036 var $md_sequence;
00037 var $sequence;
00038 var $md_affect;
00039
00040 function Document_modele($p_cn,$p_id=-1)
00041 {
00042 $this->cn=$p_cn;
00043 $this->md_id=$p_id;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053 function myList()
00054 {
00055 $s=dossier::get();
00056 $sql="select md_id,md_name,md_affect,dt_value from document_modele join document_type on(dt_id=md_type) order by md_name";
00057 $Res=$this->cn->exec_sql($sql);
00058 $all=Database::fetch_all($Res);
00059 $r='';
00060 if ( Database::num_row($Res) != 0 ) {
00061
00062 $r.='<p><form method="post">';
00063 $r.=dossier::hidden();
00064 $r.="<table>";
00065 $r.="<tr> ";
00066 $r.=th(_('Nom'));
00067 $r.=th(_('Catégorie'));
00068 $r.=th(_('Affect.'));
00069 $r.=th(_('Fichier'));
00070 $r.=th(_('Effacer'));
00071 $r.="</tr>";
00072 foreach ( $all as $row)
00073 {
00074 $r.="<tr>";
00075 $r.="<td>";
00076 $r.=h($row['md_name']);
00077 $r.="</td>";
00078 $r.="<td>";
00079 $r.=$row['dt_value'];
00080 $r.="</td>";
00081 $r.=td(h($row['md_affect']));
00082 $r.="<td>";
00083 $r.='<A HREF="show_document_modele.php?md_id='.$row['md_id'].'&'.$s.'">Document</a>';
00084 $r.="</td>";
00085 $r.="<TD>";
00086 $c=new ICheckBox();
00087 $c->name="dm_remove_".$row['md_id'];
00088 $r.=$c->input();
00089 $r.="</td>";
00090 $r.=td(HtmlInput::detail_modele_document($row['md_id'],'Modifier'));
00091
00092 $r.="</tr>";
00093 }
00094 $r.="</table>";
00095
00096
00097 $a=new IHidden();
00098 $a->name="sa";
00099 $a->value="rm_template";
00100 $r.=$a->input();
00101 $r.=HtmlInput::submit("rm_template","Effacer la sélection");
00102 }
00103 $b=new IButton('show');
00104 $b->label="Ajout d'un document";
00105 $b->javascript="$('add_modele').style.display='block';new Draggable('add_modele',{starteffect:function(){
00106 new Effect.Highlight(obj.id,{scroll:window,queue:'end'});}});";
00107 $r.=$b->input();
00108 $r.="</form></p>";
00109 return $r;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 function Save()
00121 {
00122
00123 if ( trim(strlen($this->md_name))==0)
00124 return;
00125 try
00126 {
00127
00128 $this->cn->start();
00129
00130
00131
00132
00133 if ( $this->md_id == -1)
00134 {
00135
00136
00137 $this->md_id=$this->cn->get_next_seq('document_modele_md_id_seq');
00138 $sql="insert into document_modele(md_id,md_name,md_type,md_affect)
00139 values ($1,$2,$3,$4)";
00140
00141 $Ret=$this->cn->exec_sql($sql,array($this->md_id,$this->md_name,$this->md_type,$this->md_affect));
00142
00143 $this->md_sequence="document_".$this->cn->get_next_seq("document_seq");
00144
00145
00146
00147 if ( $this->start != 0 && isNumber($this->start) == 1 )
00148 {
00149 $sql="alter sequence seq_doc_type_".$this->md_type." restart ".$this->start;
00150 $this->cn->exec_sql($sql);
00151 }
00152
00153 }
00154
00155 $new_name=tempnam($_ENV['TMP'],'document_');
00156 if ( strlen ($_FILES['doc']['tmp_name']) != 0 )
00157 {
00158 if (move_uploaded_file($_FILES['doc']['tmp_name'],
00159 $new_name))
00160 {
00161
00162 $oid= $this->cn->lo_import($new_name);
00163 if ( $oid == false )
00164 {
00165 echo_error('class_document_modele.php',__LINE__,"cannot upload document");
00166 $this->cn->rollback();
00167 return;
00168 }
00169
00170 $ret=$this->cn->exec_sql("select md_lob from document_modele where md_id=".$this->md_id);
00171 if (Database::num_row($ret) != 0)
00172 {
00173 $r=Database::fetch_array($ret,0);
00174 $old_oid=$r['md_lob'];
00175 if (strlen($old_oid) != 0)
00176 $this->cn->lo_unlink($old_oid);
00177 }
00178
00179 $this->cn->exec_sql("update document_modele set md_lob=".$oid.", md_mimetype='".$_FILES['doc']['type']."' ,md_filename='".$_FILES['doc']['name']."' where md_id=".$this->md_id);
00180 $this->cn->commit();
00181 }
00182 else
00183 {
00184 echo "<H1>Error</H1>";
00185 $this->cn->rollback();
00186 exit;
00187 }
00188 }
00189 }
00190 catch (Exception $e)
00191 {
00192 rollback($this->cn);
00193 return ;
00194 }
00195 }
00196
00197
00198
00199
00200 function Delete()
00201 {
00202 $this->cn->start();
00203
00204 $sql="select md_lob from document_modele where md_id=".$this->md_id;
00205 $res=$this->cn->exec_sql($sql);
00206 $r=Database::fetch_array($res,0);
00207
00208 if ( strlen ($r['md_lob']) &&
00209 $this->cn->exist_blob($r['md_lob']) )
00210 {
00211
00212 $this->cn->lo_unlink($r['md_lob']);
00213 }
00214
00215 $sql="delete from document_modele where md_id =".$this->md_id;
00216 $sql=$this->cn->exec_sql($sql);
00217 $this->cn->commit();
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227 function form()
00228 {
00229 $r='<p class="notice">';
00230 $r.='Veuillez introduire les modèles servant à générer vos documents';
00231 $r.='</p>';
00232 $r.='<form enctype="multipart/form-data" method="post">';
00233 $r.=dossier::hidden();
00234
00235 $h=new IHidden();
00236 $h->name="sa";
00237 $h->value="add_document";
00238
00239 $r.=$h->input();
00240
00241 $r.='<table>';
00242 $t=new IText();
00243 $t->name="md_name";
00244 $r.="<tr><td> Nom </td><td>".$t->input()."</td>";
00245
00246 $r.="</tr>";
00247 $r.="<tr><td>Catégorie de document </td>";
00248 $w=new ISelect();
00249 $w->name="md_type";
00250
00251 $w->value=$this->cn->make_array('select dt_id,dt_value from document_type order by dt_value');
00252 $r.="<td>".$w->input()."</td></tr>";
00253
00254 $r.='<tr>';
00255 $r.=td(_('Affectation'));
00256 $waffect=new ISelect();
00257 $waffect->name='md_affect';
00258 $waffect->value=array(
00259 array('value'=>'ACH','label'=>_('Uniquement journaux achat')),
00260 array('value'=>'VEN','label'=>_('Uniquement journaux vente')),
00261 array('value'=>'GES','label'=>_('Partie gestion'))
00262 );
00263
00264 $r.=td($waffect->input());
00265 $r.='</tr>';
00266
00267 $f=new IFile();
00268 $f->name="doc";
00269 $r.="<tr><td>fichier</td><td> ".$f->input()."</td></tr>";
00270
00271 $start=new IText();
00272 $start->name="start_seq";
00273 $start->size=9;
00274 $start->value="0";
00275
00276 $r.="<tr><td> Numerotation commence a</td><td> ".$start->input()."</td>";
00277 $r.='<td class="notice">Si vous laissez à 0, la numérotation ne changera pas, la prochaine facture sera n+1, n étant le n° que vous avez donné</td>';
00278 $r.="</tr>";
00279 $r.='</table>';
00280 $r.=HtmlInput::submit('add_document','Ajout');
00281 $r.="</form></p>";
00282 return $r;
00283 }
00284
00285
00286 function load()
00287 {
00288 $array=$this->cn->get_array("SELECT md_id, md_name, md_lob, md_type, md_filename, md_mimetype,md_affect".
00289 " FROM document_modele where md_id=$1",array($this->md_id));
00290 if ( count($array) == 0 ) return null;
00291 foreach ( array('md_name', 'md_lob','md_type', 'md_filename', 'md_mimetype','md_affect') as $idx)
00292 {
00293 $this->$idx=$array[0][$idx];
00294 }
00295 }
00296
00297
00298
00299 function update($p_array)
00300 {
00301 $this->load();
00302
00303 if ( trim(strlen($p_array['md_name']))==0)
00304 return;
00305 try
00306 {
00307
00308 $this->cn->start();
00309 $sql="update document_modele set md_name=$1,md_type=$2,md_affect=$3 where md_id=$4";
00310 $this->cn->exec_sql($sql,array(
00311 $p_array['md_name'],
00312 $p_array['md_type'],
00313 $p_array['md_affect'],
00314 $this->md_id
00315 ));
00316 if ( $p_array['seq'] != 0 )
00317 $this->cn->alter_seq('seq_doc_type_'.$p_array['md_type'],$p_array['seq']);
00318
00319
00320 $new_name=tempnam($_ENV['TMP'],'document_');
00321 if ( strlen ($_FILES['doc']['tmp_name']) != 0 )
00322 {
00323 if (move_uploaded_file($_FILES['doc']['tmp_name'],
00324 $new_name))
00325 {
00326
00327 $oid= $this->cn->lo_import($new_name);
00328 if ( $oid == false )
00329 {
00330 echo_error('class_document_modele.php',__LINE__,"cannot upload document");
00331 $this->cn->rollback();
00332 return;
00333 }
00334
00335 $ret=$this->cn->exec_sql("select md_lob from document_modele where md_id=".$this->md_id);
00336 if (Database::num_row($ret) != 0)
00337 {
00338 $r=Database::fetch_array($ret,0);
00339 $old_oid=$r['md_lob'];
00340 if (strlen($old_oid) != 0)
00341 $this->cn->lo_unlink($old_oid);
00342 }
00343
00344 $this->cn->exec_sql("update document_modele set md_lob=".$oid.", md_mimetype='".$_FILES['doc']['type']."' ,md_filename='".$_FILES['doc']['name']."' where md_id=".$this->md_id);
00345 $this->cn->commit();
00346 }
00347 else
00348 {
00349 echo "<H1>Error</H1>";
00350 $this->cn->rollback();
00351 exit;
00352 }
00353 }
00354 }
00355 catch (Exception $e)
00356 {
00357 rollback($this->cn);
00358 return ;
00359 }
00360 $this->cn->commit();
00361 }
00362
00363 }
00364 ?>