75 $options = is_array($options) ? $options : array();
76 $paramList = array_keys($options);
77 $paramList = preg_split(
'/[\r\n]+/', $paramList[0]);
89 $all = (string) $paramList[0];
90 $InfoFieldList = explode(
":", $all, 5);
93 if (!empty($InfoFieldList[4])) {
96 while (substr($InfoFieldList[4], $pos, 1) !==
'' && ($parenthesisopen || $pos == 0 || substr($InfoFieldList[4], $pos, 1) !=
':')) {
97 if (substr($InfoFieldList[4], $pos, 1) ==
'(') {
100 if (substr($InfoFieldList[4], $pos, 1) ==
')') {
105 $tmpbefore = substr($InfoFieldList[4], 0, $pos);
106 $tmpafter = substr($InfoFieldList[4], $pos + 1);
107 $InfoFieldList[4] = $tmpbefore;
108 if ($tmpafter !==
'') {
109 $InfoFieldList = array_merge($InfoFieldList, explode(
':', $tmpafter));
114 if (preg_match(
'/^\(?([a-z0-9]+)([=<>]+)(\d+)\)?$/i', $InfoFieldList[4], $reg)) {
115 $InfoFieldList[4] =
'(' . $reg[1] .
':' . $reg[2] .
':' . $reg[3] .
')';
119 $tableName = (string) ($InfoFieldList[0] ??
'');
120 $labelFullFields = (string) ($InfoFieldList[1] ??
'');
122 $labelFullFields = array_filter(array_map(
'trim', explode(
'|', $labelFullFields)),
'strlen');
123 $labelFields = array();
124 $labelAlias = array();
125 foreach ($labelFullFields as $labelFullField) {
127 $labelFields[] = $tmp[
'field'];
128 $labelAlias[] = $tmp[
'alias'];
130 $keyField = (string) ($InfoFieldList[2] ??
'');
131 if (empty($keyField)) $keyField =
'rowid';
132 $keyFieldParent = (string) ($InfoFieldList[3] ??
'');
133 $tmp = array_map(
'trim', explode(
'|', $keyFieldParent));
134 $parentName = (string) ($tmp[0] ??
'');
135 $parentFullField = (string) ($tmp[1] ??
'');
137 $parentField = $tmp[
'field'];
138 $parentAlias = $tmp[
'alias'];
139 $filter = (string) ($InfoFieldList[4] ??
'');
140 $categoryType = (string) ($InfoFieldList[5] ??
'');
141 if (is_numeric($categoryType)) {
142 require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
143 $categoryType = self::MAP_ID_TO_CODE[(int) $categoryType] ??
'';
145 $categoryRoots = (string) ($InfoFieldList[6] ??
'');
146 $sortField = (string) ($InfoFieldList[7] ??
'');
150 'tableName' => $tableName,
151 'labelFullFields' => $labelFullFields,
152 'labelFields' => $labelFields,
153 'labelAlias' => $labelAlias,
154 'keyField' => $keyField,
155 'parentName' => $parentName,
156 'parentFullField' => $parentFullField,
157 'parentField' => $parentField,
158 'parentAlias' => $parentAlias,
160 'categoryType' => $categoryType,
161 'categoryRoots' => $categoryRoots,
162 'sortField' => $sortField,
202 public function getOptions($fieldInfos, $key, $addEmptyValue =
false, $reload =
false, $selectedValues = array())
204 global $conf, $langs;
206 $selectedValues = array_map(
'trim', is_array($selectedValues) ? $selectedValues : array($selectedValues));
208 if (!isset(self::$options[$key]) || $reload) {
210 if (!empty($fieldInfos->options) && is_array($fieldInfos->options)) {
213 if ($optionsParams[
'tableName'] ==
'categorie' && !empty($optionsParams[
'categoryType'])) {
214 $data = self::$form->select_all_categories($optionsParams[
'categoryType'],
'',
'parent', 64, $optionsParams[
'categoryRoots'], 1, 1);
215 if (is_array($data)) {
216 foreach ($data as $data_key => $data_value) {
217 $options[$data_key] = array(
218 'label' => $data_value,
224 $filter = $optionsParams[
'filter'];
225 $hasExtra = !empty($filter) && strpos($filter,
'extra.') !==
false;
226 $keyField = ($hasExtra ?
'main.' :
'') . $optionsParams[
'keyField'];
228 $keyList = $keyField .
' AS rowid';
229 if (!empty($optionsParams[
'parentFullField'])) {
230 $keyList .=
', ' . $optionsParams[
'parentFullField'];
232 if (!empty($optionsParams[
'labelFullFields'])) {
233 $keyList .=
', ' . implode(
', ', $optionsParams[
'labelFullFields']);
236 $sql =
"SELECT " . $keyList;
237 $sql .=
" FROM " . $this->db->sanitize($this->db->prefix() . $optionsParams[
'tableName']);
240 $sql .=
" LEFT JOIN " . $this->db->sanitize($this->db->prefix() . $optionsParams[
'tableName']) .
"_extrafields AS extra ON extra.fk_object = " . $keyField;
244 if (!empty($filter)) {
246 if (strpos($filter,
'$ENTITY$') !==
false) {
247 $filter = str_replace(
'$ENTITY$', (
string) $conf->entity, $filter);
250 if (strpos($filter,
'$SEL$') !==
false && !
getDolGlobalString(
"MAIN_DISALLOW_UNSECURED_SELECT_INTO_EXTRAFIELDS_FILTER")) {
251 $filter = str_replace(
'$SEL$',
'SELECT', $filter);
254 if (strpos($filter,
'$MODE$') !==
false) {
255 $filter = str_replace(
'$MODE$', empty($fieldInfos->mode) ?
'view' : $fieldInfos->mode, $filter);
259 $objectid = isset($fieldInfos->otherParams[
'objectId']) ? (int) $fieldInfos->otherParams[
'objectId'] : (isset($fieldInfos->object) && is_object($fieldInfos->object) ? (int) $fieldInfos->object->id : 0);
260 if (strpos($filter,
'$ID$') !==
false && !empty($objectid)) {
261 $filter = str_replace(
'$ID$', (
string) $objectid, $filter);
262 } elseif (substr($_SERVER[
"PHP_SELF"], -8) ==
'list.php') {
266 $filter = preg_replace(
'#\b([a-zA-Z0-9-\.-_]+)\b *= *\$ID\$#',
'$1', $filter);
268 $filter = preg_replace(
'#\$ID\$ *= *\b([a-zA-Z0-9-\.-_]+)\b#',
'$1', $filter);
270 $filter = str_replace(
'$ID$',
'0', $filter);
274 if (isset($fieldInfos->object) && is_object($fieldInfos->object)) {
277 preg_match_all(
'/\$(.*?)\$/', $filter, $tags);
278 foreach ($tags[0] as $keytag => $valuetag) {
279 $property = preg_replace(
'/[^a-z0-9_]/',
'', strtolower($tags[1][$keytag]));
280 if (strpos($filter, $valuetag) !==
false && property_exists(
$object, $property) && !empty(
$object->$property)) {
281 $filter = str_replace($valuetag, (
string)
$object->$property, $filter);
283 $filter = str_replace($valuetag,
'0', $filter);
291 $sql .=
' WHERE 1=1';
294 if (in_array($optionsParams[
'tableName'], array(
'tablewithentity'))) {
295 $sql .=
" AND entity = " . ((int) $conf->entity);
298 if (isset($fieldInfos->optionsSqlDependencyValue)) {
300 $sql .=
" AND " . $optionsParams[
'parentField'] .
" = '" . $this->db->escape($fieldInfos->optionsSqlDependencyValue) .
"'";
303 if (!empty($selectedValues)) {
304 $tmp =
"'" . implode(
"','", array_map(array($this->db,
'escape'), $selectedValues)) .
"'";
305 $sql .=
" AND " . $keyField .
" IN (" . $this->db->sanitize($tmp, 1) .
")";
309 if (preg_match(
'/^[a-z0-9_\-,]+$/i', $optionsParams[
'sortField'])) {
310 $sql .= $this->db->order($optionsParams[
'sortField']);
312 $sql .= $this->db->order(implode(
', ', $optionsParams[
'labelFields']));
315 $limit =
getDolGlobalInt(
'MAIN_EXTRAFIELDS_LIMIT_SELLIST_SQL', $fieldInfos->optionsSqlLimit ?? 1000);
316 $offset = $fieldInfos->optionsSqlOffset ?? (isset($fieldInfos->optionsSqlPage) ? (((int) $fieldInfos->optionsSqlPage) - 1) * $limit : 0);
317 $sql .= $this->db->plimit($limit, $offset);
319 dol_syslog(get_class($this) .
'::getOptions', LOG_DEBUG);
320 $resql = $this->db->query($sql);
322 while ($obj = $this->db->fetch_object($resql)) {
323 $optionKey = (string) $obj->rowid;
326 foreach ($optionsParams[
'labelAlias'] as $fieldToShow) {
327 $toPrint[] = is_string($obj->$fieldToShow) ? $langs->trans($obj->$fieldToShow) : $obj->$fieldToShow;
329 $optionLabel = implode(
' ', $toPrint);
331 if (empty($optionLabel)) {
332 $optionLabel =
'(not defined)';
336 $fieldValueParent = !empty($optionsParams[
'parentName']) && !empty($optionsParams[
'parentAlias']) ? $optionsParams[
'parentName'] .
':' . ((string) $obj->{$optionsParams[
'parentAlias']}) :
'';
338 $options[$optionKey] = array(
340 'label' => $optionLabel,
341 'parent' => $fieldValueParent,
344 $this->db->free($resql);
346 $this->error =
'Error in request ' . $sql .
' ' . $this->db->lasterror() .
'. Check setup of extra parameters.<br>';
351 if ($addEmptyValue && (!$fieldInfos->required || count($options) > 1)) {
362 self::$options[$key] = $options;
365 $options = self::$options[$key];
367 if (!empty($selectedValues)) {
368 $options = array_intersect_key($options, array_flip($selectedValues));