noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_document_modele.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS 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  *   NOALYSS 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 NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 /*! \file
00021  * \brief Class for the document template
00022  */
00023 /*!
00024  * \brief Class for the document template
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;            /*!< $cn  database connection */
00032     var $md_id;         /*!< $md_id pk */
00033     var $md_name;         /*!< $md_name template's name */
00034     var $md_type;         /*!< $md_type template's type (letter, invoice, order...) */
00035     var $md_lob;          /*!< $md_lob Document file */
00036     var $md_sequence;     /*!<  $md_sequence sequence name (autogenerate) */
00037     var $sequence;        /*!< $sequence sequence number used by the create sequence start with */
00038     var $md_affect;     /*!< $md_affect if you can use it in VEN for sale, ACH for purchase or GES for follow-up */
00039     //Constructor parameter = database connexion
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      * \brief : show all the stored document_modele.
00049      *        return a string containing all the data
00050      *        separate by TD & TR tag
00051      * \return table in HTML Code
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           // need hidden parameter for subaction
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      * \brief :  Save a document_modele in the database,
00113      *       if the document_modele doesn't exist yet it will be
00114      *       first created (-> insert into document_modele)
00115      *       in that case the name and the type must be set
00116      *       set before calling Save, the name will be modified
00117      *       with sql_string
00118      *
00119      */
00120     function Save()
00121     {
00122         // if name is empty return immediately
00123         if ( trim(strlen($this->md_name))==0)
00124             return;
00125         try
00126         {
00127             // Start transaction
00128             $this->cn->start();
00129             // Save data into the table document_modele
00130             // if $this->md_id == -1 it means it is a new document model
00131             // so first we have to insert it
00132             // the name and the type must be set before calling save
00133             if ( $this->md_id == -1)
00134             {
00135 
00136                 // insert into the table document_modele
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                 // create the sequence for this modele of document
00143                 $this->md_sequence="document_".$this->cn->get_next_seq("document_seq");
00144                 // if start is not equal to 0 and he's a number than the user
00145                 // request a number change
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             // Save the file
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                     // echo "Image saved";
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                     // Remove old document
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                     // Load new document
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      * \brief Remove a template
00198      * \return nothing
00199      */
00200     function Delete()
00201     {
00202         $this->cn->start();
00203         // first we unlink the document
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         // if a lob is found
00208         if ( strlen ($r['md_lob']) &&
00209                 $this->cn->exist_blob($r['md_lob']) )
00210         {
00211             // we remove it first
00212             $this->cn->lo_unlink($r['md_lob']);
00213         }
00214         // now we can delete the row
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      * @brief show the form for loading a template
00222      * @param p_action for the field action = destination url
00223      *
00224      *
00225      * @return string containing the forms
00226      */
00227     function form()
00228     {
00229         $r='<p class="notice">';
00230         $r.='Veuillez introduire les mod&egrave;les servant à g&eacute;n&eacute;rer vos documents';
00231         $r.='</p>';
00232         $r.='<form enctype="multipart/form-data"  method="post">';
00233         $r.=dossier::hidden();
00234         // we need to add the sub action as hidden
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 &agrave; 0, la num&eacute;rotation ne changera pas, la prochaine facture sera n+1, n étant le n° que vous avez donn&eacute;</td>';
00278         $r.="</tr>";
00279         $r.='</table>';
00280         $r.=HtmlInput::submit('add_document','Ajout');
00281         $r.="</form></p>";
00282         return $r;
00283     }
00284     /*!\brief load the value of a document_modele,the ag_id variable must be set
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      * \brief :  update a document_modele in the database,
00298      */
00299     function update($p_array)
00300     {
00301       $this->load();
00302         // if name is empty return immediately
00303         if ( trim(strlen($p_array['md_name']))==0)
00304             return;
00305         try
00306         {
00307             // Start transaction
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             // Save the file
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                     // echo "Image saved";
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                     // Remove old document
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                     // Load new document
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 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations