From 348a0d7779b56c33d5eb01f91b2870189b28cb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 16 Oct 2024 10:37:41 +0200 Subject: [PATCH 01/29] can filter on object properties --- htdocs/core/class/commonobject.class.php | 2 +- htdocs/core/class/extrafields.class.php | 15 ++++++++++++++- htdocs/core/tpl/extrafields_view.tpl.php | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 3aee2589b3246..aeb99aad12572 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9339,7 +9339,7 @@ public function showOptionals($extrafields, $mode = 'view', $params = null, $key if (in_array($extrafields->attributes[$this->table_element]['type'][$key], $listoftypestoshowpicto)) { $out .= getPictoForType($extrafields->attributes[$this->table_element]['type'][$key], ($extrafields->attributes[$this->table_element]['type'][$key] == 'text' ? 'tdtop' : '')); } - $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this->id, $this->table_element); + $out .= $extrafields->showInputField($key, $value, '', $keysuffix, '', 0, $this->id, $this->table_element, 0, $this); break; } diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index c1a76a53a9e4c..26b6586cec22f 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1074,9 +1074,10 @@ public function fetch_name_optionals_label($elementtype, $forceload = false, $at * @param int $objectid Current object id * @param string $extrafieldsobjectkey The key to use to store retrieved data (commonly $object->table_element) * @param int $mode 1=Used for search filters + * @param ?CommonObject $object Common object dolibarr * @return string */ - public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '', $mode = 0) + public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '', $mode = 0, $object = null) { global $conf, $langs, $form; @@ -1433,6 +1434,18 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ // Add filter from 4th field if (!empty($InfoFieldList[4])) { + if (is_object($object)) { + $tags = []; + preg_match_all('/\$(.*?)\$/', $InfoFieldList[4], $tags); + foreach ($tags[0] as $keytag => $valuetag) { + $property = strtolower($tags[1][$keytag]); + if (strpos($InfoFieldList[4], $valuetag) !== false && property_exists($object, $property) && !empty($object->$property)) { + $InfoFieldList[4] = str_replace($valuetag, (string) $object->$property, $InfoFieldList[4]); + } else { + $InfoFieldList[4] = str_replace($valuetag, '0', $InfoFieldList[4]); + } + } + } // can use current entity filter if (strpos($InfoFieldList[4], '$ENTITY$') !== false) { $InfoFieldList[4] = str_replace('$ENTITY$', (string) $conf->entity, $InfoFieldList[4]); diff --git a/htdocs/core/tpl/extrafields_view.tpl.php b/htdocs/core/tpl/extrafields_view.tpl.php index e93596d2726ad..78045ca09f2e1 100644 --- a/htdocs/core/tpl/extrafields_view.tpl.php +++ b/htdocs/core/tpl/extrafields_view.tpl.php @@ -249,7 +249,7 @@ print ''; print ''; print ''; - print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', 0, $object->id, $object->table_element); + print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', 0, $object->id, $object->table_element, 0, $object); print ''; From 9b44b5e3a07464f097fc5ffc09519855797348ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 16 Oct 2024 10:51:52 +0200 Subject: [PATCH 02/29] can filter on object properties --- htdocs/core/class/extrafields.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 26b6586cec22f..7d23ad247b407 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1454,6 +1454,10 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ if (strpos($InfoFieldList[4], '$SEL$') !== false) { $InfoFieldList[4] = str_replace('$SEL$', 'SELECT', $InfoFieldList[4]); } + // can use MODE request (list or view) + if (strpos($InfoFieldList[4], '$MODE$') !== false) { + $InfoFieldList[4] = str_replace('$MODE$', $mode, $InfoFieldList[4]); + } // current object id can be use into filter if (strpos($InfoFieldList[4], '$ID$') !== false && !empty($objectid)) { @@ -1474,7 +1478,7 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ } else { $sqlwhere .= ' WHERE 1=1'; } - +print $sqlwhere; // Add Usf filter on second line /* if ($Usf) { From f0f8bc595e4c7a0cb5b8470bb2426d151e8988f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 16 Oct 2024 10:53:27 +0200 Subject: [PATCH 03/29] can filter on object properties --- htdocs/core/class/extrafields.class.php | 4 ++-- htdocs/core/upload_page.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 7d23ad247b407..29d1011eb8620 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1456,7 +1456,7 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ } // can use MODE request (list or view) if (strpos($InfoFieldList[4], '$MODE$') !== false) { - $InfoFieldList[4] = str_replace('$MODE$', $mode, $InfoFieldList[4]); + $InfoFieldList[4] = str_replace('$MODE$', (string) $mode, $InfoFieldList[4]); } // current object id can be use into filter @@ -1478,7 +1478,7 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ } else { $sqlwhere .= ' WHERE 1=1'; } -print $sqlwhere; + // Add Usf filter on second line /* if ($Usf) { diff --git a/htdocs/core/upload_page.php b/htdocs/core/upload_page.php index b1964d5eff071..cbac75cea9fdc 100644 --- a/htdocs/core/upload_page.php +++ b/htdocs/core/upload_page.php @@ -97,7 +97,7 @@ llxHeader('', $title, $help_url, '', 0, 0, $arrayofjs, $arrayofcss, '', 'mod-upload page-card'); //top_htmlhead($head, $title, 0, 0, $arrayofjs, $arrayofcss); -print load_fiche_titre('', '', '', 0, '', 0, '

'.$title.'

'); +print load_fiche_titre('', '', '', 0, '', '', '

'.$title.'

'); // Instantiate hooks of thirdparty module From befaf262d93f1b6ea87eaf403c03688bc2558b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 16 Oct 2024 11:17:24 +0200 Subject: [PATCH 04/29] can filter on object properties --- htdocs/core/class/extrafields.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 29d1011eb8620..850082d9363b4 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1497,9 +1497,10 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ $sqlwhere .= ' AND entity = '.((int) $conf->entity); } $sql .= $sqlwhere; - //print $sql; $sql .= ' ORDER BY '.implode(', ', $fields_label); + $sql .= ' LIMIT ' . getDolGlobalString('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); + // print $sql; dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG); $resql = $this->db->query($sql); From 324fe1e7a7e4f85cd7e24671aeef3e6d83da5c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 16 Oct 2024 11:56:49 +0200 Subject: [PATCH 05/29] can filter on object properties --- htdocs/core/class/commonobject.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index aeb99aad12572..6c4be4bf97f35 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7829,7 +7829,6 @@ function handlemultiinputdisabling(htmlname){ $sqlwhere .= " AND entity = " . ((int) $conf->entity); } $sql .= $sqlwhere; - //print $sql; // Note: $InfoFieldList can be 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:CategoryIdType[:CategoryIdList[:Sortfield]]]]]]' if (isset($InfoFieldList[7]) && preg_match('/^[a-z0-9_\-,]+$/i', $InfoFieldList[7])) { @@ -7837,6 +7836,8 @@ function handlemultiinputdisabling(htmlname){ } else { $sql .= " ORDER BY ".$this->db->sanitize(implode(', ', $fields_label)); } + $sql .= ' LIMIT ' . getDolGlobalString('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); + // print $sql; dol_syslog(get_class($this) . '::showInputField type=sellist', LOG_DEBUG); $resql = $this->db->query($sql); From c23f06d5db4fbbcb5be4b16e1f1a93ebbab20a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 17 Oct 2024 08:34:08 +0200 Subject: [PATCH 06/29] fix sql --- htdocs/core/class/commonobject.class.php | 2 +- htdocs/core/class/extrafields.class.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 6c4be4bf97f35..1153567e667ce 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7836,7 +7836,7 @@ function handlemultiinputdisabling(htmlname){ } else { $sql .= " ORDER BY ".$this->db->sanitize(implode(', ', $fields_label)); } - $sql .= ' LIMIT ' . getDolGlobalString('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); + $sql .= ' LIMIT ' . getDolGlobalInt('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); // print $sql; dol_syslog(get_class($this) . '::showInputField type=sellist', LOG_DEBUG); diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 850082d9363b4..8419bf8e99602 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1499,7 +1499,7 @@ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $ $sql .= $sqlwhere; $sql .= ' ORDER BY '.implode(', ', $fields_label); - $sql .= ' LIMIT ' . getDolGlobalString('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); + $sql .= ' LIMIT ' . getDolGlobalInt('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); // print $sql; dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG); From 35935a5e17b9a60df14c98437430e87f4ca4993f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 17 Oct 2024 13:28:33 +0200 Subject: [PATCH 07/29] fix --- htdocs/core/class/commonobject.class.php | 34 ++++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 2ee906b4b5744..d007d51c8d9c9 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1,22 +1,22 @@ - * Copyright (C) 2005-2013 Regis Houssin - * Copyright (C) 2010-2020 Juanjo Menent - * Copyright (C) 2012-2013 Christophe Battarel - * Copyright (C) 2011-2022 Philippe Grand - * Copyright (C) 2012-2015 Marcos García - * Copyright (C) 2012-2015 Raphaël Doursenaud - * Copyright (C) 2012 Cedric Salvador - * Copyright (C) 2015-2022 Alexandre Spangaro - * Copyright (C) 2016 Bahfir abbes - * Copyright (C) 2017 ATM Consulting - * Copyright (C) 2017-2019 Nicolas ZABOURI - * Copyright (C) 2017 Rui Strecht +/* Copyright (C) 2006-2015 Laurent Destailleur + * Copyright (C) 2005-2013 Regis Houssin + * Copyright (C) 2010-2020 Juanjo Menent + * Copyright (C) 2012-2013 Christophe Battarel + * Copyright (C) 2011-2022 Philippe Grand + * Copyright (C) 2012-2015 Marcos García + * Copyright (C) 2012-2015 Raphaël Doursenaud + * Copyright (C) 2012 Cedric Salvador + * Copyright (C) 2015-2022 Alexandre Spangaro + * Copyright (C) 2016 Bahfir abbes + * Copyright (C) 2017 ATM Consulting + * Copyright (C) 2017-2019 Nicolas ZABOURI + * Copyright (C) 2017 Rui Strecht * Copyright (C) 2018-2024 Frédéric France - * Copyright (C) 2018 Josep Lluís Amador - * Copyright (C) 2023 Gauthier VERDOL - * Copyright (C) 2021 Grégory Blémand - * Copyright (C) 2023 Lenin Rivas + * Copyright (C) 2018 Josep Lluís Amador + * Copyright (C) 2023 Gauthier VERDOL + * Copyright (C) 2021 Grégory Blémand + * Copyright (C) 2023 Lenin Rivas * Copyright (C) 2024 MDW * Copyright (C) 2024 William Mead * From 2a0d4bf2ae265ca272072aa422b99a45a362b116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Tue, 22 Oct 2024 21:46:04 +0200 Subject: [PATCH 08/29] new ajax search with infinite scroll --- htdocs/core/ajax/ajaxextrafield.php | 328 +++++++++++++++++++ htdocs/core/class/extrafields.class.php | 407 +++++++++++++----------- 2 files changed, 546 insertions(+), 189 deletions(-) create mode 100644 htdocs/core/ajax/ajaxextrafield.php diff --git a/htdocs/core/ajax/ajaxextrafield.php b/htdocs/core/ajax/ajaxextrafield.php new file mode 100644 index 0000000000000..48538b0edff16 --- /dev/null +++ b/htdocs/core/ajax/ajaxextrafield.php @@ -0,0 +1,328 @@ + + * Copyright (C) 2024 Frédéric France + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/ajax/ajaxtooltip.php + * \ingroup tooltip + * \brief This script returns content of tooltip + */ + +if (!defined('NOTOKENRENEWAL')) { + // Disables token renewal + define('NOTOKENRENEWAL', 1); +} +if (!defined('NOREQUIREMENU')) { + define('NOREQUIREMENU', '1'); +} +if (!defined('NOREQUIREHTML')) { + define('NOREQUIREHTML', '1'); +} +if (!defined('NOREQUIREAJAX')) { + define('NOREQUIREAJAX', '1'); +} +if (!defined('NOHEADERNOFOOTER')) { + define('NOHEADERNOFOOTER', '1'); +} + +include '../../main.inc.php'; +include_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php'; +/** @var DoliDB $db */ + +// object id +$id = GETPOST('id', 'aZ09'); +// 'module' or 'myobject@mymodule', 'mymodule_myobject' +$objecttype = GETPOST('objecttype', 'aZ09arobase'); +$objectkey = GETPOST('objectkey', 'restricthtml'); +$search = GETPOST('search', 'restricthtml'); +$page = GETPOSTINT('page'); +$limit = 10; +$offset = (($page - 1) * $limit); + +// Load object according to $element +$object = fetchObjectByElement($id, $objecttype); +if (empty($object->element)) { + httponly_accessforbidden('Failed to get object with fetchObjectByElement(id=' . $id . ', objecttype=' . $objecttype . ')'); +} + +$module = $object->module; +$element = $object->element; + +$usesublevelpermission = ($module != $element ? $element : ''); +if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly + $usesublevelpermission = ''; +} + +// print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n"; + +// Security check +restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission); + + +/* + * View + */ + +top_httphead(); + +$data = [ + 'results' => [], + 'pagination' => [ + 'more' => true, + ] +]; +if ($page == 1) { + $data['results'][] = [ + 'id' => -1, + 'text' => ' ', + ]; +} +if (is_object($object)) { + $extrafields = new ExtraFields($db); + $extrafields->fetch_name_optionals_label($element); + $options = $extrafields->attributes[$element]['param'][$objectkey]['options']; + if (is_array($options)) { + $tmpparamoptions = array_keys($options); + $paramoptions = preg_split('/[\r\n]+/', $tmpparamoptions[0]); + + $InfoFieldList = explode(":", $paramoptions[0], 5); + // 0 : tableName + // 1 : label field name + // 2 : key fields name (if different of rowid) + // optional parameters... + // 3 : key field parent (for dependent lists). How this is used ? + // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value. Or use USF on the second line. + // 5 : string category type. This replace the filter. + // 6 : ids categories list separated by comma for category root. This replace the filter. + // 7 : sort field (not used here but used into format for commobject) + + // If there is a filter, we extract it by taking all content inside parenthesis. + if (! empty($InfoFieldList[4])) { + $pos = 0; // $pos will be position of ending filter + $parenthesisopen = 0; + while (substr($InfoFieldList[4], $pos, 1) !== '' && ($parenthesisopen || $pos == 0 || substr($InfoFieldList[4], $pos, 1) != ':')) { + if (substr($InfoFieldList[4], $pos, 1) == '(') { + $parenthesisopen++; + } + if (substr($InfoFieldList[4], $pos, 1) == ')') { + $parenthesisopen--; + } + $pos++; + } + $tmpbefore = substr($InfoFieldList[4], 0, $pos); + $tmpafter = substr($InfoFieldList[4], $pos + 1); + //var_dump($InfoFieldList[4].' -> '.$pos); var_dump($tmpafter); + $InfoFieldList[4] = $tmpbefore; + if ($tmpafter !== '') { + $InfoFieldList = array_merge($InfoFieldList, explode(':', $tmpafter)); + } + + // Fix better compatibility with some old extrafield syntax filter "(field=123)" + $reg = array(); + if (preg_match('/^\(?([a-z0-9]+)([=<>]+)(\d+)\)?$/i', $InfoFieldList[4], $reg)) { + $InfoFieldList[4] = '(' . $reg[1] . ':' . $reg[2] . ':' . $reg[3] . ')'; + } + + //var_dump($InfoFieldList); + } + + $parentName = ''; + $parentField = ''; + $keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2] . ' as rowid'); + + if (count($InfoFieldList) > 3 && !empty($InfoFieldList[3])) { + list($parentName, $parentField) = explode('|', $InfoFieldList[3]); + $keyList .= ', ' . $parentField; + } + if (count($InfoFieldList) > 4 && !empty($InfoFieldList[4])) { + if (strpos($InfoFieldList[4], 'extra.') !== false) { + $keyList = 'main.' . $InfoFieldList[2] . ' as rowid'; + } else { + $keyList = $InfoFieldList[2] . ' as rowid'; + } + } + + $filter_categorie = false; + if (count($InfoFieldList) > 5) { + if ($InfoFieldList[0] == 'categorie') { + $filter_categorie = true; + } + } + + if (!$filter_categorie) { + $fields_label = explode('|', $InfoFieldList[1]); + if (is_array($fields_label)) { + $keyList .= ', '; + $keyList .= implode(', ', $fields_label); + } + + $sqlwhere = ''; + $sql = "SELECT " . $keyList; + $sql .= ' FROM ' . $db->prefix() . $InfoFieldList[0]; + + // Add filter from 4th field + if (!empty($InfoFieldList[4])) { + if (is_object($object)) { + $tags = []; + preg_match_all('/\$(.*?)\$/', $InfoFieldList[4], $tags); + foreach ($tags[0] as $keytag => $valuetag) { + $property = strtolower($tags[1][$keytag]); + if (strpos($InfoFieldList[4], $valuetag) !== false && property_exists($object, $property) && !empty($object->$property)) { + $InfoFieldList[4] = str_replace($valuetag, (string) $object->$property, $InfoFieldList[4]); + } else { + $InfoFieldList[4] = str_replace($valuetag, '0', $InfoFieldList[4]); + } + } + } + // can use current entity filter + if (strpos($InfoFieldList[4], '$ENTITY$') !== false) { + $InfoFieldList[4] = str_replace('$ENTITY$', (string) $conf->entity, $InfoFieldList[4]); + } + // can use SELECT request + if (strpos($InfoFieldList[4], '$SEL$') !== false) { + $InfoFieldList[4] = str_replace('$SEL$', 'SELECT', $InfoFieldList[4]); + } + // can use MODE request (list or view) + if (strpos($InfoFieldList[4], '$MODE$') !== false) { + $InfoFieldList[4] = str_replace('$MODE$', (string) $mode, $InfoFieldList[4]); + } + + // current object id can be use into filter + if (strpos($InfoFieldList[4], '$ID$') !== false && !empty($objectid)) { + $InfoFieldList[4] = str_replace('$ID$', (string) $objectid, $InfoFieldList[4]); + } else { + $InfoFieldList[4] = str_replace('$ID$', '0', $InfoFieldList[4]); + } + + // We have to join on extrafield table + $errstr = ''; + if (strpos($InfoFieldList[4], 'extra.') !== false) { + $sql .= ' as main, ' . $db->sanitize($db->prefix() . $InfoFieldList[0]) . '_extrafields as extra'; + $sqlwhere .= " WHERE extra.fk_object = main." . $db->sanitize($InfoFieldList[2]); + $sqlwhere .= " AND " . forgeSQLFromUniversalSearchCriteria($InfoFieldList[4], $errstr, 1); + } else { + $sqlwhere .= " WHERE " . forgeSQLFromUniversalSearchCriteria($InfoFieldList[4], $errstr, 1); + } + } else { + $sqlwhere .= ' WHERE 1=1'; + } + + // Some tables may have field, some other not. For the moment we disable it. + if (in_array($InfoFieldList[0], array('tablewithentity'))) { + $sqlwhere .= ' AND entity = ' . ((int) $conf->entity); + } + if ($search) { + if ($fields_label) { + $sqlwhere .= " " . natural_search($fields_label, $search, 0); + } + } + $sql .= $sqlwhere; + + $sql .= ' ORDER BY ' . implode(', ', $fields_label); + $sql .= $db->plimit($limit, $offset); + // print $sql; + $data['sql'] = $sql; + + $resql = $db->query($sql); + if ($resql) { + // $out .= ''; + // $data['results'][] = [ + // 'id' => 0, + // 'text' => ' ', + // ]; + $num = $db->num_rows($resql); + $i = 0; + while ($i < $num) { + $labeltoshow = ''; + $obj = $db->fetch_object($resql); + + // Several field into label (eq table:code|label:rowid) + $notrans = false; + $fields_label = explode('|', $InfoFieldList[1]); + if (is_array($fields_label) && count($fields_label) > 1) { + $notrans = true; + foreach ($fields_label as $field_toshow) { + $labeltoshow .= $obj->$field_toshow . ' '; + } + } else { + $labeltoshow = $obj->{$InfoFieldList[1]}; + } + + if ($value == $obj->rowid) { + if (!$notrans) { + foreach ($fields_label as $field_toshow) { + $translabel = $langs->trans($obj->$field_toshow); + $labeltoshow = $translabel . ' '; + } + } + // $out .= ''; + $data['results'][] = [ + 'id' => $obj->rowid, + 'text' => $labeltoshow, + ]; + } else { + if (!$notrans) { + $translabel = $langs->trans($obj->{$InfoFieldList[1]}); + $labeltoshow = $translabel; + } + if (empty($labeltoshow)) { + $labeltoshow = '(not defined)'; + } + + if (!empty($InfoFieldList[3]) && $parentField) { + $parent = $parentName . ':' . $obj->{$parentField}; + } + + // $out .= ''; + $data['results'][] = [ + 'id' => $obj->rowid, + 'text' => $labeltoshow, + ]; + } + + $i++; + } + $db->free($resql); + } else { + print 'Error in request ' . $sql . ' ' . $db->lasterror() . '. Check setup of extra parameters.
'; + } + } else { + // require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + // $data = $form->select_all_categories(Categorie::$MAP_ID_TO_CODE[$InfoFieldList[5]], '', 'parent', 64, $InfoFieldList[6], 1, 1); + // $out .= ''; + // if (is_array($data)) { + // foreach ($data as $data_key => $data_value) { + // $out .= ''; - $num = $this->db->num_rows($resql); - $i = 0; - while ($i < $num) { - $labeltoshow = ''; - $obj = $this->db->fetch_object($resql); + } + */ - // Several field into label (eq table:code|label:rowid) - $notrans = false; - $fields_label = explode('|', $InfoFieldList[1]); - if (is_array($fields_label) && count($fields_label) > 1) { - $notrans = true; - foreach ($fields_label as $field_toshow) { - $labeltoshow .= $obj->$field_toshow.' '; - } - } else { - $labeltoshow = $obj->{$InfoFieldList[1]}; - } + // Some tables may have field, some other not. For the moment we disable it. + if (in_array($InfoFieldList[0], array('tablewithentity'))) { + $sqlwhere .= ' AND entity = '.((int) $conf->entity); + } + $sql .= $sqlwhere; + + $sql .= ' ORDER BY '.implode(', ', $fields_label); + $sql .= ' LIMIT ' . getDolGlobalInt('MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', 1000); + // print $sql; + + dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $out .= ''; + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) { + $labeltoshow = ''; + $obj = $this->db->fetch_object($resql); - if ($value == $obj->rowid) { - if (!$notrans) { + // Several field into label (eq table:code|label:rowid) + $notrans = false; + $fields_label = explode('|', $InfoFieldList[1]); + if (is_array($fields_label) && count($fields_label) > 1) { + $notrans = true; foreach ($fields_label as $field_toshow) { - $translabel = $langs->trans($obj->$field_toshow); - $labeltoshow = $translabel.' '; + $labeltoshow .= $obj->$field_toshow.' '; } - } - $out .= ''; - } else { - if (!$notrans) { - $translabel = $langs->trans($obj->{$InfoFieldList[1]}); - $labeltoshow = $translabel; - } - if (empty($labeltoshow)) { - $labeltoshow = '(not defined)'; + } else { + $labeltoshow = $obj->{$InfoFieldList[1]}; } - if (!empty($InfoFieldList[3]) && $parentField) { - $parent = $parentName.':'.$obj->{$parentField}; + if ($value == $obj->rowid) { + if (!$notrans) { + foreach ($fields_label as $field_toshow) { + $translabel = $langs->trans($obj->$field_toshow); + $labeltoshow = $translabel.' '; + } + } + $out .= ''; + } else { + if (!$notrans) { + $translabel = $langs->trans($obj->{$InfoFieldList[1]}); + $labeltoshow = $translabel; + } + if (empty($labeltoshow)) { + $labeltoshow = '(not defined)'; + } + + if (!empty($InfoFieldList[3]) && $parentField) { + $parent = $parentName.':'.$obj->{$parentField}; + } + + $out .= ''; } - $out .= ''; + $i++; } - - $i++; + $this->db->free($resql); + } else { + print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.
'; } - $this->db->free($resql); } else { - print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.
'; - } - } else { - require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; - $data = $form->select_all_categories(Categorie::$MAP_ID_TO_CODE[$InfoFieldList[5]], '', 'parent', 64, $InfoFieldList[6], 1, 1); - $out .= ''; - if (is_array($data)) { - foreach ($data as $data_key => $data_value) { - $out .= ''; + if (is_array($data)) { + foreach ($data as $data_key => $data_value) { + $out .= '