dolibarr 25.0.0-alpha
extrafields_view.tpl.php
1<?php
2/* Copyright (C) 2014 Maxime Kohlhaas <support@atm-consulting.fr>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2021-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
38'@phan-var-force array<string,mixed> $parameters';
39
40// Protection to avoid direct call of template
41if (empty($object) || !is_object($object)) {
42 print "Error, template page can't be called as URL";
43 exit(1);
44}
45
46if (!is_object($form)) {
47 $form = new Form($db);
48}
49
50// Ensure $objectoffield is available for dol_eval visibility formulas.
51global $objectoffield;
52$objectoffield = $object;
53
54?>
55<!-- BEGIN PHP TEMPLATE core/tpl/extrafields_view.tpl.php to show formObjectOptions + extrafields -->
56<?php
57if (!isset($parameters) || !is_array($parameters)) {
58 $parameters = array();
59}
60if (!empty($cols)) {
61 $parameters['colspan'] = ' colspan="'.$cols.'"'; // deprecated, keptfor backward compatibility
62}
63if (!empty($cols) && !isset($parameters['cols'])) {
64 $parameters['cols'] = $cols;
65} elseif (isset($parameters['cols'])) {
66 $cols = $parameters['cols'];
67}
68if (!empty($object->fk_soc)) {
69 $parameters['socid'] = $object->fk_soc;
70}
71$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action);
72print $hookmanager->resPrint;
73if ($reshook < 0) {
74 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
75}
76
77
78//var_dump($extrafields->attributes[$object->table_element]);
79if (empty($reshook) && !empty($object->table_element) && isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
80 //$lastseparatorkeyfound = '';
81 $extrafields_collapse_num = '';
82 //$extrafields_collapse_num_old = '';
83 $i = 0;
84
85 // Loop on each extrafield
86 foreach ($extrafields->attributes[$object->table_element]['label'] as $tmpkeyextra => $tmplabelextra) {
87 $i++;
88
89 // Discard if extrafield is a hidden field on form
90 $enabled = 1;
91 if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra])) {
92 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra], 1, 1, '2');
93 }
94 if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$tmpkeyextra])) {
95 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['list'][$tmpkeyextra], 1, 1, '2');
96 }
97
98 $perms = 1;
99 if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
100 $perms = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '1');
101 }
102 //print $tmpkeyextra.'-'.$enabled.'-'.$perms.'<br>'."\n";
103
104 if (empty($enabled)) {
105 continue; // 0 = Never visible field
106 }
107 if (abs($enabled) != 1 && abs($enabled) != 3 && abs($enabled) != 5 && abs($enabled) != 4) {
108 continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list <> 4 = not visible at the creation
109 }
110 /* No perm means we can't edit, but we should be able to see according to visibility field.
111 if (empty($perms)) {
112 continue; // 0 = Not visible
113 }
114 */
115
116 // Load language if required
117 if (!empty($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra])) {
118 $langs->load($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra]);
119 }
120 if ($action == 'edit_extras') {
121 $value = (GETPOSTISSET("options_".$tmpkeyextra) ? GETPOST("options_".$tmpkeyextra) : (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : ''));
122 } else {
123 $value = (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : '');
124 //var_dump($tmpkeyextra.' - '.$value);
125 }
126
127 // Print line tr of extra field
128 if ($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra] == 'separate') {
129 $extrafields_collapse_num = $tmpkeyextra;
130
131 print $extrafields->showSeparator($tmpkeyextra, $object);
132
133 //$lastseparatorkeyfound = $tmpkeyextra;
134 } else {
135 $collapse_group = $extrafields_collapse_num.(!empty($object->id) ? '_'.$object->id : '');
136
137 print '<tr class="trextrafields_collapse'.$collapse_group;
138 /*if ($extrafields_collapse_num && $extrafields_collapse_num_old && $extrafields_collapse_num != $extrafields_collapse_num_old) {
139 print ' trextrafields_collapse_new';
140 }*/
141 if ($extrafields_collapse_num && $i == count($extrafields->attributes[$object->table_element]['label'])) {
142 print ' trextrafields_collapse_last';
143 }
144 print '"';
145 if (isset($extrafields->expand_display) && empty($extrafields->expand_display[$collapse_group])) {
146 print ' style="display: none;"';
147 }
148 print '>';
149 //$extrafields_collapse_num_old = $extrafields_collapse_num;
150 print '<td>';
151 print '<table class="nobordernopadding centpercent">';
152 print '<tr>';
153
154 print '<td class="';
155 if ((!empty($action) && ($action == 'create' || $action == 'edit')) && !empty($extrafields->attributes[$object->table_element]['required'][$tmpkeyextra])) {
156 print ' fieldrequired';
157 }
158 print '">';
159 if (!empty($extrafields->attributes[$object->table_element]['help'][$tmpkeyextra])) {
160 // You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
161 $tmptooltip = explode(':', $extrafields->attributes[$object->table_element]['help'][$tmpkeyextra]);
162 print $form->textwithpicto($langs->trans($tmplabelextra), $langs->trans($tmptooltip[0]), 1, 'help', '', 0, 3, (empty($tmptooltip[1]) ? '' : 'extra_'.$tmpkeyextra.'_'.$tmptooltip[1]));
163 } else {
164 print $langs->trans($tmplabelextra);
165 }
166 print '</td>';
167
168 //TODO Improve element and rights detection
169 //var_dump($user->rights);
170 $permwriteobject = false;
171 $keyforperm = $object->element;
172
173 if ($object->element == 'fichinter') {
174 $keyforperm = 'ficheinter';
175 }
176 if ($object->element == 'product') {
177 $keyforperm = 'produit';
178 }
179 if ($object->element == 'project') {
180 $keyforperm = 'projet';
181 }
182 if (isset($user->rights->$keyforperm)) {
183 $permwriteobject = $user->hasRight($keyforperm, 'creer') || $user->hasRight($keyforperm, 'create') || $user->hasRight($keyforperm, 'write');
184 }
185 if ($object->element == 'order_supplier') {
186 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
187 $permwriteobject = $user->hasRight('fournisseur', 'commande', 'creer');
188 } else {
189 $permwriteobject = $user->hasRight('supplier_order', 'creer');
190 }
191 }
192 if ($object->element == 'invoice_supplier') {
193 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
194 $permwriteobject = $user->hasRight('fournisseur', 'facture', 'creer');
195 } else {
196 $permwriteobject = $user->hasRight('supplier_invoice', 'creer');
197 }
198 }
199 if ($object->element == 'shipping') {
200 $permwriteobject = $user->hasRight('expedition', 'creer');
201 }
202 if ($object->element == 'delivery') {
203 $permwriteobject = $user->hasRight('expedition', 'delivery', 'creer');
204 }
205 if ($object->element == 'productlot') {
206 $permwriteobject = $user->hasRight('stock', 'creer');
207 }
208 if ($object->element == 'facturerec') {
209 $permwriteobject = $user->hasRight('facture', 'creer');
210 }
211 if ($object->element == 'mo') {
212 $permwriteobject = $user->hasRight('mrp', 'write');
213 }
214 if ($object->element == 'contact') {
215 $permwriteobject = $user->hasRight('societe', 'contact', 'creer');
216 }
217 if ($object->element == 'salary') {
218 $permwriteobject = $user->hasRight('salaries', 'write');
219 }
220 if ($object->element == 'member') {
221 $permwriteobject = $user->hasRight('adherent', 'creer');
222 }
223
224 // Set permission to edit/write extrafield.
225 //print "permwriteobject=".$permwriteobject." perms=".$perms;
226 $permtoeditextrafield = $perms;
227 if (!isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
228 $permtoeditextrafield = $permwriteobject;
229 }
230
231 $isdraft = ((isset($object->statut) && $object->statut == 0) || (isset($object->status) && $object->status == 0));
232 if (($isdraft || !empty($extrafields->attributes[$object->table_element]['alwayseditable'][$tmpkeyextra]))
233 && $permtoeditextrafield && $enabled != 5 && ($action != 'edit_extras' || GETPOST('attribute') != $tmpkeyextra)
234 && empty($extrafields->attributes[$object->table_element]['computed'][$tmpkeyextra])) {
235 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
236 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
237 if ($object->table_element == 'societe') {
238 $fieldid = 'socid';
239 }
240
241 print '<td class="right">';
242 if (isModEnabled("ai") && !empty($extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra])) {
243 $showlinktoai = "extrafieldfiller_".$tmpkeyextra;
244 $showlinktoailabel = $langs->trans("FillExtrafieldWithAi");
245 $htmlname = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : "options_".$tmpkeyextra;
246 $onlyenhancements = "textgenerationextrafield";
247 $morecss = "editfielda";
248 $aiprompt = $extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra];
249 $out = "";
250
251 // Fill $out
252 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
253 print $out;
254 print '<script>
255 $(document).ready(function() {
256 $("#'.$htmlname.'").on("change", function () {
257 value = $(this).html();
258 $.ajax({
259 method: "POST",
260 dataType: "json",
261 url: "'. DOL_URL_ROOT.'/core/ajax/updateextrafield.php",
262 data: {"token": "'.currentToken().'", "objectType": "'.$object->element.'", "objectId": "'.$object->id.'", "field": "'.$tmpkeyextra.'", "value": value},
263 success: function(response) {
264 console.log("Extrafield "+'.$tmpkeyextra.'+" successfully updated");
265 },
266 });
267 });
268 });
269 </script>';
270 }
271 print '<a class="reposition editfielda" href="'.$_SERVER['PHP_SELF'].'?'.$fieldid.'='.$valueid.'&action=edit_extras&token='.newToken().'&attribute='.$tmpkeyextra.'&ignorecollapsesetup=1'.(empty($moreparam) ? '' : $moreparam).'">'.img_edit().'</a>';
272 print'</td>';
273 }
274 print '</tr></table>';
275 print '</td>';
276
277 $cssview = !empty($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra]) ? ($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra] . ' ') : '';
278 $html_id = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : '';
279
280 print '<td id="' . $html_id . '" class="valuefield ' . $cssview . $object->element . '_extras_' . $tmpkeyextra . ' wordbreakimp"' . (!empty($cols) ? ' colspan="' . $cols . '"' : '') . '>';
281
282 // Convert date into timestamp format
283 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) {
284 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
285 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
286 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
287 $datenotinstring = $db->jdate($datenotinstring);
288 }
289 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
290 $value = ($action == 'edit_extras' && GETPOSTISSET("options_".$tmpkeyextra)) ? dol_mktime(12, 0, 0, GETPOSTINT("options_".$tmpkeyextra."month"), GETPOSTINT("options_".$tmpkeyextra."day"), GETPOSTINT("options_".$tmpkeyextra."year")) : $datenotinstring;
291 }
292 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) {
293 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
294 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
295 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
296 $datenotinstring = $db->jdate($datenotinstring);
297 }
298 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
299 $value = ($action == 'edit_extras' && GETPOSTISSET("options_".$tmpkeyextra)) ? dol_mktime(GETPOSTINT("options_".$tmpkeyextra."hour"), GETPOSTINT("options_".$tmpkeyextra."min"), GETPOSTINT("options_".$tmpkeyextra."sec"), GETPOSTINT("options_".$tmpkeyextra."month"), GETPOSTINT("options_".$tmpkeyextra."day"), GETPOSTINT("options_".$tmpkeyextra."year"), 'tzuserrel') : $datenotinstring;
300 }
301
302 // TODO Improve element and rights detection
303 if ($action == 'edit_extras' && $permtoeditextrafield && GETPOST('attribute', 'restricthtml') == $tmpkeyextra) {
304 // Show the extrafield in create or edit mode
305 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
306 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
307 if ($object->table_element == 'societe') {
308 $fieldid = 'socid';
309 }
310 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"] . '?' . $fieldid . '=' . $valueid . (empty($moreparam) ? '' : $moreparam) . '" method="post" name="formextra">';
311 print '<input type="hidden" name="action" value="update_extras">';
312 print '<input type="hidden" name="attribute" value="'.$tmpkeyextra.'">';
313 print '<input type="hidden" name="token" value="'.newToken().'">';
314 print '<input type="hidden" name="'.$fieldid.'" value="'.$valueid.'">';
315 print '<input type="hidden" name="page_y" value="">';
316 // Last parameter is 1 because the field is edited alone: a dependent list can't be filtered by javascript
317 // here (its parent list is not on this form), so it must be filtered on the saved value of its parent.
318 print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', '', $object, $object->table_element, 0, 1);
319
320 print '<input type="submit" class="button reposition" value="'.dolPrintHTMLForAttribute($langs->trans('Modify')).'">';
321
322 print '</form>';
323
325 if (empty($formai) || $formai instanceof FormAI) {
326 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formai.class.php';
327 $formai = new FormAI($db);
328 }
329 print $formai->getAjaxAICallFunction();
330 } else {
331 // Show the extrafield in view mode
332 //var_dump($tmpkeyextra.'-'.$value.'-'.$object->table_element);
333
334 print $extrafields->showOutputField($tmpkeyextra, $value, '', $object->table_element, null, $object);
335
336 print '<input type="hidden" value="' . dolPrintHTMLForAttribute($value) . '" name="options_' . dol_escape_htmltag($tmpkeyextra) . '" id="options_' . dol_escape_htmltag($tmpkeyextra) . '"/>'; // it's needed when to get parent value when extra-field list depend on parent extra-field list
337 }
338
339 print '</td>';
340 print '</tr>'."\n";
341 }
342 }
343
344 // Add code to manage list depending on others
345 // TODO Test/enhance this with a more generic solution
346 if (!empty($conf->use_javascript_ajax)) {
347 print "\n";
348 print '
349 <script>
350 jQuery(document).ready(function() {
351 setListDependencies();
352 });
353 </script>'."\n";
354 }
355}
356?>
357<!-- END PHP TEMPLATE core/tpl/extrafields_view.tpl.php -->
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to generate HTML forms for single email Usage: $formai = new FormAI($db) $formai->proprietes=1 ...
Class to manage generation of HTML components Only common components must be here.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
currentToken()
Return the value of token currently saved into session with name 'token'.
dol_eval($s, $returnvalue=1, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181