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;
168 } elseif (!empty($perm[1])) {
169 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
173 if ($perm[0] ==
'user' && $user->admin) {
187 $langtoload = $module->getLangFilesArray();
188 if (is_array($langtoload)) {
189 foreach ($langtoload as $key) {
196 $this->array_export_module[$i] = $module;
198 $this->array_export_perms[$i] = $bool;
200 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
202 $this->array_export_code[$i] = $module->export_code[$r];
204 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
206 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
208 $this->array_export_fields[$i] = $module->export_fields_array[$r];
210 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
212 $this->array_export_entities[$i] = $module->export_entities_array[$r];
214 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
216 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
218 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
220 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
223 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
224 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
225 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
228 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]) :
''));
254 public function build_sql($indice, $array_selected, $array_filterValue)
258 $sql = $this->array_export_sql_start[$indice];
262 foreach ($this->array_export_fields[$indice] as $key => $value) {
263 if (!array_key_exists($key, $array_selected)) {
266 if (preg_match(
'/^none\./', $key)) {
275 if (strpos($key,
' as ') ===
false) {
276 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
283 $sql .= $this->array_export_sql_end[$indice];
286 if (is_array($array_filterValue) && !empty($array_filterValue)) {
289 foreach ($array_filterValue as $key => $value) {
290 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
294 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
301 $sql .= $this->array_export_sql_order[$indice];
304 if (is_array($array_filterValue) && !empty($array_filterValue)) {
306 foreach ($array_filterValue as $key => $value) {
307 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
308 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
332 $InfoFieldList = explode(
":", $TypeField);
334 switch ($InfoFieldList[0]) {
336 if (!(strpos($ValueField,
'%') ===
false)) {
337 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
339 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
343 if (strpos($ValueField,
"+") > 0) {
345 $ValueArray = explode(
"+", $ValueField);
346 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
347 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
349 if (is_numeric(substr($ValueField, 0, 1))) {
350 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
352 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
359 if (strpos($ValueField,
"+") > 0) {
361 $ValueArray = explode(
"+", $ValueField);
362 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
363 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
365 if (is_numeric(substr($ValueField, 0, 1))) {
366 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
368 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
373 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
376 if (is_numeric($ValueField) && $ValueField > 0) {
377 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
379 $szFilterQuery =
" 1=1";
384 if (is_numeric($ValueField)) {
385 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
387 if (!(strpos($ValueField,
'%') ===
false)) {
388 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
390 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
395 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);
398 return $szFilterQuery;
434 global $conf, $langs, $form;
437 $InfoFieldList = explode(
":", $TypeField);
440 switch ($InfoFieldList[0]) {
443 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
449 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
452 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
455 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
456 $szFilterField .=
'<option ';
457 if ($ValueField ==
'') {
458 $szFilterField .=
' selected ';
460 $szFilterField .=
' value=""> </option>';
462 $szFilterField .=
'<option ';
463 if ($ValueField ==
'yes' || $ValueField ==
'1') {
464 $szFilterField .=
' selected ';
466 $szFilterField .=
' value="1">'.yn(1).
'</option>';
468 $szFilterField .=
'<option ';
469 if ($ValueField ==
'no' || $ValueField ==
'0') {
470 $szFilterField .=
' selected ';
472 $szFilterField .=
' value="0">'.yn(0).
'</option>';
473 $szFilterField .=
"</select>";
477 if ($InfoFieldList[1] ==
'select_company') {
478 $szFilterField .= $form->select_company(
'', $NameField,
'', 1, 0, 0, [], 0,
'maxwidth200');
479 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
481 $szFilterField .= $form->select_contact(0,
'', $NameField,
' ',
'',
'', 0,
'minwidth100imp maxwidth200',
true);
482 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
483 $szFilterField .= $form->select_dolusers(
'', $NameField, 1,
null, 0,
'',
'',
'', 0, 0,
"", 0,
"",
"maxwidth200");
493 if (!empty($InfoFieldList[3])) {
494 $keyList = $InfoFieldList[3];
498 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
499 if ($InfoFieldList[1] ==
'c_stcomm') {
500 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
502 if ($InfoFieldList[1] ==
'c_country') {
503 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
505 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
506 if (!empty($InfoFieldList[4])) {
507 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
510 $resql = $this->db->query($sql);
512 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
513 $szFilterField .=
'<option value="0"> </option>';
514 $num = $this->db->num_rows($resql);
519 $obj = $this->db->fetch_object($resql);
520 if ($obj->label ==
'-') {
526 $labeltoshow =
dol_trunc($obj->label, 18);
527 if ($InfoFieldList[1] ==
'c_stcomm') {
528 $langs->load(
"companies");
529 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
531 if ($InfoFieldList[1] ==
'c_country') {
533 $langs->load(
"dict");
534 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
536 if (!empty($ValueField) && $ValueField == $obj->rowid) {
537 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
539 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
544 $szFilterField .=
"</select>";
546 $this->db->free($resql);
553 return $szFilterField;
603 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'', $separator =
'')
606 global $conf, $langs, $mysoc;
609 asort($array_selected);
611 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
614 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
615 $this->error =
"ErrorBadParameter";
621 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
622 $file =
"export_".$model.
".modules.php";
623 $classname =
"Export".$model;
624 require_once $dir.$file;
625 $objmodel =
new $classname($this->db);
627 if (in_array($model, array(
'csvutf8',
'csviso')) && !empty($separator)) {
628 $objmodel->separator = $separator;
631 if (!empty($sqlquery)) {
636 foreach ($this->array_export_code as $key => $dataset) {
637 if ($datatoexport == $dataset) {
644 if (empty($foundindice)) {
645 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
648 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
652 $this->sqlusedforexport = $sql;
654 $resql = $this->db->query($sql);
660 $filename =
"export_".$datatoexport;
665 $filename .=
'.'.$objmodel->getDriverExtension();
666 $dirname = $conf->export->dir_temp.
'/'.$user->id;
668 $outputlangs = clone $langs;
672 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
676 $objmodel->write_header($outputlangs);
679 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
681 while ($obj = $this->db->fetch_object($resql)) {
683 if (!empty($this->array_export_special[$indice])) {
684 foreach ($this->array_export_special[$indice] as $key => $value) {
685 if (!array_key_exists($key, $array_selected)) {
689 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
691 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
692 if ($obj->$alias < 0) {
695 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
698 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
699 if ($obj->$alias < 0) {
702 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
704 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
706 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
708 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
711 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
713 if ($obj->f_rowid > 0) {
714 global $tmpobjforcomputecall;
715 if (!is_object($tmpobjforcomputecall)) {
716 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
717 $tmpobjforcomputecall =
new Facture($this->db);
719 $tmpobjforcomputecall->id = $obj->f_rowid;
720 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
721 $tmpobjforcomputecall->close_code = $obj->f_close_code;
722 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
724 $obj->$alias = $remaintopay;
725 } elseif (is_array($this->array_export_special[$indice][$key]) &&
726 !empty($this->array_export_special[$indice][$key][
'rule']) &&
727 $this->array_export_special[$indice][$key][
'rule'] ==
'compute'
730 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
732 if (!empty($this->array_export_special[$indice][$key][
'class']) &&
733 !empty($this->array_export_special[$indice][$key][
'classfile']) &&
734 !empty($this->array_export_special[$indice][$key][
'method'])
736 if (!
dol_include_once($this->array_export_special[$indice][$key][
'classfile'])) {
737 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['classfile']} not found";
741 if (!class_exists($this->array_export_special[$indice][$key][
'class'])) {
742 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['class']} class doesn't exist";
746 $className = $this->array_export_special[$indice][$key][
'class'];
747 $tmpObject =
new $className($this->db);
748 if (!method_exists($tmpObject, $this->array_export_special[$indice][$key][
'method'])) {
749 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['method']} method doesn't exist";
753 $methodName = $this->array_export_special[$indice][$key][
'method'];
755 if (!empty($this->array_export_special[$indice][$key][
'method_params'])) {
756 foreach ($this->array_export_special[$indice][$key][
'method_params'] as $paramName) {
757 $params[] = $obj->$paramName ??
null;
760 $value = $tmpObject->$methodName(...$params);
762 $obj->$alias = $value;
767 $computestring = $this->array_export_special[$indice][$key];
771 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
777 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
781 $objmodel->write_footer($outputlangs);
784 $objmodel->close_file();
788 $this->error = $objmodel->error;
789 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
793 $this->error = $this->db->error().
" - sql=".$sql;