110 global $langs, $conf, $mysoc;
112 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
117 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
121 foreach ($modulesdir as $dir) {
124 if (is_resource($handle)) {
126 while (($file = readdir($handle)) !==
false) {
128 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg)) {
129 $modulename = $reg[1];
133 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
134 if ($part ==
'propale') {
137 if (empty($conf->$part->enabled)) {
143 $file = $dir.$modulename.
".class.php";
144 $classname = $modulename;
146 $module =
new $classname($this->db);
148 if (isset($module->export_code) && is_array($module->export_code)) {
149 foreach ($module->export_code as $r => $value) {
151 if ($filter && ($filter != $module->export_code[$r])) {
156 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r])) {
162 if (isset($module->export_permission)) {
163 foreach ($module->export_permission[$r] as $val) {
166 if (!empty($perm[2])) {
167 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
169 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
171 if ($perm[0] ==
'user' && $user->admin) {
185 $langtoload = $module->getLangFilesArray();
186 if (is_array($langtoload)) {
187 foreach ($langtoload as $key) {
194 $this->array_export_module[$i] = $module;
196 $this->array_export_perms[$i] = $bool;
198 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
200 $this->array_export_code[$i] = $module->export_code[$r];
202 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
204 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
206 $this->array_export_fields[$i] = $module->export_fields_array[$r];
208 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
210 $this->array_export_entities[$i] = $module->export_entities_array[$r];
212 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
214 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
216 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
218 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
221 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
222 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
223 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
226 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]) :
''));
252 public function build_sql($indice, $array_selected, $array_filterValue)
256 $sql = $this->array_export_sql_start[$indice];
260 foreach ($this->array_export_fields[$indice] as $key => $value) {
261 if (!array_key_exists($key, $array_selected)) {
264 if (preg_match(
'/^none\./', $key)) {
273 if (strpos($key,
' as ') ===
false) {
274 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
281 $sql .= $this->array_export_sql_end[$indice];
284 if (is_array($array_filterValue) && !empty($array_filterValue)) {
287 foreach ($array_filterValue as $key => $value) {
288 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
292 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
299 $sql .= $this->array_export_sql_order[$indice];
302 if (is_array($array_filterValue) && !empty($array_filterValue)) {
304 foreach ($array_filterValue as $key => $value) {
305 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
306 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
330 $InfoFieldList = explode(
":", $TypeField);
332 switch ($InfoFieldList[0]) {
334 if (!(strpos($ValueField,
'%') ===
false)) {
335 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
337 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
341 if (strpos($ValueField,
"+") > 0) {
343 $ValueArray = explode(
"+", $ValueField);
344 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
345 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
347 if (is_numeric(substr($ValueField, 0, 1))) {
348 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
350 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
358 if (strpos($ValueField,
"+") > 0) {
360 $ValueArray = explode(
"+", $ValueField);
361 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
362 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
364 if (is_numeric(substr($ValueField, 0, 1))) {
365 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
367 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
372 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
375 if (is_numeric($ValueField) && $ValueField > 0) {
376 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
378 $szFilterQuery =
" 1=1";
383 if (is_numeric($ValueField)) {
384 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
386 if (!(strpos($ValueField,
'%') ===
false)) {
387 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
389 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
394 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);
397 return $szFilterQuery;
433 global $conf, $langs, $form;
436 $InfoFieldList = explode(
":", $TypeField);
439 switch ($InfoFieldList[0]) {
442 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
448 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
451 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
454 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
455 $szFilterField .=
'<option ';
456 if ($ValueField ==
'') {
457 $szFilterField .=
' selected ';
459 $szFilterField .=
' value=""> </option>';
461 $szFilterField .=
'<option ';
462 if ($ValueField ==
'yes' || $ValueField ==
'1') {
463 $szFilterField .=
' selected ';
465 $szFilterField .=
' value="1">'.yn(1).
'</option>';
467 $szFilterField .=
'<option ';
468 if ($ValueField ==
'no' || $ValueField ==
'0') {
469 $szFilterField .=
' selected ';
471 $szFilterField .=
' value="0">'.yn(0).
'</option>';
472 $szFilterField .=
"</select>";
476 if ($InfoFieldList[1] ==
'select_company') {
477 $szFilterField .= $form->select_company(
'', $NameField,
'', 1, 0, 0, [], 0,
'maxwidth200');
478 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
479 $szFilterField .= $form->selectcontacts(0,
'', $NameField,
' ',
'',
'', 0,
'maxwidth200');
480 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
481 $szFilterField .= $form->select_dolusers(
'', $NameField, 1,
null, 0,
'',
'',
'', 0, 0,
"", 0,
"",
"maxwidth200");
491 if (!empty($InfoFieldList[3])) {
492 $keyList = $InfoFieldList[3];
496 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
497 if ($InfoFieldList[1] ==
'c_stcomm') {
498 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
500 if ($InfoFieldList[1] ==
'c_country') {
501 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
503 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
504 if (!empty($InfoFieldList[4])) {
505 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
508 $resql = $this->db->query($sql);
510 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
511 $szFilterField .=
'<option value="0"> </option>';
512 $num = $this->db->num_rows($resql);
517 $obj = $this->db->fetch_object($resql);
518 if ($obj->label ==
'-') {
524 $labeltoshow =
dol_trunc($obj->label, 18);
525 if ($InfoFieldList[1] ==
'c_stcomm') {
526 $langs->load(
"companies");
527 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
529 if ($InfoFieldList[1] ==
'c_country') {
531 $langs->load(
"dict");
532 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
534 if (!empty($ValueField) && $ValueField == $obj->rowid) {
535 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
537 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
542 $szFilterField .=
"</select>";
544 $this->db->free($resql);
551 return $szFilterField;
602 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'', $separator =
'')
605 global $conf, $langs, $mysoc;
608 asort($array_selected);
610 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
613 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
614 $this->error =
"ErrorBadParameter";
620 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
621 $file =
"export_".$model.
".modules.php";
622 $classname =
"Export".$model;
623 require_once $dir.$file;
624 $objmodel =
new $classname($this->db);
626 if (in_array($model, array(
'csvutf8',
'csviso')) && !empty($separator)) {
627 $objmodel->separator = $separator;
630 if (!empty($sqlquery)) {
635 foreach ($this->array_export_code as $key => $dataset) {
636 if ($datatoexport == $dataset) {
643 if (empty($foundindice)) {
644 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
647 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
651 $this->sqlusedforexport = $sql;
653 $resql = $this->db->query($sql);
659 $filename =
"export_".$datatoexport;
664 $filename .=
'.'.$objmodel->getDriverExtension();
665 $dirname = $conf->export->dir_temp.
'/'.$user->id;
667 $outputlangs = clone $langs;
671 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
675 $objmodel->write_header($outputlangs);
678 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
680 while ($obj = $this->db->fetch_object($resql)) {
682 if (!empty($this->array_export_special[$indice])) {
683 foreach ($this->array_export_special[$indice] as $key => $value) {
684 if (!array_key_exists($key, $array_selected)) {
688 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
690 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
691 if ($obj->$alias < 0) {
694 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
697 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
698 if ($obj->$alias < 0) {
701 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
703 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
705 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
707 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
710 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
712 if ($obj->f_rowid > 0) {
713 global $tmpobjforcomputecall;
714 if (!is_object($tmpobjforcomputecall)) {
715 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
716 $tmpobjforcomputecall =
new Facture($this->db);
718 $tmpobjforcomputecall->id = $obj->f_rowid;
719 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
720 $tmpobjforcomputecall->close_code = $obj->f_close_code;
721 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
723 $obj->$alias = $remaintopay;
724 } elseif (is_array($this->array_export_special[$indice][$key]) &&
725 !empty($this->array_export_special[$indice][$key][
'rule']) &&
726 $this->array_export_special[$indice][$key][
'rule'] ==
'compute'
729 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
731 if (!empty($this->array_export_special[$indice][$key][
'class']) &&
732 !empty($this->array_export_special[$indice][$key][
'classfile']) &&
733 !empty($this->array_export_special[$indice][$key][
'method'])
735 if (!
dol_include_once($this->array_export_special[$indice][$key][
'classfile'])) {
736 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['classfile']} not found";
740 if (!class_exists($this->array_export_special[$indice][$key][
'class'])) {
741 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['class']} class doesn't exist";
745 $className = $this->array_export_special[$indice][$key][
'class'];
746 $tmpObject =
new $className($this->db);
747 if (!method_exists($tmpObject, $this->array_export_special[$indice][$key][
'method'])) {
748 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['method']} method doesn't exist";
752 $methodName = $this->array_export_special[$indice][$key][
'method'];
754 if (!empty($this->array_export_special[$indice][$key][
'method_params'])) {
755 foreach ($this->array_export_special[$indice][$key][
'method_params'] as $paramName) {
756 $params[] = $obj->$paramName ??
null;
759 $value = $tmpObject->$methodName(...$params);
761 $obj->$alias = $value;
766 $computestring = $this->array_export_special[$indice][$key];
770 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
776 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
780 $objmodel->write_footer($outputlangs);
783 $objmodel->close_file();
787 $this->error = $objmodel->error;
788 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
792 $this->error = $this->db->error().
" - sql=".$sql;