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