dolibarr 25.0.0-alpha
ajaxfield.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2024 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
25if (!defined('NOTOKENRENEWAL')) {
26 // Disables token renewal
27 define('NOTOKENRENEWAL', 1);
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('NOHEADERNOFOOTER')) {
39 define('NOHEADERNOFOOTER', '1');
40}
41
42require_once '../webportal.main.inc.php';
43require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
44require_once DOL_DOCUMENT_ROOT . '/core/class/fieldsmanager.class.php';
45
53// object id
54$objectid = GETPOST('objectid', 'aZ09');
55// 'module' or 'myobject@mymodule', 'mymodule_myobject'
56$objecttype = GETPOST('objecttype', 'aZ09arobase');
57$objectkey = GETPOST('objectkey', 'restricthtml');
58$search = GETPOST('search', 'restricthtml');
59$page = GETPOSTINT('page');
60$mode = GETPOST('mode', 'aZ09');
61$value = GETPOST('value', 'alphanohtml');
62$dependencyvalue = GETPOST('dependencyvalue', 'alphanohtml');
63$limit = 10;
64$element_ref = '';
65if (is_numeric($objectid)) {
66 $objectid = (int) $objectid;
67} else {
68 $element_ref = $objectid;
69 $objectid = 0;
70}
71// Load object according to $element
72$object = fetchObjectByElement($objectid, $objecttype, $element_ref);
73if (empty($object->element)) {
74 httponly_accessforbidden('Failed to get object with fetchObjectByElement(id=' . $objectid . ', objecttype=' . $objecttype . ')');
75}
76
77$module = $object->module;
78$element = $object->element;
79
80$usesublevelpermission = ($module != $element ? $element : '');
81if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
82 $usesublevelpermission = '';
83}
84
85// print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
86
87// Security check
88restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission);
89if (!isModEnabled('webportal')) {
91}
92
93
94/*
95 * View
96 */
97
99
100$data = [
101 'results' => [],
102 'pagination' => [
103 'more' => true,
104 ]
105];
106$nbResult = 0;
107if ($object instanceof CommonObject) {
108 $extrafields = new ExtraFields($db);
109 $extrafields->fetch_name_optionals_label($object->table_element);
110
111 $fieldManager = new FieldsManager($db);
112 $fieldInfos = $fieldManager->getFieldsInfos($objectkey, $object, $extrafields, $mode);
113 // The options are returned only for a field the user can see in this mode, as in the forms (not for a hidden field)
114 if (!is_object($fieldInfos) || empty($fieldInfos->visible)) {
115 httponly_accessforbidden('Field '.$objectkey.' is not visible');
116 }
117 $field = $fieldManager->getFieldClass($fieldInfos->type);
118 if (isset($field)) {
119 if (method_exists($field, 'getOptions')) {
120 $fieldInfos->optionsSqlPage = $page;
121 $fieldInfos->optionsSqlLimit = $limit;
122 if ($dependencyvalue !== '') {
123 $fieldInfos->optionsSqlDependencyValue = $dependencyvalue;
124 }
125 if ($search !== '') {
126 $fieldInfos->optionsSqlSearch = $search;
127 }
128
132 '@phan-var-force CommonSellistField $field';
133 $options = $field->getOptions($fieldInfos, $objectkey, $page == 1, true);
134 if (is_array($options)) {
135 $nbResult = count($options);
136 foreach ($options as $key => $option) {
137 $data['results'][] = [
138 'id' => $key,
139 'text' => $option['label'],
140 ];
141 }
142 } else {
143 dol_syslog('htdocs/core/ajax/ajaxfield.php ' . $field->errorsToString(), LOG_ERR);
144 }
145 } else {
146 dol_syslog('htdocs/core/ajax/ajaxfield.php method getOptions() don\'t exist in class ' . get_class($field), LOG_ERR);
147 }
148 } else {
149 dol_syslog('htdocs/core/ajax/ajaxfield.php ' . $fieldManager->errorsToString(), LOG_ERR);
150 }
151}
152
153if ($page > 1 && $nbResult < 10) {
154 $data['pagination'] = [
155 'more' => false,
156 ];
157}
158print json_encode($data);
159
160$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage standard extra fields.
Class to manage fields.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.