asset.lib.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /* Copyright (C) 2018-2022 OpenDSI <support@open-dsi.fr>
  3. * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/lib/asset.lib.php
  20. * \ingroup asset
  21. * \brief Library files with common functions for Assets
  22. */
  23. /**
  24. * Prepare admin pages header
  25. *
  26. * @return array head array with tabs
  27. */
  28. function assetAdminPrepareHead()
  29. {
  30. global $langs, $conf, $db;
  31. $extrafields = new ExtraFields($db);
  32. $extrafields->fetch_name_optionals_label('asset');
  33. $extrafields->fetch_name_optionals_label('asset_model');
  34. $langs->load("assets");
  35. $h = 0;
  36. $head = array();
  37. $head[$h][0] = DOL_URL_ROOT.'/asset/admin/setup.php';
  38. $head[$h][1] = $langs->trans("Settings");
  39. $head[$h][2] = 'settings';
  40. $h++;
  41. // Show more tabs from modules
  42. // Entries must be declared in modules descriptor with line
  43. //$this->tabs = array(
  44. // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  45. //); // to add new tab
  46. //$this->tabs = array(
  47. // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  48. //); // to remove a tab
  49. complete_head_from_modules($conf, $langs, null, $head, $h, 'asset_admin');
  50. $head[$h][0] = DOL_URL_ROOT.'/asset/admin/asset_extrafields.php';
  51. $head[$h][1] = $langs->trans("ExtraFields");
  52. $nbExtrafields = $extrafields->attributes['asset']['count'];
  53. if ($nbExtrafields > 0) {
  54. $head[$h][1] .= ' <span class="badge">'.$nbExtrafields.'</span>';
  55. }
  56. $head[$h][2] = 'asset_extrafields';
  57. $h++;
  58. $head[$h][0] = DOL_URL_ROOT.'/asset/admin/assetmodel_extrafields.php';
  59. $head[$h][1] = $langs->trans("ExtraFieldsAssetModel");
  60. $nbExtrafields = $extrafields->attributes['asset_model']['count'];
  61. if ($nbExtrafields > 0) {
  62. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbExtrafields.'</span>';
  63. }
  64. $head[$h][2] = 'assetmodel_extrafields';
  65. $h++;
  66. complete_head_from_modules($conf, $langs, null, $head, $h, 'asset_admin', 'remove');
  67. return $head;
  68. }
  69. /**
  70. * Prepare array of tabs for Asset
  71. *
  72. * @param Asset $object Asset
  73. * @return array Array of tabs
  74. */
  75. function assetPrepareHead(Asset $object)
  76. {
  77. global $db, $langs, $conf;
  78. $langs->load("assets", "admin");
  79. $h = 0;
  80. $head = array();
  81. $head[$h][0] = DOL_URL_ROOT . '/asset/card.php?id=' . $object->id;
  82. $head[$h][1] = $langs->trans("Card");
  83. $head[$h][2] = 'card';
  84. $h++;
  85. if (empty($object->not_depreciated)) {
  86. $head[$h][0] = DOL_URL_ROOT . '/asset/depreciation_options.php?id=' . $object->id;
  87. $head[$h][1] = $langs->trans("AssetDepreciationOptions");
  88. $head[$h][2] = 'depreciation_options';
  89. $h++;
  90. }
  91. $head[$h][0] = DOL_URL_ROOT . '/asset/accountancy_codes.php?id=' . $object->id;
  92. $head[$h][1] = $langs->trans("AssetAccountancyCodes");
  93. $head[$h][2] = 'accountancy_codes';
  94. $h++;
  95. if (empty($object->not_depreciated)) {
  96. $head[$h][0] = DOL_URL_ROOT . '/asset/depreciation.php?id=' . $object->id;
  97. $head[$h][1] = $langs->trans("AssetDepreciation");
  98. $head[$h][2] = 'depreciation';
  99. $h++;
  100. }
  101. if (isset($object->disposal_date) && $object->disposal_date !== "") {
  102. $head[$h][0] = DOL_URL_ROOT . '/asset/disposal.php?id=' . $object->id;
  103. $head[$h][1] = $langs->trans("AssetDisposal");
  104. $head[$h][2] = 'disposal';
  105. $h++;
  106. }
  107. if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) {
  108. $nbNote = 0;
  109. if (!empty($object->note_private)) {
  110. $nbNote++;
  111. }
  112. if (!empty($object->note_public)) {
  113. $nbNote++;
  114. }
  115. $head[$h][0] = DOL_URL_ROOT . '/asset/note.php?id=' . $object->id;
  116. $head[$h][1] = $langs->trans('Notes');
  117. if ($nbNote > 0) {
  118. $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">' . $nbNote . '</span>' : '');
  119. }
  120. $head[$h][2] = 'note';
  121. $h++;
  122. }
  123. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  124. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  125. $upload_dir = $conf->asset->dir_output . "/asset/" . dol_sanitizeFileName($object->ref);
  126. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  127. $nbLinks = Link::count($db, $object->element, $object->id);
  128. $head[$h][0] = DOL_URL_ROOT . '/asset/document.php?id=' . $object->id;
  129. $head[$h][1] = $langs->trans('Documents');
  130. if (($nbFiles + $nbLinks) > 0) {
  131. $head[$h][1] .= '<span class="badge marginleftonlyshort">' . ($nbFiles + $nbLinks) . '</span>';
  132. }
  133. $head[$h][2] = 'document';
  134. $h++;
  135. $head[$h][0] = DOL_URL_ROOT . '/asset/agenda.php?id=' . $object->id;
  136. $head[$h][1] = $langs->trans("Events");
  137. $head[$h][2] = 'agenda';
  138. $h++;
  139. // Show more tabs from modules
  140. // Entries must be declared in modules descriptor with line
  141. //$this->tabs = array(
  142. // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  143. //); // to add new tab
  144. //$this->tabs = array(
  145. // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  146. //); // to remove a tab
  147. complete_head_from_modules($conf, $langs, $object, $head, $h, 'asset');
  148. complete_head_from_modules($conf, $langs, $object, $head, $h, 'asset', 'remove');
  149. return $head;
  150. }
  151. /**
  152. * Prepare array of tabs for AssetModel
  153. *
  154. * @param AssetModel $object AssetModel
  155. * @return array Array of tabs
  156. */
  157. function assetModelPrepareHead($object)
  158. {
  159. global $langs, $conf;
  160. $langs->load("assets", "admin");
  161. $h = 0;
  162. $head = array();
  163. $head[$h][0] = DOL_URL_ROOT . '/asset/model/card.php?id=' . $object->id;
  164. $head[$h][1] = $langs->trans("Card");
  165. $head[$h][2] = 'card';
  166. $h++;
  167. $head[$h][0] = DOL_URL_ROOT . '/asset/model/depreciation_options.php?id=' . $object->id;
  168. $head[$h][1] = $langs->trans("AssetDepreciationOptions");
  169. $head[$h][2] = 'depreciation_options';
  170. $h++;
  171. $head[$h][0] = DOL_URL_ROOT . '/asset/model/accountancy_codes.php?id=' . $object->id;
  172. $head[$h][1] = $langs->trans("AssetAccountancyCodes");
  173. $head[$h][2] = 'accountancy_codes';
  174. $h++;
  175. if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) {
  176. $nbNote = 0;
  177. if (!empty($object->note_private)) {
  178. $nbNote++;
  179. }
  180. if (!empty($object->note_public)) {
  181. $nbNote++;
  182. }
  183. $head[$h][0] = DOL_URL_ROOT . '/asset/model/note.php?id=' . $object->id;
  184. $head[$h][1] = $langs->trans('Notes');
  185. if ($nbNote > 0) {
  186. $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">' . $nbNote . '</span>' : '');
  187. }
  188. $head[$h][2] = 'note';
  189. $h++;
  190. }
  191. $head[$h][0] = DOL_URL_ROOT . '/asset/model/agenda.php?id=' . $object->id;
  192. $head[$h][1] = $langs->trans("Events");
  193. $head[$h][2] = 'agenda';
  194. $h++;
  195. // Show more tabs from modules
  196. // Entries must be declared in modules descriptor with line
  197. //$this->tabs = array(
  198. // 'entity:+tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  199. //); // to add new tab
  200. //$this->tabs = array(
  201. // 'entity:-tabname:Title:@asset:/asset/mypage.php?id=__ID__'
  202. //); // to remove a tab
  203. complete_head_from_modules($conf, $langs, $object, $head, $h, 'assetmodel');
  204. complete_head_from_modules($conf, $langs, $object, $head, $h, 'assetmodel', 'remove');
  205. return $head;
  206. }