Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_rapav_formulaire.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of NOALYSS.
5  *
6  * NOALYSS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * NOALYSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with NOALYSS; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 /* $Revision$ */
21 
22 // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
23 
24 /**
25  * @file
26  * @brief Manage les formulaires
27  *
28  */
29 require_once 'class_rapport_avance_sql.php';
30 require_once 'class_formulaire_param.php';
31 
32 class RAPAV_Formulaire extends Formulaire_Sql
33 {
34 
35  function __construct($f_id = -1)
36  {
37  $this->f_id = $f_id;
38  $this->definition = array();
39  parent::__construct($f_id);
40  }
41 
42  /**
43  * show a list of all existing declaration
44  * @global type $cn database connection
45  */
46  static function listing()
47  {
48  global $cn;
49  $alist = $cn->get_array("select f_id,f_title,f_description
50  from rapport_advanced.formulaire order by 2");
51  require 'template/formulaire_listing.php';
52  }
53  /**
54  * Anchor to the template
55  * @return html anchor string
56  */
57  function anchor_document()
58  {
59  $url=HtmlInput::request_to_string(array('gDossier','ac','plugin_code'));
60  $url='extension.raw.php'.$url.'&amp;act=export_definition_modele&amp;id='.$this->f_id;
61  return HtmlInput::anchor($this->f_filename,$url);
62  }
63  /**
64  * Get data from database, from the table rapport_advanced.formulaire_param
65  */
66  function load_definition()
67  {
68  $f = new Formulaire_Param_Sql();
69  $ret = $f->seek(" where f_id=" . sql_string($this->f_id) . " order by p_order ");
70  $max = Database::num_row($ret);
71 
72  for ($i = 0; $i < $max; $i++)
73  {
74  $o = new Formulaire_Param_Sql();
75  $o = $f->next($ret, $i);
76  $this->definition[] = clone $o;
77  }
78  }
79 
80  function input_formulaire()
81  {
82  $this->load();
83  require_once 'template/formulaire_titre.php';
84  }
85 
86  /**
87  * input the definition
88  */
89  function input_definition()
90  {
91  $max = count($this->definition);
92  global $cn;
93 
94  require 'template/formulaire_definition.php';
95  }
96 
97  /**
98  * save the definition
99  * $p_array contains
100  * - f_id id of the formulaire
101  * - f_title title of the formulaire
102  * - f_description description of the formulaire
103  * - p_id array of the row in formulaire_param
104  * - p_code array of the row in formulaire_param
105  * - p_libelle array of the row in formulaire_param
106  * - p_type array of the row in formulaire_param
107  * - t_id array of the row in formulaire_param
108  * - p_order array of the row in formulaire_param
109  *
110  */
111  static function save_definition($p_array)
112  {
113  self::verify_definition($p_array);
114  self::update_definition($p_array);
115  return;
116  }
117  /**
118  * @brief Check data and change them if needed
119  * @global database connection $cn
120  * @param array $p_array normally $_POST
121  */
122  static function verify_definition(&$p_array)
123  {
124  global $cn;
125  $count_code=count($p_array['p_code']);
126  for ($i=0;$i<$count_code;$i++)
127  {
128  $c=$cn->get_value('select count(*) from rapport_advanced.formulaire_param
129  where p_code=$1 and p_id <> $2 and f_id=$3',
130  array($p_array['p_code'][$i],$p_array['p_id'][$i],$p_array['f_id']));
131 
132  if ( $c > 0 ) {
133  $p_array['p_code'][$i]=RAPAV_Formulaire::generate_code();
134  }
135  }
136 
137  for ($i=0;$i<$count_code;$i++)
138  {
139  for ($e=0;$e<$count_code;$e++) {
140  if ($p_array['p_code'][$i] == $p_array['p_code'][$e] && $i != $e)
141  {
142  $p_array['p_code'][$e]= RAPAV_Formulaire::generate_code();
143  }
144  }
145  }
146  }
147 
148  /**
149  *
150  * @see save_definition
151  * @param type $p_array
152  */
153  static function update_definition($p_array)
154  {
155  global $cn;
156  $rapav = new RAPAV_Formulaire($p_array['f_id']);
157  // save into table formulaire
158  $rapav->f_title = $p_array['f_title'];
159  $rapav->f_description = $p_array['f_description'];
160  $rapav->update();
161  $nb_line=count($p_array['p_id']);
162  for ($i = 0; $i < $nb_line ; $i++)
163  {
164  $form_param = new formulaire_param_sql($p_array['p_id'][$i]);
165  $form_param->p_code = (trim($p_array['p_code'][$i])!="")?$p_array['p_code'][$i]: RAPAV_Formulaire::generate_code();
166  // remove space from p_code
167  $form_param->p_code=str_replace(' ', "",$form_param->p_code);
168 
169  $form_param->p_libelle = trim($p_array['p_libelle'][$i]);
170  if ( $form_param->p_libelle == '') continue;
171  $form_param->p_type = $p_array['p_type'][$i];
172  $form_param->p_order = (isNumber($p_array['p_order'][$i]) == 0) ? ($i+1) * 10 : $p_array['p_order'][$i];
173  $form_param->t_id = $p_array['t_id'][$i];
174  $form_param->f_id = $p_array['f_id'];
175  // update or insert the row
176  if ($p_array['p_id'][$i] == -1)
177  $form_param->insert();
178  else
179  $form_param->update();
180  }
181 
182  // delete checked rows
183  if ( isset ($p_array["del_row"]))
184  {
185  for ($i=0;$i<count($p_array['del_row']);$i++)
186  {
187  if (isNumber($p_array['del_row'][$i]) == 1 && $p_array['del_row'][$i]!=-1) {
188  $cn->exec_sql('delete from rapport_advanced.formulaire_param where p_id=$1',array($p_array['del_row'][$i]));
189  }
190  }
191  }
192  // Reorder
193  //
194  $order_param=new formulaire_param_sql();
195  $ret=$order_param->seek(' where f_id=$1 order by p_order',array($p_array['f_id']));
196  $nb_rows=Database::num_row($ret);
197  for ($i=0;$i < $nb_rows;$i++)
198  {
199  $param=$order_param->next($ret,$i);
200  $param->p_order=($i+1)*10;
201  $param->update();
202  }
203  self::load_file($rapav);
204  }
205 
206  static function load_file(RAPAV_Formulaire $p_rapav)
207  {
208  global $cn;
209  // nothing to save
210  if (sizeof($_FILES) == 0)
211  return;
212 
213  // Start Transaction
214  $cn->start();
215  $name = $_FILES['rapav_template']['name'];
216  $new_name = tempnam($_ENV['TMP'], 'rapav_template');
217  // check if a file is submitted
218  if (strlen($_FILES['rapav_template']['tmp_name']) != 0)
219  {
220  // upload the file and move it to temp directory
221  if (move_uploaded_file($_FILES['rapav_template']['tmp_name'], $new_name))
222  {
223  $oid = $cn->lo_import($new_name);
224  // check if the lob is in the database
225  if ($oid == false)
226  {
227  $cn->rollback();
228  return 1;
229  }
230  }
231  // the upload in the database is successfull
232  $p_rapav->f_lob = $oid;
233  $p_rapav->f_filename = $_FILES['rapav_template']['name'];
234  $p_rapav->f_mimetype = $_FILES['rapav_template']['type'];
235  $p_rapav->f_size= $_FILES['rapav_template']['size'];
236 
237  // update rapav
238  $p_rapav->update();
239  }
240  $cn->commit();
241  }
242 
243  function echo_formulaire()
244  {
245  echo '<h2>' . h($this->f_title) . '</h2>';
246  echo '<p>' . h($this->f_description) . '<p>';
247  }
248 
249  function input_parameter()
250  {
251  $max = count($this->definition);
252  for ($i = 0; $i < $max; $i++)
253  {
254  $obj = Formulaire_Param::factory($this->definition[$i]);
255 
256  $obj->input();
257  }
258  }
259  /**
260  * @brief remove a doc template
261  */
263  {
264  global $cn;
265  $cn->lo_unlink($this->f_lob);
266  $this->f_filename=null;
267  $this->f_size=null;
268  $this->f_mimetype=null;
269  $this->f_lob=null;
270  $this->update();
271  }
272  static function generate_code() {
273  $a_possible= explode(',', 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9');
274  $max=count($a_possible);
275  $max--;
276  $code=$a_possible[rand(0,$max)].
277  $a_possible[rand(0,$max)].
278  $a_possible[rand(0,$max)].
279  $a_possible[rand(0, $max)].
280  $a_possible[rand(0, $max)].
281  $a_possible[rand(0, $max)]
282  ;
283  return$code;
284 
285  }
286 }
287 
288 ?>
$url
$f
Definition: show_field.php:4
$ret
remove_doc_template()
remove a doc template
input_definition()
input the definition
static verify_definition(&$p_array)
Check data and change them if needed database connection $cn.
static listing()
show a list of all existing declaration type $cn database connection
static load_file(RAPAV_Formulaire $p_rapav)
load_definition()
Get data from database, from the table rapport_advanced.formulaire_param.
static update_definition($p_array)
static save_definition($p_array)
save the definition $p_array contains
static factory(Formulaire_Param_Sql $form)
Factory, create an object following the $form->p_type,.
anchor_document()
Anchor to the template.
global $cn
$name