49 public $array_export_icon;
54 public $array_export_perms;
73 public $array_export_code = array();
77 public $array_export_code_for_sort = array();
81 public $array_export_module = array();
85 public $array_export_label = array();
89 public $array_export_sql_start = array();
93 public $array_export_sql_end = array();
97 public $array_export_sql_order = array();
102 public $array_export_fields = array();
106 public $array_export_TypeFields = array();
110 public $array_export_FilterValue = array();
114 public $array_export_entities = array();
118 public $array_export_dependencies = array();
122 public $array_export_special = array();
126 public $array_export_examplevalues = array();
130 public $array_export_help = array();
140 public $hexafiltervalue;
144 public $datatoexport;
157 public $sqlusedforexport;
182 global $langs, $conf,
$mysoc;
184 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
189 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
193 foreach ($modulesdir as $dir) {
196 if (is_resource($handle)) {
198 while (($file = readdir($handle)) !==
false) {
200 if (preg_match(
'/\.back$/i', $file)) {
205 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg)) {
206 $modulename = $reg[1];
210 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
211 if ($part ==
'propale') {
214 if (empty($conf->$part->enabled)) {
220 $file = $dir.$modulename.
".class.php";
221 $classname = $modulename;
223 $module =
new $classname($this->db);
224 '@phan-var-force DolibarrModules $module';
226 if (isset($module->export_code) && is_array($module->export_code)) {
227 foreach ($module->export_code as $r => $value) {
229 if ($filter && ($filter != $module->export_code[$r])) {
234 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r])) {
240 if (isset($module->export_permission)) {
241 foreach ($module->export_permission[$r] as $val) {
244 if (!empty($perm[2])) {
245 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
246 } elseif (!empty($perm[1])) {
247 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
251 if ($perm[0] ==
'user' && $user->admin) {
265 $langtoload = $module->getLangFilesArray();
266 if (is_array($langtoload)) {
267 foreach ($langtoload as $key) {
274 $this->array_export_module[$i] = $module;
276 $this->array_export_perms[$i] = $bool;
278 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
280 $this->array_export_code[$i] = $module->export_code[$r];
282 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
284 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
286 $this->array_export_fields[$i] = (isset($module->export_fields_array[$r]) ? $module->export_fields_array[$r] : []);
288 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
290 $this->array_export_entities[$i] = (isset($module->export_entities_array[$r]) ? $module->export_entities_array[$r] :
'');
292 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
294 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
296 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
298 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
301 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
302 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
303 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
307 dol_syslog(get_class($this).
"::load_arrays loaded for module ".$modulename.
" with index ".$i.
", dataset=".$module->export_code[$r].
", nb of fields=".(property_exists($module,
'export_fields_code') && !empty($module->export_fields_code[$r]) ? count($module->export_fields_code[$r]) :
''));
333 public function build_sql($indice, $array_selected, $array_filterValue)
337 $sql = $this->array_export_sql_start[$indice];
341 foreach ($this->array_export_fields[$indice] as $key => $value) {
342 if (!array_key_exists($key, $array_selected)) {
345 if (preg_match(
'/^none\./', $key)) {
354 if (strpos($key,
' as ') ===
false) {
355 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
362 $sql .= $this->array_export_sql_end[$indice];
365 if (is_array($array_filterValue) && !empty($array_filterValue)) {
368 foreach ($array_filterValue as $key => $value) {
369 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
373 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
380 $sql .= $this->array_export_sql_order[$indice];
383 if (is_array($array_filterValue) && !empty($array_filterValue)) {
385 foreach ($array_filterValue as $key => $value) {
386 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
387 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
411 $InfoFieldList = explode(
":", $TypeField);
413 switch ($InfoFieldList[0]) {
415 if (!(strpos($ValueField,
'%') ===
false)) {
416 $szFilterQuery =
" ".$this->db->sanitize($NameField).
" LIKE '".$this->db->escape($ValueField).
"'";
418 $szFilterQuery =
" ".$this->db->sanitize($NameField).
" = '".$this->db->escape($ValueField).
"'";
422 if (strpos($ValueField,
"+") > 0) {
424 $ValueArray = explode(
"+", $ValueField);
425 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
426 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
428 if (is_numeric(substr($ValueField, 0, 1))) {
429 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
431 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
438 if (strpos($ValueField,
"+") > 0) {
440 $ValueArray = explode(
"+", $ValueField);
441 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
442 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
444 if (is_numeric(substr($ValueField, 0, 1))) {
445 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
447 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
452 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
455 if (is_numeric($ValueField) && $ValueField > 0) {
456 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
458 $szFilterQuery =
" 1=1";
463 if (is_numeric($ValueField)) {
464 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
466 if (!(strpos($ValueField,
'%') ===
false)) {
467 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
469 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
474 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);
477 return $szFilterQuery;
491 if (strlen($Value) == 4) {
492 $Condition =
" date_format(".$Field.
",'%Y') ".$Sens.
" '".$this->db->escape($Value).
"'";
493 } elseif (strlen($Value) == 6) {
494 $Condition =
" date_format(".$Field.
",'%Y%m') ".$Sens.
" '".$this->db->escape($Value).
"'";
496 $Condition =
" date_format(".$Field.
",'%Y%m%d') ".$Sens.
" '".$this->db->escape($Value).
"'";
513 global $langs, $form;
516 $InfoFieldList = explode(
":", $TypeField);
519 switch ($InfoFieldList[0]) {
522 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
528 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
531 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
534 $szFilterField =
'<select name="'.$NameField.
'" id="'.
dol_escape_all($NameField).
'" class="flat width75 maxwidth75">';
535 $szFilterField .=
'<option ';
536 if ($ValueField ==
'') {
537 $szFilterField .=
' selected ';
539 $szFilterField .=
' value=""> </option>';
541 $szFilterField .=
'<option ';
542 if ($ValueField ==
'yes' || $ValueField ==
'1') {
543 $szFilterField .=
' selected ';
545 $szFilterField .=
' value="1">'.yn(1).
'</option>';
547 $szFilterField .=
'<option ';
548 if ($ValueField ==
'no' || $ValueField ==
'0') {
549 $szFilterField .=
' selected ';
551 $szFilterField .=
' value="0">'.yn(0).
'</option>';
552 $szFilterField .=
"</select>";
557 if ($InfoFieldList[1] ==
'select_company') {
558 $szFilterField .= $form->select_company(
'', $NameField,
'', 1, 0, 0, [], 0,
'maxwidth200');
559 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
561 $szFilterField .= $form->select_contact(0,
'', $NameField,
' ',
'',
'', 0,
'minwidth100imp maxwidth200',
true);
562 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
563 $szFilterField .= $form->select_dolusers(
'', $NameField, 1,
null, 0,
'',
'',
'', 0, 0,
"", 0,
"",
"maxwidth200");
573 if (!empty($InfoFieldList[3])) {
574 $keyList = $InfoFieldList[3];
578 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
579 if ($InfoFieldList[1] ==
'c_stcomm') {
580 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
582 if ($InfoFieldList[1] ==
'c_country') {
583 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
585 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
586 if (!empty($InfoFieldList[4])) {
587 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
590 $resql = $this->db->query($sql);
592 $szFilterField =
'<select class="minwidth300 maxwidth500" name="'.$NameField.
'" id="'.
dol_escape_all($NameField).
'">';
593 $szFilterField .=
'<option value="0"> </option>';
594 $num = $this->db->num_rows($resql);
599 $obj = $this->db->fetch_object($resql);
600 if ($obj->label ==
'-') {
606 $labeltoshow = $obj->label;
607 if ($InfoFieldList[1] ==
'c_stcomm') {
608 $langs->load(
"companies");
609 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
611 if ($InfoFieldList[1] ==
'c_country') {
613 $langs->load(
"dict");
614 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
616 if (!empty($ValueField) && $ValueField == $obj->rowid) {
619 $szFilterField .=
'<option value="'.$obj->rowid.
'" data-html="'.
dolPrintHTMLForAttribute($labeltoshow).
'">'.$labeltoshow.
'</option>';
624 $szFilterField .=
"</select>";
627 $this->db->free($resql);
634 return $szFilterField;
648 $InfoFieldList = explode(
":", $TypeField);
650 switch ($InfoFieldList[0]) {
652 $szMsg = $langs->trans(
'ExportStringFilter');
655 $szMsg = $langs->trans(
'ExportDateFilter');
659 $szMsg = $langs->trans(
'ExportNumericFilter');
684 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'', $separator =
'')
687 global $conf, $langs,
$mysoc;
690 asort($array_selected);
692 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
695 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
696 $this->error =
"ErrorBadParameter";
702 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
703 $file =
"export_".$model.
".modules.php";
704 $classname =
"Export".$model;
705 require_once $dir.$file;
706 $objmodel =
new $classname($this->db);
708 '@phan-var-force ModeleExports $objmodel';
710 if (in_array($model, array(
'csvutf8',
'csviso')) && !empty($separator) && empty($objmodel->separator)) {
711 $objmodel->separator = $separator;
714 if (!empty($sqlquery)) {
719 foreach ($this->array_export_code as $key => $dataset) {
720 if ($datatoexport == $dataset) {
727 if (empty($foundindice)) {
728 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
731 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
735 $this->sqlusedforexport = $sql;
737 $resql = $this->db->query($sql);
743 $filename =
"export_".$datatoexport;
748 $filename .=
'.'.$objmodel->getDriverExtension();
749 $dirname = $conf->export->dir_temp.
'/'.$user->id;
751 $outputlangs = clone $langs;
755 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
759 $objmodel->write_header($outputlangs);
762 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] : null);
765 $counterlineexported = 0;
766 while ($obj = $this->db->fetch_object($resql)) {
767 $counterlineexported++;
773 if (!empty($this->array_export_special[$indice])) {
774 foreach ($this->array_export_special[$indice] as $key => $value) {
775 if (!array_key_exists($key, $array_selected)) {
780 $item = $this->array_export_special[$indice][$key];
782 if (is_string($item) && $item ==
'NULLIFNEG') {
784 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
785 if ($obj->$alias < 0) {
788 } elseif (is_string($item) && $item ==
'ZEROIFNEG') {
791 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
792 if ($obj->$alias < 0) {
795 } elseif (is_string($item) && $item ==
'getNumOpenDays') {
797 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
800 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
801 $country_id =
$mysoc->country_id;
802 if ($obj->u_fk_country > 0) {
803 $country_id = $obj->u_fk_country;
807 } elseif (is_string($item) && $item ==
'getRemainToPay') {
810 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
812 if ($obj->f_rowid > 0) {
813 global $tmpobjforcomputecall;
814 if (!is_object($tmpobjforcomputecall)) {
815 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
816 $tmpobjforcomputecall =
new Facture($this->db);
818 $tmpobjforcomputecall->id = $obj->f_rowid;
819 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
820 $tmpobjforcomputecall->close_code = $obj->f_close_code;
821 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
823 $obj->$alias = $remaintopay;
824 } elseif (is_array($item) && array_key_exists(
'rule', $item) && $item[
'rule'] ==
'compute') {
826 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
828 if (!empty($item[
'class']) &&
829 !empty($item[
'classfile']) &&
830 !empty($item[
'method'])
833 $this->error =
"Computed field bad configuration: {$item['classfile']} not found";
837 if (!class_exists($item[
'class'])) {
838 $this->error =
"Computed field bad configuration: {$item['class']} class doesn't exist";
842 $className = $item[
'class'];
843 $tmpObject =
new $className($this->db);
844 '@phan-var-force CommonObject $tmpObject';
846 if (!method_exists($tmpObject, $item[
'method'])) {
847 $this->error =
"Computed field bad configuration: {$item['method']} method doesn't exist";
853 if (!empty($item[
'method_params'])) {
855 foreach ($item[
'method_params'] as $paramName) {
856 if (property_exists($obj, $paramName)) {
857 $params[] = $obj->$paramName;
859 $params[] = $paramName;
864 $value = $tmpObject->$methodName(...$params);
866 $obj->$alias = $value;
875 $this->error =
"ERRORNOTSUPPORTED. Operation not supported. Export of ".var_export($key,
true).
' '.var_export($item,
true).
" computed extrafields is not yet supported, please remove field.";
881 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] : null);
885 $objmodel->write_footer($outputlangs);
888 $objmodel->close_file();
892 $this->error = $objmodel->error;
893 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
897 $this->error = $this->db->error().
" - sql=".$sql;
914 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'export_model (';
920 $sql .=
') VALUES (';
921 $sql .=
"'".$this->db->escape($this->model_name).
"',";
922 $sql .=
" '".$this->db->escape($this->datatoexport).
"',";
923 $sql .=
" '".$this->db->escape($this->hexa).
"',";
924 $sql .=
' '.(isset($this->fk_user) ? (int) $this->fk_user :
'null').
",";
925 $sql .=
" '".$this->db->escape($this->hexafiltervalue).
"'";
928 $resql = $this->db->query($sql);
933 $this->error = $this->db->lasterror();
934 $this->errno = $this->db->lasterrno();
935 $this->db->rollback();
948 $sql =
'SELECT em.rowid, em.label, em.type, em.field, em.filter';
949 $sql .=
' FROM '.MAIN_DB_PREFIX.
'export_model as em';
950 $sql .=
' WHERE em.rowid = '.((int)
$id);
953 $result = $this->db->query($sql);
955 $obj = $this->db->fetch_object($result);
957 $this->
id = $obj->rowid;
958 $this->model_name = $obj->label;
959 $this->datatoexport = $obj->type;
961 $this->hexa = $obj->field;
962 $this->hexafiltervalue = $obj->filter;
966 $this->error =
"ModelNotFound";
983 public function delete($user, $notrigger = 0)
987 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"export_model";
988 $sql .=
" WHERE rowid=".((int) $this->
id);
992 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
993 $resql = $this->db->query($sql);
996 $this->errors[] =
"Error ".$this->db->lasterror();
1001 foreach ($this->errors as $errmsg) {
1002 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1003 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1005 $this->db->rollback();
1008 $this->db->commit();
1025 $sql =
"SELECT em.rowid, em.field, em.label, em.type, em.filter";
1026 $sql .=
" FROM ".MAIN_DB_PREFIX.
"export_model as em";
1027 $sql .=
" ORDER BY rowid";
1029 $result = $this->db->query($sql);
1031 $num = $this->db->num_rows($result);
1034 $obj = $this->db->fetch_object($result);
1035 $keyModel = array_search($obj->type, $this->array_export_code);
1037 print
'<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.
'&datatoexport='.$obj->type.
'>'.$obj->label.
'</a></td>';
1039 print
img_object($this->array_export_module[$keyModel]->getName(), $this->array_export_icon[$keyModel]).
' ';
1040 print $this->array_export_module[$keyModel]->getName().
' - ';
1043 $string = $langs->trans($this->array_export_label[$keyModel]);
1044 print($string != $this->array_export_label[$keyModel] ? $string : $this->array_export_label[$keyModel]);
1047 print
'<td>'.str_replace(
',',
' , ', $obj->field).
'</td>';
1048 if (!empty($obj->filter)) {
1049 $filter = json_decode($obj->filter,
true);
1050 print
'<td>'.str_replace(
',',
' , ', $filter[
'field']).
'</td>';
1051 print
'<td>'.str_replace(
',',
' , ', $filter[
'value']).
'</td>';
1054 print
'<td class="right">';
1055 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=deleteprof&token='.
newToken().
'&id='.$obj->rowid.
'">';