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