dolibarr 21.0.0-alpha
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24if (!defined('NOTOKENRENEWAL')) {
25 define('NOTOKENRENEWAL', 1); // Disables token renewal
26}
27if (!defined('NOREQUIREMENU')) {
28 define('NOREQUIREMENU', '1');
29}
30if (!defined('NOREQUIREHTML')) {
31 define('NOREQUIREHTML', '1');
32}
33if (!defined('NOREQUIREAJAX')) {
34 define('NOREQUIREAJAX', '1');
35}
36if (!defined('NOREQUIRESOC')) {
37 define('NOREQUIRESOC', '1');
38}
39
40// Load Dolibarr environment
41require '../../main.inc.php';
42require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
44
45$extrafields = new ExtraFields($db);
46
47$objectdesc = GETPOST('objectdesc', 'alphanohtml', 0, null, null, 1);
48$htmlname = GETPOST('htmlname', 'aZ09');
49$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
50$id = GETPOSTINT('id');
51$objectfield = GETPOST('objectfield', 'alpha'); // 'MyObject:field' or 'MyModule_MyObject:field' or 'MyObject:option_field' or 'MyModule_MyObject:option_field'
52
53if (empty($htmlname)) {
54 httponly_accessforbidden('Bad value for param htmlname');
55}
56
57if (!empty($objectfield)) {
58 // Recommended method to call selectobject.
59 // $objectfield is Object:Field that contains the definition (in table $fields or extrafield). Example: 'Societe:t.ddd' or 'Societe:options_xxx'
60
61 $tmparray = explode(':', $objectfield);
62 $objectdesc = '';
63
64 // Load object according to $id and $element
65 $objectforfieldstmp = fetchObjectByElement(0, strtolower($tmparray[0]));
66
67 $reg = array();
68 if (preg_match('/^options_(.*)$/', $tmparray[1], $reg)) {
69 // For a property in extrafields
70 $key = $reg[1];
71 // fetch optionals attributes and labels
72 $extrafields->fetch_name_optionals_label($objectforfieldstmp->table_element);
73
74 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key]) && $extrafields->attributes[$objectforfieldstmp->table_element]['type'][$key] == 'link') {
75 if (!empty($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options'])) {
76 $tmpextrafields = array_keys($extrafields->attributes[$objectforfieldstmp->table_element]['param'][$key]['options']);
77 $objectdesc = $tmpextrafields[0];
78 }
79 }
80 } else {
81 // For a property in ->fields
82 $objectdesc = $objectforfieldstmp->fields[$tmparray[1]]['type'];
83 $objectdesc = preg_replace('/^integer[^:]*:/', '', $objectdesc);
84 }
85}
86
87if ($objectdesc) {
88 // Example of value for $objectdesc:
89 // Bom:bom/class/bom.class.php:0:t.status=1
90 // Bom:bom/class/bom.class.php:0:t.status=1:ref
91 // Bom:bom/class/bom.class.php:0:(t.status:=:1) OR (t.field2:=:2):ref
92 $InfoFieldList = explode(":", $objectdesc, 4);
93 $vartmp = (empty($InfoFieldList[3]) ? '' : $InfoFieldList[3]);
94 $reg = array();
95 if (preg_match('/^.*:(\w*)$/', $vartmp, $reg)) {
96 $InfoFieldList[4] = $reg[1]; // take the sort field
97 }
98 $InfoFieldList[3] = preg_replace('/:\w*$/', '', $vartmp); // take the filter field
99
100 $classname = $InfoFieldList[0];
101 $classpath = $InfoFieldList[1];
102 //$addcreatebuttonornot = empty($InfoFieldList[2]) ? 0 : $InfoFieldList[2];
103 $filter = empty($InfoFieldList[3]) ? '' : $InfoFieldList[3];
104 $sortfield = empty($InfoFieldList[4]) ? '' : $InfoFieldList[4];
105
106 // Load object according to $id and $element
107 $objecttmp = fetchObjectByElement(0, strtolower($InfoFieldList[0]));
108
109 // Fallback to another solution to get $objecttmp
110 if (empty($objecttmp) && !empty($classpath)) {
111 dol_include_once($classpath);
112
113 if ($classname && class_exists($classname)) {
114 $objecttmp = new $classname($db);
115 }
116 }
117}
118
119// Make some replacement
120$sharedentities = getEntity(strtolower($objecttmp->element));
121
122$filter = str_replace(
123 array('__ENTITY__', '__SHARED_ENTITIES__', '__USER_ID__', '$ID$'),
124 array($conf->entity, $sharedentities, $user->id, $id),
125 $filter
126);
127
128/*
129$module = $object->module;
130$element = $object->element;
131$usesublevelpermission = ($module != $element ? $element : '');
132if ($usesublevelpermission && !isset($user->rights->$module->$element)) { // There is no permission on object defined, we will check permission on module directly
133 $usesublevelpermission = '';
134}
135*/
136
137// When used from jQuery, the search term is added as GET param "term".
138$searchkey = (($id && GETPOST((string) $id, 'alpha')) ? GETPOST((string) $id, 'alpha') : (($htmlname && GETPOST($htmlname, 'alpha')) ? GETPOST($htmlname, 'alpha') : ''));
139
140// Add a security test to avoid to get content of all tables
141if (!empty($objecttmp->module)) {
142 restrictedArea($user, $objecttmp->module, $id, $objecttmp->table_element, $objecttmp->element);
143} else {
144 restrictedArea($user, $objecttmp->element, $id);
145}
146
147
148/*
149 * View
150 */
151
152$form = new Form($db);
153
154top_httphead($outjson ? 'application/json' : 'text/html');
155
156//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
157
158$arrayresult = $form->selectForFormsList($objecttmp, $htmlname, '', 0, $searchkey, '', '', '', 0, 1, 0, '', $filter);
159
160$db->close();
161
162if ($outjson) {
163 print json_encode($arrayresult);
164}
$id
Definition account.php:39
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.
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.