modAsset.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /* Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.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. * \defgroup asset Module Assets
  20. * \brief Asset module descriptor.
  21. *
  22. * \file htdocs/core/modules/modAsset.class.php
  23. * \ingroup asset
  24. * \brief Description and activation file for the module assets
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  27. /**
  28. * Description and activation class for module FixedAssets
  29. */
  30. class modAsset extends DolibarrModules
  31. {
  32. /**
  33. * Constructor. Define names, constants, directories, boxes, permissions
  34. *
  35. * @param DoliDB $db Database handler
  36. */
  37. public function __construct($db)
  38. {
  39. global $langs, $conf;
  40. $this->db = $db;
  41. // Id for module (must be unique).
  42. // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id).
  43. $this->numero = 51000; // TODO Go on page https://wiki.dolibarr.org/index.php/List_of_modules_id to reserve id number for your module
  44. // Family can be 'crm','financial','hr','projects','products','ecm','technic','interface','other'
  45. // It is used to group modules by family in module setup page
  46. $this->family = "financial";
  47. // Module position in the family on 2 digits ('01', '10', '20', ...)
  48. $this->module_position = '70';
  49. // Gives the possibility to the module, to provide his own family info and position of this family (Overwrite $this->family and $this->module_position. Avoid this)
  50. //$this->familyinfo = array('myownfamily' => array('position' => '01', 'label' => $langs->trans("MyOwnFamily")));
  51. // Module label (no space allowed), used if translation string 'ModuleAssetsName' not found (MyModue is name of module).
  52. $this->name = preg_replace('/^mod/i', '', get_class($this));
  53. // Module description, used if translation string 'ModuleAssetsDesc' not found (MyModue is name of module).
  54. $this->description = "Asset module";
  55. // Used only if file README.md and README-LL.md not found.
  56. $this->descriptionlong = "Asset module to manage assets module and depreciation charge on Dolibarr";
  57. // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated' or a version string like 'x.y.z'
  58. $this->version = 'development';
  59. // Key used in llx_const table to save module status enabled/disabled (where ASSETS is value of property name of module in uppercase)
  60. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  61. // Name of image file used for this module.
  62. // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
  63. // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
  64. $this->picto = 'asset';
  65. // Defined all module parts (triggers, login, substitutions, menus, css, etc...)
  66. // for default path (eg: /asset/core/xxxxx) (0=disable, 1=enable)
  67. // for specific path of parts (eg: /asset/core/modules/barcode)
  68. // for specific css file (eg: /asset/css/assets.css.php)
  69. $this->module_parts = array();
  70. // Data directories to create when module is enabled.
  71. // Example: this->dirs = array("/asset/temp","/asset/subdir");
  72. $this->dirs = array("/asset/temp");
  73. // Config pages. Put here list of php page, stored into asset/admin directory, to use to setup module.
  74. $this->config_page_url = array("setup.php@asset");
  75. // Dependencies
  76. $this->hidden = false; // A condition to hide module
  77. $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
  78. $this->requiredby = array(); // List of module ids to disable if this one is disabled
  79. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  80. $this->langfiles = array("assets");
  81. $this->phpmin = array(5, 6); // Minimum version of PHP required by module
  82. $this->need_dolibarr_version = array(7, 0); // Minimum version of Dolibarr required by module
  83. $this->warnings_activation = array(); // Warning to show when we activate module. array('always'='text') or array('FR'='textfr','ES'='textes'...)
  84. $this->warnings_activation_ext = array(); // Warning to show when we activate an external module. array('always'='text') or array('FR'='textfr','ES'='textes'...)
  85. //$this->automatic_activation = array('FR'=>'AssetsWasAutomaticallyActivatedBecauseOfYourCountryChoice');
  86. //$this->always_enabled = true; // If true, can't be disabled
  87. // Constants
  88. // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  89. // Example: $this->const=array(0=>array('ASSETS_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
  90. // 1=>array('ASSETS_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
  91. // );
  92. $this->const = array();
  93. $this->const[1] = array(
  94. "ASSET_DEPRECIATION_DURATION_PER_YEAR",
  95. "chaine",
  96. "365",
  97. "Duration per year to calculate depreciation. In some case, can be 360 days",
  98. 0,
  99. 'current',
  100. 1
  101. );
  102. if (!isset($conf->asset) || !isset($conf->asset->enabled)) {
  103. $conf->asset = new stdClass();
  104. $conf->asset->enabled = 0;
  105. }
  106. // Array to add new pages in new tabs
  107. $this->tabs = array();
  108. // Example:
  109. // $this->tabs[] = array('data'=>'objecttype:+tabname1:Title1:mylangfile@assets:$user->rights->assets->read:/asset/mynewtab1.php?id=__ID__'); // To add a new tab identified by code tabname1
  110. // $this->tabs[] = array('data'=>'objecttype:+tabname2:SUBSTITUTION_Title2:mylangfile@assets:$user->rights->othermodule->read:/asset/mynewtab2.php?id=__ID__', // To add another new tab identified by code tabname2. Label will be result of calling all substitution functions on 'Title2' key.
  111. // $this->tabs[] = array('data'=>'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname
  112. //
  113. // Where objecttype can be
  114. // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
  115. // 'contact' to add a tab in contact view
  116. // 'contract' to add a tab in contract view
  117. // 'group' to add a tab in group view
  118. // 'intervention' to add a tab in intervention view
  119. // 'invoice' to add a tab in customer invoice view
  120. // 'invoice_supplier' to add a tab in supplier invoice view
  121. // 'member' to add a tab in fundation member view
  122. // 'opensurveypoll' to add a tab in opensurvey poll view
  123. // 'order' to add a tab in customer order view
  124. // 'order_supplier' to add a tab in supplier order view
  125. // 'payment' to add a tab in payment view
  126. // 'payment_supplier' to add a tab in supplier payment view
  127. // 'product' to add a tab in product view
  128. // 'propal' to add a tab in propal view
  129. // 'project' to add a tab in project view
  130. // 'stock' to add a tab in stock view
  131. // 'thirdparty' to add a tab in third party view
  132. // 'user' to add a tab in user view
  133. // Dictionaries
  134. $this->dictionaries = array();
  135. // Boxes/Widgets
  136. // Add here list of php file(s) stored in asset/core/boxes that contains class to show a widget.
  137. $this->boxes = array(
  138. //0=>array('file'=>'assetswidget1.php@asset','note'=>'Widget provided by Assets','enabledbydefaulton'=>'Home'),
  139. //1=>array('file'=>'assetswidget2.php@asset','note'=>'Widget provided by Assets'),
  140. //2=>array('file'=>'assetswidget3.php@asset','note'=>'Widget provided by Assets')
  141. );
  142. // Cronjobs (List of cron jobs entries to add when module is enabled)
  143. // unit_frequency must be 60 for minute, 3600 for hour, 86400 for day, 604800 for week
  144. //$this->cronjobs = array(
  145. // 0=>array('label'=>'MyJob label', 'jobtype'=>'method', 'class'=>'/asset/class/asset.class.php', 'objectname'=>'Asset', 'method'=>'doScheduledJob', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true)
  146. //);
  147. // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true),
  148. // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'status'=>0, 'test'=>true)
  149. // );
  150. // Permissions
  151. $this->rights = array(); // Permission array used by this module
  152. $this->rights_class = 'asset';
  153. $r = 0;
  154. $r++;
  155. $this->rights[$r][0] = 51001; // Permission id (must not be already used)
  156. $this->rights[$r][1] = 'Read assets'; // Permission label
  157. $this->rights[$r][2] = 'r';
  158. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  159. $this->rights[$r][4] = 'read'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  160. $this->rights[$r][5] = ''; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  161. $r++;
  162. $this->rights[$r][0] = 51002; // Permission id (must not be already used)
  163. $this->rights[$r][1] = 'Create/Update assets'; // Permission label
  164. $this->rights[$r][2] = 'w';
  165. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  166. $this->rights[$r][4] = 'write'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  167. $this->rights[$r][5] = ''; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  168. $r++;
  169. $this->rights[$r][0] = 51003; // Permission id (must not be already used)
  170. $this->rights[$r][1] = 'Delete assets'; // Permission label
  171. $this->rights[$r][2] = 'd';
  172. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  173. $this->rights[$r][4] = 'delete'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  174. $this->rights[$r][5] = ''; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  175. $r++;
  176. $this->rights[$r][0] = 51004; // Permission id (must not be already used)
  177. $this->rights[$r][1] = 'Read asset models'; // Permission label
  178. $this->rights[$r][2] = 'r';
  179. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  180. $this->rights[$r][4] = 'model_advance'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  181. $this->rights[$r][5] = 'read'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  182. $r++;
  183. $this->rights[$r][0] = 51005; // Permission id (must not be already used)
  184. $this->rights[$r][1] = 'Create/Update asset models'; // Permission label
  185. $this->rights[$r][2] = 'w';
  186. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  187. $this->rights[$r][4] = 'model_advance'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  188. $this->rights[$r][5] = 'write'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  189. $r++;
  190. $this->rights[$r][0] = 51006; // Permission id (must not be already used)
  191. $this->rights[$r][1] = 'Delete asset models'; // Permission label
  192. $this->rights[$r][2] = 'd';
  193. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  194. $this->rights[$r][4] = 'model_advance'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  195. $this->rights[$r][5] = 'delete'; // In php code, permission will be checked by test if ($user->rights->asset->level1->level2)
  196. // Menus
  197. //-------
  198. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  199. }
  200. /**
  201. * Function called when module is enabled.
  202. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  203. * It also creates data directories
  204. *
  205. * @param string $options Options when enabling module ('', 'noboxes')
  206. * @return int 1 if OK, 0 if KO
  207. */
  208. public function init($options = '')
  209. {
  210. $result = $this->_load_tables('/install/mysql/', 'asset');
  211. if ($result < 0) {
  212. return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
  213. }
  214. // Permissions
  215. $this->remove($options);
  216. $sql = array();
  217. return $this->_init($sql, $options);
  218. }
  219. }