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  *
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 
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', 1); // Disables token renewal
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREHTML')) {
30  define('NOREQUIREHTML', '1');
31 }
32 if (!defined('NOREQUIREAJAX')) {
33  define('NOREQUIREAJAX', '1');
34 }
35 if (!defined('NOREQUIRESOC')) {
36  define('NOREQUIRESOC', '1');
37 }
38 
39 // Load Dolibarr environment
40 require '../../main.inc.php';
41 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
42 require_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 = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 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 
52 if (empty($htmlname)) {
53  httponly_accessforbidden('Bad value for param htmlname');
54 }
55 
56 if (!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 
86 if ($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 : '');
131 if ($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
140 if (!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 
153 top_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 
157 $arrayresult = $form->selectForFormsList($objecttmp, $htmlname, '', 0, $searchkey, '', '', '', 0, 1, 0, '', $filter);
158 
159 $db->close();
160 
161 if ($outjson) {
162  print json_encode($arrayresult);
163 }
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.
Definition: main.inc.php:1650
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.