40 public $array_export_code = array();
41 public $array_export_code_for_sort = array();
42 public $array_export_module = array();
43 public $array_export_label = array();
44 public $array_export_sql_start = array();
45 public $array_export_sql_end = array();
46 public $array_export_sql_order = array();
48 public $array_export_fields = array();
49 public $array_export_TypeFields = array();
50 public $array_export_FilterValue = array();
51 public $array_export_entities = array();
52 public $array_export_dependencies = array();
53 public $array_export_special = array();
54 public $array_export_examplevalues = array();
55 public $array_export_help = array();
59 public $hexafiltervalue;
64 public $sqlusedforexport;
89 global $langs, $conf, $mysoc;
91 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
96 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
100 foreach ($modulesdir as $dir) {
103 if (is_resource($handle)) {
105 while (($file = readdir($handle)) !==
false) {
107 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg)) {
108 $modulename = $reg[1];
112 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
113 if ($part ==
'propale') {
116 if (empty($conf->$part->enabled)) {
122 $file = $dir.$modulename.
".class.php";
123 $classname = $modulename;
125 $module =
new $classname($this->
db);
127 if (isset($module->export_code) && is_array($module->export_code)) {
128 foreach ($module->export_code as $r => $value) {
130 if ($filter && ($filter != $module->export_code[$r])) {
135 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r])) {
141 if (isset($module->export_permission)) {
142 foreach ($module->export_permission[$r] as $val) {
145 if (!empty($perm[2])) {
146 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
148 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
150 if ($perm[0] ==
'user' && $user->admin) {
164 $langtoload = $module->getLangFilesArray();
165 if (is_array($langtoload)) {
166 foreach ($langtoload as $key) {
173 $this->array_export_module[$i] = $module;
175 $this->array_export_perms[$i] = $bool;
177 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
179 $this->array_export_code[$i] = $module->export_code[$r];
181 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
183 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
185 $this->array_export_fields[$i] = $module->export_fields_array[$r];
187 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
189 $this->array_export_entities[$i] = $module->export_entities_array[$r];
191 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
193 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
195 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] :
null);
197 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
200 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
201 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
202 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] :
null);
205 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]) :
''));
231 public function build_sql($indice, $array_selected, $array_filterValue)
235 $sql = $this->array_export_sql_start[$indice];
239 foreach ($this->array_export_fields[$indice] as $key => $value) {
240 if (!array_key_exists($key, $array_selected)) {
243 if (preg_match(
'/^none\./', $key)) {
252 if (strpos($key,
' as ') ===
false) {
253 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
260 $sql .= $this->array_export_sql_end[$indice];
263 if (is_array($array_filterValue) && !empty($array_filterValue)) {
266 foreach ($array_filterValue as $key => $value) {
267 if (preg_match(
'/GROUP_CONCAT/i', $key)) {
271 $sqlWhere .=
" AND ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
278 $sql .= $this->array_export_sql_order[$indice];
281 if (is_array($array_filterValue) && !empty($array_filterValue)) {
283 foreach ($array_filterValue as $key => $value) {
284 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') {
285 $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
309 $InfoFieldList = explode(
":", $TypeField);
311 switch ($InfoFieldList[0]) {
313 if (!(strpos($ValueField,
'%') ===
false)) {
314 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->
db->escape($ValueField).
"'";
316 $szFilterQuery =
" ".$NameField.
" = '".$this->
db->escape($ValueField).
"'";
320 if (strpos($ValueField,
"+") > 0) {
322 $ValueArray = explode(
"+", $ValueField);
323 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
324 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
326 if (is_numeric(substr($ValueField, 0, 1))) {
327 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
329 $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
337 if (strpos($ValueField,
"+") > 0) {
339 $ValueArray = explode(
"+", $ValueField);
340 $szFilterQuery =
"(".$NameField.
" >= ".((
float) $ValueArray[0]);
341 $szFilterQuery .=
" AND ".$NameField.
" <= ".((
float) $ValueArray[1]).
")";
343 if (is_numeric(substr($ValueField, 0, 1))) {
344 $szFilterQuery =
" ".$NameField.
" = ".((
float) $ValueField);
346 $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).((float) substr($ValueField, 1));
351 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
354 if (is_numeric($ValueField) && $ValueField > 0) {
355 $szFilterQuery =
" ".$NameField.
" = ".((
float) $ValueField);
357 $szFilterQuery =
" 1=1";
362 if (is_numeric($ValueField)) {
363 $szFilterQuery =
" ".$NameField.
" = ".((
float) $ValueField);
365 if (!(strpos($ValueField,
'%') ===
false)) {
366 $szFilterQuery =
" ".$NameField.
" LIKE '".$this->
db->escape($ValueField).
"'";
368 $szFilterQuery =
" ".$NameField.
" = '".$this->
db->escape($ValueField).
"'";
373 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);
376 return $szFilterQuery;
390 if (strlen($Value) == 4) {
391 $Condition =
" date_format(".$Field.
",'%Y') ".$Sens.
" '".$this->
db->escape($Value).
"'";
392 } elseif (strlen($Value) == 6) {
393 $Condition =
" date_format(".$Field.
",'%Y%m') ".$Sens.
" '".$this->
db->escape($Value).
"'";
395 $Condition =
" date_format(".$Field.
",'%Y%m%d') ".$Sens.
" '".$this->
db->escape($Value).
"'";
412 global $conf, $langs,
$form;
415 $InfoFieldList = explode(
":", $TypeField);
418 switch ($InfoFieldList[0]) {
421 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
427 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
430 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
433 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
434 $szFilterField .=
'<option ';
435 if ($ValueField ==
'') {
436 $szFilterField .=
' selected ';
438 $szFilterField .=
' value=""> </option>';
440 $szFilterField .=
'<option ';
441 if ($ValueField ==
'yes' || $ValueField ==
'1') {
442 $szFilterField .=
' selected ';
444 $szFilterField .=
' value="1">'.yn(1).
'</option>';
446 $szFilterField .=
'<option ';
447 if ($ValueField ==
'no' || $ValueField ==
'0') {
448 $szFilterField .=
' selected ';
450 $szFilterField .=
' value="0">'.yn(0).
'</option>';
451 $szFilterField .=
"</select>";
455 if ($InfoFieldList[1] ==
'select_company') {
456 $szFilterField .=
$form->select_company(
'', $NameField,
'', 1);
457 } elseif ($InfoFieldList[1] ==
'selectcontacts') {
458 $szFilterField .=
$form->selectcontacts(0,
'', $NameField,
' ');
459 } elseif ($InfoFieldList[1] ==
'select_dolusers') {
460 $szFilterField .=
$form->select_dolusers(
'', $NameField, 1);
470 if (!empty($InfoFieldList[3])) {
471 $keyList = $InfoFieldList[3];
475 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
" as code");
476 if ($InfoFieldList[1] ==
'c_stcomm') {
477 $sql =
"SELECT id as id, ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label".(empty($InfoFieldList[3]) ?
"" :
", ".$InfoFieldList[3].
' as code');
479 if ($InfoFieldList[1] ==
'c_country') {
480 $sql =
"SELECT ".$keyList.
" as rowid, ".$InfoFieldList[2].
" as label, code as code";
482 $sql .=
" FROM ".MAIN_DB_PREFIX.$InfoFieldList[1];
483 if (!empty($InfoFieldList[4])) {
484 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
489 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
490 $szFilterField .=
'<option value="0"> </option>';
496 $obj = $this->
db->fetch_object(
$resql);
497 if ($obj->label ==
'-') {
503 $labeltoshow =
dol_trunc($obj->label, 18);
504 if ($InfoFieldList[1] ==
'c_stcomm') {
505 $langs->load(
"companies");
506 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
508 if ($InfoFieldList[1] ==
'c_country') {
510 $langs->load(
"dict");
511 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
513 if (!empty($ValueField) && $ValueField == $obj->rowid) {
514 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
516 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
521 $szFilterField .=
"</select>";
530 return $szFilterField;
544 $InfoFieldList = explode(
":", $TypeField);
546 switch ($InfoFieldList[0]) {
548 $szMsg = $langs->trans(
'ExportStringFilter');
551 $szMsg = $langs->trans(
'ExportDateFilter');
556 $szMsg = $langs->trans(
'ExportNumericFilter');
580 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'')
583 global $conf, $langs, $mysoc;
586 asort($array_selected);
588 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
591 if (empty($this->array_export_fields) || !is_array($this->array_export_fields)) {
592 $this->error =
"ErrorBadParameter";
598 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
599 $file =
"export_".$model.
".modules.php";
600 $classname =
"Export".$model;
601 require_once $dir.$file;
602 $objmodel =
new $classname($this->
db);
604 if (!empty($sqlquery)) {
609 foreach ($this->array_export_code as $key => $dataset) {
610 if ($datatoexport == $dataset) {
617 if (empty($foundindice)) {
618 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
621 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
625 $this->sqlusedforexport = $sql;
630 if (!empty($conf->global->EXPORT_PREFIX_SPEC)) {
631 $filename = $conf->global->EXPORT_PREFIX_SPEC.
"_".$datatoexport;
633 $filename =
"export_".$datatoexport;
635 if (!empty($conf->global->EXPORT_NAME_WITH_DT)) {
638 $filename .=
'.'.$objmodel->getDriverExtension();
639 $dirname = $conf->export->dir_temp.
'/'.$user->id;
641 $outputlangs = clone $langs;
645 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
649 $objmodel->write_header($outputlangs);
652 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
654 while ($obj = $this->
db->fetch_object(
$resql)) {
656 if (!empty($this->array_export_special[$indice])) {
657 foreach ($this->array_export_special[$indice] as $key => $value) {
658 if (!array_key_exists($key, $array_selected)) {
662 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG') {
664 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
665 if ($obj->$alias < 0) {
668 } elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG') {
671 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
672 if ($obj->$alias < 0) {
675 } elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays') {
677 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
679 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
681 } elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay') {
684 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
686 if ($obj->f_rowid > 0) {
687 global $tmpobjforcomputecall;
688 if (!is_object($tmpobjforcomputecall)) {
689 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
690 $tmpobjforcomputecall =
new Facture($this->
db);
692 $tmpobjforcomputecall->id = $obj->f_rowid;
693 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
694 $tmpobjforcomputecall->close_code = $obj->f_close_code;
695 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
697 $obj->$alias = $remaintopay;
702 $computestring = $this->array_export_special[$indice][$key];
706 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
712 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] :
null);
716 $objmodel->write_footer($outputlangs);
719 $objmodel->close_file();
723 $this->error = $objmodel->error;
724 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
728 $this->error = $this->
db->error().
" - sql=".$sql;
747 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'export_model (';
753 $sql .=
') VALUES (';
754 $sql .=
"'".$this->db->escape($this->model_name).
"',";
755 $sql .=
" '".$this->db->escape($this->datatoexport).
"',";
756 $sql .=
" '".$this->db->escape($this->hexa).
"',";
757 $sql .=
' '.(isset($this->fk_user) ? (int) $this->fk_user :
'null').
",";
758 $sql .=
" '".$this->db->escape($this->hexafiltervalue).
"'";
766 $this->error = $this->
db->lasterror();
767 $this->errno = $this->
db->lasterrno();
768 $this->
db->rollback();
781 $sql =
'SELECT em.rowid, em.label, em.type, em.field, em.filter';
782 $sql .=
' FROM '.MAIN_DB_PREFIX.
'export_model as em';
783 $sql .=
' WHERE em.rowid = '.((int) $id);
786 $result = $this->
db->query($sql);
788 $obj = $this->
db->fetch_object($result);
790 $this->
id = $obj->rowid;
791 $this->model_name = $obj->label;
792 $this->datatoexport = $obj->type;
794 $this->hexa = $obj->field;
795 $this->hexafiltervalue = $obj->filter;
799 $this->error =
"ModelNotFound";
816 public function delete($user, $notrigger = 0)
818 global $conf, $langs;
821 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"export_model";
822 $sql .=
" WHERE rowid=".((int) $this->
id);
826 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
829 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
834 foreach ($this->errors as $errmsg) {
835 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
836 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
838 $this->
db->rollback();
856 global $conf, $langs;
858 $sql =
"SELECT em.rowid, em.field, em.label, em.type, em.filter";
859 $sql .=
" FROM ".MAIN_DB_PREFIX.
"export_model as em";
860 $sql .=
" ORDER BY rowid";
862 $result = $this->
db->query($sql);
864 $num = $this->
db->num_rows($result);
867 $obj = $this->
db->fetch_object($result);
868 $keyModel = array_search($obj->type, $this->array_export_code);
870 print
'<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.
'&datatoexport='.$obj->type.
'>'.$obj->label.
'</a></td>';
872 print
img_object($this->array_export_module[$keyModel]->getName(), $this->array_export_icon[$keyModel]).
' ';
873 print $this->array_export_module[$keyModel]->getName().
' - ';
876 $string = $langs->trans($this->array_export_label[$keyModel]);
877 print ($string != $this->array_export_label[$keyModel] ? $string : $this->array_export_label[$keyModel]);
880 print
'<td>'.str_replace(
',',
' , ', $obj->field).
'</td>';
881 if (!empty($obj->filter)) {
882 $filter = json_decode($obj->filter,
true);
883 print
'<td>'.str_replace(
',',
' , ', $filter[
'field']).
'</td>';
884 print
'<td>'.str_replace(
',',
' , ', $filter[
'value']).
'</td>';
887 print
'<td class="right">';
888 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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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)
div float
Buy price without taxes.
$conf db
API class for accounts.