modApi.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  5. * Copyright (C) 2018 Regis Houssin <regis.houssin@inodbox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \defgroup api Module Api
  22. * \brief Descriptor file for Api modulee
  23. * \file htdocs/core/modules/modApi.class.php
  24. * \ingroup api
  25. * \brief Description and activation file for the module Api
  26. */
  27. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  28. /**
  29. * Description and activation class for module Api
  30. */
  31. class modApi 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 here a free id (See in Home -> System information -> Dolibarr for list of used modules id).
  44. $this->numero = 2610;
  45. // Key text used to identify module (for permissions, menus, etc...)
  46. $this->rights_class = 'api';
  47. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  48. // It is used to group modules in module setup page
  49. $this->family = "interface";
  50. $this->module_position = '24';
  51. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  52. $this->name = preg_replace('/^mod/i', '', get_class($this));
  53. // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
  54. $this->description = "REST interface";
  55. // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version
  56. $this->version = 'dolibarr';
  57. // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
  58. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  59. // Can be enabled / disabled only in the main company with superadmin account
  60. $this->core_enabled = 1;
  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 = 'technic';
  65. $this->module_parts = array();
  66. // Data directories to create when module is enabled.
  67. // Example: this->dirs = array("/api/temp");
  68. $this->dirs = array('/api/temp');
  69. // Config pages. Put here list of php page, stored into api/admin directory, to use to setup module.
  70. $this->config_page_url = array("index.php@api");
  71. // Dependencies
  72. $this->hidden = false; // A condition to hide module
  73. $this->depends = array(); // List of modules id that must be enabled if this module is enabled
  74. $this->requiredby = array('modZapier'); // List of modules id to disable if this one is disabled
  75. $this->conflictwith = array(); // List of modules id this module is in conflict with
  76. $this->phpmin = array(7, 0); // Minimum version of PHP required by module
  77. $this->langfiles = array("other");
  78. // Constants
  79. // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  80. // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
  81. // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
  82. // );
  83. $this->const = array();
  84. // Array to add new pages in new tabs
  85. // Example: $this->tabs = array('objecttype:+tabname1:Title1:mylangfile@api:$user->rights->api->read:/api/mynewtab1.php?id=__ID__', // To add a new tab identified by code tabname1
  86. // 'objecttype:+tabname2:SUBSTITUTION_Title2:mylangfile@api:$user->rights->othermodule->read:/api/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.
  87. // 'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname
  88. // where objecttype can be
  89. // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
  90. // 'contact' to add a tab in contact view
  91. // 'contract' to add a tab in contract view
  92. // 'group' to add a tab in group view
  93. // 'intervention' to add a tab in intervention view
  94. // 'invoice' to add a tab in customer invoice view
  95. // 'invoice_supplier' to add a tab in supplier invoice view
  96. // 'member' to add a tab in fundation member view
  97. // 'opensurveypoll' to add a tab in opensurvey poll view
  98. // 'order' to add a tab in sales order view
  99. // 'order_supplier' to add a tab in supplier order view
  100. // 'payment' to add a tab in payment view
  101. // 'payment_supplier' to add a tab in supplier payment view
  102. // 'product' to add a tab in product view
  103. // 'propal' to add a tab in propal view
  104. // 'project' to add a tab in project view
  105. // 'stock' to add a tab in stock view
  106. // 'thirdparty' to add a tab in third party view
  107. // 'user' to add a tab in user view
  108. $this->tabs = array();
  109. // Dictionaries
  110. if (!isset($conf->api->enabled)) {
  111. $conf->api = new stdClass();
  112. $conf->api->enabled = 0;
  113. }
  114. $this->dictionaries = array();
  115. // Boxes
  116. // Add here list of php file(s) stored in core/boxes that contains class to show a box.
  117. $this->boxes = array(); // List of boxes
  118. // Example:
  119. //$this->boxes=array(array(0=>array('file'=>'myboxa.php','note'=>'','enabledbydefaulton'=>'Home'),1=>array('file'=>'myboxb.php','note'=>''),2=>array('file'=>'myboxc.php','note'=>'')););
  120. // Permissions
  121. $this->rights = array(); // Permission array used by this module
  122. $this->rights_admin_allowed = 1; // Admin is always granted of permission (even when module is disabled)
  123. $r = 0;
  124. // Add here list of permission defined by an id, a label, a boolean and two constant strings.
  125. // Example:
  126. $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used)
  127. $this->rights[$r][1] = 'Generate/modify users API key'; // Permission label
  128. $this->rights[$r][3] = 0; // Permission by default for new user (0/1)
  129. $this->rights[$r][4] = 'apikey'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  130. $this->rights[$r][5] = 'generate'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  131. $r++;
  132. // Main menu entries
  133. $this->menu = array(); // List of menus to add
  134. $r = 0;
  135. $this->menu[$r] = array('fk_menu'=>'fk_mainmenu=tools',
  136. 'type'=>'left',
  137. 'titre'=>'ApiExplorer',
  138. 'prefix' => img_picto('', $this->picto, 'class="paddingright pictofixedwidth"'),
  139. 'mainmenu'=>'tools',
  140. 'leftmenu'=>'devtools_api',
  141. 'url'=>'/api/index.php/explorer',
  142. 'langs'=>'modulebuilder',
  143. 'position'=>100,
  144. 'perms'=>'1',
  145. //'enabled'=>'isModEnabled("api") && preg_match(\'/^(devtools)/\',$leftmenu)',
  146. 'enabled'=>'isModEnabled("api")',
  147. 'target'=>'_apiexplorer',
  148. 'user'=>0);
  149. // Exports
  150. $r = 1;
  151. // Example:
  152. // $this->export_code[$r]=$this->rights_class.'_'.$r;
  153. // $this->export_label[$r]='CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  154. // $this->export_enabled[$r]='1'; // Condition to show export in list (ie: '$user->id==3'). Set to 1 to always show when module is enabled.
  155. // $this->export_permission[$r]=array(array("facture","facture","export"));
  156. // $this->export_fields_array[$r]=array(
  157. // 's.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','s.fk_pays'=>'Country','s.phone'=>'Phone',
  158. // 's.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.code_compta'=>'CustomerAccountancyCode',
  159. // 's.code_compta_fournisseur'=>'SupplierAccountancyCode','f.rowid'=>"InvoiceId",'f.ref'=>"InvoiceRef",'f.datec'=>"InvoiceDateCreation",
  160. // 'f.datef'=>"DateInvoice",'f.total_ht'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.total_tva'=>"TotalVAT",'f.paye'=>"InvoicePaid",'f.fk_statut'=>'InvoiceStatus',
  161. // 'f.note'=>"InvoiceNote",'fd.rowid'=>'LineId','fd.description'=>"LineDescription",'fd.price'=>"LineUnitPrice",'fd.tva_tx'=>"LineVATRate",
  162. // 'fd.qty'=>"LineQty",'fd.total_ht'=>"LineTotalHT",'fd.total_tva'=>"LineTotalTVA",'fd.total_ttc'=>"LineTotalTTC",'fd.date_start'=>"DateStart",
  163. // 'fd.date_end'=>"DateEnd",'fd.fk_product'=>'ProductId','p.ref'=>'ProductRef'
  164. //);
  165. // $this->export_entities_array[$r]=array(
  166. // 's.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','s.fk_pays'=>'company','s.phone'=>'company',
  167. // 's.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.code_compta'=>'company','s.code_compta_fournisseur'=>'company',
  168. // 'f.rowid'=>"invoice",'f.ref'=>"invoice",'f.datec'=>"invoice",'f.datef'=>"invoice",'f.total_ht'=>"invoice",'f.total_ttc'=>"invoice",'f.total_tva'=>"invoice",
  169. // 'f.paye'=>"invoice",'f.fk_statut'=>'invoice','f.note'=>"invoice",'fd.rowid'=>'invoice_line','fd.description'=>"invoice_line",'fd.price'=>"invoice_line",
  170. // 'fd.total_ht'=>"invoice_line",'fd.total_tva'=>"invoice_line",'fd.total_ttc'=>"invoice_line",'fd.tva_tx'=>"invoice_line",'fd.qty'=>"invoice_line",
  171. // 'fd.date_start'=>"invoice_line",'fd.date_end'=>"invoice_line",'fd.fk_product'=>'product','p.ref'=>'product'
  172. //);
  173. // $this->export_sql_start[$r]='SELECT DISTINCT ';
  174. // $this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'facturedet as fd, '.MAIN_DB_PREFIX.'societe as s)';
  175. // $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  176. // $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
  177. // $this->export_sql_order[$r] .=' ORDER BY s.nom';
  178. // $r++;
  179. }
  180. /**
  181. * Function called when module is enabled.
  182. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  183. * It also creates data directories
  184. *
  185. * @param string $options Options when enabling module ('', 'noboxes')
  186. * @return int 1 if OK, 0 if KO
  187. */
  188. public function init($options = '')
  189. {
  190. $sql = array();
  191. return $this->_init($sql, $options);
  192. }
  193. /**
  194. * Function called when module is disabled.
  195. * Remove from database constants, boxes and permissions from Dolibarr database.
  196. * Data directories are not deleted.
  197. *
  198. * @param string $options Options when enabling module ('', 'noboxes')
  199. * @return int 1 if OK, 0 if KO
  200. */
  201. public function remove($options = '')
  202. {
  203. // Remove old constants with entity fields different of 0
  204. $sql = array(
  205. "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = ".$this->db->encrypt('MAIN_MODULE_API'), // API can't be enabled per environment. Why ?
  206. "DELETE FROM ".MAIN_DB_PREFIX."const WHERE name = ".$this->db->encrypt('API_PRODUCTION_MODE') // Not in production mode by default at activation
  207. );
  208. return $this->_remove($sql, $options);
  209. }
  210. }