modApi.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \defgroup api Module Api
  21. * \brief Descriptor file for Api modulee
  22. * \file htdocs/api/core/modules/modApi.class.php
  23. * \ingroup api
  24. * \brief Description and activation file for module Api
  25. */
  26. include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php';
  27. /**
  28. * Description and activation class for module Api
  29. */
  30. class modApi extends DolibarrModules
  31. {
  32. /**
  33. * Constructor. Define names, constants, directories, boxes, permissions
  34. *
  35. * @param DoliDB $db Database handler
  36. */
  37. 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 = 2610;
  44. // Key text used to identify module (for permissions, menus, etc...)
  45. $this->rights_class = 'api';
  46. // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
  47. // It is used to group modules in module setup page
  48. $this->family = "technic";
  49. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  50. $this->name = preg_replace('/^mod/i','',get_class($this));
  51. // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
  52. $this->description = "REST interface";
  53. // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version
  54. $this->version = 'development';
  55. // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
  56. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  57. // Where to store the module in setup page (0=common,1=interface,2=others,3=very specific)
  58. $this->special = 1;
  59. // Name of image file used for this module.
  60. // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
  61. // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
  62. $this->picto='technic';
  63. $this->module_parts = array();
  64. // Data directories to create when module is enabled.
  65. // Example: this->dirs = array("/api/temp");
  66. $this->dirs = array();
  67. // Config pages. Put here list of php page, stored into api/admin directory, to use to setup module.
  68. $this->config_page_url = array("api.php@api");
  69. // Dependencies
  70. $this->hidden = false; // A condition to hide module
  71. $this->depends = array(); // List of modules id that must be enabled if this module is enabled
  72. $this->requiredby = array(); // List of modules id to disable if this one is disabled
  73. $this->conflictwith = array(); // List of modules id this module is in conflict with
  74. $this->phpmin = array(5,3); // Minimum version of PHP required by module
  75. $this->langfiles = array("other");
  76. // Constants
  77. // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive)
  78. // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1),
  79. // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1)
  80. // );
  81. $this->const = array();
  82. // Array to add new pages in new tabs
  83. // 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
  84. // '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.
  85. // 'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname
  86. // where objecttype can be
  87. // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member)
  88. // 'contact' to add a tab in contact view
  89. // 'contract' to add a tab in contract view
  90. // 'group' to add a tab in group view
  91. // 'intervention' to add a tab in intervention view
  92. // 'invoice' to add a tab in customer invoice view
  93. // 'invoice_supplier' to add a tab in supplier invoice view
  94. // 'member' to add a tab in fundation member view
  95. // 'opensurveypoll' to add a tab in opensurvey poll view
  96. // 'order' to add a tab in customer order view
  97. // 'order_supplier' to add a tab in supplier order view
  98. // 'payment' to add a tab in payment view
  99. // 'payment_supplier' to add a tab in supplier payment view
  100. // 'product' to add a tab in product view
  101. // 'propal' to add a tab in propal view
  102. // 'project' to add a tab in project view
  103. // 'stock' to add a tab in stock view
  104. // 'thirdparty' to add a tab in third party view
  105. // 'user' to add a tab in user view
  106. $this->tabs = array();
  107. // Dictionaries
  108. if (! isset($conf->api->enabled))
  109. {
  110. $conf->api=new stdClass();
  111. $conf->api->enabled=0;
  112. }
  113. $this->dictionaries=array();
  114. /* Example:
  115. if (! isset($conf->api->enabled)) $conf->api->enabled=0; // This is to avoid warnings
  116. $this->dictionaries=array(
  117. 'langs'=>'mylangfile@api',
  118. 'tabname'=>array(MAIN_DB_PREFIX."table1",MAIN_DB_PREFIX."table2",MAIN_DB_PREFIX."table3"), // List of tables we want to see into dictonnary editor
  119. 'tablib'=>array("Table1","Table2","Table3"), // Label of tables
  120. 'tabsql'=>array('SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table1 as f','SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table2 as f','SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table3 as f'), // Request to select fields
  121. 'tabsqlsort'=>array("label ASC","label ASC","label ASC"), // Sort order
  122. 'tabfield'=>array("code,label","code,label","code,label"), // List of fields (result of select to show dictionary)
  123. 'tabfieldvalue'=>array("code,label","code,label","code,label"), // List of fields (list of fields to edit a record)
  124. 'tabfieldinsert'=>array("code,label","code,label","code,label"), // List of fields (list of fields for insert)
  125. 'tabrowid'=>array("rowid","rowid","rowid"), // Name of columns with primary key (try to always name it 'rowid')
  126. 'tabcond'=>array($conf->api->enabled,$conf->api->enabled,$conf->api->enabled) // Condition to show each dictionary
  127. );
  128. */
  129. // Boxes
  130. // Add here list of php file(s) stored in core/boxes that contains class to show a box.
  131. $this->boxes = array(); // List of boxes
  132. // Example:
  133. //$this->boxes=array(array(0=>array('file'=>'myboxa.php','note'=>'','enabledbydefaulton'=>'Home'),1=>array('file'=>'myboxb.php','note'=>''),2=>array('file'=>'myboxc.php','note'=>'')););
  134. // Permissions
  135. $this->rights = array(); // Permission array used by this module
  136. $r=0;
  137. // Add here list of permission defined by an id, a label, a boolean and two constant strings.
  138. // Example:
  139. // $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used)
  140. // $this->rights[$r][1] = 'Permision label'; // Permission label
  141. // $this->rights[$r][3] = 1; // Permission by default for new user (0/1)
  142. // $this->rights[$r][4] = 'level1'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  143. // $this->rights[$r][5] = 'level2'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2)
  144. // $r++;
  145. // Main menu entries
  146. $this->menu = array(); // List of menus to add
  147. $r=0;
  148. // Add here entries to declare new menus
  149. //
  150. // Example to declare a new Top Menu entry and its Left menu entry:
  151. // $this->menu[$r]=array( 'fk_menu'=>0, // Put 0 if this is a top menu
  152. // 'type'=>'top', // This is a Top menu entry
  153. // 'titre'=>'Api top menu',
  154. // 'mainmenu'=>'api',
  155. // 'leftmenu'=>'api',
  156. // 'url'=>'/api/pagetop.php',
  157. // 'langs'=>'mylangfile@api', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  158. // 'position'=>100,
  159. // 'enabled'=>'$conf->api->enabled', // Define condition to show or hide menu entry. Use '$conf->api->enabled' if entry must be visible if module is enabled.
  160. // 'perms'=>'1', // Use 'perms'=>'$user->rights->api->level1->level2' if you want your menu with a permission rules
  161. // 'target'=>'',
  162. // 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  163. // $r++;
  164. //
  165. // Example to declare a Left Menu entry into an existing Top menu entry:
  166. // $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=xxx', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
  167. // 'type'=>'left', // This is a Left menu entry
  168. // 'titre'=>'Api left menu',
  169. // 'mainmenu'=>'xxx',
  170. // 'leftmenu'=>'api',
  171. // 'url'=>'/api/pagelevel2.php',
  172. // 'langs'=>'mylangfile@api', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
  173. // 'position'=>100,
  174. // 'enabled'=>'$conf->api->enabled', // Define condition to show or hide menu entry. Use '$conf->api->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
  175. // 'perms'=>'1', // Use 'perms'=>'$user->rights->api->level1->level2' if you want your menu with a permission rules
  176. // 'target'=>'',
  177. // 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both
  178. // $r++;
  179. // Exports
  180. $r=1;
  181. // Example:
  182. // $this->export_code[$r]=$this->rights_class.'_'.$r;
  183. // $this->export_label[$r]='CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
  184. // $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.
  185. // $this->export_permission[$r]=array(array("facture","facture","export"));
  186. // $this->export_fields_array[$r]=array('s.rowid'=>"IdCompany",'s.nom'=>'CompanyName','s.address'=>'Address','s.zip'=>'Zip','s.town'=>'Town','s.fk_pays'=>'Country','s.phone'=>'Phone','s.siren'=>'ProfId1','s.siret'=>'ProfId2','s.ape'=>'ProfId3','s.idprof4'=>'ProfId4','s.code_compta'=>'CustomerAccountancyCode','s.code_compta_fournisseur'=>'SupplierAccountancyCode','f.rowid'=>"InvoiceId",'f.facnumber'=>"InvoiceRef",'f.datec'=>"InvoiceDateCreation",'f.datef'=>"DateInvoice",'f.total'=>"TotalHT",'f.total_ttc'=>"TotalTTC",'f.tva'=>"TotalVAT",'f.paye'=>"InvoicePaid",'f.fk_statut'=>'InvoiceStatus','f.note'=>"InvoiceNote",'fd.rowid'=>'LineId','fd.description'=>"LineDescription",'fd.price'=>"LineUnitPrice",'fd.tva_tx'=>"LineVATRate",'fd.qty'=>"LineQty",'fd.total_ht'=>"LineTotalHT",'fd.total_tva'=>"LineTotalTVA",'fd.total_ttc'=>"LineTotalTTC",'fd.date_start'=>"DateStart",'fd.date_end'=>"DateEnd",'fd.fk_product'=>'ProductId','p.ref'=>'ProductRef');
  187. // $this->export_entities_array[$r]=array('s.rowid'=>"company",'s.nom'=>'company','s.address'=>'company','s.zip'=>'company','s.town'=>'company','s.fk_pays'=>'company','s.phone'=>'company','s.siren'=>'company','s.siret'=>'company','s.ape'=>'company','s.idprof4'=>'company','s.code_compta'=>'company','s.code_compta_fournisseur'=>'company','f.rowid'=>"invoice",'f.facnumber'=>"invoice",'f.datec'=>"invoice",'f.datef'=>"invoice",'f.total'=>"invoice",'f.total_ttc'=>"invoice",'f.tva'=>"invoice",'f.paye'=>"invoice",'f.fk_statut'=>'invoice','f.note'=>"invoice",'fd.rowid'=>'invoice_line','fd.description'=>"invoice_line",'fd.price'=>"invoice_line",'fd.total_ht'=>"invoice_line",'fd.total_tva'=>"invoice_line",'fd.total_ttc'=>"invoice_line",'fd.tva_tx'=>"invoice_line",'fd.qty'=>"invoice_line",'fd.date_start'=>"invoice_line",'fd.date_end'=>"invoice_line",'fd.fk_product'=>'product','p.ref'=>'product');
  188. // $this->export_sql_start[$r]='SELECT DISTINCT ';
  189. // $this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'facturedet as fd, '.MAIN_DB_PREFIX.'societe as s)';
  190. // $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)';
  191. // $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture';
  192. // $this->export_sql_order[$r] .=' ORDER BY s.nom';
  193. // $r++;
  194. }
  195. /**
  196. * Function called when module is enabled.
  197. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  198. * It also creates data directories
  199. *
  200. * @param string $options Options when enabling module ('', 'noboxes')
  201. * @return int 1 if OK, 0 if KO
  202. */
  203. function init($options='')
  204. {
  205. $sql = array();
  206. $result=$this->_load_tables('/api/sql/');
  207. return $this->_init($sql, $options);
  208. }
  209. }