dolibarr 23.0.3
selectobject.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', 1); // Disables token renewal
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1');
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1');
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1');
37}
38if (!defined('NOREQUIRESOC')) {
39 define('NOREQUIRESOC', '1');
40}
41
42// Load Dolibarr environment
43require '../../main.inc.php';
51require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
52require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
53
54$extrafields = new ExtraFields($db);
55
56$objectdesc = GETPOST('objectdesc', 'alphanohtml', 0, null, null, 1); // Deprecated. Do not use this anymore. Use param 'objectfield' instead.
57$htmlname = GETPOST('htmlname', 'aZ09');
58$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
59$id = GETPOSTINT('id');
60$objectfield = GETPOST('objectfield', 'alpha'); // 'MyObject:field' or 'MyModule_MyObject:field' or 'MyObject:option_field' or 'MyModule_MyObject:option_field'
61
62if (empty($htmlname)) {
63 httponly_accessforbidden('Bad value for param htmlname');
64}
65
66if (!empty($objectfield)) {
67 // Recommended method to call selectobject.
68 // $objectfield is Object:Field that contains the definition (in table $fields or extrafield). Example: 'Societe:t.ddd' or 'Societe:options_xxx'
69
70 $tmparray = explode(':', $objectfield);
71 $objectdesc = '';
72
73 // Load object according to $id and $element
74 $objectforfieldstmp = fetchObjectByElement(0, strtolower($tmparray[0]));
75
76 $reg = array();
77 if (preg_match('/^options_(.*)$/', $tmparray[1], $reg)) {
78 // For a property in extrafields
79 $key = $reg[1];
80 // fetch optionals attributes and labels
81 $extrafields->fetch_name_optionals_label($objectforfieldstmp->table_element);
82
83 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key]) && $extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key] == 'link') {
84 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options'])) {
85 $tmpextrafields = array_keys($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options']);
86 $objectdesc = $tmpextrafields[0];
87 }
88 }
89 } else {
90 // For a property in ->fields
91 $objectdesc = $objectforfieldstmp->fields[$tmparray[1]]['type'];
92 $objectdesc = preg_replace('/^integer[^:]*:/', '', $objectdesc);
93 }
94}
95
96$objecttmp = null;
97if ($objectdesc) {
98 // Example of value for $objectdesc:
99 // Bom:bom/class/bom.class.php:0:t.status=1
100 // Bom:bom/class/bom.class.php:0:t.status=1:ref
101 // Bom:bom/class/bom.class.php:0:(t.status:=:1) OR (t.field2:=:2):ref
102 $InfoFieldList = explode(":", $objectdesc, 4);
103 $vartmp = (empty($InfoFieldList[3]) ? '' : $InfoFieldList[3]);
104 $reg = array();
105 if (preg_match('/^.*:(\w*)$/', $vartmp, $reg)) {
106 $InfoFieldList[4] = $reg[1]; // take the sort field
107 }
108 $InfoFieldList[3] = preg_replace('/:\w*$/', '', $vartmp); // take the filter field
109
110 $classname = $InfoFieldList[0];
111 $classpath = '';
112 if (!empty($InfoFieldList[1])) {
113 $classpath = dol_sanitizePathName($InfoFieldList[1]);
114 }
115
116 //$addcreatebuttonornot = empty($InfoFieldList[2]) ? 0 : $InfoFieldList[2];
117 $filter = empty($InfoFieldList[3]) ? '' : $InfoFieldList[3];
118 $sortfield = empty($InfoFieldList[4]) ? '' : $InfoFieldList[4];
119
120 // Load object according to $id and $element
121 $objecttmp = fetchObjectByElement(0, strtolower($InfoFieldList[0]));
122
123 // Fallback to another solution to get $objecttmp
124 if (empty($objecttmp) && !empty($classpath) && preg_match('/\.class\.php$/', $classpath)) {
125 dol_include_once($classpath);
126
127 if ($classname && class_exists($classname)) {
128 $objecttmp = new $classname($db);
129 }
130 }
131}
132
133// Make some replacement
134$sharedentities = getEntity(strtolower($objecttmp->element));
135
136$filter = str_replace(
137 array('__ENTITY__', '__SHARED_ENTITIES__', '__USER_ID__', '$ID$'),
138 array($conf->entity, $sharedentities, $user->id, $id),
139 $filter
140);
141
142/*
143$module = $object->module;
144$element = $object->element;
145$usesublevelpermission = ($module != $element ? $element : '');
146if ($usesublevelpermission && !isset($user->rights->$module->$element)) { // There is no permission on object defined, we will check permission on module directly
147 $usesublevelpermission = '';
148}
149*/
150
151// When used from jQuery, the search term is added as GET param "term".
152$searchkey = (($id && GETPOST((string) $id, 'alpha')) ? GETPOST((string) $id, 'alpha') : (($htmlname && GETPOST($htmlname, 'alpha')) ? GETPOST($htmlname, 'alpha') : ''));
153
154// Add a security test to avoid to get content of all tables
155$allowModules = ['bom'];
156if ($objecttmp !== null && !empty($objecttmp->module) && !in_array($objecttmp->module, $allowModules)) {
157 restrictedArea($user, $objecttmp->module, $id, $objecttmp->table_element, $objecttmp->element);
158} else {
159 restrictedArea($user, $objecttmp !== null ? $objecttmp->element : 'unknownobject', $id); // If object is unknown, we force to 'unknownobject' instead of '' to be sure access is forbidden
160}
161
162
163/*
164 * View
165 */
166
167$form = new Form($db);
168
169top_httphead($outjson ? 'application/json' : 'text/html');
170
171//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
172
173$arrayresult = $form->selectForFormsList($objecttmp, (string) $htmlname, 0, 0, $searchkey, '', '', '', 0, 1, 0, '', $filter);
174
175$db->close();
176
177if ($outjson) {
178 print json_encode($arrayresult);
179}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.