dolibarr  9.0.0
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@inodbox.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 {
36  public $db;
37 
38  var $array_export_code=array(); // Tableau de "idmodule_numlot"
39  var $array_export_module=array(); // Tableau de "nom de modules"
40  var $array_export_label=array(); // Tableau de "libelle de lots"
41  var $array_export_sql_start=array(); // Tableau des "requetes sql"
42  var $array_export_sql_end=array(); // Tableau des "requetes sql"
43  var $array_export_sql_order=array(); // Tableau des "requetes sql"
44 
45  var $array_export_fields=array(); // Tableau des listes de champ+libelle a exporter
46  var $array_export_TypeFields=array(); // Tableau des listes de champ+Type de filtre
47  var $array_export_FilterValue=array(); // Tableau des listes de champ+Valeur a filtrer
48  var $array_export_entities=array(); // Tableau des listes de champ+alias a exporter
49  var $array_export_dependencies=array(); // array of list of entities that must take care of the DISTINCT if a field is added into export
50  var $array_export_special=array(); // Tableau des operations speciales sur champ
51  var $array_export_examplevalues=array(); // array with examples
52 
53  // To store export modules
54  var $hexa;
55  var $hexafiltervalue;
56  var $datatoexport;
57  var $model_name;
58 
59  var $sqlusedforexport;
60 
61 
67  function __construct($db)
68  {
69  $this->db=$db;
70  }
71 
72 
73  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
81  function load_arrays($user,$filter='')
82  {
83  // phpcs:enable
84  global $langs,$conf,$mysoc;
85 
86  dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
87 
88  $i=0;
89 
90  // Define list of modules directories into modulesdir
91  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
92 
93  $modulesdir = dolGetModulesDirs();
94 
95  foreach($modulesdir as $dir)
96  {
97  // Search available exports
98  $handle=@opendir(dol_osencode($dir));
99  if (is_resource($handle))
100  {
101  // Search module files
102  while (($file = readdir($handle))!==false)
103  {
104  if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
105  {
106  $modulename=$reg[1];
107 
108  // Defined if module is enabled
109  $enabled=true;
110  $part=strtolower(preg_replace('/^mod/i','',$modulename));
111  if ($part == 'propale') $part='propal';
112  if (empty($conf->$part->enabled)) $enabled=false;
113 
114  if ($enabled)
115  {
116  // Loading Class
117  $file = $dir.$modulename.".class.php";
118  $classname = $modulename;
119  require_once $file;
120  $module = new $classname($this->db);
121 
122  if (isset($module->export_code) && is_array($module->export_code))
123  {
124  foreach($module->export_code as $r => $value)
125  {
126  //print $i.'-'.$filter.'-'.$modulename.'-'.join(',',$module->export_code).'<br>';
127  if ($filter && ($filter != $module->export_code[$r])) continue;
128 
129  // Test if condition to show are ok
130  if (! empty($module->export_enabled[$r]) && ! verifCond($module->export_enabled[$r])) continue;
131 
132  // Test if permissions are ok
133  $bool=true;
134  if (isset($module->export_permission))
135  {
136  foreach($module->export_permission[$r] as $val)
137  {
138  $perm=$val;
139  //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
140  if (! empty($perm[2]))
141  {
142  $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
143  }
144  else
145  {
146  $bool=$user->rights->{$perm[0]}->{$perm[1]};
147  }
148  if ($perm[0]=='user' && $user->admin) $bool=true;
149  if (! $bool) break;
150  }
151  }
152  //print $bool." $perm[0]"."<br>";
153 
154  // Permissions ok
155  // if ($bool)
156  // {
157  // Charge fichier lang en rapport
158  $langtoload=$module->getLangFilesArray();
159  if (is_array($langtoload))
160  {
161  foreach($langtoload as $key)
162  {
163  $langs->load($key);
164  }
165  }
166 
167  // Module
168  $this->array_export_module[$i]=$module;
169  // Permission
170  $this->array_export_perms[$i]=$bool;
171  // Icon
172  $this->array_export_icon[$i]=(isset($module->export_icon[$r])?$module->export_icon[$r]:$module->picto);
173  // Code du dataset export
174  $this->array_export_code[$i]=$module->export_code[$r];
175  // Libelle du dataset export
176  $this->array_export_label[$i]=$module->getExportDatasetLabel($r);
177  // Tableau des champ a exporter (cle=champ, valeur=libelle)
178  $this->array_export_fields[$i]=$module->export_fields_array[$r];
179  // Tableau des champs a filtrer (cle=champ, valeur1=type de donnees) on verifie que le module a des filtres
180  $this->array_export_TypeFields[$i]=(isset($module->export_TypeFields_array[$r])?$module->export_TypeFields_array[$r]:'');
181  // Tableau des entites a exporter (cle=champ, valeur=entite)
182  $this->array_export_entities[$i]=$module->export_entities_array[$r];
183  // Tableau des entites qui requiert abandon du DISTINCT (cle=entite, valeur=champ id child records)
184  $this->array_export_dependencies[$i]=(! empty($module->export_dependencies_array[$r])?$module->export_dependencies_array[$r]:'');
185  // Tableau des operations speciales sur champ
186  $this->array_export_special[$i]=(! empty($module->export_special_array[$r])?$module->export_special_array[$r]:'');
187  // Array of examples
188  $this->array_export_examplevalues[$i]=$module->export_examplevalues_array[$r];
189 
190  // Requete sql du dataset
191  $this->array_export_sql_start[$i]=$module->export_sql_start[$r];
192  $this->array_export_sql_end[$i]=$module->export_sql_end[$r];
193  $this->array_export_sql_order[$i]=$module->export_sql_order[$r];
194  //$this->array_export_sql[$i]=$module->export_sql[$r];
195 
196  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]):''));
197  $i++;
198  // }
199  }
200  }
201  }
202  }
203  }
204  closedir($handle);
205  }
206  }
207 
208  return 1;
209  }
210 
211 
212  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
222  function build_sql($indice, $array_selected, $array_filterValue)
223  {
224  // phpcs:enable
225  // Build the sql request
226  $sql=$this->array_export_sql_start[$indice];
227  $i=0;
228 
229  //print_r($array_selected);
230  foreach ($this->array_export_fields[$indice] as $key => $value)
231  {
232  if (! array_key_exists($key, $array_selected)) continue; // Field not selected
233  if (preg_match('/^none\./', $key)) continue; // A field that must not appears into SQL
234  if ($i > 0) $sql.=', ';
235  else $i++;
236 
237  if (strpos($key, ' as ')===false) {
238  $newfield=$key.' as '.str_replace(array('.', '-','(',')'),'_',$key);
239  } else {
240  $newfield=$key;
241  }
242 
243  $sql.=$newfield;
244  }
245  $sql.=$this->array_export_sql_end[$indice];
246 
247  // Add the WHERE part. Filtering into sql if a filtering array is provided
248  if (is_array($array_filterValue) && !empty($array_filterValue))
249  {
250  $sqlWhere='';
251  // Loop on each condition to add
252  foreach ($array_filterValue as $key => $value)
253  {
254  if (preg_match('/GROUP_CONCAT/i', $key)) continue;
255  if ($value != '') $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
256  }
257  $sql.=$sqlWhere;
258  }
259 
260  // Add the order
261  $sql.=$this->array_export_sql_order[$indice];
262 
263  // Add the HAVING part.
264  if (is_array($array_filterValue) && !empty($array_filterValue))
265  {
266  // Loop on each condition to add
267  foreach ($array_filterValue as $key => $value)
268  {
269  if (preg_match('/GROUP_CONCAT/i', $key) and $value != '') $sql.=" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
270  }
271  }
272 
273  return $sql;
274  }
275 
276  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
285  function build_filterQuery($TypeField, $NameField, $ValueField)
286  {
287  // phpcs:enable
288  //print $TypeField." ".$NameField." ".$ValueField;
289  $InfoFieldList = explode(":", $TypeField);
290  // build the input field on depend of the type of file
291  switch ($InfoFieldList[0]) {
292  case 'Text':
293  if (! (strpos($ValueField, '%') === false))
294  $szFilterQuery.=" ".$NameField." LIKE '".$ValueField."'";
295  else
296  $szFilterQuery.=" ".$NameField." = '".$ValueField."'";
297  break;
298  case 'Date':
299  if (strpos($ValueField, "+") > 0)
300  {
301  // mode plage
302  $ValueArray = explode("+", $ValueField);
303  $szFilterQuery ="(".$this->conditionDate($NameField,trim($ValueArray[0]),">=");
304  $szFilterQuery.=" AND ".$this->conditionDate($NameField,trim($ValueArray[1]),"<=").")";
305  }
306  else
307  {
308  if (is_numeric(substr($ValueField,0,1)))
309  $szFilterQuery=$this->conditionDate($NameField,trim($ValueField),"=");
310  else
311  $szFilterQuery=$this->conditionDate($NameField,trim(substr($ValueField,1)),substr($ValueField,0,1));
312  }
313  break;
314  case 'Duree':
315  break;
316  case 'Numeric':
317  // si le signe -
318  if (strpos($ValueField, "+") > 0)
319  {
320  // mode plage
321  $ValueArray = explode("+", $ValueField);
322  $szFilterQuery ="(".$NameField.">=".$ValueArray[0];
323  $szFilterQuery.=" AND ".$NameField."<=".$ValueArray[1].")";
324  }
325  else
326  {
327  if (is_numeric(substr($ValueField,0,1)))
328  $szFilterQuery=" ".$NameField."=".$ValueField;
329  else
330  $szFilterQuery=" ".$NameField.substr($ValueField,0,1).substr($ValueField,1);
331  }
332  break;
333  case 'Boolean':
334  $szFilterQuery=" ".$NameField."=".(is_numeric($ValueField) ? $ValueField : ($ValueField =='yes' ? 1: 0) );
335  break;
336  case 'Status':
337  case 'List':
338  if (is_numeric($ValueField))
339  $szFilterQuery=" ".$NameField."=".$ValueField;
340  else {
341  if (! (strpos($ValueField, '%') === false))
342  $szFilterQuery=" ".$NameField." LIKE '".$ValueField."'";
343  else
344  $szFilterQuery=" ".$NameField." = '".$ValueField."'";
345  }
346  break;
347  default:
348  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);
349  }
350 
351  return $szFilterQuery;
352  }
353 
362  function conditionDate($Field, $Value, $Sens)
363  {
364  // TODO date_format is forbidden, not performant and not portable. Use instead BETWEEN
365  if (strlen($Value)==4) $Condition=" date_format(".$Field.",'%Y') ".$Sens." '".$Value."'";
366  elseif (strlen($Value)==6) $Condition=" date_format(".$Field.",'%Y%m') ".$Sens." '".$Value."'";
367  else $Condition=" date_format(".$Field.",'%Y%m%d') ".$Sens." ".$Value;
368  return $Condition;
369  }
370 
371  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
380  function build_filterField($TypeField, $NameField, $ValueField)
381  {
382  // phpcs:enable
383  global $conf,$langs;
384 
385  $szFilterField='';
386  $InfoFieldList = explode(":", $TypeField);
387 
388  // build the input field on depend of the type of file
389  switch ($InfoFieldList[0])
390  {
391  case 'Text':
392  case 'Date':
393  $szFilterField='<input type="text" name="'.$NameField.'" value="'.$ValueField.'">';
394  break;
395  case 'Duree':
396  case 'Numeric':
397  case 'Number':
398  // Must be a string text to allow to use comparison strings like "<= 999"
399  $szFilterField='<input type="text" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
400  break;
401  case 'Status':
402  if (! empty($conf->global->MAIN_ACTIVATE_HTML5)) $szFilterField='<input type="number" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
403  else $szFilterField='<input type="text" size="6" name="'.$NameField.'" value="'.$ValueField.'">';
404  break;
405  case 'Boolean':
406  $szFilterField='<select name="'.$NameField.'" class="flat">';
407  $szFilterField.='<option ';
408  if ($ValueField=='') $szFilterField.=' selected ';
409  $szFilterField.=' value="">&nbsp;</option>';
410 
411  $szFilterField.='<option ';
412  if ($ValueField=='yes' || $ValueField == '1') $szFilterField.=' selected ';
413  $szFilterField.=' value="1">'.yn(1).'</option>';
414 
415  $szFilterField.='<option ';
416  if ($ValueField=='no' || $ValueField=='0') $szFilterField.=' selected ';
417  $szFilterField.=' value="0">'.yn(0).'</option>';
418  $szFilterField.="</select>";
419  break;
420  case 'List':
421  // 0 : Type du champ
422  // 1 : Nom de la table
423  // 2 : Nom du champ contenant le libelle
424  // 3 : Name of field with key (if it is not "rowid"). Used this field as key for combo list.
425  if (count($InfoFieldList)==4)
426  $keyList=$InfoFieldList[3];
427  else
428  $keyList='rowid';
429  $sql = 'SELECT '.$keyList.' as rowid, '.$InfoFieldList[2].' as label'.(empty($InfoFieldList[3])?'':', '.$InfoFieldList[3].' as code');
430  if ($InfoFieldList[1] == 'c_stcomm') $sql = 'SELECT id as id, '.$keyList.' as rowid, '.$InfoFieldList[2].' as label'.(empty($InfoFieldList[3])?'':', '.$InfoFieldList[3].' as code');
431  if ($InfoFieldList[1] == 'c_country') $sql = 'SELECT '.$keyList.' as rowid, '.$InfoFieldList[2].' as label, code as code';
432  $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[1];
433 
434  $resql = $this->db->query($sql);
435  if ($resql)
436  {
437  $szFilterField='<select class="flat" name="'.$NameField.'">';
438  $szFilterField.='<option value="0">&nbsp;</option>';
439  $num = $this->db->num_rows($resql);
440 
441  $i = 0;
442  if ($num)
443  {
444  while ($i < $num)
445  {
446  $obj = $this->db->fetch_object($resql);
447  if ($obj->label == '-')
448  {
449  // Discard entry '-'
450  $i++;
451  continue;
452  }
453  //var_dump($InfoFieldList[1]);
454  $labeltoshow=dol_trunc($obj->label,18);
455  if ($InfoFieldList[1] == 'c_stcomm')
456  {
457  $langs->load("companies");
458  $labeltoshow=(($langs->trans("StatusProspect".$obj->id) != "StatusProspect".$obj->id)?$langs->trans("StatusProspect".$obj->id):$obj->label);
459  }
460  if ($InfoFieldList[1] == 'c_country')
461  {
462  //var_dump($sql);
463  $langs->load("dict");
464  $labeltoshow=(($langs->trans("Country".$obj->code) != "Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label);
465  }
466  if (!empty($ValueField) && $ValueField == $obj->rowid)
467  {
468  $szFilterField.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
469  }
470  else
471  {
472  $szFilterField.='<option value="'.$obj->rowid.'" >'.$labeltoshow.'</option>';
473  }
474  $i++;
475  }
476  }
477  $szFilterField.="</select>";
478 
479  $this->db->free($resql);
480  }
481  else dol_print_error($this->db);
482  break;
483  }
484 
485  return $szFilterField;
486  }
487 
494  function genDocFilter($TypeField)
495  {
496  global $langs;
497 
498  $szMsg='';
499  $InfoFieldList = explode(":", $TypeField);
500  // build the input field on depend of the type of file
501  switch ($InfoFieldList[0]) {
502  case 'Text':
503  $szMsg= $langs->trans('ExportStringFilter');
504  break;
505  case 'Date':
506  $szMsg = $langs->trans('ExportDateFilter');
507  break;
508  case 'Duree':
509  break;
510  case 'Numeric':
511  $szMsg = $langs->trans('ExportNumericFilter');
512  break;
513  case 'Boolean':
514  break;
515  case 'List':
516  break;
517  }
518  return $szMsg;
519  }
520 
521  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
535  function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery = '')
536  {
537  // phpcs:enable
538  global $conf,$langs;
539 
540  $indice=0;
541  asort($array_selected);
542 
543  dol_syslog(get_class($this)."::".__FUNCTION__." ".$model.", ".$datatoexport.", ".implode(",", $array_selected));
544 
545  // Check parameters or context properties
546  if (empty($this->array_export_fields) || ! is_array($this->array_export_fields))
547  {
548  $this->error="ErrorBadParameter";
549  return -1;
550  }
551 
552  // Creation of class to export using model ExportXXX
553  $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/";
554  $file = "export_".$model.".modules.php";
555  $classname = "Export".$model;
556  require_once $dir.$file;
557  $objmodel = new $classname($this->db);
558 
559  if (! empty($sqlquery)) $sql = $sqlquery;
560  else
561  {
562  // Define value for indice from $datatoexport
563  $foundindice=0;
564  foreach($this->array_export_code as $key => $dataset)
565  {
566  if ($datatoexport == $dataset)
567  {
568  $indice=$key;
569  $foundindice++;
570  //print "Found indice = ".$indice." for dataset=".$datatoexport."\n";
571  break;
572  }
573  }
574  if (empty($foundindice))
575  {
576  $this->error="ErrorBadParameter can't find dataset ".$datatoexport." into preload arrays this->array_export_code";
577  return -1;
578  }
579  $sql=$this->build_sql($indice, $array_selected, $array_filterValue);
580  }
581 
582  // Run the sql
583  $this->sqlusedforexport=$sql;
584  dol_syslog(get_class($this)."::".__FUNCTION__."", LOG_DEBUG);
585  $resql = $this->db->query($sql);
586  if ($resql)
587  {
588  //$this->array_export_label[$indice]
589  if ($conf->global->EXPORT_PREFIX_SPEC)
590  $filename=$conf->global->EXPORT_PREFIX_SPEC."_".$datatoexport;
591  else
592  $filename="export_".$datatoexport;
593  $filename.='.'.$objmodel->getDriverExtension();
594  $dirname=$conf->export->dir_temp.'/'.$user->id;
595 
596  $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
597 
598  // Open file
599  dol_mkdir($dirname);
600  $result=$objmodel->open_file($dirname."/".$filename, $outputlangs);
601 
602  if ($result >= 0)
603  {
604  // Genere en-tete
605  $objmodel->write_header($outputlangs);
606 
607  // Genere ligne de titre
608  $objmodel->write_title($this->array_export_fields[$indice],$array_selected,$outputlangs,$this->array_export_TypeFields[$indice]);
609 
610  while ($obj = $this->db->fetch_object($resql))
611  {
612  // Process special operations
613  if (! empty($this->array_export_special[$indice]))
614  {
615  foreach ($this->array_export_special[$indice] as $key => $value)
616  {
617  if (! array_key_exists($key, $array_selected)) continue; // Field not selected
618  // Operation NULLIFNEG
619  if ($this->array_export_special[$indice][$key]=='NULLIFNEG')
620  {
621  //$alias=$this->array_export_alias[$indice][$key];
622  $alias=str_replace(array('.', '-','(',')'),'_',$key);
623  if ($obj->$alias < 0) $obj->$alias='';
624  }
625  // Operation ZEROIFNEG
626  elseif ($this->array_export_special[$indice][$key]=='ZEROIFNEG')
627  {
628  //$alias=$this->array_export_alias[$indice][$key];
629  $alias=str_replace(array('.', '-','(',')'),'_',$key);
630  if ($obj->$alias < 0) $obj->$alias='0';
631  }
632  // Operation INVOICEREMAINTOPAY
633  elseif ($this->array_export_special[$indice][$key]=='getRemainToPay')
634  {
635  //$alias=$this->array_export_alias[$indice][$key];
636  $alias=str_replace(array('.', '-','(',')'),'_',$key);
637  $remaintopay='';
638  if ($obj->f_rowid > 0)
639  {
640  global $tmpobjforcomputecall;
641  if (! is_object($tmpobjforcomputecall))
642  {
643  include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
644  $tmpobjforcomputecall=new Facture($this->db);
645  }
646  $tmpobjforcomputecall->id = $obj->f_rowid;
647  $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
648  $remaintopay=$tmpobjforcomputecall->getRemainToPay();
649  }
650  $obj->$alias=$remaintopay;
651  }
652  else
653  {
654  // TODO FIXME Export of compute field does not work. $obj containt $obj->alias_field and formulat will contains $obj->field
655  $computestring=$this->array_export_special[$indice][$key];
656  $tmp=dol_eval($computestring, 1, 0);
657  $obj->$alias=$tmp;
658 
659  $this->error="ERROPNOTSUPPORTED. Operation ".$this->array_export_special[$indice][$key]." not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
660  return -1;
661  }
662  }
663  }
664  // end of special operation processing
665  $objmodel->write_record($array_selected,$obj,$outputlangs,$this->array_export_TypeFields[$indice]);
666  }
667 
668  // Genere en-tete
669  $objmodel->write_footer($outputlangs);
670 
671  // Close file
672  $objmodel->close_file();
673 
674  return 1;
675  }
676  else
677  {
678  $this->error=$objmodel->error;
679  dol_syslog("Export::build_file Error: ".$this->error, LOG_ERR);
680  return -1;
681  }
682  }
683  else
684  {
685  $this->error=$this->db->error()." - sql=".$sql;
686  return -1;
687  }
688  }
689 
696  function create($user)
697  {
698  global $conf;
699 
700  dol_syslog("Export.class.php::create");
701 
702  $this->db->begin();
703 
704  $filter='';
705 
706  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'export_model (';
707  $sql.= 'label,';
708  $sql.= 'type,';
709  $sql.= 'field,';
710  $sql.= 'fk_user,';
711  $sql.= 'filter';
712  $sql.= ') VALUES (';
713  $sql.= "'".$this->db->escape($this->model_name)."',";
714  $sql.= "'".$this->db->escape($this->datatoexport)."',";
715  $sql.= "'".$this->db->escape($this->hexa)."',";
716  $sql.= "'".$user->id."',";
717  $sql.= "'".$this->db->escape($this->hexafiltervalue)."'";
718  $sql.= ")";
719 
720  dol_syslog(get_class($this)."::create", LOG_DEBUG);
721  $resql=$this->db->query($sql);
722  if ($resql)
723  {
724  $this->db->commit();
725  return 1;
726  }
727  else
728  {
729  $this->error=$this->db->lasterror();
730  $this->errno=$this->db->lasterrno();
731  $this->db->rollback();
732  return -1;
733  }
734  }
735 
742  function fetch($id)
743  {
744  $sql = 'SELECT em.rowid, em.label, em.type, em.field, em.filter';
745  $sql.= ' FROM '.MAIN_DB_PREFIX.'export_model as em';
746  $sql.= ' WHERE em.rowid = '.$id;
747 
748  dol_syslog("Export::fetch", LOG_DEBUG);
749  $result = $this->db->query($sql);
750  if ($result)
751  {
752  $obj = $this->db->fetch_object($result);
753  if ($obj)
754  {
755  $this->id = $obj->rowid;
756  $this->model_name = $obj->label;
757  $this->datatoexport = $obj->type;
758 
759  $this->hexa = $obj->field;
760  $this->hexafiltervalue = $obj->filter;
761 
762  return 1;
763  }
764  else
765  {
766  $this->error="ModelNotFound";
767  return -2;
768  }
769  }
770  else
771  {
772  dol_print_error($this->db);
773  return -3;
774  }
775  }
776 
777 
785  function delete($user, $notrigger=0)
786  {
787  global $conf, $langs;
788  $error=0;
789 
790  $sql = "DELETE FROM ".MAIN_DB_PREFIX."export_model";
791  $sql.= " WHERE rowid=".$this->id;
792 
793  $this->db->begin();
794 
795  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
796  $resql = $this->db->query($sql);
797  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
798 
799  if (! $error)
800  {
801  if (! $notrigger)
802  {
803  // Uncomment this and change MYOBJECT to your own tag if you
804  // want this action call a trigger.
805 
807  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
808  //$interface=new Interfaces($this->db);
809  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
810  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
812  }
813  }
814 
815  // Commit or rollback
816  if ($error)
817  {
818  foreach($this->errors as $errmsg)
819  {
820  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
821  $this->error.=($this->error?', '.$errmsg:$errmsg);
822  }
823  $this->db->rollback();
824  return -1*$error;
825  }
826  else
827  {
828  $this->db->commit();
829  return 1;
830  }
831  }
832 
833  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
840  function list_export_model()
841  {
842  // phpcs:enable
843  global $conf, $langs;
844 
845  $sql = "SELECT em.rowid, em.field, em.label, em.type, em.filter";
846  $sql.= " FROM ".MAIN_DB_PREFIX."export_model as em";
847  $sql.= " ORDER BY rowid";
848 
849  $result = $this->db->query($sql);
850  if ($result)
851  {
852  $num = $this->db->num_rows($result);
853  $i = 0;
854  while ($i < $num)
855  {
856  $obj = $this->db->fetch_object($result);
857  $keyModel = array_search($obj->type, $this->array_export_code);
858  print "<tr>";
859  print '<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.'&datatoexport='.$obj->type.'>'.$obj->label.'</a></td>';
860  print '<td>';
861  print img_object($this->array_export_module[$keyModel]->getName(),$this->array_export_icon[$keyModel]).' ';
862  print $this->array_export_module[$keyModel]->getName().' - ';
863  // recuperation du nom de l'export
864 
865  $string=$langs->trans($this->array_export_label[$keyModel]);
866  print ($string!=$this->array_export_label[$keyModel]?$string:$this->array_export_label[$keyModel]);
867  print '</td>';
868  //print '<td>'.$obj->type.$keyModel.'</td>';
869  print '<td>'.str_replace(',',' , ',$obj->field).'</td>';
870  if (! empty($obj->filter)) {
871  $filter = json_decode($obj->filter, true);
872  print '<td>'.str_replace(',',' , ',$filter['field']).'</td>';
873  print '<td>'.str_replace(',',' , ',$filter['value']).'</td>';
874  }
875  // suppression de l'export
876  print '<td align="right">';
877  print '<a href="'.$_SERVER["PHP_SELF"].'?action=deleteprof&id='.$obj->rowid.'">';
878  print img_delete();
879  print '</a>';
880  print "</tr>";
881 
882  $i++;
883  }
884  }
885  else {
886  dol_print_error($this->db);
887  }
888  }
889 }
verifCond($strRights)
Verify if condition in string is ok or not.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
print
Draft customers invoices.
Definition: index.php:91
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 &#39;...&#39; if string larger than length.
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:1053
build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery='')
Build export file.
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.
img_delete($titlealt='default', $other='class="pictodelete"')
Show delete logo.
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.
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
list_export_model()
Output list all export models TODO Move this into a class htmlxxx.class.php.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
Class to manage invoices.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
create($user)
Save an export model in database.