noalyss Version-9
document_modele.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19// Copyright Author Dany De Bontridder danydb@aevalys.eu
20/*! \file
21 * \brief Class for the document template
22 */
23/*!
24 * \brief Class for the document template
25 */
27{
28 var $cn; /*!< $cn database connection */
29 var $md_id; /*!< $md_id pk */
30 var $md_name; /*!< $md_name template's name */
31 var $md_type; /*!< $md_type template's type (letter, invoice, order...) */
32 var $md_lob; /*!< $md_lob Document file */
33 var $md_sequence; /*!< $md_sequence sequence name (autogenerate) */
34 var $sequence; /*!< $sequence sequence number used by the create sequence start with */
35 var $md_affect; /*!< $md_affect if you can use it in VEN for sale, ACH for purchase or GES for follow-up */
36 var $md_filename; /*! < $md_filename is the filename of the template */
37 var $start; /*! < $md_filename is the filename of the template */
38 //Constructor parameter = database connexion
39 function __construct($p_cn,$p_id=-1)
40 {
41 $this->cn=$p_cn;
42 $this->md_id=$p_id;
43 $this->start=0;
44 }
45
46 /*!
47 **************************************************
48 * \brief : show all the stored document_modele.
49 * return a string containing all the data
50 * separate by TD & TR tag
51 * \return table in HTML Code
52 */
53 function myList()
54 {
55 $dosid=dossier::id();
56 $sql="select md_id,md_name,md_affect,dt_value,md_filename from document_modele join document_type on(dt_id=md_type) order by md_name";
57 $Res=$this->cn->exec_sql($sql);
59 $r='';
60 if ( Database::num_row($Res) != 0 ) {
61
62 $r.='<p><form method="post">';
63 $r.=dossier::hidden();
64 $r.=\HtmlInput::filter_table("document_template_tb",'0,1,2',1);
65 $r.='<table id="document_template_tb" class="result" >';
66 $r.="<tr> ";
67 $r.=th(_('Nom'));
68 $r.=th(_('Catégorie'));
69 $r.=th(_('Affect.'));
70 $r.=th(_('Fichier'));
71 $r.=th(_('Effacer'));
72 $r.="</tr>";
73 $idx=0;
74 foreach ( $all as $row)
75 {
76 $even=($idx%2==0)?'class="odd"':'class="even"';
77 $idx++;
78 $r.="<tr $even>";
79 $r.="<td>";
80 $r.=h($row['md_name']);
81 $r.="</td>";
82 $r.="<td>";
83 $r.=$row['dt_value'];
84 $r.="</td>";
85 $r.=td(h($row['md_affect']));
86 $r.="<td>";
87 $href=http_build_query(array('gDossier'=>$dosid,'md_id'=>$row['md_id'],'act'=>'RAW:document_template'));
88 $r.='<A HREF="export.php?'.$href.'">'.h($row['md_filename']).'</a>';
89 $r.="</td>";
90 $r.="<TD>";
91 $c=new ICheckBox();
92 $c->name="dm_remove_".$row['md_id'];
93 $r.=$c->input();
94 $r.="</td>";
95 $r.=td(HtmlInput::detail_modele_document($row['md_id'],_('Modifier')));
96
97 $r.="</tr>";
98 }
99 $r.="</table>";
100
101 // need hidden parameter for subaction
102 $a=new IHidden();
103 $a->name="sa";
104 $a->value="rm_template";
105 $r.=$a->input();
106 $r.=HtmlInput::submit("rm_template",_("Effacer la sélection"));
107 } else {
108 echo h2(_("Aucun document"));
109 echo _("Il n'y a aucun document, cliquez sur Ajouter pour en ajouter un");
110 }
111 $b=new IButton('show');
112 $b->label="Ajout d'un document";
113 $b->javascript="$('add_modele').style.display='block';new Draggable('add_modele',{starteffect:function(){
114 new Effect.Highlight(obj.id,{scroll:window,queue:'end'});}});";
115 $r.=$b->input();
116 $r.="</form></p>";
117 return $r;
118 }
119 /*!
120 * \brief : Save a document_modele in the database,
121 * if the document_modele doesn't exist yet it will be
122 * first created (-> insert into document_modele)
123 * in that case the name and the type must be set
124 * set before calling Save, the name will be modified
125 * with sql_string
126 *
127 */
128 function Save()
129 {
130 // if name is empty return immediately
131 if ( trim(strlen($this->md_name))==0)
132 return;
133 try
134 {
135 // Start transaction
136 $this->cn->start();
137 // Save data into the table document_modele
138 // if $this->md_id == -1 it means it is a new document model
139 // so first we have to insert it
140 // the name and the type must be set before calling save
141 if ( $this->md_id == -1)
142 {
143
144 // insert into the table document_modele
145 $this->md_id=$this->cn->get_next_seq('document_modele_md_id_seq');
146 $sql="insert into document_modele(md_id,md_name,md_type,md_affect)
147 values ($1,$2,$3,$4)";
148
149 $Ret=$this->cn->exec_sql($sql,array($this->md_id,$this->md_name,$this->md_type,$this->md_affect));
150 // create the sequence for this modele of document
151 $this->md_sequence="document_".$this->cn->get_next_seq("document_seq");
152 // if start is not equal to 0 and he's a number than the user
153 // request a number change
154
155 if ( $this->start != 0 && isNumber($this->start) == 1 )
156 {
157 $sql="alter sequence seq_doc_type_".$this->md_type." restart ".$this->start;
158 $this->cn->exec_sql($sql);
159 }
160
161 }
162 // Save the file
163 $new_name=tempnam($_ENV['TMP'],'document_');
164 if ( strlen ($_FILES['doc']['tmp_name']) != 0 )
165 {
166 if ($this->move_uploaded_file($_FILES['doc']['tmp_name'],
167 $new_name))
168 {
169 // echo "Image saved";
170 $oid= $this->cn->lo_import($new_name);
171 if ( $oid == false )
172 {
173 echo_error('class/class_document_modele.php',__LINE__,"cannot upload document");
174 $this->cn->rollback();
175 return;
176 }
177 // Remove old document
178 $ret=$this->cn->exec_sql("select md_lob from document_modele where md_id=".$this->md_id);
179 if (Database::num_row($ret) != 0)
180 {
182 $old_oid=$r['md_lob'];
183 if (strlen(""??$old_oid) != 0)
184 $this->cn->lo_unlink($old_oid);
185 }
186 // Load new document
187 $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);
188 $this->cn->commit();
189 }
190 else
191 {
192 echo "<H1>"._("Erreur opération annulée")."</H1>";
193 $this->cn->rollback();
194 throw new Exception("DM191 : error loading template".__FILE__.__LINE__);
195 }
196 }
197 }
198 catch (Exception $e)
199 {
200 record_log($e);
201 $this->cn->rollback();
202 return ;
203 }
204 }
205 function move_uploaded_file($temporary_name, $target_path)
206 {
207 return move_uploaded_file ($temporary_name, $target_path);
208 }
209 /*!
210 * \brief Remove a template
211 * \return nothing
212 */
213 function Delete()
214 {
215 $this->cn->start();
216 // first we unlink the document
217 $sql="select md_lob from document_modele where md_id=$1";
218 $res=$this->cn->exec_sql($sql,[$this->md_id]);
220 // if a lob is found
221 if ( !empty ($r['md_lob']) &&$this->cn->exist_blob($r['md_lob']) )
222 {
223 // we remove it first
224 $this->cn->lo_unlink($r['md_lob']);
225 }
226 // now we can delete the row
227 $sql="delete from document_modele where md_id =$1";
228 $res=$this->cn->exec_sql($sql,[$this->md_id]);
229 $this->cn->commit();
230 }
231
232 /**
233 * @brief show the form for loading a template
234 * @param p_action for the field action = destination url
235 *
236 *
237 * @return string containing the forms
238 */
239 function form()
240 {
241 $r='<p class="notice">';
242 $r.='Veuillez introduire les mod&egrave;les servant à g&eacute;n&eacute;rer vos documents';
243 $r.='</p>';
244 $r.='<form enctype="multipart/form-data" method="post" onsubmit="return check_file_size(this,'. MAX_FILE_SIZE.')">';
245 $r.=\HtmlInput::hidden("MAX_FILE_SIZE", MAX_FILE_SIZE);
246 $r.=dossier::hidden();
247 // we need to add the sub action as hidden
248 $h=new IHidden();
249 $h->name="sa";
250 $h->value="add_document";
251
252 $r.=$h->input();
253
254 $r.='<table>';
255 $t=new IText();
256 $t->name="md_name";
257 $r.="<tr><td> Nom </td><td>".$t->input()."</td>";
258
259 $r.="</tr>";
260 $r.="<tr><td>Catégorie de document </td>";
261 $w=new ISelect();
262 $w->name="md_type";
263
264 $w->value=$this->cn->make_array('select dt_id,dt_value from document_type order by dt_value');
265 $r.="<td>".$w->input()."</td></tr>";
266
267 $r.='<tr>';
268 $r.=td(_('Affectation'));
269 $waffect=new ISelect();
270 $waffect->name='md_affect';
271 $waffect->value=$this->cn->make_array("select dc_code,dc_comment from public.document_component order by dc_code");
272
273 $r.=td($waffect->input());
274 $r.='</tr>';
275
276 $f=new IFile();
277 $f->name="doc";
278 $f->setAlertOnSize(true);
279 $r.="<tr><td>fichier</td><td> ".$f->input()."</td></tr>";
280
281 $start=new IText();
282 $start->name="start_seq";
283 $start->size=9;
284 $start->value="0";
285
286 $r.="<tr><td> Numerotation commence a</td><td> ".$start->input()."</td>";
287 $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>';
288 $r.="</tr>";
289 $r.='</table>';
290 $r.='<ul class="aligned-block">';
291 $r.='<li>';
292 $r.=HtmlInput::submit('add_document','Ajout');
293 $r.='</li>';
294 $r.='<li>';
295 $r.=HtmlInput::button_hide("add_modele");
296 $r.='</li>';
297 $r.="</form></p>";
298 return $r;
299 }
300 /*!\brief load the value of a document_modele,the ag_id variable must be set
301 */
302 function load()
303 {
304 $array=$this->cn->get_array("SELECT md_id, md_name, md_lob, md_type, md_filename, md_mimetype,md_affect".
305 " FROM document_modele where md_id=$1",array($this->md_id));
306 if ( count($array) == 0 ) return null;
307 foreach ( array('md_name', 'md_lob','md_type', 'md_filename', 'md_mimetype','md_affect') as $idx)
308 {
309 $this->$idx=$array[0][$idx];
310 }
311 }
312 /*!
313 * \brief : update a document_modele in the database,
314 */
315 function update($p_array)
316 {
317 $this->load();
318 // if name is empty return immediately
319 if ( trim(strlen($p_array['md_name']))==0)
320 return;
321 try
322 {
323 // Start transaction
324 $this->cn->start();
325 $sql="update document_modele set md_name=$1,md_type=$2,md_affect=$3 where md_id=$4";
326 $this->cn->exec_sql($sql,array(
327 $p_array['md_name'],
328 $p_array['md_type'],
329 $p_array['md_affect'],
330 $this->md_id
331 ));
332 if ( $p_array['seq'] != 0 )
333 $this->cn->alter_seq('seq_doc_type_'.$p_array['md_type'],$p_array['seq']);
334
335 // Save the file
336 $new_name=tempnam($_ENV['TMP'],'document_');
337 if ( strlen ($_FILES['doc']['tmp_name']) != 0 )
338 {
339 if ($this->move_uploaded_file($_FILES['doc']['tmp_name'],
340 $new_name))
341 {
342 // echo "Image saved";
343 $oid= $this->cn->lo_import($new_name);
344 if ( $oid == false )
345 {
346 echo_error('class/class_document_modele.php',__LINE__,"cannot upload document");
347 $this->cn->rollback();
348 return;
349 }
350 // Remove old document
351 $ret=$this->cn->exec_sql("select md_lob from document_modele where md_id=".$this->md_id);
352 if (Database::num_row($ret) != 0)
353 {
355 $old_oid=$r['md_lob'];
356 if (strlen($old_oid) != 0)
357 $this->cn->lo_unlink($old_oid);
358 }
359 // Load new document
360
361 $this->cn->exec_sql("update document_modele
362 set md_lob=$1,
363 md_mimetype=$2,
364 md_filename=$3
365 where md_id=$4",[
366 $oid,
367 $_FILES['doc']['type'],
368 $_FILES['doc']['name'],
369 $this->md_id]);
370 $this->cn->commit();
371 }
372 else
373 {
374 echo "<H1>Error</H1>";
375 $this->cn->rollback();
376 throw new Exception("Erreur".__FILE__.__LINE__);
377 }
378 }
379 }
380 catch (Exception $e)
381 {
382 record_log($e);
383 $this->cn->rollback();
384 return ;
385 }
386 $this->cn->commit();
387 }
388
389}
390?>
h2($p_string, $p_class="", $raw="")
Definition: ac_common.php:68
isNumber($p_int)
Definition: ac_common.php:215
th($p_string, $p_extra='', $raw='')
Definition: ac_common.php:58
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
Definition: ac_common.php:169
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
$href
Definition: adm.inc.php:31
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
$idx
foreach($array as $idx=> $m) $w
$input_from cn
Definition: balance.inc.php:66
$input_from id
Definition: balance.inc.php:63
static fetch_all($ret)
wrapper for the function pg_fetch_all
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static num_row($ret)
wrapper for the function pg_num_rows
Class for the document template.
update($p_array)
: update a document_modele in the database,
__construct($p_cn, $p_id=-1)
load()
load the value of a document_modele,the ag_id variable must be set
Save()
: Save a document_modele in the database, if the document_modele doesn't exist yet it will be first c...
move_uploaded_file($temporary_name, $target_path)
form()
show the form for loading a template
myList()
: show all the stored document_modele. return a string containing all the data separate by TD & TR ta...
Delete()
Remove a template.
static detail_modele_document($p_id, $p_mesg)
return a string containing the html code for calling the modifyModeleDocument
static button_hide($div_name)
Hide the HTML popup.
static filter_table($p_table_id, $p_col, $start_row)
filter the rows in a table and keep the colored row in alternance
static hidden($p_name, $p_value, $p_id="")
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
Html Input.
Html Input.
Html Input for uploading file, must be in a form with enctype="multipart/form-data".
Definition: ifile.class.php:31
Html Input.
Html Input , create a tag <SELECT> ... </SELECT> if readonly == true then display the label correspon...
Html Input.
Definition: itext.class.php:30
$all
$Res
$new_name