Plugins  LAST
 All Data Structures Files Functions Variables Pages
Public Member Functions | Static Public Member Functions | Private Attributes
Import_Card Class Reference
Collaboration diagram for Import_Card:
Collaboration graph

Public Member Functions

 __construct ($p_file_id=-1, $p_format_id=-1)
 
 create_format_temp ($p_fiche_card)
 fill the CSV format object Importcard_Format_SQL with default values More...
 
 hidden ()
 for the form we have here all the hidden variables More...
 
 propose_format ()
 propose different possibilities to use your files More...
 
 new_import ()
 show the first screen, More...
 
 save_file ()
 Upload the file and record the default format and the file type $cn. More...
 
 get_post_format ()
 fill object Importcard_Format_SQL with the data send by post More...
 
 test_import ()
 Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute. More...
 
 record_import ()
 record all rows More...
 
 propose_save_template ()
 
 show_template ()
 

Static Public Member Functions

static hidden ()
 for the form we have here all the hidden variables More...
 
static new_import ()
 show the first screen, you must here enter the date format the file, the card category, More...
 
static test_import ()
 Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute. More...
 
static record_import ()
 record all rows More...
 

Private Attributes

 $record
 
 $format
 

Detailed Description

Definition at line 26 of file class_import_card.php.

Constructor & Destructor Documentation

Import_Card::__construct (   $p_file_id = -1,
  $p_format_id = -1 
)

Definition at line 30 of file class_import_card.php.

References $cn.

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  }
global $cn

Member Function Documentation

Import_Card::create_format_temp (   $p_fiche_card)

fill the CSV format object Importcard_Format_SQL with default values

Parameters
int$p_fiche_cardif 0 then f_position is empty otherwise , f_position contains the attribute of this card category

Definition at line 43 of file class_import_card.php.

References $cn.

Referenced by save_file().

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  }
global $cn
Import_Card::get_post_format ( )

fill object Importcard_Format_SQL with the data send by post

Definition at line 144 of file class_import_card.php.

References $_REQUEST, and $cn.

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  }
$_REQUEST['sb']
Definition: am_print.php:39
global $cn
static Import_Card::hidden ( )
static

for the form we have here all the hidden variables

Returns
html string with the hidden dossier, plugin_code,action(sb)

Definition at line 34 of file class_import_card.php.

References $r.

35  {
36  $r = HtmlInput::extension() . Dossier::hidden();
37  return $r;
38  }
$r
Import_Card::hidden ( )

for the form we have here all the hidden variables

Returns
html string with the hidden dossier, plugin_code,action(sa)

Definition at line 67 of file class_import_card.php.

References $r.

Referenced by new_import(), propose_format(), and save_file().

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  }
$r
static Import_Card::new_import ( )
static

show the first screen, you must here enter the date format the file, the card category,

Returns
html string

Definition at line 45 of file class_import_card.php.

References $cn, $file, $hidden, and $r.

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  }
$r
$file
Definition: import_bank.php:60
global $cn
Import_Card::new_import ( )

show the first screen,

Returns
html string

Definition at line 103 of file class_import_card.php.

References $cn, $file, $hidden, $r, and hidden().

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  }
$r
hidden()
for the form we have here all the hidden variables
$file
Definition: import_bank.php:60
global $cn
Import_Card::propose_format ( )

propose different possibilities to use your files

Definition at line 77 of file class_import_card.php.

References $cn, $hidden, $r, $skip_row, and hidden().

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  }
$r
$skip_row
hidden()
for the form we have here all the hidden variables
global $cn
Import_Card::propose_save_template ( )

Definition at line 373 of file class_import_card.php.

374  {
375  require_once __DIR__."/template/template_save.php";
376  }
static Import_Card::record_import ( )
static

record all rows

array
'plugin_code' => string 'IMPCARD' (length=7)
'gDossier' => string '30' (length=2)
'sb' => string 'record' (length=6)
'rfichedef' => string '17' (length=2)
'rdelimiter' => string ',' (length=1)
'encodage' => string '' (length=0)
'record_import' => string 'Valider' (length=7)
'head_col' =>
array
0 => string '15' (length=2)
1 => string '14' (length=2)
2 => string '-1' (length=2)
3 => string '-1' (length=2)
4 => string '-1' (length=2)
5 => string '-1' (length=2)

Definition at line 112 of file class_import_card.php.

References $array, $cn, $fiche, $g_failed, $g_succeed, $header, $name, $row, and $row_count.

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  }
global $g_failed
for($i=0;$i< Database::num_row($ret);$i++) $row
$header
if(isset($_POST['remove'])) $array
global $g_succeed
$row_count
global $cn
$name
Import_Card::record_import ( )

record all rows

Parameters
@return
Note
See Also
array
'plugin_code' => string 'IMPCARD' (length=7)
'gDossier' => string '30' (length=2)
'sa' => string 'record' (length=6)
'rfichedef' => string '17' (length=2)
'rdelimiter' => string ',' (length=1)
'encodage' => string '' (length=0)
'record_import' => string 'Valider' (length=7)
'head_col' =>
array
0 => string '15' (length=2)
1 => string '14' (length=2)
2 => string '-1' (length=2)
3 => string '-1' (length=2)
4 => string '-1' (length=2)
5 => string '-1' (length=2)

if qcode already exists then update otherwise insert

Definition at line 225 of file class_import_card.php.

References $array, $cn, $fiche, $g_failed, $g_succeed, $header, $msg, $name, $row, $row_count, and $skip_row.

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  }
global $g_failed
$msg
for($i=0;$i< Database::num_row($ret);$i++) $row
$header
if(isset($_POST['remove'])) $array
$skip_row
global $g_succeed
$row_count
global $cn
$name
Import_Card::save_file ( )

Upload the file and record the default format and the file type $cn.

Returns
type

Definition at line 122 of file class_import_card.php.

References $cn, $hidden, create_format_temp(), and hidden().

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  }
create_format_temp($p_fiche_card)
fill the CSV format object Importcard_Format_SQL with default values
hidden()
for the form we have here all the hidden variables
global $cn
Import_Card::show_template ( )

Definition at line 377 of file class_import_card.php.

References $cn.

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  }
global $cn
static Import_Card::test_import ( )
static

Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute.

Returns
0 ok, -1 error

Definition at line 70 of file class_import_card.php.

References $_REQUEST, $cn, $hidden, and $skip_row.

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  }
$skip_row
$_REQUEST['sb']
Definition: am_print.php:39
global $cn
Import_Card::test_import ( )

Test the CSV file, show the choosed delimiter, the CSV parsed, and replace column header by attribute.

Returns
0 ok, -1 error

Definition at line 174 of file class_import_card.php.

References $cn, $header, $skip_row, and $sql.

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  }
$header
$skip_row
$sql
global $cn

Field Documentation

Import_Card::$format
private

Definition at line 29 of file class_import_card.php.

Import_Card::$record
private

Definition at line 28 of file class_import_card.php.


The documentation for this class was generated from the following file: