dolibarr 21.0.0-beta
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 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
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', 1); // Disables token renewal
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOREQUIRESOC')) {
38 define('NOREQUIRESOC', '1');
39}
40
41// Load Dolibarr environment
42require '../../main.inc.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
44require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
53$extrafields = new ExtraFields($db);
54
55$objectdesc = GETPOST('objectdesc', 'alphanohtml', 0, null, null, 1);
56$htmlname = GETPOST('htmlname', 'aZ09');
57$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
58$id = GETPOSTINT('id');
59$objectfield = GETPOST('objectfield', 'alpha'); // 'MyObject:field' or 'MyModule_MyObject:field' or 'MyObject:option_field' or 'MyModule_MyObject:option_field'
60
61if (empty($htmlname)) {
62 httponly_accessforbidden('Bad value for param htmlname');
63}
64
65if (!empty($objectfield)) {
66 // Recommended method to call selectobject.
67 // $objectfield is Object:Field that contains the definition (in table $fields or extrafield). Example: 'Societe:t.ddd' or 'Societe:options_xxx'
68
69 $tmparray = explode(':', $objectfield);
70 $objectdesc = '';
71
72 // Load object according to $id and $element
73 $objectforfieldstmp = fetchObjectByElement(0, strtolower($tmparray[0]));
74
75 $reg = array();
76 if (preg_match('/^options_(.*)$/', $tmparray[1], $reg)) {
77 // For a property in extrafields
78 $key = $reg[1];
79 // fetch optionals attributes and labels
80 $extrafields->fetch_name_optionals_label($objectforfieldstmp->table_element);
81
82 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key]) && $extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key] == 'link') {
83 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options'])) {
84 $tmpextrafields = array_keys($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options']);
85 $objectdesc = $tmpextrafields[0];
86 }
87 }
88 } else {
89 // For a property in ->fields
90 $objectdesc = $objectforfieldstmp->fields[$tmparray[1]]['type'];
91 $objectdesc = preg_replace('/^integer[^:]*:/', '', $objectdesc);
92 }
93}
94
95$objecttmp = null;
96if ($objectdesc) {
97 // Example of value for $objectdesc:
98 // Bom:bom/class/bom.class.php:0:t.status=1
99 // Bom:bom/class/bom.class.php:0:t.status=1:ref
100 // Bom:bom/class/bom.class.php:0:(t.status:=:1) OR (t.field2:=:2):ref
101 $InfoFieldList = explode(":", $objectdesc, 4);
102 $vartmp = (empty($InfoFieldList[3]) ? '' : $InfoFieldList[3]);
103 $reg = array();
104 if (preg_match('/^.*:(\w*)$/', $vartmp, $reg)) {
105 $InfoFieldList[4] = $reg[1]; // take the sort field
106 }
107 $InfoFieldList[3] = preg_replace('/:\w*$/', '', $vartmp); // take the filter field
108
109 $classname = $InfoFieldList[0];
110 $classpath = $InfoFieldList[1];
111 //$addcreatebuttonornot = empty($InfoFieldList[2]) ? 0 : $InfoFieldList[2];
112 $filter = empty($InfoFieldList[3]) ? '' : $InfoFieldList[3];
113 $sortfield = empty($InfoFieldList[4]) ? '' : $InfoFieldList[4];
114
115 // Load object according to $id and $element
116 $objecttmp = fetchObjectByElement(0, strtolower($InfoFieldList[0]));
117
118 // Fallback to another solution to get $objecttmp
119 if (empty($objecttmp) && !empty($classpath)) {
120 dol_include_once($classpath);
121
122 if ($classname && class_exists($classname)) {
123 $objecttmp = new $classname($db);
124 }
125 }
126}
127
128// Make some replacement
129$sharedentities = getEntity(strtolower($objecttmp->element));
130
131$filter = str_replace(
132 array('__ENTITY__', '__SHARED_ENTITIES__', '__USER_ID__', '$ID$'),
133 array($conf->entity, $sharedentities, $user->id, $id),
134 $filter
135);
136
137/*
138$module = $object->module;
139$element = $object->element;
140$usesublevelpermission = ($module != $element ? $element : '');
141if ($usesublevelpermission && !isset($user->rights->$module->$element)) { // There is no permission on object defined, we will check permission on module directly
142 $usesublevelpermission = '';
143}
144*/
145
146// When used from jQuery, the search term is added as GET param "term".
147$searchkey = (($id && GETPOST((string) $id, 'alpha')) ? GETPOST((string) $id, 'alpha') : (($htmlname && GETPOST($htmlname, 'alpha')) ? GETPOST($htmlname, 'alpha') : ''));
148
149// Add a security test to avoid to get content of all tables
150if ($objecttmp !== null && !empty($objecttmp->module)) {
151 restrictedArea($user, $objecttmp->module, $id, $objecttmp->table_element, $objecttmp->element);
152} else {
153 restrictedArea($user, $objecttmp !== null ? $objecttmp->element : '', $id);
154}
155
156
157/*
158 * View
159 */
160
161$form = new Form($db);
162
163top_httphead($outjson ? 'application/json' : 'text/html');
164
165//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
166
167$arrayresult = $form->selectForFormsList($objecttmp, $htmlname, 0, 0, $searchkey, '', '', '', 0, 1, 0, '', $filter);
168
169$db->close();
170
171if ($outjson) {
172 print json_encode($arrayresult);
173}
$id
Definition account.php:48
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.
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.
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.