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 
21 // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
22 
23 /**\file
24  * \brief Manage import
25  */
26 
27 class Import_Card
28 {
29 
30  /**
31  * @brief for the form we have here all the hidden variables
32  * @return html string with the hidden dossier, plugin_code,action(sb)
33  */
34  static function hidden()
35  {
36  $r = HtmlInput::extension() . Dossier::hidden();
37  return $r;
38  }
39 
40  /**
41  * @brief show the first screen, you must here enter the date format
42  * the file, the card category,
43  * @return html string
44  */
45  static function new_import()
46  {
47  global $cn;
48  ob_start();
49  $hidden = self::hidden() . HtmlInput::hidden('sb', 'test');
50  $delimiter = new IText('rdelimiter');
51  $delimiter->size = 1;
52  $delimiter->value = ',';
53 
54  $fd = new ISelect('rfichedef');
55  $fd->value = $cn->make_array('select fd_id,fd_label from fiche_def order by 2');
56  $file = new IFile('csv_file');
57  $encodage = new ICheckBox('encodage');
58  $encodage->selected = true;
59  require_once('template/input_file.php');
60  $r = ob_get_contents();
61  ob_end_clean();
62  echo $r;
63  }
64 
65  /**
66  * Test the CSV file, show the choosed delimiter, the CSV parsed,
67  * and replace column header by attribute
68  * @return 0 ok, -1 error
69  */
70  static function test_import()
71  {
72  global $cn;
73  $hidden = self::hidden() . HtmlInput::hidden('sb', 'record');
74 
75  if (trim($_FILES['csv_file']['name']) == '')
76  {
77  alert('Pas de fichier donné');
78  return -1;
79  }
80  $filename = tempnam($_ENV['TMP'], 'upload_');
81  move_uploaded_file($_FILES["csv_file"]["tmp_name"], $filename);
82 
83  $file_cat = $cn->get_value('select fd_label from fiche_def where fd_id=$1', array($_POST['rfichedef']));
84  $encoding = (isset($_REQUEST['encodage'])) ? 'Unicode' : 'latin1';
85  $skip_row=HtmlInput::default_value_request("skip_row", 0);
86  require_once('template/test_file.php');
87  return 0;
88  }
89 
90  /**
91  * @brief record all rows
92  @code
93  array
94  'plugin_code' => string 'IMPCARD' (length=7)
95  'gDossier' => string '30' (length=2)
96  'sb' => string 'record' (length=6)
97  'rfichedef' => string '17' (length=2)
98  'rdelimiter' => string ',' (length=1)
99  'encodage' => string '' (length=0)
100  'record_import' => string 'Valider' (length=7)
101  'head_col' =>
102  array
103  0 => string '15' (length=2)
104  1 => string '14' (length=2)
105  2 => string '-1' (length=2)
106  3 => string '-1' (length=2)
107  4 => string '-1' (length=2)
108  5 => string '-1' (length=2)
109 
110  @endcode
111  */
112  static function record_import()
113  {
114 
115  global $cn, $g_failed, $g_succeed;
116  extract($_POST);
117  $fd = fopen($filename, 'r');
118  /*
119  * Check the column
120  */
121  $valid_col = 0;
122  $valid_name = 0;
123  $duplicate = 0;
124  $valid_qcode = 0;
125  $valid_accounting = 0;
126  for ($i = 0; $i < count($head_col); $i++)
127  {
128  if ($head_col[$i] != -1)
129  $valid_col++;
130  if ($head_col[$i] == 1)
131  $valid_name = 1;
132  if ($head_col[$i] == ATTR_DEF_QUICKCODE)
133  $valid_qcode = 1;
134  if ($head_col[$i] == ATTR_DEF_ACCOUNT)
135  $valid_accounting = 1;
136 
137  for ($e = $i + 1; $e < count($head_col); $e++)
138  if ($head_col[$i] == $head_col[$e] && $head_col[$e] != -1)
139  $duplicate++;
140  }
141 
142  if ($valid_col == 0)
143  {
144  alert("Aucune colonne n'est définie");
145  return -1;
146  }
147  if ($valid_name == 0)
148  {
149  alert("Les fiches doivent avoir au minimum un nom");
150  return -1;
151  }
152  if ($duplicate != 0)
153  {
154  alert('Vous avez défini plusieurs fois la même colonne');
155  return -1;
156  }
157  if ($valid_qcode == 0)
158  {
159  alert("Vous devez donner la colonne quick_code");
160  return 1;
161  }
162  /*
163  * read the file and record card
164  */
165 
166  $row_count = 0;
167 
168  echo '<table>';
169 
170  ob_start();
171  while (($row = fgetcsv($fd, 0, $_POST['rdelimiter'], $_POST['rsurround'])) !== false)
172  {
173  $fiche = new Fiche($cn);
174  $array = array();
175  $row_count++;
176  echo '<tr style="border:solid 1px black">';
177  echo td($row_count);
178  $count_col = count($row);
179  $col_count = 0;
180  for ($i = 0; $i < $count_col; $i++)
181  {
182  if ($head_col[$i] == -1)
183  continue;
184 
185  $header[$col_count] = $head_col[$i];
186  $col_count++;
187 
188  echo td($row[$i]);
189  $attr = sprintf('av_text%d', $head_col[$i]);
190  $array[$attr] = $row[$i];
191  }
192 
193  try
194  {
195  // If quick_code already exists then update otherwise insert
196  $quick_code= sprintf('av_text%d',ATTR_DEF_QUICKCODE);
197  if ( $fiche->get_by_qcode($array[$quick_code],false)==0 )
198  {
199  // accounting cannot change
200  $attr = sprintf('av_text%d', ATTR_DEF_ACCOUNT);
201  $array[$attr] = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
202  $fiche->update($array);
203  }
204  else
205  {
206  /*
207  * Force the creating of an accounting
208  */
209  if ($valid_accounting == 0)
210  {
211  $attr = sprintf('av_text%d', ATTR_DEF_ACCOUNT);
212  $array[$attr] = '';
213  }
214  $fiche->insert($rfichedef, $array);
215  }
216  echo td($g_succeed);
217  }
218  catch (Exception $e)
219  {
220  echo td($g_failed);
221  echo td($e->getMessage());
222  }
223  echo '</tr>';
224  }
225  $table_content = ob_get_contents();
226  ob_end_clean();
227  echo '<tr>';
228  echo th('');
229  for ($e = 0; $e < count($header); $e++)
230  {
231  $name = $cn->get_value('select ad_text from attr_def where ad_id=$1', array($header[$e]));
232  echo th($name);
233  }
234  echo '</tr>';
235  echo $table_content;
236 
237  echo '</table>';
238  $name = $cn->get_value('select fd_label from fiche_def where fd_id=$1', array($rfichedef));
239  $cn->get_value('select comptaproc.fiche_attribut_synchro($1)', array($rfichedef));
240  echo '<span class="notice">';
241  echo $row_count . ' fiches sont insérées dans la catégorie ' . $name;
242  echo '</span>';
243  return 0;
244  }
245 
246 }
global $g_failed
for($i=0;$i< Database::num_row($ret);$i++) $row
$header
$r
if(isset($_POST['remove'])) $array
static new_import()
show the first screen, you must here enter the date format the file, the card category,
$skip_row
static record_import()
record all rows
static hidden()
for the form we have here all the hidden variables
global $g_succeed
$_REQUEST['sb']
Definition: am_print.php:39
$file
Definition: import_bank.php:60
$row_count
global $cn
static test_import()
Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute...
$name