dolibarr  7.0.0-beta
export.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
4  * Copyright (C) 2012 Charles-Fr BENKE <charles.fr@benke.fr>
5  * Copyright (C) 2016 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
31 class Export
32 {
33  var $db;
34 
35  var $array_export_code=array(); // Tableau de "idmodule_numlot"
36  var $array_export_module=array(); // Tableau de "nom de modules"
37  var $array_export_label=array(); // Tableau de "libelle de lots"
38  var $array_export_sql_start=array(); // Tableau des "requetes sql"
39  var $array_export_sql_end=array(); // Tableau des "requetes sql"
40  var $array_export_sql_order=array(); // Tableau des "requetes sql"
41 
42  var $array_export_fields=array(); // Tableau des listes de champ+libelle a exporter
43  var $array_export_TypeFields=array(); // Tableau des listes de champ+Type de filtre
44  var $array_export_FilterValue=array(); // Tableau des listes de champ+Valeur a filtrer
45  var $array_export_entities=array(); // Tableau des listes de champ+alias a exporter
46  var $array_export_dependencies=array(); // array of list of entities that must take care of the DISTINCT if a field is added into export
47  var $array_export_special=array(); // Tableau des operations speciales sur champ
48  var $array_export_examplevalues=array(); // array with examples
49 
50  // To store export modules
51  var $hexa;
52  var $hexafiltervalue;
53  var $datatoexport;
54  var $model_name;
55 
56  var $sqlusedforexport;
57 
58 
64  function __construct($db)
65  {
66  $this->db=$db;
67  }
68 
69 
77  function load_arrays($user,$filter='')
78  {
79  global $langs,$conf,$mysoc;
80 
81  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
82 
83  $var=true;
84  $i=0;
85 
86  // Define list of modules directories into modulesdir
87  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
88 
89  $modulesdir = dolGetModulesDirs();
90 
91  foreach($modulesdir as $dir)
92  {
93  // Search available exports
94  $handle=@opendir(dol_osencode($dir));
95  if (is_resource($handle))
96  {
97  // Search module files
98  while (($file = readdir($handle))!==false)
99  {
100  if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
101  {
102  $modulename=$reg[1];
103 
104  // Defined if module is enabled
105  $enabled=true;
106  $part=strtolower(preg_replace('/^mod/i','',$modulename));
107  if ($part == 'propale') $part='propal';
108  if (empty($conf->$part->enabled)) $enabled=false;
109 
110  if ($enabled)
111  {
112  // Loading Class
113  $file = $dir.$modulename.".class.php";
114  $classname = $modulename;
115  require_once $file;
116  $module = new $classname($this->db);
117 
118  if (isset($module->export_code) && is_array($module->export_code))
119  {
120  foreach($module->export_code as $r => $value)
121  {
122  //print $i.'-'.$filter.'-'.$modulename.'-'.join(',',$module->export_code).'<br>';
123  if ($filter && ($filter != $module->export_code[$r])) continue;
124 
125  // Test if condition to show are ok
126  if (! empty($module->export_enabled[$r]) && ! verifCond($module->export_enabled[$r])) continue;
127 
128  // Test if permissions are ok
129  $bool=true;
130  if (isset($module->export_permission))
131  {
132  foreach($module->export_permission[$r] as $val)
133  {
134  $perm=$val;
135  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
136  if (! empty($perm[2]))
137  {
138  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
139  }
140  else
141  {
142  $bool=$user->rights->{$perm[0]}->{$perm[1]};
143  }
144  if ($perm[0]=='user' && $user->admin) $bool=true;
145  if (! $bool) break;
146  }
147  }
148  //print $bool." $perm[0]"."<br>";
149 
150  // Permissions ok
151  // if ($bool)
152  // {
153  // Charge fichier lang en rapport
154  $langtoload=$module->getLangFilesArray();
155  if (is_array($langtoload))
156  {
157  foreach($langtoload as $key)
158  {
159  $langs->load($key);
160  }
161  }
162 
163  // Module
164  $this->array_export_module[$i]=$module;
165  // Permission
166  $this->array_export_perms[$i]=$bool;
167  // Icon
168  $this->array_export_icon[$i]=(isset($module->export_icon[$r])?$module->export_icon[$r]:$module->picto);
169  // Code du dataset export
170  $this->array_export_code[$i]=$module->export_code[$r];
171  // Libelle du dataset export
172  $this->array_export_label[$i]=$module->getExportDatasetLabel($r);
173  // Tableau des champ a exporter (cle=champ, valeur=libelle)
174  $this->array_export_fields[$i]=$module->export_fields_array[$r];
175  // Tableau des champs a filtrer (cle=champ, valeur1=type de donnees) on verifie que le module a des filtres
176  $this->array_export_TypeFields[$i]=(isset($module->export_TypeFields_array[$r])?$module->export_TypeFields_array[$r]:'');
177  // Tableau des entites a exporter (cle=champ, valeur=entite)
178  $this->array_export_entities[$i]=$module->export_entities_array[$r];
179  // Tableau des entites qui requiert abandon du DISTINCT (cle=entite, valeur=champ id child records)
180  $this->array_export_dependencies[$i]=(! empty($module->export_dependencies_array[$r])?$module->export_dependencies_array[$r]:'');
181  // Tableau des operations speciales sur champ
182  $this->array_export_special[$i]=(! empty($module->export_special_array[$r])?$module->export_special_array[$r]:'');
183  // Array of examples
184  $this->array_export_examplevalues[$i]=$module->export_examplevalues_array[$r];
185 
186  // Requete sql du dataset
187  $this->array_export_sql_start[$i]=$module->export_sql_start[$r];
188  $this->array_export_sql_end[$i]=$module->export_sql_end[$r];
189  $this->array_export_sql_order[$i]=$module->export_sql_order[$r];
190  //$this->array_export_sql[$i]=$module->export_sql[$r];
191 
192  dol_syslog(get_class($this)."::load_arrays loaded for module ".$modulename." with index ".$i.", dataset=".$module->export_code[$r].", nb of fields=".(! empty($module->export_fields_code[$r])?count($module->export_fields_code[$r]):''));
193  $i++;
194  // }
195  }
196  }
197  }
198  }
199  }
200  closedir($handle);
201  }
202  }
203 
204  return 1;
205  }
206 
207 
217  function build_sql($indice, $array_selected, $array_filterValue)
218  {
219  // Build the sql request
220  $sql=$this->array_export_sql_start[$indice];
221  $i=0;
222 
223  //print_r($array_selected);
224  foreach ($this->array_export_fields[$indice] as $key => $value)
225  {
226  if (! array_key_exists($key, $array_selected)) continue; // Field not selected
227  if (preg_match('/^none\./', $key)) continue; // A field that must not appears into SQL
228  if ($i > 0) $sql.=', ';
229  else $i++;
230 
231  if (strpos($key, ' as ')===false) {
232  $newfield=$key.' as '.str_replace(array('.', '-','(',')'),'_',$key);
233  } else {
234  $newfield=$key;
235  }
236 
237  $sql.=$newfield;
238  }
239  $sql.=$this->array_export_sql_end[$indice];
240 
241  // Add the WHERE part. Filtering into sql if a filtering array is provided
242  if (is_array($array_filterValue) && !empty($array_filterValue))
243  {
244  $sqlWhere='';
245  // Loop on each condition to add
246  foreach ($array_filterValue as $key => $value)
247  {
248  if (preg_match('/GROUP_CONCAT/i', $key)) continue;
249  if ($value != '') $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
250  }
251  $sql.=$sqlWhere;
252  }
253 
254  // Add the order
255  $sql.=$this->array_export_sql_order[$indice];
256 
257  // Add the HAVING part.
258  if (is_array($array_filterValue) && !empty($array_filterValue))
259  {
260  // Loop on each condition to add
261  foreach ($array_filterValue as $key => $value)
262  {
263  if (preg_match('/GROUP_CONCAT/i', $key) and $value != '') $sql.=" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
264  }
265  }
266 
267  return $sql;
268  }
269 
278  function build_filterQuery($TypeField, $NameField, $ValueField)
279  {
280  //print $TypeField." ".$NameField." ".$ValueField;
281  $InfoFieldList = explode(":", $TypeField);
282  // build the input field on depend of the type of file
283  switch ($InfoFieldList[0]) {
284  case 'Text':
285  if (! (strpos($ValueField, '%') === false))
286  $szFilterQuery.=" ".$NameField." LIKE '".$ValueField."'";
287  else
288  $szFilterQuery.=" ".$NameField." = '".$ValueField."'";
289  break;
290  case 'Date':
291  if (strpos($ValueField, "+") > 0)
292  {
293  // mode plage
294  $ValueArray = explode("+", $ValueField);
295  $szFilterQuery ="(".$this->conditionDate($NameField,trim($ValueArray[0]),">=");
296  $szFilterQuery.=" AND ".$this->conditionDate($NameField,trim($ValueArray[1]),"<=").")";
297  }
298  else
299  {
300  if (is_numeric(substr($ValueField,0,1)))
301  $szFilterQuery=$this->conditionDate($NameField,trim($ValueField),"=");
302  else
303  $szFilterQuery=$this->conditionDate($NameField,trim(substr($ValueField,1)),substr($ValueField,0,1));
304  }
305  break;
306  case 'Duree':
307  break;
308  case 'Numeric':
309  // si le signe -
310  if (strpos($ValueField, "+") > 0)
311  {
312  // mode plage
313  $ValueArray = explode("+", $ValueField);
314  $szFilterQuery ="(".$NameField.">=".$ValueArray[0];
315  $szFilterQuery.=" AND ".$NameField."<=".$ValueArray[1].")";
316  }
317  else
318  {
319  if (is_numeric(substr($ValueField,0,1)))
320  $szFilterQuery=" ".$NameField."=".$ValueField;
321  else
322  $szFilterQuery=" ".$NameField.substr($ValueField,0,1).substr($ValueField,1);
323  }
324  break;
325  case 'Boolean':
326  $szFilterQuery=" ".$NameField."=".(is_numeric($ValueField) ? $ValueField : ($ValueField =='yes' ? 1: 0) );
327  break;
328  case 'Status':
329  case 'List':
330  if (is_numeric($ValueField))
331  $szFilterQuery=" ".$NameField."=".$ValueField;
332  else {
333  if (! (strpos($ValueField, '%') === false))
334  $szFilterQuery=" ".$NameField." LIKE '".$ValueField."'";
335  else
336  $szFilterQuery=" ".$NameField." = '".$ValueField."'";
337  }
338  break;
339  default:
340  dol_syslog("Error we try to forge an sql export request with a condition on a field with type '".$InfoFieldList[0]."' (defined into module descriptor) but this type is unknown/not supported. It looks like a bug into module descriptor.", LOG_ERR);
341  }
342 
343  return $szFilterQuery;
344  }
345 
354  function conditionDate($Field, $Value, $Sens)
355  {
356  // TODO date_format is forbidden, not performant and not portable. Use instead BETWEEN
357  if (strlen($Value)==4) $Condition=" date_format(".$Field.",'%Y') ".$Sens." '".$Value."'";
358  elseif (strlen($Value)==6) $Condition=" date_format(".$Field.",'%Y%m') ".$Sens." '".$Value."'";
359  else $Condition=" date_format(".$Field.",'%Y%m%d') ".$Sens." ".$Value;
360  return $Condition;
361  }
362 
371  function build_filterField($TypeField, $NameField, $ValueField)
372  {
373  global $conf,$langs;
374 
375  $szFilterField='';
376  $InfoFieldList = explode(":", $TypeField);
377 
378  // build the input field on depend of the type of file
379  switch ($InfoFieldList[0])
380  {
381  case 'Text':
382  case 'Date':
383  $szFilterField='<input type="text" name="'.$NameField.'" value="'.$ValueField.'">';
384  break;
385  case 'Duree':
386  case 'Numeric':
387  case 'Number':
388  // Must be a string text to allow to use comparison strings like "<= 999"
389  $szFilterField='<input type="text" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
390  break;
391  case 'Status':
392  if (! empty($conf->global->MAIN_ACTIVATE_HTML5)) $szFilterField='<input type="number" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
393  else $szFilterField='<input type="text" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
394  break;
395  case 'Boolean':
396  $szFilterField='<select name="'.$NameField.'" class="flat">';
397  $szFilterField.='<option ';
398  if ($ValueField=='') $szFilterField.=' selected ';
399  $szFilterField.=' value="">&nbsp;</option>';
400 
401  $szFilterField.='<option ';
402  if ($ValueField=='yes' || $ValueField == '1') $szFilterField.=' selected ';
403  $szFilterField.=' value="1">'.yn(1).'</option>';
404 
405  $szFilterField.='<option ';
406  if ($ValueField=='no' || $ValueField=='0') $szFilterField.=' selected ';
407  $szFilterField.=' value="0">'.yn(0).'</option>';
408  $szFilterField.="</select>";
409  break;
410  case 'List':
411  // 0 : Type du champ
412  // 1 : Nom de la table
413  // 2 : Nom du champ contenant le libelle
414  // 3 : Name of field with key (if it is not "rowid"). Used this field as key for combo list.
415  if (count($InfoFieldList)==4)
416  $keyList=$InfoFieldList[3];
417  else
418  $keyList='rowid';
419  $sql = 'SELECT '.$keyList.' as rowid, '.$InfoFieldList[2].' as label'.(empty($InfoFieldList[3])?'':', '.$InfoFieldList[3].' as code');
420  if ($InfoFieldList[1] == 'c_stcomm') $sql = 'SELECT id as id, '.$keyList.' as rowid, '.$InfoFieldList[2].' as label'.(empty($InfoFieldList[3])?'':', '.$InfoFieldList[3].' as code');
421  if ($InfoFieldList[1] == 'c_country') $sql = 'SELECT '.$keyList.' as rowid, '.$InfoFieldList[2].' as label, code as code';
422  $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[1];
423 
424  $resql = $this->db->query($sql);
425  if ($resql)
426  {
427  $szFilterField='<select class="flat" name="'.$NameField.'">';
428  $szFilterField.='<option value="0">&nbsp;</option>';
429  $num = $this->db->num_rows($resql);
430 
431  $i = 0;
432  if ($num)
433  {
434  while ($i < $num)
435  {
436  $obj = $this->db->fetch_object($resql);
437  if ($obj->label == '-')
438  {
439  // Discard entry '-'
440  $i++;
441  continue;
442  }
443  //var_dump($InfoFieldList[1]);
444  $labeltoshow=dol_trunc($obj->label,18);
445  if ($InfoFieldList[1] == 'c_stcomm')
446  {
447  $langs->load("companies");
448  $labeltoshow=(($langs->trans("StatusProspect".$obj->id) != "StatusProspect".$obj->id)?$langs->trans("StatusProspect".$obj->id):$obj->label);
449  }
450  if ($InfoFieldList[1] == 'c_country')
451  {
452  //var_dump($sql);
453  $langs->load("dict");
454  $labeltoshow=(($langs->trans("Country".$obj->code) != "Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label);
455  }
456  if (!empty($ValueField) && $ValueField == $obj->rowid)
457  {
458  $szFilterField.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
459  }
460  else
461  {
462  $szFilterField.='<option value="'.$obj->rowid.'" >'.$labeltoshow.'</option>';
463  }
464  $i++;
465  }
466  }
467  $szFilterField.="</select>";
468 
469  $this->db->free($resql);
470  }
471  else dol_print_error($this->db);
472  break;
473  }
474 
475  return $szFilterField;
476  }
477 
484  function genDocFilter($TypeField)
485  {
486  global $langs;
487 
488  $szMsg='';
489  $InfoFieldList = explode(":", $TypeField);
490  // build the input field on depend of the type of file
491  switch ($InfoFieldList[0]) {
492  case 'Text':
493  $szMsg= $langs->trans('ExportStringFilter');
494  break;
495  case 'Date':
496  $szMsg = $langs->trans('ExportDateFilter');
497  break;
498  case 'Duree':
499  break;
500  case 'Numeric':
501  $szMsg = $langs->trans('ExportNumericFilter');
502  break;
503  case 'Boolean':
504  break;
505  case 'List':
506  break;
507  }
508  return $szMsg;
509  }
510 
524  function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery = '')
525  {
526  global $conf,$langs;
527 
528  $indice=0;
529  asort($array_selected);
530 
531  dol_syslog(get_class($this)."::".__FUNCTION__." ".$model.", ".$datatoexport.", ".implode(",", $array_selected));
532 
533  // Check parameters or context properties
534  if (empty($this->array_export_fields) || ! is_array($this->array_export_fields))
535  {
536  $this->error="ErrorBadParameter";
537  return -1;
538  }
539 
540  // Creation of class to export using model ExportXXX
541  $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/";
542  $file = "export_".$model.".modules.php";
543  $classname = "Export".$model;
544  require_once $dir.$file;
545  $objmodel = new $classname($this->db);
546 
547  if (! empty($sqlquery)) $sql = $sqlquery;
548  else
549  {
550  // Define value for indice from $datatoexport
551  $foundindice=0;
552  foreach($this->array_export_code as $key => $dataset)
553  {
554  if ($datatoexport == $dataset)
555  {
556  $indice=$key;
557  $foundindice++;
558  //print "Found indice = ".$indice." for dataset=".$datatoexport."\n";
559  break;
560  }
561  }
562  if (empty($foundindice))
563  {
564  $this->error="ErrorBadParameter can't find dataset ".$datatoexport." into preload arrays this->array_export_code";
565  return -1;
566  }
567  $sql=$this->build_sql($indice, $array_selected, $array_filterValue);
568  }
569 
570  // Run the sql
571  $this->sqlusedforexport=$sql;
572  dol_syslog(get_class($this)."::".__FUNCTION__."", LOG_DEBUG);
573  $resql = $this->db->query($sql);
574  if ($resql)
575  {
576  //$this->array_export_label[$indice]
577  if ($conf->global->EXPORT_PREFIX_SPEC)
578  $filename=$conf->global->EXPORT_PREFIX_SPEC."_".$datatoexport;
579  else
580  $filename="export_".$datatoexport;
581  $filename.='.'.$objmodel->getDriverExtension();
582  $dirname=$conf->export->dir_temp.'/'.$user->id;
583 
584  $outputlangs = clone $langs; // We clone to have an object we can modify (for example to change output charset by csv handler) without changing original value
585 
586  // Open file
587  dol_mkdir($dirname);
588  $result=$objmodel->open_file($dirname."/".$filename, $outputlangs);
589 
590  if ($result >= 0)
591  {
592  // Genere en-tete
593  $objmodel->write_header($outputlangs);
594 
595  // Genere ligne de titre
596  $objmodel->write_title($this->array_export_fields[$indice],$array_selected,$outputlangs,$this->array_export_TypeFields[$indice]);
597 
598  $var=true;
599 
600  while ($obj = $this->db->fetch_object($resql))
601  {
602  // Process special operations
603  if (! empty($this->array_export_special[$indice]))
604  {
605  foreach ($this->array_export_special[$indice] as $key => $value)
606  {
607  if (! array_key_exists($key, $array_selected)) continue; // Field not selected
608  // Operation NULLIFNEG
609  if ($this->array_export_special[$indice][$key]=='NULLIFNEG')
610  {
611  //$alias=$this->array_export_alias[$indice][$key];
612  $alias=str_replace(array('.', '-','(',')'),'_',$key);
613  if ($obj->$alias < 0) $obj->$alias='';
614  }
615  // Operation ZEROIFNEG
616  elseif ($this->array_export_special[$indice][$key]=='ZEROIFNEG')
617  {
618  //$alias=$this->array_export_alias[$indice][$key];
619  $alias=str_replace(array('.', '-','(',')'),'_',$key);
620  if ($obj->$alias < 0) $obj->$alias='0';
621  }
622  // Operation INVOICEREMAINTOPAY
623  elseif ($this->array_export_special[$indice][$key]=='getRemainToPay')
624  {
625  //$alias=$this->array_export_alias[$indice][$key];
626  $alias=str_replace(array('.', '-','(',')'),'_',$key);
627  $remaintopay='';
628  if ($obj->f_rowid > 0)
629  {
630  global $tmpobjforcomputecall;
631  if (! is_object($tmpobjforcomputecall))
632  {
633  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
634  $tmpobjforcomputecall=new Facture($this->db);
635  }
636  $tmpobjforcomputecall->id = $obj->f_rowid;
637  $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
638  $remaintopay=$tmpobjforcomputecall->getRemainToPay();
639  }
640  $obj->$alias=$remaintopay;
641  }
642  else
643  {
644  // TODO FIXME Export of compute field does not work. $obj containt $obj->alias_field and formulat will contains $obj->field
645  $computestring=$this->array_export_special[$indice][$key];
646  $tmp=dol_eval($computestring, 1, 0);
647  $obj->$alias=$tmp;
648 
649  $this->error="ERROPNOTSUPPORTED. Operation ".$this->array_export_special[$indice][$key]." not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
650  return -1;
651  }
652  }
653  }
654  // end of special operation processing
655  $objmodel->write_record($array_selected,$obj,$outputlangs,$this->array_export_TypeFields[$indice]);
656  }
657 
658  // Genere en-tete
659  $objmodel->write_footer($outputlangs);
660 
661  // Close file
662  $objmodel->close_file();
663 
664  return 1;
665  }
666  else
667  {
668  $this->error=$objmodel->error;
669  dol_syslog("Export::build_file Error: ".$this->error, LOG_ERR);
670  return -1;
671  }
672  }
673  else
674  {
675  $this->error=$this->db->error()." - sql=".$sql;
676  return -1;
677  }
678  }
679 
686  function create($user)
687  {
688  global $conf;
689 
690  dol_syslog("Export.class.php::create");
691 
692  $this->db->begin();
693 
694  $filter='';
695 
696  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'export_model (';
697  $sql.= 'label,';
698  $sql.= 'type,';
699  $sql.= 'field,';
700  $sql.= 'filter';
701  $sql.= ') VALUES (';
702  $sql.= "'".$this->db->escape($this->model_name)."',";
703  $sql.= "'".$this->db->escape($this->datatoexport)."',";
704  $sql.= "'".$this->db->escape($this->hexa)."',";
705  $sql.= "'".$this->db->escape($this->hexafiltervalue)."'";
706  $sql.= ")";
707 
708  dol_syslog(get_class($this)."::create", LOG_DEBUG);
709  $resql=$this->db->query($sql);
710  if ($resql)
711  {
712  $this->db->commit();
713  return 1;
714  }
715  else
716  {
717  $this->error=$this->db->lasterror();
718  $this->errno=$this->db->lasterrno();
719  $this->db->rollback();
720  return -1;
721  }
722  }
723 
730  function fetch($id)
731  {
732  $sql = 'SELECT em.rowid, em.label, em.type, em.field, em.filter';
733  $sql.= ' FROM '.MAIN_DB_PREFIX.'export_model as em';
734  $sql.= ' WHERE em.rowid = '.$id;
735 
736  dol_syslog("Export::fetch", LOG_DEBUG);
737  $result = $this->db->query($sql);
738  if ($result)
739  {
740  $obj = $this->db->fetch_object($result);
741  if ($obj)
742  {
743  $this->id = $obj->rowid;
744  $this->model_name = $obj->label;
745  $this->datatoexport = $obj->type;
746 
747  $this->hexa = $obj->field;
748  $this->hexafiltervalue = $obj->filter;
749 
750  return 1;
751  }
752  else
753  {
754  $this->error="ModelNotFound";
755  return -2;
756  }
757  }
758  else
759  {
760  dol_print_error($this->db);
761  return -3;
762  }
763  }
764 
765 
773  function delete($user, $notrigger=0)
774  {
775  global $conf, $langs;
776  $error=0;
777 
778  $sql = "DELETE FROM ".MAIN_DB_PREFIX."export_model";
779  $sql.= " WHERE rowid=".$this->id;
780 
781  $this->db->begin();
782 
783  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
784  $resql = $this->db->query($sql);
785  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
786 
787  if (! $error)
788  {
789  if (! $notrigger)
790  {
791  // Uncomment this and change MYOBJECT to your own tag if you
792  // want this action call a trigger.
793 
795  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
796  //$interface=new Interfaces($this->db);
797  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
798  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
800  }
801  }
802 
803  // Commit or rollback
804  if ($error)
805  {
806  foreach($this->errors as $errmsg)
807  {
808  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
809  $this->error.=($this->error?', '.$errmsg:$errmsg);
810  }
811  $this->db->rollback();
812  return -1*$error;
813  }
814  else
815  {
816  $this->db->commit();
817  return 1;
818  }
819  }
820 
827  function list_export_model()
828  {
829  global $conf, $langs;
830 
831  $sql = "SELECT em.rowid, em.field, em.label, em.type, em.filter";
832  $sql.= " FROM ".MAIN_DB_PREFIX."export_model as em";
833  $sql.= " ORDER BY rowid";
834 
835  $result = $this->db->query($sql);
836  if ($result)
837  {
838  $num = $this->db->num_rows($result);
839  $i = 0;
840  while ($i < $num)
841  {
842  $obj = $this->db->fetch_object($result);
843  $keyModel = array_search($obj->type, $this->array_export_code);
844  print "<tr>";
845  print '<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.'&datatoexport='.$obj->type.'>'.$obj->label.'</a></td>';
846  print '<td>';
847  print img_object($this->array_export_module[$keyModel]->getName(),$this->array_export_icon[$keyModel]).' ';
848  print $this->array_export_module[$keyModel]->getName().' - ';
849  // recuperation du nom de l'export
850 
851  $string=$langs->trans($this->array_export_label[$keyModel]);
852  print ($string!=$this->array_export_label[$keyModel]?$string:$this->array_export_label[$keyModel]);
853  print '</td>';
854  //print '<td>'.$obj->type.$keyModel.'</td>';
855  print '<td>'.str_replace(',',' , ',$obj->field).'</td>';
856  if (! empty($obj->filter)) {
857  $filter = json_decode($obj->filter, true);
858  print '<td>'.str_replace(',',' , ',$filter['field']).'</td>';
859  print '<td>'.str_replace(',',' , ',$filter['value']).'</td>';
860  }
861  // suppression de l'export
862  print '<td align="right">';
863  print '<a href="'.$_SERVER["PHP_SELF"].'?action=deleteprof&id='.$obj->rowid.'">';
864  print img_delete();
865  print '</a>';
866  print "</tr>";
867 
868  $i++;
869  }
870  }
871  else {
872  dol_print_error($this->db);
873  }
874  }
875 
876 }
877 
verifCond($strRights)
Verify if condition in string is ok or not.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_eval($s, $returnvalue=0, $hideerrors=1)
Replace eval function to add more security.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
dolGetModulesDirs($subdir='')
Return list of modules directories.
load_arrays($user, $filter='')
Load an exportable dataset.
conditionDate($Field, $Value, $Sens)
conditionDate
Class to manage exports.
build_filterQuery($TypeField, $NameField, $ValueField)
Build the conditionnal string from filter the query.
__construct($db)
Constructor.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
build_filterField($TypeField, $NameField, $ValueField)
Build an input field used to filter the query.
fetch($id)
Load an export profil from database.
build_sql($indice, $array_selected, $array_filterValue)
Build the sql export request.
genDocFilter($TypeField)
Build an input field used to filter the query.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_delete($titlealt= 'default', $other= 'class="pictodelete"')
Show delete logo.
list_export_model()
Output list all export models TODO Move this into a class htmlxxx.class.php.
print
Draft customers invoices.
Definition: index.php:91
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery= '')
Build export file.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Class to manage invoices.
create($user)
Save an export model in database.