extrafields_view.tpl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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) : $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. } else {
  102. $collapse_group = $extrafields_collapse_num.(!empty($object->id) ? '_'.$object->id : '');
  103. print '<tr class="trextrafields_collapse'.$collapse_group;
  104. /*if ($extrafields_collapse_num && $extrafields_collapse_num_old && $extrafields_collapse_num != $extrafields_collapse_num_old) {
  105. print ' trextrafields_collapse_new';
  106. }*/
  107. if ($extrafields_collapse_num && $i == count($extrafields->attributes[$object->table_element]['label'])) {
  108. print ' trextrafields_collapse_last';
  109. }
  110. print '"';
  111. if (isset($extrafields->expand_display) && empty($extrafields->expand_display[$collapse_group])) {
  112. print ' style="display: none;"';
  113. }
  114. print '>';
  115. $extrafields_collapse_num_old = $extrafields_collapse_num;
  116. print '<td class="titlefield">';
  117. print '<table class="nobordernopadding centpercent">';
  118. print '<tr>';
  119. print '<td class="';
  120. if ((!empty($action) && ($action == 'create' || $action == 'edit')) && !empty($extrafields->attributes[$object->table_element]['required'][$tmpkeyextra])) {
  121. print ' fieldrequired';
  122. }
  123. print '">';
  124. if (!empty($extrafields->attributes[$object->table_element]['help'][$tmpkeyextra])) {
  125. // You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  126. $tmptooltip = explode(':', $extrafields->attributes[$object->table_element]['help'][$tmpkeyextra]);
  127. print $form->textwithpicto($langs->trans($tmplabelextra), $langs->trans($tmptooltip[0]), 1, 'help', '', 0, 3, (empty($tmptooltip[1]) ? '' : 'extra_'.$tmpkeyextra.'_'.$tmptooltip[1]));
  128. } else {
  129. print $langs->trans($tmplabelextra);
  130. }
  131. print '</td>';
  132. //TODO Improve element and rights detection
  133. //var_dump($user->rights);
  134. $permok = false;
  135. $keyforperm = $object->element;
  136. if ($object->element == 'fichinter') {
  137. $keyforperm = 'ficheinter';
  138. }
  139. if (isset($user->rights->$keyforperm)) {
  140. $permok = !empty($user->rights->$keyforperm->creer) || !empty($user->rights->$keyforperm->create) || !empty($user->rights->$keyforperm->write);
  141. }
  142. if ($object->element == 'order_supplier') {
  143. if (empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) {
  144. $permok = $user->rights->fournisseur->commande->creer;
  145. } else {
  146. $permok = $user->rights->supplier_order->creer;
  147. }
  148. }
  149. if ($object->element == 'invoice_supplier') {
  150. if (empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) {
  151. $permok = $user->rights->fournisseur->facture->creer;
  152. } else {
  153. $permok = $user->rights->supplier_invoice->creer;
  154. }
  155. }
  156. if ($object->element == 'shipping') {
  157. $permok = $user->rights->expedition->creer;
  158. }
  159. if ($object->element == 'delivery') {
  160. $permok = $user->rights->expedition->delivery->creer;
  161. }
  162. if ($object->element == 'productlot') {
  163. $permok = $user->rights->stock->creer;
  164. }
  165. if ($object->element == 'facturerec') {
  166. $permok = $user->rights->facture->creer;
  167. }
  168. if ($object->element == 'mo') {
  169. $permok = $user->rights->mrp->write;
  170. }
  171. if ($object->element == 'contact') {
  172. $permok = $user->rights->societe->contact->creer;
  173. }
  174. if ($object->element == 'salary') {
  175. $permok = $user->rights->salaries->read;
  176. }
  177. $isdraft = ((isset($object->statut) && $object->statut == 0) || (isset($object->status) && $object->status == 0));
  178. if (($isdraft || !empty($extrafields->attributes[$object->table_element]['alwayseditable'][$tmpkeyextra]))
  179. && $permok && $enabled != 5 && ($action != 'edit_extras' || GETPOST('attribute') != $tmpkeyextra)
  180. && empty($extrafields->attributes[$object->table_element]['computed'][$tmpkeyextra])) {
  181. $fieldid = empty($forcefieldid) ? 'id' : $forcefieldid;
  182. $valueid = empty($forceobjectid) ? $object->id : $forceobjectid;
  183. if ($object->table_element == 'societe') {
  184. $fieldid = 'socid';
  185. }
  186. 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>';
  187. }
  188. print '</tr></table>';
  189. print '</td>';
  190. $html_id = !empty($object->id) ? $object->element.'_extras_'.$tmpkeyextra.'_'.$object->id : '';
  191. print '<td id="'.$html_id.'" class="valuefield '.$object->element.'_extras_'.$tmpkeyextra.' wordbreak"'.(!empty($cols) ? ' colspan="'.$cols.'"' : '').'>';
  192. // Convert date into timestamp format
  193. if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) {
  194. $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
  195. // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
  196. if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
  197. $datenotinstring = $db->jdate($datenotinstring);
  198. }
  199. //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
  200. $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;
  201. }
  202. if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) {
  203. $datenotinstring = empty($object->array_options['options_'.$tmpkeyextra]) ? '' : $object->array_options['options_'.$tmpkeyextra];
  204. // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x';
  205. if (!empty($object->array_options['options_'.$tmpkeyextra]) && !is_numeric($object->array_options['options_'.$tmpkeyextra])) { // For backward compatibility
  206. $datenotinstring = $db->jdate($datenotinstring);
  207. }
  208. //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour');
  209. $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;
  210. }
  211. //TODO Improve element and rights detection
  212. if ($action == 'edit_extras' && $permok && GETPOST('attribute', 'restricthtml') == $tmpkeyextra) {
  213. $fieldid = 'id';
  214. if ($object->table_element == 'societe') {
  215. $fieldid = 'socid';
  216. }
  217. print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"] . '?' . $fieldid . '=' . $object->id . '" method="post" name="formextra">';
  218. print '<input type="hidden" name="action" value="update_extras">';
  219. print '<input type="hidden" name="attribute" value="'.$tmpkeyextra.'">';
  220. print '<input type="hidden" name="token" value="'.newToken().'">';
  221. print '<input type="hidden" name="'.$fieldid.'" value="'.$object->id.'">';
  222. print $extrafields->showInputField($tmpkeyextra, $value, '', '', '', 0, $object->id, $object->table_element);
  223. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Modify')).'">';
  224. print '</form>';
  225. } else {
  226. //var_dump($tmpkeyextra.'-'.$value.'-'.$object->table_element);
  227. print $extrafields->showOutputField($tmpkeyextra, $value, '', $object->table_element);
  228. }
  229. print '</td>';
  230. print '</tr>'."\n";
  231. }
  232. }
  233. // Add code to manage list depending on others
  234. // TODO Test/enhance this with a more generic solution
  235. if (!empty($conf->use_javascript_ajax)) {
  236. print "\n";
  237. print '
  238. <script>
  239. jQuery(document).ready(function() {
  240. function showOptions(child_list, parent_list)
  241. {
  242. var val = $("select[name="+parent_list+"]").val();
  243. var parentVal = parent_list + ":" + val;
  244. if(val > 0) {
  245. $("select[name=\""+child_list+"\"] option[parent]").hide();
  246. $("select[name=\""+child_list+"\"] option[parent=\""+parentVal+"\"]").show();
  247. } else {
  248. $("select[name=\""+child_list+"\"] option").show();
  249. }
  250. }
  251. function setListDependencies() {
  252. jQuery("select option[parent]").parent().each(function() {
  253. var child_list = $(this).attr("name");
  254. var parent = $(this).find("option[parent]:first").attr("parent");
  255. var infos = parent.split(":");
  256. var parent_list = infos[0];
  257. showOptions(child_list, parent_list);
  258. /* Activate the handler to call showOptions on each future change */
  259. $("select[name=\""+parent_list+"\"]").change(function() {
  260. showOptions(child_list, parent_list);
  261. });
  262. });
  263. }
  264. setListDependencies();
  265. });
  266. </script>'."\n";
  267. }
  268. }
  269. ?>
  270. <!-- END PHP TEMPLATE extrafields_view.tpl.php -->