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));
360 if (strpos($ValueField,
"+") > 0) {
362 $ValueArray = explode(
"+", $ValueField);
363 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
364 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
366 if (is_numeric(substr($ValueField, 0, 1))) {
367 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
369 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
374 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
377 if (is_numeric($ValueField) && $ValueField > 0) {
378 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
380 $szFilterQuery =
" 1=1";
385 if (is_numeric($ValueField)) {
386 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
388 if (!(strpos($ValueField,
'%') ===
false)) {
389 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
391 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
396 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);
399 return $szFilterQuery;
435 global $conf, $langs, $form;
438 $InfoFieldList = explode(
":", $TypeField);
441 switch ($InfoFieldList[0]) {
444 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
450 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
453 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
456 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
457 $szFilterField .=
'<option ';
458 if ($ValueField ==
'') {
459 $szFilterField .=
' selected ';
461 $szFilterField .=
' value=""> </option>';
463 $szFilterField .=
'<option ';
464 if ($ValueField ==
'yes' || $ValueField ==
'1') {
465 $szFilterField .=
' selected ';
467 $szFilterField .=
' value="1">'.yn(1).
'</option>';
469 $szFilterField .=
'<option ';
470 if ($ValueField ==
'no' || $ValueField ==
'0') {
471 $szFilterField .=
' selected ';
473 $szFilterField .=
' value="0">'.yn(0).
'</option>';
474 $szFilterField .=
"</select>";
478 if ($InfoFieldList[1] ==
'select_company') {
479 $szFilterField .= $form->select_company(
'', $NameField,
'', 1, 0, 0, [], 0,
'maxwidth200');
480 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
482 $szFilterField .= $form->select_contact(0,
'', $NameField,
' ',
'',
'', 0,
'minwidth100imp maxwidth200',
true);
483 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
484 $szFilterField .= $form->select_dolusers(
'', $NameField, 1,
null, 0,
'',
'',
'', 0, 0,
"", 0,
"",
"maxwidth200");
494 if (!empty($InfoFieldList[3])) {
495 $keyList = $InfoFieldList[3];
499 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
500 if ($InfoFieldList[1] ==
'c_stcomm') {
501 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
503 if ($InfoFieldList[1] ==
'c_country') {
504 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
506 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
507 if (!empty($InfoFieldList[4])) {
508 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
511 $resql = $this->db->query($sql);
513 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
514 $szFilterField .=
'<option value="0"> </option>';
515 $num = $this->db->num_rows($resql);
520 $obj = $this->db->fetch_object($resql);
521 if ($obj->label ==
'-') {
527 $labeltoshow =
dol_trunc($obj->label, 18);
528 if ($InfoFieldList[1] ==
'c_stcomm') {
529 $langs->load(
"companies");
530 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
532 if ($InfoFieldList[1] ==
'c_country') {
534 $langs->load(
"dict");
535 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
537 if (!empty($ValueField) && $ValueField == $obj->rowid) {
538 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
540 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
545 $szFilterField .=
"</select>";
547 $this->db->free($resql);
554 return $szFilterField;
605 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'', $separator =
'')
608 global $conf, $langs, $mysoc;
611 asort($array_selected);
613 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
616 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
617 $this->error =
"ErrorBadParameter";
623 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
624 $file =
"export_".$model.
".modules.php";
625 $classname =
"Export".$model;
626 require_once $dir.$file;
627 $objmodel =
new $classname($this->db);
629 if (in_array($model, array(
'csvutf8',
'csviso')) && !empty($separator)) {
630 $objmodel->separator = $separator;
633 if (!empty($sqlquery)) {
638 foreach ($this->array_export_code as $key => $dataset) {
639 if ($datatoexport == $dataset) {
646 if (empty($foundindice)) {
647 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
650 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
654 $this->sqlusedforexport = $sql;
656 $resql = $this->db->query($sql);
662 $filename =
"export_".$datatoexport;
667 $filename .=
'.'.$objmodel->getDriverExtension();
668 $dirname = $conf->export->dir_temp.
'/'.$user->id;
670 $outputlangs = clone $langs;
674 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
678 $objmodel->write_header($outputlangs);
681 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
683 while ($obj = $this->db->fetch_object($resql)) {
685 if (!empty($this->array_export_special[$indice])) {
686 foreach ($this->array_export_special[$indice] as $key => $value) {
687 if (!array_key_exists($key, $array_selected)) {
691 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
693 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
694 if ($obj->$alias < 0) {
697 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
700 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
701 if ($obj->$alias < 0) {
704 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
706 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
708 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
710 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
713 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
715 if ($obj->f_rowid > 0) {
716 global $tmpobjforcomputecall;
717 if (!is_object($tmpobjforcomputecall)) {
718 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
719 $tmpobjforcomputecall =
new Facture($this->db);
721 $tmpobjforcomputecall->id = $obj->f_rowid;
722 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
723 $tmpobjforcomputecall->close_code = $obj->f_close_code;
724 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
726 $obj->$alias = $remaintopay;
727 } elseif (is_array($this->array_export_special[$indice][$key]) &&
728 !empty($this->array_export_special[$indice][$key][
'rule']) &&
729 $this->array_export_special[$indice][$key][
'rule'] ==
'compute'
732 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
734 if (!empty($this->array_export_special[$indice][$key][
'class']) &&
735 !empty($this->array_export_special[$indice][$key][
'classfile']) &&
736 !empty($this->array_export_special[$indice][$key][
'method'])
738 if (!
dol_include_once($this->array_export_special[$indice][$key][
'classfile'])) {
739 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['classfile']} not found";
743 if (!class_exists($this->array_export_special[$indice][$key][
'class'])) {
744 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['class']} class doesn't exist";
748 $className = $this->array_export_special[$indice][$key][
'class'];
749 $tmpObject =
new $className($this->db);
750 if (!method_exists($tmpObject, $this->array_export_special[$indice][$key][
'method'])) {
751 $this->error =
"Computed field bad configuration: {$this->array_export_special[$indice][$key]['method']} method doesn't exist";
755 $methodName = $this->array_export_special[$indice][$key][
'method'];
757 if (!empty($this->array_export_special[$indice][$key][
'method_params'])) {
758 foreach ($this->array_export_special[$indice][$key][
'method_params'] as $paramName) {
759 $params[] = $obj->$paramName ??
null;
762 $value = $tmpObject->$methodName(...$params);
764 $obj->$alias = $value;
769 $computestring = $this->array_export_special[$indice][$key];
773 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
779 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
783 $objmodel->write_footer($outputlangs);
786 $objmodel->close_file();
790 $this->error = $objmodel->error;
791 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
795 $this->error = $this->db->error().
" - sql=".$sql;