Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_import_card.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 /* $Revision$ */
20 // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
21 /**\file
22  * \brief Manage import
23  */
24 require __DIR__."/class_impcard_file_csv_sql.php";
25 require __DIR__."/class_impcard_format_sql.php";
27 {
28  private $record;
29  private $format;
30  function __construct($p_file_id=-1,$p_format_id=-1)
31  {
32  global $cn;
33  $this->record=new Impcard_File_Csv_SQL($cn, $p_file_id);
34  $this->format=new Importcard_Format_SQL($cn, $p_format_id);
35 
36  }
37  /**
38  * @brief fill the CSV format object Importcard_Format_SQL
39  * with default values
40  * @param int $p_fiche_card if 0 then f_position is empty otherwise , f_position
41  * contains the attribute of this card category
42  */
43  function create_format_temp($p_fiche_card)
44  {
45  global $cn;
46  $this->format->f_name='TEMP';
47  $this->format->f_unicode_encoding='Y';
48  $this->format->f_card_category=$p_fiche_card;
49  $this->format->f_skiprow=0;
50  $this->format->f_surround='"';
51  $this->format->f_delimiter=";";
52  $this->format->f_saved=0;
53  if ($p_fiche_card == 0 ){
54  $this->format->f_position="-1";
55  } else {
56  $a_attribute=$cn->make_list(
57  "select ad_id from jnt_fic_attr join attr_def using(ad_id) where fd_id=$1
58  order by jnt_order ",
59  array($p_fiche_card));
60  $this->format->f_position=$a_attribute;
61  }
62  }
63  /**
64  * @brief for the form we have here all the hidden variables
65  * @return html string with the hidden dossier, plugin_code,action(sa)
66  */
67  function hidden()
68  {
69  $r = HtmlInput::extension() . Dossier::hidden();
70  $r.= HtmlInput::hidden("record", $this->record->id);
71  $r.= HtmlInput::hidden("format", $this->format->id);
72  return $r;
73  }
74  /**
75  * @brief propose different possibilities to use your files
76  */
77  function propose_format()
78  {
79  global $cn;
80  ob_start();
81  $hidden = $this->hidden() . HtmlInput::hidden('sa', 'test');
82  $delimiter = new IText('rdelimiter');
83  $delimiter->size = 1;
84  $delimiter->value = $this->format->f_delimiter;
85  $fd = new ISelect('rfichedef');
86  $fd->value = $cn->make_array('select fd_id,fd_label from fiche_def order by 2');
87  $fd->selected=$this->format->f_card_category;
88  $encodage = new ICheckBox('encodage');
89  $encodage->selected = ($this->format->f_unicode_encoding=="Y")?TRUE:FALSE;
90  $skip_row=new INum('skip_row');
91  $skip_row->value=$this->format->f_skiprow;
92  $select_template = new ISelect("select_template");
93  $select_template->value=$cn->make_array("select id,f_name from importcard.format order by 2");
94  require_once('template/input_format.php');
95  $r = ob_get_contents();
96  ob_end_clean();
97  echo $r;
98  }
99  /**
100  * @brief show the first screen,
101  * @return html string
102  */
103  function new_import()
104  {
105  global $cn;
106  ob_start();
107  $hidden = $this->hidden() . HtmlInput::hidden('sa', 'import');
108  $fd = new ISelect('rfichedef');
109  $fd->value = $cn->make_array('select fd_id,fd_label from fiche_def order by 2');
110  $file = new IFile('csv_file');
111 
112  require_once('template/input_file.php');
113  $r = ob_get_contents();
114  ob_end_clean();
115  echo $r;
116  }
117  /**
118  * Upload the file and record the default format and the file
119  * @global type $cn
120  * @return type
121  */
122  function save_file()
123  {
124  global $cn;
125  if (trim($_FILES['csv_file']['name']) == '')
126  {
127  alert('Pas de fichier donné');
128  return -1;
129  }
130  $this->record->file_name = tempnam($_ENV['TMP'], 'upload_');
131  move_uploaded_file($_FILES["csv_file"]["tmp_name"], $this->record->file_name );
132  $this->record->save();
133  $file_def=HtmlInput::default_value_request('rfichedef', 0);
134  $this->create_format_temp($file_def);
135  $this->format->f_card_category=$file_def;
136  $this->format->save();
137  $hidden = $this->hidden() . HtmlInput::hidden('sa', 'record');
138 
139 
140  }
141  /**
142  * fill object Importcard_Format_SQL with the data send by post
143  */
144  function get_post_format()
145  {
146  global $cn;
147  $this->format->f_card_category= HtmlInput::default_value_request('rfichedef',0);
148 
149  $this->format->f_skiprow=HtmlInput::default_value_request("skip_row", 0);
150  $this->format->f_delimiter = HtmlInput::default_value_request("rdelimiter", $this->format->f_delimiter );
151  $this->format->f_surround = HtmlInput::default_value_request("rsurround", $this->format->f_surround);
152  /* If not set , give the default of card category */
153  $head_col = HtmlInput::default_value_request("head_col", $this->format->f_position);
154 
155  // If there is no column head set , then load the default order of the card attribute
156  if ( is_array($head_col) ) {
157  $this->format->f_position = join($head_col,",");
158  } else {
159  $a_attribute=$cn->make_list(
160  "select ad_id from jnt_fic_attr join attr_def using(ad_id) where fd_id=$1
161  order by jnt_order ",
162  array($this->format->f_card_category));
163  $this->format->f_position=$a_attribute;
164  }
165  $this->format->f_unicode_encoding= (isset($_REQUEST['encodage'])) ? 'Y' : 'N';
166  $this->format->save();
167  }
168 
169  /**
170  * Test the CSV file, show the choosed delimiter, the CSV parsed,
171  * and replace column header by attribute
172  * @return 0 ok, -1 error
173  */
174  function test_import()
175  {
176  global $cn;
177  $filename=$this->record->file_name;
178  $delimiter=$this->format->f_delimiter;
179  $surround=$this->format->f_surround;
180  $skip_row=$this->format->f_skiprow;
181  $fiche_def=$this->format->f_card_category;
182  // Column Header are a select
183  $a_header=explode(",", $this->format->f_position);
184  $sql=sprintf('select ad_id,ad_text from jnt_fic_attr join attr_def using(ad_id) where fd_id=%d order by ad_text ',$fiche_def);
185  $header=new ISelect('head_col[]');
186  $header->value=$cn->make_array($sql);
187  $header->value[]=array('value'=>-1,'label'=>'-- Non Utilisé --');
188  $header->selected=-1;
189 
190 
191  $a_attribute=$cn->get_array(
192  "select ad_id,ad_text from jnt_fic_attr join attr_def using(ad_id) where fd_id=$1 order by jnt_order ",
193  array($fiche_def)
194  );
195  $t1_valid_header=$cn->make_list("select ad_id from jnt_fic_attr join attr_def using(ad_id) where fd_id=$1 ",array($fiche_def));
196  $a_valid_header=explode(",", $t1_valid_header);
197  require_once('template/test_file.php');
198  return 0;
199  }
200  /**
201  * @brief record all rows
202  * @param
203  * @return
204  * @note
205  * @see
206  @code
207  array
208  'plugin_code' => string 'IMPCARD' (length=7)
209  'gDossier' => string '30' (length=2)
210  'sa' => string 'record' (length=6)
211  'rfichedef' => string '17' (length=2)
212  'rdelimiter' => string ',' (length=1)
213  'encodage' => string '' (length=0)
214  'record_import' => string 'Valider' (length=7)
215  'head_col' =>
216  array
217  0 => string '15' (length=2)
218  1 => string '14' (length=2)
219  2 => string '-1' (length=2)
220  3 => string '-1' (length=2)
221  4 => string '-1' (length=2)
222  5 => string '-1' (length=2)
223  @endcode
224  */
225  function record_import()
226  {
227  global $cn, $g_failed, $g_succeed;
228  $fd = fopen($this->record->file_name, 'r');
229  if ($fd == FALSE)
230  {
231  throw new Exception(sprintf(_("Ne peut ouvrir le fichier %s"),
232  $this->record->file_name));
233  }
234  /*
235  * Check the column
236  */
237  $valid_col = 0;
238  $valid_name = 0;
239  $duplicate = 0;
240  $valid_qcode = 0;
241  $valid_accounting = 0;
242  // Data from DB
243  $head_col = explode(",",$this->format->f_position);
244 
245 
246  for ($i = 0; $i < count($head_col); $i++)
247  {
248  if ($head_col[$i] != -1)
249  $valid_col++;
250  if ($head_col[$i] == 1)
251  $valid_name = 1;
252  if ($head_col[$i] == ATTR_DEF_QUICKCODE)
253  $valid_qcode = 1;
254  if ($head_col[$i] == ATTR_DEF_ACCOUNT)
255  $valid_accounting = 1;
256  for ($e = $i + 1; $e < count($head_col); $e++)
257  if ($head_col[$i] == $head_col[$e] && $head_col[$e] != -1)
258  $duplicate++;
259  }
260  if ($valid_col == 0)
261  {
262  alert(_("Aucune colonne n'est définie"));
263  return -1;
264  }
265  if ($valid_name == 0)
266  {
267  alert(_("Les fiches doivent avoir au minimum un nom"));
268  return -1;
269  }
270  if ($duplicate != 0)
271  {
272  alert(_('Vous avez défini plusieurs fois la même colonne'));
273  return -1;
274  }
275  /*
276  * read the file and record card
277  */
278  $row_count = 0;
279  $skip_row=$this->format->f_skiprow;
280 
281  echo '<table>';
282  ob_start();
283  while (($row = fgetcsv($fd, 0, $this->format->f_delimiter, $this->format->f_surround)) !== false)
284  {
285 
286  $row_count++;
287  if ( $skip_row >= $row_count ) continue;
288  $qcode="";
289  $fiche = new Fiche($cn);
290  $array = array();
291  echo '<tr style="border:solid 1px black">';
292  echo td($row_count);
293  $count_col = count($row);
294  $col_count = 0;
295  for ($i = 0; $i < $count_col; $i++)
296  {
297  if ($head_col[$i] == -1)
298  continue;
299  $header[$col_count] = $head_col[$i];
300  $col_count++;
301  echo td($row[$i]);
302  $attr = sprintf('av_text%d', $head_col[$i]);
303  $array[$attr] = $row[$i];
304  if ( $head_col [$i] == ATTR_DEF_QUICKCODE) {
305  $qcode=$row[$i];
306  }
307  }
308  /*
309  * If no quick code is given we compute it ourself
310  */
311  if ($valid_qcode == 0)
312  {
313  $attr = sprintf('av_text%d', ATTR_DEF_QUICKCODE);
314  $array[$attr] = '';
315 
316  }
317  /*
318  * Force the creating of an accounting
319  */
320  if ($valid_accounting == 0)
321  {
322  $attr = sprintf('av_text%d', ATTR_DEF_ACCOUNT);
323  $array[$attr] = '';
324  }
325  try
326  {
327  /**
328  * if qcode already exists then update otherwise insert
329  */
330  $msg=(_('Ajout'));
331  if ($valid_qcode == 0 || trim($qcode) == "")
332  {
333  $fiche->insert($this->format->f_card_category, $array);
334  } else {
335  // Retrieve the card with the qcode
336  $fiche->get_by_qcode($qcode,false);
337  // if qcode is found update otherwise insert
338  if ( $fiche->id !=0) {
339  $fiche->update($array);
340  $msg=(_('Mise à jour'));
341  } else {
342  $fiche->insert($this->format->f_card_category,$array);
343  }
344  }
345  echo td($g_succeed." ".$msg);
346  }
347  catch (Exception $e)
348  {
349  echo td($g_failed);
350  echo td($e->getMessage());
351  }
352  echo '</tr>';
353  }
354  $table_content = ob_get_contents();
355  ob_end_clean();
356  echo '<tr>';
357  echo th('');
358  for ($e = 0; $e < count($header); $e++)
359  {
360  $name = $cn->get_value('select ad_text from attr_def where ad_id=$1', array($header[$e]));
361  echo th($name);
362  }
363  echo '</tr>';
364  echo $table_content;
365  echo '</table>';
366  $name = $cn->get_value('select fd_label from fiche_def where fd_id=$1', array($this->format->f_card_category));
367  $cn->get_value('select comptaproc.fiche_attribut_synchro($1)', array($this->format->f_card_category));
368  echo '<span class="notice">';
369  printf (_('%d fiches sont insérées dans la catégorie %s') ,$row_count , $name);
370  echo '</span>';
371  return 0;
372  }
374  {
375  require_once __DIR__."/template/template_save.php";
376  }
377  function show_template()
378  {
379  global $cn;
380  $select_template=new ISelect("template_id");
381  $select_template->value=$cn->make_array("select id,f_name from importcard.format where f_saved=1 order by f_name");
382  require_once __DIR__."/template/template_show.php";
383 
384  }
385 }
global $g_failed
$msg
create_format_temp($p_fiche_card)
fill the CSV format object Importcard_Format_SQL with default values
for($i=0;$i< Database::num_row($ret);$i++) $row
$header
$r
propose_format()
propose different possibilities to use your files
if(isset($_POST['remove'])) $array
test_import()
Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute...
__construct($p_file_id=-1, $p_format_id=-1)
$skip_row
hidden()
for the form we have here all the hidden variables
save_file()
Upload the file and record the default format and the file type $cn.
new_import()
show the first screen,
global $g_succeed
$_REQUEST['sb']
Definition: am_print.php:39
$sql
record_import()
record all rows
$file
Definition: import_bank.php:60
get_post_format()
fill object Importcard_Format_SQL with the data send by post
$row_count
global $cn
$name