dolibarr 24.0.0-beta
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
37'@phan-var-force array<string,mixed> $parameters';
38
39// Protection to avoid direct call of template
40if (empty($object) || !is_object($object)) {
41 print "Error, template page can't be called as URL";
42 exit(1);
43}
44
45if (!is_object($form)) {
46 $form = new Form($db);
47}
48
49// Ensure $objectoffield is available for dol_eval visibility formulas.
50global $objectoffield;
51$objectoffield = $object;
52
53?>
54<!-- BEGIN PHP TEMPLATE core/tpl/extrafields_view.tpl.php to show formObjectOptions + extrafields -->
55<?php
56if (!isset($parameters) || !is_array($parameters)) {
57 $parameters = array();
58}
59if (!empty($cols)) {
60 $parameters['colspan'] = ' colspan="'.$cols.'"'; // deprecated, keptfor backward compatibility
61}
62if (!empty($cols) && !isset($parameters['cols'])) {
63 $parameters['cols'] = $cols;
64} elseif (isset($parameters['cols'])) {
65 $cols = $parameters['cols'];
66}
67if (!empty($object->fk_soc)) {
68 $parameters['socid'] = $object->fk_soc;
69}
70$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action);
71print $hookmanager->resPrint;
72if ($reshook < 0) {
73 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
74}
75
76
77//var_dump($extrafields->attributes[$object->table_element]);
78if (empty($reshook) && !empty($object->table_element) && isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
79 //$lastseparatorkeyfound = '';
80 $extrafields_collapse_num = '';
81 //$extrafields_collapse_num_old = '';
82 $i = 0;
83
84 // Loop on each extrafield
85 foreach ($extrafields->attributes[$object->table_element]['label'] as $tmpkeyextra => $tmplabelextra) {
86 $i++;
87
88 // Discard if extrafield is a hidden field on form
89 $enabled = 1;
90 if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra])) {
91 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra], 1, 1, '2');
92 }
93 if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$tmpkeyextra])) {
94 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['list'][$tmpkeyextra], 1, 1, '2');
95 }
96
97 $perms = 1;
98 if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
99 $perms = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '1');
100 }
101 //print $tmpkeyextra.'-'.$enabled.'-'.$perms.'<br>'."\n";
102
103 if (empty($enabled)) {
104 continue; // 0 = Never visible field
105 }
106 if (abs($enabled) != 1 && abs($enabled) != 3 && abs($enabled) != 5 && abs($enabled) != 4) {
107 continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list <> 4 = not visible at the creation
108 }
109 /* No perm means we can't edit, but we should be able to see according to visibility field.
110 if (empty($perms)) {
111 continue; // 0 = Not visible
112 }
113 */
114
115 // Load language if required
116 if (!empty($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra])) {
117 $langs->load($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra]);
118 }
119 if ($action == 'edit_extras') {
120 $value = (GETPOSTISSET("options_".$tmpkeyextra) ? GETPOST("options_".$tmpkeyextra) : (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : ''));
121 } else {
122 $value = (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : '');
123 //var_dump($tmpkeyextra.' - '.$value);
124 }
125
126 // Print line tr of extra field
127 if ($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra] == 'separate') {
128 $extrafields_collapse_num = $tmpkeyextra;
129
130 print $extrafields->showSeparator($tmpkeyextra, $object);
131
132 //$lastseparatorkeyfound = $tmpkeyextra;
133 } else {
134 $collapse_group = $extrafields_collapse_num.(!empty($object->id) ? '_'.$object->id : '');
135
136 print '<tr class="trextrafields_collapse'.$collapse_group;
137 /*if ($extrafields_collapse_num && $extrafields_collapse_num_old && $extrafields_collapse_num != $extrafields_collapse_num_old) {
138 print ' trextrafields_collapse_new';
139 }*/
140 if ($extrafields_collapse_num && $i == count($extrafields->attributes[$object->table_element]['label'])) {
141 print ' trextrafields_collapse_last';
142 }
143 print '"';
144 if (isset($extrafields->expand_display) && empty($extrafields->expand_display[$collapse_group])) {
145 print ' style="display: none;"';
146 }
147 print '>';
148 //$extrafields_collapse_num_old = $extrafields_collapse_num;
149 print '<td>';
150 print '<table class="nobordernopadding centpercent">';
151 print '<tr>';
152
153 print '<td class="';
154 if ((!empty($action) && ($action == 'create' || $action == 'edit')) && !empty($extrafields->attributes[$object->table_element]['required'][$tmpkeyextra])) {
155 print ' fieldrequired';
156 }
157 print '">';
158 if (!empty($extrafields->attributes[$object->table_element]['help'][$tmpkeyextra])) {
159 // You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
160 $tmptooltip = explode(':', $extrafields->attributes[$object->table_element]['help'][$tmpkeyextra]);
161 print $form->textwithpicto($langs->trans($tmplabelextra), $langs->trans($tmptooltip[0]), 1, 'help', '', 0, 3, (empty($tmptooltip[1]) ? '' : 'extra_'.$tmpkeyextra.'_'.$tmptooltip[1]));
162 } else {
163 print $langs->trans($tmplabelextra);
164 }
165 print '</td>';
166
167 //TODO Improve element and rights detection
168 //var_dump($user->rights);
169 $permwriteobject = false;
170 $keyforperm = $object->element;
171
172 if ($object->element == 'fichinter') {
173 $keyforperm = 'ficheinter';
174 }
175 if ($object->element == 'product') {
176 $keyforperm = 'produit';
177 }
178 if ($object->element == 'project') {
179 $keyforperm = 'projet';
180 }
181 if (isset($user->rights->$keyforperm)) {
182 $permwriteobject = $user->hasRight($keyforperm, 'creer') || $user->hasRight($keyforperm, 'create') || $user->hasRight($keyforperm, 'write');
183 }
184 if ($object->element == 'order_supplier') {
185 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
186 $permwriteobject = $user->hasRight('fournisseur', 'commande', 'creer');
187 } else {
188 $permwriteobject = $user->hasRight('supplier_order', 'creer');
189 }
190 }
191 if ($object->element == 'invoice_supplier') {
192 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
193 $permwriteobject = $user->hasRight('fournisseur', 'facture', 'creer');
194 } else {
195 $permwriteobject = $user->hasRight('supplier_invoice', 'creer');
196 }
197 }
198 if ($object->element == 'shipping') {
199 $permwriteobject = $user->hasRight('expedition', 'creer');
200 }
201 if ($object->element == 'delivery') {
202 $permwriteobject = $user->hasRight('expedition', 'delivery', 'creer');
203 }
204 if ($object->element == 'productlot') {
205 $permwriteobject = $user->hasRight('stock', 'creer');
206 }
207 if ($object->element == 'facturerec') {
208 $permwriteobject = $user->hasRight('facture', 'creer');
209 }
210 if ($object->element == 'mo') {
211 $permwriteobject = $user->hasRight('mrp', 'write');
212 }
213 if ($object->element == 'contact') {
214 $permwriteobject = $user->hasRight('societe', 'contact', 'creer');
215 }
216 if ($object->element == 'salary') {
217 $permwriteobject = $user->hasRight('salaries', 'write');
218 }
219 if ($object->element == 'member') {
220 $permwriteobject = $user->hasRight('adherent', 'creer');
221 }
222
223 // Set permission to edit/write extrafield.
224 //print "permwriteobject=".$permwriteobject." perms=".$perms;
225 $permtoeditextrafield = $perms;
226 if (!isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
227 $permtoeditextrafield = $permwriteobject;
228 }
229
230 $isdraft = ((isset($object->statut) && $object->statut == 0) || (isset($object->status) && $object->status == 0));
231 if (($isdraft || !empty($extrafields->attributes[$object->table_element]['alwayseditable'][$tmpkeyextra]))
232 && $permtoeditextrafield && $enabled != 5 && ($action != 'edit_extras' || GETPOST('attribute') != $tmpkeyextra)
233 && empty($extrafields->attributes[$object->table_element]['computed'][$tmpkeyextra])) {
234 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
235 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
236 if ($object->table_element == 'societe') {
237 $fieldid = 'socid';
238 }
239
240 print '<td class="right">';
241 if (isModEnabled("ai") && !empty($extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra])) {
242 $showlinktoai = "extrafieldfiller_".$tmpkeyextra;
243 $showlinktoailabel = $langs->trans("FillExtrafieldWithAi");
244 $htmlname = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : "options_".$tmpkeyextra;
245 $onlyenhancements = "textgenerationextrafield";
246 $morecss = "editfielda";
247 $aiprompt = $extrafields->attributes[$object->table_element]["aiprompt"][$tmpkeyextra];
248 $out = "";
249
250 // Fill $out
251 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
252 print $out;
253 print '<script>
254 $(document).ready(function() {
255 $("#'.$htmlname.'").on("change", function () {
256 value = $(this).html();
257 $.ajax({
258 method: "POST",
259 dataType: "json",
260 url: "'. DOL_URL_ROOT.'/core/ajax/updateextrafield.php",
261 data: {"token": "'.currentToken().'", "objectType": "'.$object->element.'", "objectId": "'.$object->id.'", "field": "'.$tmpkeyextra.'", "value": value},
262 success: function(response) {
263 console.log("Extrafield "+'.$tmpkeyextra.'+" successfully updated");
264 },
265 });
266 });
267 });
268 </script>';
269 }
270 print '<a class="reposition editfielda" href="'.$_SERVER['PHP_SELF'].'?'.$fieldid.'='.$valueid.'&action=edit_extras&token='.newToken().'&attribute='.$tmpkeyextra.'&ignorecollapsesetup=1">'.img_edit().'</a>';
271 print'</td>';
272 }
273 print '</tr></table>';
274 print '</td>';
275
276 $cssview = !empty($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra]) ? ($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra] . ' ') : '';
277 $html_id = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : '';
278
279 print '<td id="' . $html_id . '" class="valuefield ' . $cssview . $object->element . '_extras_' . $tmpkeyextra . ' wordbreakimp"' . (!empty($cols) ? ' colspan="' . $cols . '"' : '') . '>';
280
281 // Convert date into timestamp format
282 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) {
283 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
284 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
285 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
286 $datenotinstring = $db->jdate($datenotinstring);
287 }
288 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
289 $value = GETPOSTISSET("options_".$tmpkeyextra) ? dol_mktime(12, 0, 0, GETPOSTINT("options_".$tmpkeyextra."month"), GETPOSTINT("options_".$tmpkeyextra."day"), GETPOSTINT("options_".$tmpkeyextra."year")) : $datenotinstring;
290 }
291 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) {
292 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
293 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
294 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
295 $datenotinstring = $db->jdate($datenotinstring);
296 }
297 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
298 $value = 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;
299 }
300
301 // TODO Improve element and rights detection
302 if ($action == 'edit_extras' && $permtoeditextrafield && GETPOST('attribute', 'restricthtml') == $tmpkeyextra) {
303 // Show the extrafield in create or edit mode
304 $fieldid = 'id';
305 if ($object->table_element == 'societe') {
306 $fieldid = 'socid';
307 }
308 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"] . '?' . $fieldid . '=' . $object->id . '" method="post" name="formextra">';
309 print '<input type="hidden" name="action" value="update_extras">';
310 print '<input type="hidden" name="attribute" value="'.$tmpkeyextra.'">';
311 print '<input type="hidden" name="token" value="'.newToken().'">';
312 print '<input type="hidden" name="'.$fieldid.'" value="'.$object->id.'">';
313 print '<input type="hidden" name="page_y" value="">';
314 print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', '', $object, $object->table_element);
315
316 print '<input type="submit" class="button reposition" value="'.dolPrintHTMLForAttribute($langs->trans('Modify')).'">';
317
318 print '</form>';
319
321 if (empty($formai) || $formai instanceof FormAI) {
322 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formai.class.php';
323 $formai = new FormAI($db);
324 }
325 print $formai->getAjaxAICallFunction();
326 } else {
327 // Show the extrafield in view mode
328 //var_dump($tmpkeyextra.'-'.$value.'-'.$object->table_element);
329
330 print $extrafields->showOutputField($tmpkeyextra, $value, '', $object->table_element, null, $object);
331
332 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
333 }
334
335 print '</td>';
336 print '</tr>'."\n";
337 }
338 }
339
340 // Add code to manage list depending on others
341 // TODO Test/enhance this with a more generic solution
342 if (!empty($conf->use_javascript_ajax)) {
343 print "\n";
344 print '
345 <script>
346 jQuery(document).ready(function() {
347 setListDependencies();
348 });
349 </script>'."\n";
350 }
351}
352?>
353<!-- 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:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_eval($s, $returnvalue=1, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
dolPrintHTMLForAttribute($s, $escapeonlyhtmltags=0, $allowothertags=array())
Return a string ready to be output into an HTML attribute (alt, title, data-html, ....
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
img_edit($titlealt='default', $float=0, $other='')
Show logo edit/modify fiche.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...