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