dolibarr 21.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-2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Show extrafields. It also shows fields from hook formObjectOptions. Need to have the following variables defined:
20 * $object (invoice, order, ...)
21 * $action
22 * $conf
23 * $langs
24 *
25 * $parameters
26 * $cols
27 */
37// Protection to avoid direct call of template
38if (empty($object) || !is_object($object)) {
39 print "Error, template page can't be called as URL";
40 exit(1);
41}
42
43if (!is_object($form)) {
44 $form = new Form($db);
45}
46
47
48?>
49<!-- BEGIN PHP TEMPLATE extrafields_view.tpl.php -->
50<?php
51if (!isset($parameters) || !is_array($parameters)) {
52 $parameters = array();
53}
54if (!empty($cols)) {
55 $parameters['colspan'] = ' colspan="'.$cols.'"';
56}
57if (!empty($cols)) {
58 $parameters['cols'] = $cols;
59}
60if (!empty($object->fk_soc)) {
61 $parameters['socid'] = $object->fk_soc;
62}
63$reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action);
64print $hookmanager->resPrint;
65if ($reshook < 0) {
66 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
67}
68
69
70//var_dump($extrafields->attributes[$object->table_element]);
71if (empty($reshook) && !empty($object->table_element) && isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
72 $lastseparatorkeyfound = '';
73 $extrafields_collapse_num = '';
74 $extrafields_collapse_num_old = '';
75 $i = 0;
76
77 // Loop on each extrafield
78 foreach ($extrafields->attributes[$object->table_element]['label'] as $tmpkeyextra => $tmplabelextra) {
79 $i++;
80
81 // Discard if extrafield is a hidden field on form
82
83 $enabled = 1;
84 if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra])) {
85 $enabled = (int) dol_eval((string) $extrafields->attributes[$object->table_element]['enabled'][$tmpkeyextra], 1, 1, '2');
86 }
87 if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$tmpkeyextra])) {
88 $enabled = (int) dol_eval($extrafields->attributes[$object->table_element]['list'][$tmpkeyextra], 1, 1, '2');
89 }
90
91 $perms = 1;
92 if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra])) {
93 $perms = (int) dol_eval($extrafields->attributes[$object->table_element]['perms'][$tmpkeyextra], 1, 1, '2');
94 }
95 //print $tmpkeyextra.'-'.$enabled.'-'.$perms.'<br>'."\n";
96
97 if (empty($enabled)) {
98 continue; // 0 = Never visible field
99 }
100 if (abs($enabled) != 1 && abs($enabled) != 3 && abs($enabled) != 5 && abs($enabled) != 4) {
101 continue; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list <> 4 = not visible at the creation
102 }
103 if (empty($perms)) {
104 continue; // 0 = Not visible
105 }
106
107 // Load language if required
108 if (!empty($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra])) {
109 $langs->load($extrafields->attributes[$object->table_element]['langfile'][$tmpkeyextra]);
110 }
111 if ($action == 'edit_extras') {
112 $value = (GETPOSTISSET("options_".$tmpkeyextra) ? GETPOST("options_".$tmpkeyextra) : (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : ''));
113 } else {
114 $value = (isset($object->array_options["options_".$tmpkeyextra]) ? $object->array_options["options_".$tmpkeyextra] : '');
115 //var_dump($tmpkeyextra.' - '.$value);
116 }
117
118 // Print line tr of extra field
119 if ($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra] == 'separate') {
120 $extrafields_collapse_num = $tmpkeyextra;
121
122 print $extrafields->showSeparator($tmpkeyextra, $object);
123
124 $lastseparatorkeyfound = $tmpkeyextra;
125 } else {
126 $collapse_group = $extrafields_collapse_num.(!empty($object->id) ? '_'.$object->id : '');
127
128 print '<tr class="trextrafields_collapse'.$collapse_group;
129 /*if ($extrafields_collapse_num && $extrafields_collapse_num_old && $extrafields_collapse_num != $extrafields_collapse_num_old) {
130 print ' trextrafields_collapse_new';
131 }*/
132 if ($extrafields_collapse_num && $i == count($extrafields->attributes[$object->table_element]['label'])) {
133 print ' trextrafields_collapse_last';
134 }
135 print '"';
136 if (isset($extrafields->expand_display) && empty($extrafields->expand_display[$collapse_group])) {
137 print ' style="display: none;"';
138 }
139 print '>';
140 $extrafields_collapse_num_old = $extrafields_collapse_num;
141 print '<td>';
142 print '<table class="nobordernopadding centpercent">';
143 print '<tr>';
144
145 print '<td class="';
146 if ((!empty($action) && ($action == 'create' || $action == 'edit')) && !empty($extrafields->attributes[$object->table_element]['required'][$tmpkeyextra])) {
147 print ' fieldrequired';
148 }
149 print '">';
150 if (!empty($extrafields->attributes[$object->table_element]['help'][$tmpkeyextra])) {
151 // You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
152 $tmptooltip = explode(':', $extrafields->attributes[$object->table_element]['help'][$tmpkeyextra]);
153 print $form->textwithpicto($langs->trans($tmplabelextra), $langs->trans($tmptooltip[0]), 1, 'help', '', 0, 3, (empty($tmptooltip[1]) ? '' : 'extra_'.$tmpkeyextra.'_'.$tmptooltip[1]));
154 } else {
155 print $langs->trans($tmplabelextra);
156 }
157 print '</td>';
158
159 //TODO Improve element and rights detection
160 //var_dump($user->rights);
161 $permok = false;
162 $keyforperm = $object->element;
163
164 if ($object->element == 'fichinter') {
165 $keyforperm = 'ficheinter';
166 }
167 if ($object->element == 'product') {
168 $keyforperm = 'produit';
169 }
170 if (isset($user->rights->$keyforperm)) {
171 $permok = $user->hasRight($keyforperm, 'creer') || $user->hasRight($keyforperm, 'create') || $user->hasRight($keyforperm, 'write');
172 }
173 if ($object->element == 'order_supplier') {
174 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
175 $permok = $user->hasRight('fournisseur', 'commande', 'creer');
176 } else {
177 $permok = $user->hasRight('supplier_order', 'creer');
178 }
179 }
180 if ($object->element == 'invoice_supplier') {
181 if (!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD')) {
182 $permok = $user->hasRight('fournisseur', 'facture', 'creer');
183 } else {
184 $permok = $user->hasRight('supplier_invoice', 'creer');
185 }
186 }
187 if ($object->element == 'shipping') {
188 $permok = $user->hasRight('expedition', 'creer');
189 }
190 if ($object->element == 'delivery') {
191 $permok = $user->hasRight('expedition', 'delivery', 'creer');
192 }
193 if ($object->element == 'productlot') {
194 $permok = $user->hasRight('stock', 'creer');
195 }
196 if ($object->element == 'facturerec') {
197 $permok = $user->hasRight('facture', 'creer');
198 }
199 if ($object->element == 'mo') {
200 $permok = $user->hasRight('mrp', 'write');
201 }
202 if ($object->element == 'contact') {
203 $permok = $user->hasRight('societe', 'contact', 'creer');
204 }
205 if ($object->element == 'salary') {
206 $permok = $user->hasRight('salaries', 'read');
207 }
208
209 $isdraft = ((isset($object->statut) && $object->statut == 0) || (isset($object->status) && $object->status == 0));
210 if (($isdraft || !empty($extrafields->attributes[$object->table_element]['alwayseditable'][$tmpkeyextra]))
211 && $permok && $enabled != 5 && ($action != 'edit_extras' || GETPOST('attribute') != $tmpkeyextra)
212 && empty($extrafields->attributes[$object->table_element]['computed'][$tmpkeyextra])) {
213 $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
214 $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
215 if ($object->table_element == 'societe') {
216 $fieldid = 'socid';
217 }
218
219 print '<td class="right"><a class="reposition editfielda" href="'.$_SERVER['PHP_SELF'].'?'.$fieldid.'='.$valueid.'&action=edit_extras&token='.newToken().'&attribute='.$tmpkeyextra.'&ignorecollapsesetup=1">'.img_edit().'</a></td>';
220 }
221 print '</tr></table>';
222 print '</td>';
223
224 $cssview = !empty($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra]) ? ($extrafields->attributes[$object->table_element]['cssview'][$tmpkeyextra] . ' ') : '';
225 $html_id = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : '';
226
227 print '<td id="' . $html_id . '" class="valuefield ' . $cssview . $object->element . '_extras_' . $tmpkeyextra . ' wordbreakimp"' . (!empty($cols) ? ' colspan="' . $cols . '"' : '') . '>';
228
229 // Convert date into timestamp format
230 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) {
231 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
232 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
233 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
234 $datenotinstring = $db->jdate($datenotinstring);
235 }
236 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
237 $value = GETPOSTISSET("options_".$tmpkeyextra) ? dol_mktime(12, 0, 0, GETPOSTINT("options_".$tmpkeyextra."month"), GETPOSTINT("options_".$tmpkeyextra."day"), GETPOSTINT("options_".$tmpkeyextra."year")) : $datenotinstring;
238 }
239 if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) {
240 $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
241 // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
242 if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
243 $datenotinstring = $db->jdate($datenotinstring);
244 }
245 //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
246 $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;
247 }
248
249 //TODO Improve element and rights detection
250 if ($action == 'edit_extras' && $permok && GETPOST('attribute', 'restricthtml') == $tmpkeyextra) {
251 $fieldid = 'id';
252 if ($object->table_element == 'societe') {
253 $fieldid = 'socid';
254 }
255 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"] . '?' . $fieldid . '=' . $object->id . '" method="post" name="formextra">';
256 print '<input type="hidden" name="action" value="update_extras">';
257 print '<input type="hidden" name="attribute" value="'.$tmpkeyextra.'">';
258 print '<input type="hidden" name="token" value="'.newToken().'">';
259 print '<input type="hidden" name="'.$fieldid.'" value="'.$object->id.'">';
260 print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', 0, $object, $object->table_element);
261
262 print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Modify')).'">';
263
264 print '</form>';
265 } else {
266 //var_dump($tmpkeyextra.'-'.$value.'-'.$object->table_element);
267 print $extrafields->showOutputField($tmpkeyextra, $value, '', $object->table_element, null, $object);
268 }
269
270 print '</td>';
271 print '</tr>'."\n";
272 }
273 }
274
275 // Add code to manage list depending on others
276 // TODO Test/enhance this with a more generic solution
277 if (!empty($conf->use_javascript_ajax)) {
278 print "\n";
279 print '
280 <script>
281 jQuery(document).ready(function() {
282 function showOptions(child_list, parent_list)
283 {
284 var val = $("select[name="+parent_list+"]").val();
285 var parentVal = parent_list + ":" + val;
286 if(val > 0) {
287 $("select[name=\""+child_list+"\"] option[parent]").hide();
288 $("select[name=\""+child_list+"\"] option[parent=\""+parentVal+"\"]").show();
289 } else {
290 $("select[name=\""+child_list+"\"] option").show();
291 }
292 }
293 function setListDependencies() {
294 jQuery("select option[parent]").parent().each(function() {
295 var child_list = $(this).attr("name");
296 var parent = $(this).find("option[parent]:first").attr("parent");
297 var infos = parent.split(":");
298 var parent_list = infos[0];
299 showOptions(child_list, parent_list);
300
301 /* Activate the handler to call showOptions on each future change */
302 $("select[name=\""+parent_list+"\"]").change(function() {
303 showOptions(child_list, parent_list);
304 });
305 });
306 }
307 setListDependencies();
308 });
309 </script>'."\n";
310 }
311}
312?>
313<!-- END PHP TEMPLATE extrafields_view.tpl.php -->
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
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)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
img_edit($titlealt='default', $float=0, $other='')
Show logo edit/modify fiche.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79