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 (property_exists($object, 'fk_soc') && !empty($object->fk_soc)) {
69 // @phan-suppress-next-line PhanUndeclaredProperty
70 $parameters['socid'] = $object->fk_soc;
71}
72$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action);
73print $hookmanager->resPrint;
74if ($reshook < 0) {
75 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
76}
77
78
79//var_dump($extrafields->attributes[$object->table_element]);
80if (empty($reshook) && !empty($object->table_element) && isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
81 //$lastseparatorkeyfound = '';
82 $extrafields_collapse_num = '';
83 //$extrafields_collapse_num_old = '';
84 $i = 0;
85
86 // Loop on each extrafield
87 foreach ($extrafields->attributes[$object->table_element]['label'] as $tmpkeyextra => $tmplabelextra) {
88 $i++;
89
90 // Discard if extrafield is a hidden field on form
91 $enabled = 1;
92 if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra])) {
93 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra], 1, 1, '2');
94 }
95 if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$tmpkeyextra])) {
96 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['list'][$tmpkeyextra], 1, 1, '2');
97 }
98
99 $perms = 1;
100 if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
101 $perms = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '1');
102 }
103 //print $tmpkeyextra.'-'.$enabled.'-'.$perms.'<br>'."\n";
104
105 if (empty($enabled)) {
106 continue; // 0 = Never visible field
107 }
108 if (abs($enabled) != 1 && abs($enabled) != 3 && abs($enabled) != 5 && abs($enabled) != 4) {
109 continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list <> 4 = not visible at the creation
110 }
111 /* No perm means we can't edit, but we should be able to see according to visibility field.
112 if (empty($perms)) {
113 continue; // 0 = Not visible
114 }
115 */
116
117 // Load language if required
118 if (!empty($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra])) {
119 $langs->load($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra]);
120 }
121 if ($action == 'edit_extras') {
122 $value = (GETPOSTISSET("options_".$tmpkeyextra) ? GETPOST("options_".$tmpkeyextra) : (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : ''));
123 } else {
124 $value = (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : '');
125 //var_dump($tmpkeyextra.' - '.$value);
126 }
127
128 // Print line tr of extra field
129 if ($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra] == 'separate') {
130 $extrafields_collapse_num = $tmpkeyextra;
131
132 print $extrafields->showSeparator($tmpkeyextra, $object);
133
134 //$lastseparatorkeyfound = $tmpkeyextra;
135 } else {
136 $collapse_group = $extrafields_collapse_num.(!empty($object->id) ? '_'.$object->id : '');
137
138 print '<tr class="trextrafields_collapse'.$collapse_group;
139 /*if ($extrafields_collapse_num && $extrafields_collapse_num_old && $extrafields_collapse_num != $extrafields_collapse_num_old) {
140 print ' trextrafields_collapse_new';
141 }*/
142 if ($extrafields_collapse_num && $i == count($extrafields->attributes[$object->table_element]['label'])) {
143 print ' trextrafields_collapse_last';
144 }
145 print '"';
146 if (isset($extrafields->expand_display) && empty($extrafields->expand_display[$collapse_group])) {
147 print ' style="display: none;"';
148 }
149 print '>';
150 //$extrafields_collapse_num_old = $extrafields_collapse_num;
151 print '<td>';
152 print '<table class="nobordernopadding centpercent">';
153 print '<tr>';
154
155 print '<td class="';
156 if ((!empty($action) && ($action == 'create' || $action == 'edit')) && !empty($extrafields->attributes[$object->table_element]['required'][$tmpkeyextra])) {
157 print ' fieldrequired';
158 }
159 print '">';
160 if (!empty($extrafields->attributes[$object->table_element]['help'][$tmpkeyextra])) {
161 // You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
162 $tmptooltip = explode(':', $extrafields->attributes[$object->table_element]['help'][$tmpkeyextra]);
163 print $form->textwithpicto($langs->trans($tmplabelextra), $langs->trans($tmptooltip[0]), 1, 'help', '', 0, 3, (empty($tmptooltip[1]) ? '' : 'extra_'.$tmpkeyextra.'_'.$tmptooltip[1]));
164 } else {
165 print $langs->trans($tmplabelextra);
166 }
167 print '</td>';
168
169 //TODO Improve element and rights detection
170 //var_dump($user->rights);
171 $permwriteobject = false;
172 $keyforperm = $object->element;
173
174 if ($object->element == 'fichinter') {
175 $keyforperm = 'ficheinter';
176 }
177 if ($object->element == 'product') {
178 $keyforperm = 'produit';
179 }
180 if ($object->element == 'project') {
181 $keyforperm = 'projet';
182 }
183 if (isset($user->rights->$keyforperm)) {
184 $permwriteobject = $user->hasRight($keyforperm, 'creer') || $user->hasRight($keyforperm, 'create') || $user->hasRight($keyforperm, 'write');
185 }
186 if ($object->element == 'order_supplier') {
187 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
188 $permwriteobject = $user->hasRight('fournisseur', 'commande', 'creer');
189 } else {
190 $permwriteobject = $user->hasRight('supplier_order', 'creer');
191 }
192 }
193 if ($object->element == 'invoice_supplier') {
194 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
195 $permwriteobject = $user->hasRight('fournisseur', 'facture', 'creer');
196 } else {
197 $permwriteobject = $user->hasRight('supplier_invoice', 'creer');
198 }
199 }
200 if ($object->element == 'shipping') {
201 $permwriteobject = $user->hasRight('expedition', 'creer');
202 }
203 if ($object->element == 'delivery') {
204 $permwriteobject = $user->hasRight('expedition', 'delivery', 'creer');
205 }
206 if ($object->element == 'productlot') {
207 $permwriteobject = $user->hasRight('stock', 'creer');
208 }
209 if ($object->element == 'facturerec') {
210 $permwriteobject = $user->hasRight('facture', 'creer');
211 }
212 if ($object->element == 'mo') {
213 $permwriteobject = $user->hasRight('mrp', 'write');
214 }
215 if ($object->element == 'contact') {
216 $permwriteobject = $user->hasRight('societe', 'contact', 'creer');
217 }
218 if ($object->element == 'salary') {
219 $permwriteobject = $user->hasRight('salaries', 'write');
220 }
221 if ($object->element == 'member') {
222 $permwriteobject = $user->hasRight('adherent', 'creer');
223 }
224
225 // Set permission to edit/write extrafield.
226 //print "permwriteobject=".$permwriteobject." perms=".$perms;
227 $permtoeditextrafield = $perms;
228 if (!isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
229 $permtoeditextrafield = $permwriteobject;
230 }
231
232 $isdraft = ((isset($object->statut) && $object->statut == 0) || (isset($object->status) && $object->status == 0));
233 if (($isdraft || !empty($extrafields->attributes[$object->table_element]['alwayseditable'][$tmpkeyextra]))
234 && $permtoeditextrafield && $enabled != 5 && ($action != 'edit_extras' || GETPOST('attribute') != $tmpkeyextra)
235 && empty($extrafields->attributes[$object->table_element]['computed'][$tmpkeyextra])) {
236 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
237 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
238 if ($object->table_element == 'societe') {
239 $fieldid = 'socid';
240 }
241
242 print '<td class="right">';
243 if (isModEnabled("ai") && !empty($extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra])) {
244 $showlinktoai = "extrafieldfiller_".$tmpkeyextra;
245 $showlinktoailabel = $langs->trans("FillExtrafieldWithAi");
246 $htmlname = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : "options_".$tmpkeyextra;
247 $onlyenhancements = "textgenerationextrafield";
248 $morecss = "editfielda";
249 $aiprompt = $extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra];
250 $out = "";
251
252 // Fill $out
253 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
254 print $out;
255 print '<script>
256 $(document).ready(function() {
257 $("#'.$htmlname.'").on("change", function () {
258 value = $(this).html();
259 $.ajax({
260 method: "POST",
261 dataType: "json",
262 url: "'. DOL_URL_ROOT.'/core/ajax/updateextrafield.php",
263 data: {"token": "'.currentToken().'", "objectType": "'.$object->element.'", "objectId": "'.$object->id.'", "field": "'.$tmpkeyextra.'", "value": value},
264 success: function(response) {
265 console.log("Extrafield "+'.$tmpkeyextra.'+" successfully updated");
266 },
267 });
268 });
269 });
270 </script>';
271 }
272 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>';
273 print'</td>';
274 }
275 print '</tr></table>';
276 print '</td>';
277
278 $cssview = !empty($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra]) ? ($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra] . ' ') : '';
279 $html_id = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : '';
280
281 print '<td id="' . $html_id . '" class="valuefield ' . $cssview . $object->element . '_extras_' . $tmpkeyextra . ' wordbreakimp"' . (!empty($cols) ? ' colspan="' . $cols . '"' : '') . '>';
282
283 // Convert date into timestamp format
284 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) {
285 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
286 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
287 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
288 $datenotinstring = $db->jdate($datenotinstring);
289 }
290 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
291 $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;
292 }
293 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) {
294 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
295 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
296 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
297 $datenotinstring = $db->jdate($datenotinstring);
298 }
299 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
300 $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;
301 }
302
303 // TODO Improve element and rights detection
304 if ($action == 'edit_extras' && $permtoeditextrafield && GETPOST('attribute', 'restricthtml') == $tmpkeyextra) {
305 // Show the extrafield in create or edit mode
306 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
307 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
308 if ($object->table_element == 'societe') {
309 $fieldid = 'socid';
310 }
311 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"] . '?' . $fieldid . '=' . $valueid . (empty($moreparam) ? '' : $moreparam) . '" method="post" name="formextra">';
312 print '<input type="hidden" name="action" value="update_extras">';
313 print '<input type="hidden" name="attribute" value="'.$tmpkeyextra.'">';
314 print '<input type="hidden" name="token" value="'.newToken().'">';
315 print '<input type="hidden" name="'.$fieldid.'" value="'.$valueid.'">';
316 print '<input type="hidden" name="page_y" value="">';
317 // Last parameter is 1 because the field is edited alone: a dependent list can't be filtered by javascript
318 // here (its parent list is not on this form), so it must be filtered on the saved value of its parent.
319 print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', '', $object, $object->table_element, 0, 1);
320
321 print '<input type="submit" class="button reposition" value="'.dolPrintHTMLForAttribute($langs->trans('Modify')).'">';
322
323 print '</form>';
324
326 if (empty($formai) || $formai instanceof FormAI) {
327 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formai.class.php';
328 $formai = new FormAI($db);
329 }
330 print $formai->getAjaxAICallFunction();
331 } else {
332 // Show the extrafield in view mode
333 //var_dump($tmpkeyextra.'-'.$value.'-'.$object->table_element);
334
335 print $extrafields->showOutputField($tmpkeyextra, $value, '', $object->table_element, null, $object);
336
337 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
338 }
339
340 print '</td>';
341 print '</tr>'."\n";
342 }
343 }
344
345 // Add code to manage list depending on others
346 // TODO Test/enhance this with a more generic solution
347 if (!empty($conf->use_javascript_ajax)) {
348 print "\n";
349 print '
350 <script>
351 jQuery(document).ready(function() {
352 setListDependencies();
353 });
354 </script>'."\n";
355 }
356}
357?>
358<!-- END PHP TEMPLATE core/tpl/extrafields_view.tpl.php -->
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
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:183