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