PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/modResource.class.php

http://github.com/Dolibarr/dolibarr
PHP | 321 lines | 150 code | 37 blank | 134 comment | 2 complexity | 3b9288891aadfa5abe009758029a8357 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /* Copyright (C) 2013-2014 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  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. * Module to manage resources into Dolibarr ERP/CRM
  19. */
  20. /**
  21. * \defgroup resource Module resource
  22. * \brief Resource module descriptor.
  23. * \file core/modules/modResource.class.php
  24. * \ingroup resource
  25. * \brief Description and activation file for the module Resource
  26. */
  27. include_once DOL_DOCUMENT_ROOT."/core/modules/DolibarrModules.class.php";
  28. /**
  29. * Description and activation class for module Resource
  30. */
  31. class modResource extends DolibarrModules
  32. {
  33. /**
  34. * Constructor. Define names, constants, directories, boxes, permissions
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. public function __construct($db)
  39. {
  40. global $langs, $conf;
  41. $this->db = $db;
  42. // Id for module (must be unique).
  43. // Use a free id here
  44. // (See in Home -> System information -> Dolibarr for list of used modules id).
  45. $this->numero = 63000;
  46. // Key text used to identify module (for permissions, menus, etc...)
  47. $this->rights_class = 'resource';
  48. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  49. // It is used to group modules in module setup page
  50. $this->family = "projects";
  51. $this->module_position = '16';
  52. // Module label (no space allowed)
  53. // used if translation string 'ModuleXXXName' not found
  54. // (where XXX is value of numeric property 'numero' of module)
  55. $this->name = preg_replace('/^mod/i', '', get_class($this));
  56. // Module description
  57. // used if translation string 'ModuleXXXDesc' not found
  58. // (where XXX is value of numeric property 'numero' of module)
  59. $this->description = "Manage resources (printers, cars, room, ...) you can then share into events";
  60. // Possible values for version are: 'development', 'experimental' or version
  61. $this->version = 'dolibarr';
  62. // Key used in llx_const table to save module status enabled/disabled
  63. // (where MYMODULE is value of property name of module in uppercase)
  64. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  65. // Name of image file used for this module.
  66. // If file is in theme/yourtheme/img directory under name object_pictovalue.png
  67. // use this->picto='pictovalue'
  68. // If file is in module/img directory under name object_pictovalue.png
  69. // use this->picto='pictovalue@module'
  70. $this->picto = 'resource'; // mypicto@resource
  71. // Defined all module parts (triggers, login, substitutions, menus, css, etc...)
  72. // for default path (eg: /resource/core/xxxxx) (0=disable, 1=enable)
  73. // for specific path of parts (eg: /resource/core/modules/barcode)
  74. // for specific css file (eg: /resource/css/resource.css.php)
  75. $this->module_parts = array();
  76. // Data directories to create when module is enabled.
  77. // Example: this->dirs = array("/resource/temp");
  78. //$this->dirs = array("/resource");
  79. // Config pages. Put here list of php pages
  80. // stored into resource/admin directory, used to setup module.
  81. $this->config_page_url = array("resource.php");
  82. // Dependencies
  83. // List of modules id that must be enabled if this module is enabled
  84. $this->depends = array();
  85. // List of modules id to disable if this one is disabled
  86. $this->requiredby = array('modPlace');
  87. // Minimum version of PHP required by module
  88. $this->phpmin = array(5, 6);
  89. $this->langfiles = array("resource"); // langfiles@resource
  90. // Constants
  91. // List of particular constants to add when module is enabled
  92. // (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  93. // Example:
  94. $this->const = array();
  95. // Array to add new pages in new tabs
  96. // Example:
  97. $this->tabs = array(
  98. // // To add a new tab identified by code tabname1
  99. // 'objecttype:+tabname1:Title1:langfile@resource:$user->rights->resource->read:/resource/mynewtab1.php?id=__ID__',
  100. // // To add another new tab identified by code tabname2
  101. // 'objecttype:+tabname2:Title2:langfile@resource:$user->rights->othermodule->read:/resource/mynewtab2.php?id=__ID__',
  102. // // To remove an existing tab identified by code tabname
  103. // 'objecttype:-tabname'
  104. );
  105. // where objecttype can be
  106. // 'thirdparty' to add a tab in third party view
  107. // 'intervention' to add a tab in intervention view
  108. // 'order_supplier' to add a tab in supplier order view
  109. // 'invoice_supplier' to add a tab in supplier invoice view
  110. // 'invoice' to add a tab in customer invoice view
  111. // 'order' to add a tab in customer order view
  112. // 'product' to add a tab in product view
  113. // 'stock' to add a tab in stock view
  114. // 'propal' to add a tab in propal view
  115. // 'member' to add a tab in fundation member view
  116. // 'contract' to add a tab in contract view
  117. // 'user' to add a tab in user view
  118. // 'group' to add a tab in group view
  119. // 'contact' to add a tab in contact view
  120. // 'categories_x' to add a tab in category view
  121. // (reresource 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
  122. // Boxes
  123. // Add here list of php file(s) stored in core/boxes that contains class to show a box.
  124. $this->boxes = array(); // Boxes list
  125. $r = 0;
  126. // Example:
  127. //$this->boxes[$r][1] = "MyBox@resource";
  128. //$r ++;
  129. /*
  130. $this->boxes[$r][1] = "myboxb.php";
  131. $r++;
  132. */
  133. // Permissions
  134. $this->rights = array(); // Permission array used by this module
  135. $r = 0;
  136. $this->rights[$r][0] = 63001;
  137. $this->rights[$r][1] = 'Read resources';
  138. $this->rights[$r][3] = 0;
  139. $this->rights[$r][4] = 'read';
  140. $r++;
  141. $this->rights[$r][0] = 63002;
  142. $this->rights[$r][1] = 'Create/Modify resources';
  143. $this->rights[$r][3] = 0;
  144. $this->rights[$r][4] = 'write';
  145. $r++;
  146. $this->rights[$r][0] = 63003;
  147. $this->rights[$r][1] = 'Delete resources';
  148. $this->rights[$r][3] = 0;
  149. $this->rights[$r][4] = 'delete';
  150. $r++;
  151. $this->rights[$r][0] = 63004;
  152. $this->rights[$r][1] = 'Link resources to agenda events';
  153. $this->rights[$r][3] = 0;
  154. $this->rights[$r][4] = 'link';
  155. $r++;
  156. // Menus
  157. //-------
  158. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  159. // Main menu entries
  160. $this->menu = array(); // List of menus to add
  161. $r = 0;
  162. // Menus declaration
  163. $this->menu[$r] = array(
  164. 'fk_menu'=>'fk_mainmenu=tools',
  165. 'type'=>'left',
  166. 'titre'=> 'MenuResourceIndex',
  167. 'prefix' => img_picto('', $this->picto, 'class="paddingright pictofixedwidth em92"'),
  168. 'mainmenu'=>'tools',
  169. 'leftmenu'=> 'resource',
  170. 'url'=> '/resource/list.php',
  171. 'langs'=> 'resource',
  172. 'position'=> 100,
  173. 'enabled'=> '1',
  174. 'perms'=> '$user->rights->resource->read',
  175. 'user'=> 0
  176. );
  177. $r++;
  178. $this->menu[$r++] = array(
  179. 'fk_menu'=>'fk_mainmenu=tools,fk_leftmenu=resource', //On utilise les ancres définis dans le menu parent déclaré au dessus
  180. 'type'=> 'left', // Toujours un menu gauche
  181. 'titre'=> 'MenuResourceAdd',
  182. 'mainmenu'=> 'tools',
  183. 'leftmenu'=> 'resource_add',
  184. 'url'=> '/resource/card.php?action=create',
  185. 'langs'=> 'resource',
  186. 'position'=> 101,
  187. 'enabled'=> '1',
  188. 'perms'=> '$user->rights->resource->write',
  189. 'target'=> '',
  190. 'user'=> 0
  191. );
  192. $this->menu[$r++] = array(
  193. 'fk_menu'=>'fk_mainmenu=tools,fk_leftmenu=resource', //On utilise les ancres définis dans le menu parent déclaré au dessus
  194. 'type'=> 'left', // Toujours un menu gauche
  195. 'titre'=> 'List',
  196. 'mainmenu'=> 'tools',
  197. 'leftmenu'=> 'resource_list',
  198. 'url'=> '/resource/list.php',
  199. 'langs'=> 'resource',
  200. 'position'=> 102,
  201. 'enabled'=> '1',
  202. 'perms'=> '$user->rights->resource->read',
  203. 'target'=> '',
  204. 'user'=> 0
  205. );
  206. // Exports
  207. //--------
  208. $r = 0;
  209. $r++;
  210. $this->export_code[$r] = $this->rights_class.'_'.$r;
  211. $this->export_label[$r] = "ResourceSingular"; // Translation key (used only if key ExportDataset_xxx_z not found)
  212. $this->export_permission[$r] = array(array("resource", "read"));
  213. $this->export_fields_array[$r] = array('r.rowid'=>'IdResource', 'r.ref'=>'ResourceFormLabel_ref', 'c.code'=>'ResourceTypeCode', 'c.label'=>'ResourceType', 'r.description'=>'ResourceFormLabel_description', 'r.note_private'=>"NotePrivate", 'r.note_public'=>"NotePublic", 'r.asset_number'=>'AssetNumber', 'r.datec'=>"DateCreation", 'r.tms'=>"DateLastModification");
  214. $this->export_TypeFields_array[$r] = array('r.rowid'=>'List:resource:ref', 'r.ref'=>'Text', 'r.asset_number'=>'Text', 'r.description'=>'Text', 'c.code'=>'Text', 'c.label'=>'List:c_type_resource:label', 'r.datec'=>'Date', 'r.tms'=>'Date', 'r.note_private'=>'Text', 'r.note_public'=>'Text');
  215. $this->export_entities_array[$r] = array('r.rowid'=>'resource', 'r.ref'=>'resource', 'c.code'=>'resource', 'c.label'=>'resource', 'r.description'=>'resource', 'r.note_private'=>"resource", 'r.resource'=>"resource", 'r.asset_number'=>'resource', 'r.datec'=>"resource", 'r.tms'=>"resource");
  216. $keyforselect = 'resource';
  217. $keyforelement = 'resource';
  218. $keyforaliasextra = 'extra';
  219. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  220. $this->export_dependencies_array[$r] = array('resource'=>array('r.rowid')); // We must keep this until the aggregate_array is used. To add unique key if we ask a field of a child to avoid the DISTINCT to discard them.
  221. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  222. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'resource as r';
  223. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_resource as c ON c.rowid=r.fk_code_type_resource';
  224. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'resource_extrafields as extra ON extra.fk_object = r.rowid';
  225. $this->export_sql_end[$r] .= ' AND r.entity IN ('.getEntity('resource').')';
  226. // Imports
  227. //--------
  228. $r = 0;
  229. // Import list of third parties and attributes
  230. $r++;
  231. $this->import_code[$r] = $this->rights_class.'_'.$r;
  232. $this->import_label[$r] = 'ImportDataset_resource_1';
  233. $this->import_icon[$r] = 'resource';
  234. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  235. $this->import_tables_array[$r] = array('r'=>MAIN_DB_PREFIX.'resource', 'extra'=>MAIN_DB_PREFIX.'resource_extrafields'); // List of tables to insert into (insert done in same order)
  236. $this->import_fields_array[$r] = array('r.ref'=>"ResourceFormLabel_ref*", 'r.fk_code_type_resource'=>'ResourceTypeCode', 'r.description'=>'ResourceFormLabel_description', 'r.note_private'=>"NotePrivate", 'r.note_public'=>"NotePublic", 'r.asset_number'=>'AssetNumber', 'r.datec'=>'DateCreation');
  237. // Add extra fields
  238. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'resource' AND entity IN (0,".$conf->entity.")";
  239. $resql = $this->db->query($sql);
  240. if ($resql) { // This can fail when class is used on old database (during migration for example)
  241. while ($obj = $this->db->fetch_object($resql)) {
  242. $fieldname = 'extra.'.$obj->name;
  243. $fieldlabel = ucfirst($obj->label);
  244. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  245. }
  246. }
  247. // End add extra fields
  248. $this->import_fieldshidden_array[$r] = array('r.fk_user_author'=>'user->id', 'extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'resource'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  249. $this->import_convertvalue_array[$r] = array(
  250. 'r.fk_code_type_resource'=>array('rule'=>'fetchidfromcodeorlabel', 'classfile'=>'/core/class/ctyperesource.class.php', 'class'=>'Ctyperesource', 'method'=>'fetch', 'dict'=>'DictionaryResourceType'),
  251. );
  252. //$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
  253. $this->import_regex_array[$r] = array('s.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$');
  254. $this->import_examplevalues_array[$r] = array('r.ref'=>"REF1", 'r.fk_code_type_resource'=>"Code from dictionary resource type", 'r.datec'=>"2017-01-01 or 2017-01-01 12:30:00");
  255. $this->import_updatekeys_array[$r] = array('r.rf'=>'ResourceFormLabel_ref');
  256. }
  257. /**
  258. * Function called when module is enabled.
  259. * The init function add constants, boxes, permissions and menus
  260. * (defined in constructor) into Dolibarr database.
  261. * It also creates data directories
  262. *
  263. * @param string $options Options when enabling module ('', 'noboxes')
  264. * @return int 1 if OK, 0 if KO
  265. */
  266. public function init($options = '')
  267. {
  268. $sql = array();
  269. $result = $this->loadTables();
  270. return $this->_init($sql, $options);
  271. }
  272. /**
  273. * Create tables, keys and data required by module
  274. * Files llx_table1.sql, llx_table1.key.sql llx_data.sql with create table, create keys
  275. * and create data commands must be stored in directory /resource/sql/
  276. * This function is called by this->init
  277. *
  278. * @return int <=0 if KO, >0 if OK
  279. */
  280. protected function loadTables()
  281. {
  282. return $this->_load_tables('/resource/sql/');
  283. }
  284. }