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