113 global $langs, $conf, $mysoc;
115 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
120 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
124 foreach ($modulesdir as $dir) {
127 if (is_resource($handle)) {
129 while (($file = readdir($handle)) !==
false) {
131 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg)) {
132 $modulename = $reg[1];
136 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
137 if ($part ==
'propale') {
140 if (empty($conf->$part->enabled)) {
146 $file = $dir.$modulename.
".class.php";
147 $classname = $modulename;
149 $module =
new $classname($this->db);
151 if (isset($module->export_code) && is_array($module->export_code)) {
152 foreach ($module->export_code as $r => $value) {
154 if ($filter && ($filter != $module->export_code[$r])) {
159 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r])) {
165 if (isset($module->export_permission)) {
166 foreach ($module->export_permission[$r] as $val) {
169 if (!empty($perm[2])) {
170 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
171 } elseif (!empty($perm[1])) {
172 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
176 if ($perm[0] ==
'user' && $user->admin) {
190 $langtoload = $module->getLangFilesArray();
191 if (is_array($langtoload)) {
192 foreach ($langtoload as $key) {
199 $this->array_export_module[$i] = $module;
201 $this->array_export_perms[$i] = $bool;
203 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
205 $this->array_export_code[$i] = $module->export_code[$r];
207 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
209 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
211 $this->array_export_fields[$i] = $module->export_fields_array[$r];
213 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
215 $this->array_export_entities[$i] = $module->export_entities_array[$r];
217 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
219 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
221 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
223 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
226 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
227 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
228 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
231 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]) :
''));
257 public function build_sql($indice, $array_selected, $array_filterValue)
261 $sql = $this->array_export_sql_start[$indice];
265 foreach ($this->array_export_fields[$indice] as $key => $value) {
266 if (!array_key_exists($key, $array_selected)) {
269 if (preg_match(
'/^none\./', $key)) {
278 if (strpos($key,
' as ') ===
false) {
279 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
286 $sql .= $this->array_export_sql_end[$indice];
289 if (is_array($array_filterValue) && !empty($array_filterValue)) {
292 foreach ($array_filterValue as $key => $value) {
293 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
297 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
304 $sql .= $this->array_export_sql_order[$indice];
307 if (is_array($array_filterValue) && !empty($array_filterValue)) {
309 foreach ($array_filterValue as $key => $value) {
310 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
311 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
335 $InfoFieldList = explode(
":", $TypeField);
337 switch ($InfoFieldList[0]) {
339 if (!(strpos($ValueField,
'%') ===
false)) {
340 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
342 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
346 if (strpos($ValueField,
"+") > 0) {
348 $ValueArray = explode(
"+", $ValueField);
349 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
350 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
352 if (is_numeric(substr($ValueField, 0, 1))) {
353 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
355 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
362 if (strpos($ValueField,
"+") > 0) {
364 $ValueArray = explode(
"+", $ValueField);
365 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
366 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
368 if (is_numeric(substr($ValueField, 0, 1))) {
369 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
371 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
376 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
379 if (is_numeric($ValueField) && $ValueField > 0) {
380 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
382 $szFilterQuery =
" 1=1";
387 if (is_numeric($ValueField)) {
388 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
390 if (!(strpos($ValueField,
'%') ===
false)) {
391 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
393 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
398 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);
401 return $szFilterQuery;
437 global $conf, $langs, $form;
440 $InfoFieldList = explode(
":", $TypeField);
443 switch ($InfoFieldList[0]) {
446 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
452 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
455 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
458 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
459 $szFilterField .=
'<option ';
460 if ($ValueField ==
'') {
461 $szFilterField .=
' selected ';
463 $szFilterField .=
' value=""> </option>';
465 $szFilterField .=
'<option ';
466 if ($ValueField ==
'yes' || $ValueField ==
'1') {
467 $szFilterField .=
' selected ';
469 $szFilterField .=
' value="1">'.yn(1).
'</option>';
471 $szFilterField .=
'<option ';
472 if ($ValueField ==
'no' || $ValueField ==
'0') {
473 $szFilterField .=
' selected ';
475 $szFilterField .=
' value="0">'.yn(0).
'</option>';
476 $szFilterField .=
"</select>";
480 if ($InfoFieldList[1] ==
'select_company') {
481 $szFilterField .= $form->select_company(
'', $NameField,
'', 1, 0, 0, [], 0,
'maxwidth200');
482 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
484 $szFilterField .= $form->select_contact(0,
'', $NameField,
' ',
'',
'', 0,
'minwidth100imp maxwidth200',
true);
485 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
486 $szFilterField .= $form->select_dolusers(
'', $NameField, 1,
null, 0,
'',
'',
'', 0, 0,
"", 0,
"",
"maxwidth200");
496 if (!empty($InfoFieldList[3])) {
497 $keyList = $InfoFieldList[3];
501 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
502 if ($InfoFieldList[1] ==
'c_stcomm') {
503 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
505 if ($InfoFieldList[1] ==
'c_country') {
506 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
508 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
509 if (!empty($InfoFieldList[4])) {
510 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
513 $resql = $this->db->query($sql);
515 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
516 $szFilterField .=
'<option value="0"> </option>';
517 $num = $this->db->num_rows($resql);
522 $obj = $this->db->fetch_object($resql);
523 if ($obj->label ==
'-') {
529 $labeltoshow =
dol_trunc($obj->label, 18);
530 if ($InfoFieldList[1] ==
'c_stcomm') {
531 $langs->load(
"companies");
532 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
534 if ($InfoFieldList[1] ==
'c_country') {
536 $langs->load(
"dict");
537 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
539 if (!empty($ValueField) && $ValueField == $obj->rowid) {
540 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
542 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
547 $szFilterField .=
"</select>";
549 $this->db->free($resql);
556 return $szFilterField;
606 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'', $separator =
'')
609 global $conf, $langs, $mysoc;
612 asort($array_selected);
614 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
617 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
618 $this->error =
"ErrorBadParameter";
624 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
625 $file =
"export_".$model.
".modules.php";
626 $classname =
"Export".$model;
627 require_once $dir.$file;
628 $objmodel =
new $classname($this->db);
630 if (in_array($model, array(
'csvutf8',
'csviso')) && !empty($separator)) {
631 $objmodel->separator = $separator;
634 if (!empty($sqlquery)) {
639 foreach ($this->array_export_code as $key => $dataset) {
640 if ($datatoexport == $dataset) {
647 if (empty($foundindice)) {
648 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
651 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
655 $this->sqlusedforexport = $sql;
657 $resql = $this->db->query($sql);
663 $filename =
"export_".$datatoexport;
668 $filename .=
'.'.$objmodel->getDriverExtension();
669 $dirname = $conf->export->dir_temp.
'/'.$user->id;
671 $outputlangs = clone $langs;
675 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
679 $objmodel->write_header($outputlangs);
682 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
684 while ($obj = $this->db->fetch_object($resql)) {
686 if (!empty($this->array_export_special[$indice])) {
687 foreach ($this->array_export_special[$indice] as $key => $value) {
688 if (!array_key_exists($key, $array_selected)) {
692 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
694 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
695 if ($obj->$alias < 0) {
698 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
701 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
702 if ($obj->$alias < 0) {
705 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
707 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
709 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
711 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
714 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
716 if ($obj->f_rowid > 0) {
717 global $tmpobjforcomputecall;
718 if (!is_object($tmpobjforcomputecall)) {
719 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
720 $tmpobjforcomputecall =
new Facture($this->db);
722 $tmpobjforcomputecall->id = $obj->f_rowid;
723 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
724 $tmpobjforcomputecall->close_code = $obj->f_close_code;
725 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
727 $obj->$alias = $remaintopay;
728 } elseif (is_array($this->array_export_special[$indice][$key]) &&
729 !empty($this->array_export_special[$indice][$key][
'rule']) &&
730 $this->array_export_special[$indice][$key][
'rule'] ==
'compute'
733 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
735 if (!empty($this->array_export_special[$indice][$key][
'class']) &&
736 !empty($this->array_export_special[$indice][$key][
'classfile']) &&
737 !empty($this->array_export_special[$indice][$key][
'method'])
739 if (!
dol_include_once($this->array_export_special[$indice][$key][
'classfile'])) {
740 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['classfile']} not found";
744 if (!class_exists($this->array_export_special[$indice][$key][
'class'])) {
745 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['class']} class doesn't exist";
749 $className = $this->array_export_special[$indice][$key][
'class'];
750 $tmpObject =
new $className($this->db);
751 if (!method_exists($tmpObject, $this->array_export_special[$indice][$key][
'method'])) {
752 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['method']} method doesn't exist";
756 $methodName = $this->array_export_special[$indice][$key][
'method'];
758 if (!empty($this->array_export_special[$indice][$key][
'method_params'])) {
759 foreach ($this->array_export_special[$indice][$key][
'method_params'] as $paramName) {
760 $params[] = $obj->$paramName ??
null;
763 $value = $tmpObject->$methodName(...$params);
765 $obj->$alias = $value;
770 $computestring = $this->array_export_special[$indice][$key];
774 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
780 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
784 $objmodel->write_footer($outputlangs);
787 $objmodel->close_file();
791 $this->error = $objmodel->error;
792 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
796 $this->error = $this->db->error().
" - sql=".$sql;