extrafields_view.tpl.php 14 KB

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