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