depreciation_options_edit.tpl.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* Copyright (C) 2021 Open-Dsi <support@open-dsi.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. *
  17. * Show extrafields. It also show fields from hook formAssetAccountancyCode. Need to have following variables defined:
  18. * $object (asset, assetmodel, ...)
  19. * $assetaccountancycodes
  20. * $action
  21. * $conf
  22. * $langs
  23. *
  24. * $parameters
  25. */
  26. // Protection to avoid direct call of template
  27. if (empty($object) || !is_object($object)) {
  28. print "Error, template page can't be called as URL";
  29. exit;
  30. }
  31. if (!is_object($form)) {
  32. $form = new Form($db);
  33. }
  34. if (!is_object($formadmin)) {
  35. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formadmin.class.php';
  36. $formadmin = new FormAdmin($db);
  37. }
  38. ?>
  39. <!-- BEGIN PHP TEMPLATE depreciation_options_edit.tpl.php -->
  40. <?php
  41. if (!is_array($parameters)) {
  42. $parameters = array();
  43. }
  44. $enabled_field_info = array();
  45. if (empty($parameters['enabled_field_info'])) {
  46. $parameters['enabled_field_info'] = &$enabled_field_info;
  47. }
  48. if (empty($parameters['assetdepreciationoptions'])) {
  49. $parameters['assetdepreciationoptions'] = &$assetdepreciationoptions;
  50. }
  51. $reshook = $hookmanager->executeHooks('formAssetDeprecationOptions', $parameters, $object, $action);
  52. print $hookmanager->resPrint;
  53. if ($reshook < 0) {
  54. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  55. }
  56. if (empty($reshook)) {
  57. $class_type = get_class($object) == 'Asset' ? 0 : 1;
  58. foreach ($assetdepreciationoptions->deprecation_options_fields as $mode_key => $mode_info) {
  59. if (!empty($mode_info['enabled_field'])) {
  60. $info = explode(':', $mode_info['enabled_field']);
  61. $enabled_field_info[] = array(
  62. 'mode_key' => $info[0],
  63. 'field_key' => $info[1],
  64. 'value' => $info[2],
  65. 'target' => 'block_' . $mode_key,
  66. );
  67. }
  68. $assetdepreciationoptions->setInfosForMode($mode_key, $class_type, true);
  69. $prefix_html_name = $mode_key . '_';
  70. print '<div id="block_' . $mode_key . '">';
  71. print load_fiche_titre($langs->trans($mode_info['label']), '', '');
  72. print '<div class="fichecenter">';
  73. print '<div class="fichehalfleft">';
  74. print '<div class="underbanner clearboth"></div>';
  75. print '<table class="border centpercent tableforfield">' . "\n";
  76. $mode_info['fields'] = dol_sort_array($mode_info['fields'], 'position');
  77. foreach ($mode_info['fields'] as $field_key => $field_info) {
  78. // Discard if extrafield is a hidden field on form
  79. if (abs($field_info['visible']) != 1 && abs($field_info['visible']) != 3 && abs($field_info['visible']) != 4) {
  80. continue;
  81. }
  82. if (array_key_exists('enabled', $field_info) && isset($field_info['enabled']) && !verifCond($field_info['enabled'])) {
  83. continue; // We don't want this field
  84. }
  85. if (!empty($field_info['column_break'])) {
  86. print '</table>';
  87. // We close div and reopen for second column
  88. print '</div>';
  89. print '<div class="fichehalfright">';
  90. print '<div class="underbanner clearboth"></div>';
  91. print '<table class="border centpercent tableforfield">';
  92. }
  93. $html_name = $prefix_html_name . $field_key;
  94. if (!empty($field_info['enabled_field'])) {
  95. $info = explode(':', $field_info['enabled_field']);
  96. $enabled_field_info[] = array(
  97. 'mode_key' => $info[0],
  98. 'field_key' => $info[1],
  99. 'value' => $info[2],
  100. 'target' => 'field_' . $html_name,
  101. );
  102. }
  103. $more_class = '';
  104. if (!empty($field_info['required']) || (isset($field_info['notnull']) && $field_info['notnull'] > 0)) {
  105. $more_class .= ' fieldrequired';
  106. }
  107. if (preg_match('/^(text|html)/', $val['type'])) {
  108. $more_class .= ' tdtop';
  109. }
  110. print '<tr class="field_' . $html_name . '" id="field_' . $html_name . '"><td';
  111. print ' class="titlefieldcreate' . $more_class . '">';
  112. if (!empty($field_info['help'])) {
  113. print $form->textwithpicto($langs->trans($field_info['label']), $langs->trans($field_info['help']));
  114. } else {
  115. print $langs->trans($field_info['label']);
  116. }
  117. print '</td>';
  118. print '<td class="valuefieldcreate">';
  119. if (!empty($field_info['picto'])) {
  120. print img_picto('', $field_info['picto'], '', false, 0, 0, '', 'pictofixedwidth');
  121. }
  122. if (in_array($field_info['type'], array('int', 'integer'))) {
  123. $value = GETPOSTISSET($html_name) ?GETPOST($html_name, 'int') : $assetdepreciationoptions->$field_key;
  124. } elseif ($field_info['type'] == 'double') {
  125. $value = GETPOSTISSET($html_name) ? price2num(GETPOST($html_name, 'alphanohtml')) : $assetdepreciationoptions->$field_key;
  126. } elseif (preg_match('/^(text|html)/', $field_info['type'])) {
  127. $tmparray = explode(':', $field_info['type']);
  128. if (!empty($tmparray[1])) {
  129. $check = $tmparray[1];
  130. } else {
  131. $check = 'restricthtml';
  132. }
  133. $value = GETPOSTISSET($html_name) ? GETPOST($html_name, $check) : $assetdepreciationoptions->$field_key;
  134. } elseif ($field_info['type'] == 'price') {
  135. $value = GETPOSTISSET($html_name) ? price2num(GETPOST($html_name)) : ($assetdepreciationoptions->$field_key ? price2num($assetdepreciationoptions->$field_key) : (!empty($field_info['default']) ? dol_eval($field_info['default'], 1) : 0));
  136. } elseif ($field_key == 'lang') {
  137. $value = GETPOSTISSET($html_name) ? GETPOST($html_name, 'aZ09') : $assetdepreciationoptions->lang;
  138. } else {
  139. $value = GETPOSTISSET($html_name) ? GETPOST($html_name, 'alpha') : $assetdepreciationoptions->$field_key;
  140. }
  141. if (!empty($field_info['noteditable'])) {
  142. print $assetdepreciationoptions->showOutputField($field_info, $field_key, $value, '', '', $prefix_html_name, 0);
  143. } else {
  144. if ($field_key == 'lang') {
  145. print img_picto('', 'language', 'class="pictofixedwidth"');
  146. print $formadmin->select_language($value, $html_name, 0, null, 1, 0, 0, 'minwidth300', 2);
  147. } else {
  148. print $assetdepreciationoptions->showInputField($field_info, $field_key, $value, '', '', $prefix_html_name, 0);
  149. }
  150. }
  151. print '</td>';
  152. print '</tr>';
  153. }
  154. print '</table>';
  155. print '</div>';
  156. print '</div>';
  157. print '<div class="clearboth"></div>';
  158. print '</div>';
  159. }
  160. }
  161. if (!empty($enabled_field_info)) {
  162. $enabled_field_info = json_encode($enabled_field_info);
  163. print <<<SCRIPT
  164. <script type="text/javascript">
  165. jQuery(document).ready(function () {
  166. var enabled_field_info = $enabled_field_info;
  167. // Init fields
  168. enabled_field_info.map(function(info) {
  169. var html_name = info.mode_key + '_' + info.field_key;
  170. var source = $('#' + html_name);
  171. if (!(source.length > 0)) source = $('[name="' + html_name + '"]');
  172. if (source.length > 0) {
  173. source.attr('data-asset-enabled-field-value', info.value);
  174. source.attr('data-asset-enabled-field-target', info.target);
  175. updateEnabledField(source);
  176. source.on('change click', function() {
  177. updateEnabledField(jQuery(this));
  178. });
  179. }
  180. });
  181. function updateEnabledField(_this) {
  182. var value = _this.attr('data-asset-enabled-field-value');
  183. var target_name = _this.attr('data-asset-enabled-field-target');
  184. // for block mode
  185. var target = $('div#' + target_name);
  186. // for field
  187. if (!(target.length > 0)) {
  188. target = $('#' + target_name);
  189. if (!(target.length > 0)) target = $('[name="' + target_name + '"]');
  190. if (target.length > 0) target = target.closest('tr');
  191. }
  192. if (target.length > 0) {
  193. var source_value = _this.attr('type') == 'checkbox' ? (_this.is(':checked') ? 1 : 0) : _this.val();
  194. if (source_value != value) {
  195. target.hide();
  196. } else {
  197. target.show();
  198. }
  199. }
  200. }
  201. });
  202. </script>
  203. SCRIPT;
  204. }
  205. ?>
  206. <!-- END PHP TEMPLATE depreciation_options_edit.tpl.php -->