51 public $array_export_code = array();
52 public $array_export_code_for_sort = array();
53 public $array_export_module = array();
54 public $array_export_label = array();
55 public $array_export_sql_start = array();
56 public $array_export_sql_end = array();
57 public $array_export_sql_order = array();
59 public $array_export_fields = array();
60 public $array_export_TypeFields = array();
61 public $array_export_FilterValue = array();
62 public $array_export_entities = array();
63 public $array_export_dependencies = array();
64 public $array_export_special = array();
65 public $array_export_examplevalues = array();
66 public $array_export_help = array();
70 public $hexafiltervalue;
75 public $sqlusedforexport;
100 global $langs, $conf, $mysoc;
102 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
107 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
111 foreach ($modulesdir as $dir) {
114 if (is_resource($handle)) {
116 while (($file = readdir($handle)) !==
false) {
118 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg)) {
119 $modulename = $reg[1];
123 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
124 if ($part ==
'propale') {
127 if (empty($conf->$part->enabled)) {
133 $file = $dir.$modulename.
".class.php";
134 $classname = $modulename;
136 $module =
new $classname($this->db);
138 if (isset($module->export_code) && is_array($module->export_code)) {
139 foreach ($module->export_code as $r => $value) {
141 if ($filter && ($filter != $module->export_code[$r])) {
146 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r])) {
152 if (isset($module->export_permission)) {
153 foreach ($module->export_permission[$r] as $val) {
156 if (!empty($perm[2])) {
157 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
159 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
161 if ($perm[0] ==
'user' && $user->admin) {
175 $langtoload = $module->getLangFilesArray();
176 if (is_array($langtoload)) {
177 foreach ($langtoload as $key) {
184 $this->array_export_module[$i] = $module;
186 $this->array_export_perms[$i] = $bool;
188 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
190 $this->array_export_code[$i] = $module->export_code[$r];
192 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
194 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
196 $this->array_export_fields[$i] = $module->export_fields_array[$r];
198 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
200 $this->array_export_entities[$i] = $module->export_entities_array[$r];
202 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
204 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
206 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
208 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
211 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
212 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
213 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
216 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]) :
''));
242 public function build_sql($indice, $array_selected, $array_filterValue)
246 $sql = $this->array_export_sql_start[$indice];
250 foreach ($this->array_export_fields[$indice] as $key => $value) {
251 if (!array_key_exists($key, $array_selected)) {
254 if (preg_match(
'/^none\./', $key)) {
263 if (strpos($key,
' as ') ===
false) {
264 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
271 $sql .= $this->array_export_sql_end[$indice];
274 if (is_array($array_filterValue) && !empty($array_filterValue)) {
277 foreach ($array_filterValue as $key => $value) {
278 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
282 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
289 $sql .= $this->array_export_sql_order[$indice];
292 if (is_array($array_filterValue) && !empty($array_filterValue)) {
294 foreach ($array_filterValue as $key => $value) {
295 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
296 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
320 $InfoFieldList = explode(
":", $TypeField);
322 switch ($InfoFieldList[0]) {
324 if (!(strpos($ValueField,
'%') ===
false)) {
325 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
327 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
331 if (strpos($ValueField,
"+") > 0) {
333 $ValueArray = explode(
"+", $ValueField);
334 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
335 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
337 if (is_numeric(substr($ValueField, 0, 1))) {
338 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
340 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
348 if (strpos($ValueField,
"+") > 0) {
350 $ValueArray = explode(
"+", $ValueField);
351 $szFilterQuery =
"(".$NameField.
" >= ".((float) $ValueArray[0]);
352 $szFilterQuery .=
" AND ".$NameField.
" <= ".((float) $ValueArray[1]).
")";
354 if (is_numeric(substr($ValueField, 0, 1))) {
355 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
357 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
362 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
365 if (is_numeric($ValueField) && $ValueField > 0) {
366 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
368 $szFilterQuery =
" 1=1";
373 if (is_numeric($ValueField)) {
374 $szFilterQuery =
" ".$NameField.
" = ".((float) $ValueField);
376 if (!(strpos($ValueField,
'%') ===
false)) {
377 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->db->escape($ValueField).
"'";
379 $szFilterQuery =
" ".$NameField.
" = '".$this->db->escape($ValueField).
"'";
384 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);
387 return $szFilterQuery;
401 if (strlen($Value) == 4) {
402 $Condition =
" date_format(".$Field.
",'%Y') ".$Sens.
" '".$this->db->escape($Value).
"'";
403 } elseif (strlen($Value) == 6) {
404 $Condition =
" date_format(".$Field.
",'%Y%m') ".$Sens.
" '".$this->db->escape($Value).
"'";
406 $Condition =
" date_format(".$Field.
",'%Y%m%d') ".$Sens.
" '".$this->db->escape($Value).
"'";
423 global $conf, $langs, $form;
426 $InfoFieldList = explode(
":", $TypeField);
429 switch ($InfoFieldList[0]) {
432 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
438 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
441 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
444 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
445 $szFilterField .=
'<option ';
446 if ($ValueField ==
'') {
447 $szFilterField .=
' selected ';
449 $szFilterField .=
' value=""> </option>';
451 $szFilterField .=
'<option ';
452 if ($ValueField ==
'yes' || $ValueField ==
'1') {
453 $szFilterField .=
' selected ';
455 $szFilterField .=
' value="1">'.yn(1).
'</option>';
457 $szFilterField .=
'<option ';
458 if ($ValueField ==
'no' || $ValueField ==
'0') {
459 $szFilterField .=
' selected ';
461 $szFilterField .=
' value="0">'.yn(0).
'</option>';
462 $szFilterField .=
"</select>";
466 if ($InfoFieldList[1] ==
'select_company') {
467 $szFilterField .= $form->select_company(
'', $NameField,
'', 1);
468 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
469 $szFilterField .= $form->selectcontacts(0,
'', $NameField,
' ');
470 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
471 $szFilterField .= $form->select_dolusers(
'', $NameField, 1);
481 if (!empty($InfoFieldList[3])) {
482 $keyList = $InfoFieldList[3];
486 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
487 if ($InfoFieldList[1] ==
'c_stcomm') {
488 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
490 if ($InfoFieldList[1] ==
'c_country') {
491 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
493 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
494 if (!empty($InfoFieldList[4])) {
495 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
498 $resql = $this->db->query($sql);
500 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
501 $szFilterField .=
'<option value="0"> </option>';
502 $num = $this->db->num_rows($resql);
507 $obj = $this->db->fetch_object($resql);
508 if ($obj->label ==
'-') {
514 $labeltoshow =
dol_trunc($obj->label, 18);
515 if ($InfoFieldList[1] ==
'c_stcomm') {
516 $langs->load(
"companies");
517 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
519 if ($InfoFieldList[1] ==
'c_country') {
521 $langs->load(
"dict");
522 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
524 if (!empty($ValueField) && $ValueField == $obj->rowid) {
525 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
527 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
532 $szFilterField .=
"</select>";
534 $this->db->free($resql);
541 return $szFilterField;
555 $InfoFieldList = explode(
":", $TypeField);
557 switch ($InfoFieldList[0]) {
559 $szMsg = $langs->trans(
'ExportStringFilter');
562 $szMsg = $langs->trans(
'ExportDateFilter');
567 $szMsg = $langs->trans(
'ExportNumericFilter');
591 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'')
594 global $conf, $langs, $mysoc;
597 asort($array_selected);
599 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
602 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
603 $this->error =
"ErrorBadParameter";
609 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
610 $file =
"export_".$model.
".modules.php";
611 $classname =
"Export".$model;
612 require_once $dir.$file;
613 $objmodel =
new $classname($this->db);
615 if (!empty($sqlquery)) {
620 foreach ($this->array_export_code as $key => $dataset) {
621 if ($datatoexport == $dataset) {
628 if (empty($foundindice)) {
629 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
632 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
636 $this->sqlusedforexport = $sql;
638 $resql = $this->db->query($sql);
641 if (!empty($conf->global->EXPORT_PREFIX_SPEC)) {
642 $filename = $conf->global->EXPORT_PREFIX_SPEC.
"_".$datatoexport;
644 $filename =
"export_".$datatoexport;
646 if (!empty($conf->global->EXPORT_NAME_WITH_DT)) {
649 $filename .=
'.'.$objmodel->getDriverExtension();
650 $dirname = $conf->export->dir_temp.
'/'.$user->id;
652 $outputlangs = clone $langs;
656 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
660 $objmodel->write_header($outputlangs);
663 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
665 while ($obj = $this->db->fetch_object($resql)) {
667 if (!empty($this->array_export_special[$indice])) {
668 foreach ($this->array_export_special[$indice] as $key => $value) {
669 if (!array_key_exists($key, $array_selected)) {
673 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
675 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
676 if ($obj->$alias < 0) {
679 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
682 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
683 if ($obj->$alias < 0) {
686 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
688 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
690 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
692 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
695 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
697 if ($obj->f_rowid > 0) {
698 global $tmpobjforcomputecall;
699 if (!is_object($tmpobjforcomputecall)) {
700 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
701 $tmpobjforcomputecall =
new Facture($this->db);
703 $tmpobjforcomputecall->id = $obj->f_rowid;
704 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
705 $tmpobjforcomputecall->close_code = $obj->f_close_code;
706 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
708 $obj->$alias = $remaintopay;
713 $computestring = $this->array_export_special[$indice][$key];
717 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
723 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
727 $objmodel->write_footer($outputlangs);
730 $objmodel->close_file();
734 $this->error = $objmodel->error;
735 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
739 $this->error = $this->db->error().
" - sql=".$sql;
758 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'export_model (';
764 $sql .=
') VALUES (';
765 $sql .=
"'".$this->db->escape($this->model_name).
"',";
766 $sql .=
" '".$this->db->escape($this->datatoexport).
"',";
767 $sql .=
" '".$this->db->escape($this->hexa).
"',";
768 $sql .=
' '.(isset($this->fk_user) ? (int) $this->fk_user :
'null').
",";
769 $sql .=
" '".$this->db->escape($this->hexafiltervalue).
"'";
772 $resql = $this->db->query($sql);
777 $this->error = $this->db->lasterror();
778 $this->errno = $this->db->lasterrno();
779 $this->db->rollback();
792 $sql =
'SELECT em.rowid, em.label, em.type, em.field, em.filter';
793 $sql .=
' FROM '.MAIN_DB_PREFIX.
'export_model as em';
794 $sql .=
' WHERE em.rowid = '.((int) $id);
797 $result = $this->db->query($sql);
799 $obj = $this->db->fetch_object($result);
801 $this->
id = $obj->rowid;
802 $this->model_name = $obj->label;
803 $this->datatoexport = $obj->type;
805 $this->hexa = $obj->field;
806 $this->hexafiltervalue = $obj->filter;
810 $this->error =
"ModelNotFound";
827 public function delete($user, $notrigger = 0)
829 global $conf, $langs;
832 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"export_model";
833 $sql .=
" WHERE rowid=".((int) $this->
id);
837 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
838 $resql = $this->db->query($sql);
840 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
845 foreach ($this->errors as $errmsg) {
846 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
847 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
849 $this->db->rollback();
867 global $conf, $langs;
869 $sql =
"SELECT em.rowid, em.field, em.label, em.type, em.filter";
870 $sql .=
" FROM ".MAIN_DB_PREFIX.
"export_model as em";
871 $sql .=
" ORDER BY rowid";
873 $result = $this->db->query($sql);
875 $num = $this->db->num_rows($result);
878 $obj = $this->db->fetch_object($result);
879 $keyModel = array_search($obj->type, $this->array_export_code);
881 print
'<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.
'&datatoexport='.$obj->type.
'>'.$obj->label.
'</a></td>';
883 print
img_object($this->array_export_module[$keyModel]->getName(), $this->array_export_icon[$keyModel]).
' ';
884 print $this->array_export_module[$keyModel]->getName().
' - ';
887 $string = $langs->trans($this->array_export_label[$keyModel]);
888 print ($string != $this->array_export_label[$keyModel] ? $string : $this->array_export_label[$keyModel]);
891 print
'<td>'.str_replace(
',',
' , ', $obj->field).
'</td>';
892 if (!empty($obj->filter)) {
893 $filter = json_decode($obj->filter,
true);
894 print
'<td>'.str_replace(
',',
' , ', $filter[
'field']).
'</td>';
895 print
'<td>'.str_replace(
',',
' , ', $filter[
'value']).
'</td>';
898 print
'<td class="right">';
899 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=deleteprof&token='.newToken().
'&id='.$obj->rowid.
'">';
fetch($id)
Load an export profil from database.
build_sql($indice, $array_selected, $array_filterValue)
Build the sql export request.
build_filterField($TypeField, $NameField, $ValueField)
Build an input field used to filter the query.
build_filterQuery($TypeField, $NameField, $ValueField)
Build the conditionnal string from filter the query.
build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery='')
Build export file.
conditionDate($Field, $Value, $Sens)
conditionDate
list_export_model()
Output list all export models –TODO Move this into a class htmlxxx.class.php–.
create($user)
Save an export model in database.
__construct($db)
Constructor.
load_arrays($user, $filter='')
Load an exportable dataset.
genDocFilter($TypeField)
Build an input field used to filter the query.
Class to manage invoices.
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code='')
Function to return number of working days (and text of units) between two dates (working days)
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
dolGetModulesDirs($subdir='')
Return list of modules directories.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
verifCond($strToEvaluate)
Verify if condition in string is ok or not.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)