| Conditions | 2 |
| Paths | 2 |
| Total Lines | 177 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 41 | function __construct($db) |
||
| 42 | { |
||
| 43 | global $langs,$conf; |
||
| 44 | |||
| 45 | $this->db = $db; |
||
| 46 | |||
| 47 | // Id for module (must be unique). |
||
| 48 | // Use here a free id (See in Home -> System information -> Dolibarr for list of used modules id). |
||
| 49 | $this->numero = 2610; |
||
| 50 | // Key text used to identify module (for permissions, menus, etc...) |
||
| 51 | $this->rights_class = 'api'; |
||
| 52 | |||
| 53 | // Family can be 'crm','financial','hr','projects','products','ecm','technic','other' |
||
| 54 | // It is used to group modules in module setup page |
||
| 55 | $this->family = "interface"; |
||
| 56 | // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module) |
||
| 57 | $this->name = preg_replace('/^mod/i','',get_class($this)); |
||
| 58 | // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module) |
||
| 59 | $this->description = "REST interface"; |
||
| 60 | // Possible values for version are: 'development', 'experimental', 'dolibarr' or 'dolibarr_deprecated' or version |
||
| 61 | $this->version = 'dolibarr'; |
||
| 62 | // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase) |
||
| 63 | $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name); |
||
| 64 | // Can be enabled / disabled only in the main company with superadmin account |
||
| 65 | $this->core_enabled = 1; |
||
|
|
|||
| 66 | // Name of image file used for this module. |
||
| 67 | // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue' |
||
| 68 | // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module' |
||
| 69 | $this->picto='technic'; |
||
| 70 | |||
| 71 | $this->module_parts = array(); |
||
| 72 | |||
| 73 | // Data directories to create when module is enabled. |
||
| 74 | // Example: this->dirs = array("/api/temp"); |
||
| 75 | $this->dirs = array('/api/temp'); |
||
| 76 | |||
| 77 | // Config pages. Put here list of php page, stored into api/admin directory, to use to setup module. |
||
| 78 | $this->config_page_url = array("index.php@api"); |
||
| 79 | |||
| 80 | // Dependencies |
||
| 81 | $this->hidden = false; // A condition to hide module |
||
| 82 | $this->depends = array(); // List of modules id that must be enabled if this module is enabled |
||
| 83 | $this->requiredby = array(); // List of modules id to disable if this one is disabled |
||
| 84 | $this->conflictwith = array(); // List of modules id this module is in conflict with |
||
| 85 | $this->phpmin = array(5,3); // Minimum version of PHP required by module |
||
| 86 | $this->langfiles = array("other"); |
||
| 87 | |||
| 88 | // Constants |
||
| 89 | // List of particular constants to add when module is enabled (key, 'chaine', value, desc, visible, 'current' or 'allentities', deleteonunactive) |
||
| 90 | // Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',1), |
||
| 91 | // 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0, 'current', 1) |
||
| 92 | // ); |
||
| 93 | $this->const = array(); |
||
| 94 | |||
| 95 | // Array to add new pages in new tabs |
||
| 96 | // 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 |
||
| 97 | // '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. |
||
| 98 | // 'objecttype:-tabname:NU:conditiontoremove'); // To remove an existing tab identified by code tabname |
||
| 99 | // where objecttype can be |
||
| 100 | // 'categories_x' to add a tab in category view (replace 'x' by type of category (0=product, 1=supplier, 2=customer, 3=member) |
||
| 101 | // 'contact' to add a tab in contact view |
||
| 102 | // 'contract' to add a tab in contract view |
||
| 103 | // 'group' to add a tab in group view |
||
| 104 | // 'intervention' to add a tab in intervention view |
||
| 105 | // 'invoice' to add a tab in customer invoice view |
||
| 106 | // 'invoice_supplier' to add a tab in supplier invoice view |
||
| 107 | // 'member' to add a tab in fundation member view |
||
| 108 | // 'opensurveypoll' to add a tab in opensurvey poll view |
||
| 109 | // 'order' to add a tab in customer order view |
||
| 110 | // 'order_supplier' to add a tab in supplier order view |
||
| 111 | // 'payment' to add a tab in payment view |
||
| 112 | // 'payment_supplier' to add a tab in supplier payment view |
||
| 113 | // 'product' to add a tab in product view |
||
| 114 | // 'propal' to add a tab in propal view |
||
| 115 | // 'project' to add a tab in project view |
||
| 116 | // 'stock' to add a tab in stock view |
||
| 117 | // 'thirdparty' to add a tab in third party view |
||
| 118 | // 'user' to add a tab in user view |
||
| 119 | $this->tabs = array(); |
||
| 120 | |||
| 121 | // Dictionaries |
||
| 122 | if (! isset($conf->api->enabled)) |
||
| 123 | { |
||
| 124 | $conf->api=new stdClass(); |
||
| 125 | $conf->api->enabled=0; |
||
| 126 | } |
||
| 127 | $this->dictionaries=array(); |
||
| 128 | /* Example: |
||
| 129 | if (! isset($conf->api->enabled)) $conf->api->enabled=0; // This is to avoid warnings |
||
| 130 | $this->dictionaries=array( |
||
| 131 | 'langs'=>'mylangfile@api', |
||
| 132 | 'tabname'=>array(MAIN_DB_PREFIX."table1",MAIN_DB_PREFIX."table2",MAIN_DB_PREFIX."table3"), // List of tables we want to see into dictonnary editor |
||
| 133 | 'tablib'=>array("Table1","Table2","Table3"), // Label of tables |
||
| 134 | '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 |
||
| 135 | 'tabsqlsort'=>array("label ASC","label ASC","label ASC"), // Sort order |
||
| 136 | 'tabfield'=>array("code,label","code,label","code,label"), // List of fields (result of select to show dictionary) |
||
| 137 | 'tabfieldvalue'=>array("code,label","code,label","code,label"), // List of fields (list of fields to edit a record) |
||
| 138 | 'tabfieldinsert'=>array("code,label","code,label","code,label"), // List of fields (list of fields for insert) |
||
| 139 | 'tabrowid'=>array("rowid","rowid","rowid"), // Name of columns with primary key (try to always name it 'rowid') |
||
| 140 | 'tabcond'=>array($conf->api->enabled,$conf->api->enabled,$conf->api->enabled) // Condition to show each dictionary |
||
| 141 | ); |
||
| 142 | */ |
||
| 143 | |||
| 144 | // Boxes |
||
| 145 | // Add here list of php file(s) stored in core/boxes that contains class to show a box. |
||
| 146 | $this->boxes = array(); // List of boxes |
||
| 147 | // Example: |
||
| 148 | //$this->boxes=array(array(0=>array('file'=>'myboxa.php','note'=>'','enabledbydefaulton'=>'Home'),1=>array('file'=>'myboxb.php','note'=>''),2=>array('file'=>'myboxc.php','note'=>''));); |
||
| 149 | |||
| 150 | // Permissions |
||
| 151 | $this->rights = array(); // Permission array used by this module |
||
| 152 | $r=0; |
||
| 153 | |||
| 154 | // Add here list of permission defined by an id, a label, a boolean and two constant strings. |
||
| 155 | // Example: |
||
| 156 | // $this->rights[$r][0] = $this->numero + $r; // Permission id (must not be already used) |
||
| 157 | // $this->rights[$r][1] = 'Permision label'; // Permission label |
||
| 158 | // $this->rights[$r][3] = 0; // Permission by default for new user (0/1) |
||
| 159 | // $this->rights[$r][4] = 'level1'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2) |
||
| 160 | // $this->rights[$r][5] = 'level2'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2) |
||
| 161 | // $r++; |
||
| 162 | |||
| 163 | |||
| 164 | // Main menu entries |
||
| 165 | $this->menu = array(); // List of menus to add |
||
| 166 | $r=0; |
||
| 167 | |||
| 168 | // Add here entries to declare new menus |
||
| 169 | // |
||
| 170 | // Example to declare a new Top Menu entry and its Left menu entry: |
||
| 171 | // $this->menu[$r]=array( 'fk_menu'=>0, // Put 0 if this is a top menu |
||
| 172 | // 'type'=>'top', // This is a Top menu entry |
||
| 173 | // 'titre'=>'Api top menu', |
||
| 174 | // 'mainmenu'=>'api', |
||
| 175 | // 'leftmenu'=>'api', |
||
| 176 | // 'url'=>'/api/pagetop.php', |
||
| 177 | // 'langs'=>'mylangfile@api', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. |
||
| 178 | // 'position'=>100, |
||
| 179 | // '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. |
||
| 180 | // 'perms'=>'1', // Use 'perms'=>'$user->rights->api->level1->level2' if you want your menu with a permission rules |
||
| 181 | // 'target'=>'', |
||
| 182 | // 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both |
||
| 183 | // $r++; |
||
| 184 | // |
||
| 185 | // Example to declare a Left Menu entry into an existing Top menu entry: |
||
| 186 | // $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 |
||
| 187 | // 'type'=>'left', // This is a Left menu entry |
||
| 188 | // 'titre'=>'Api left menu', |
||
| 189 | // 'mainmenu'=>'xxx', |
||
| 190 | // 'leftmenu'=>'api', |
||
| 191 | // 'url'=>'/api/pagelevel2.php', |
||
| 192 | // 'langs'=>'mylangfile@api', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. |
||
| 193 | // 'position'=>100, |
||
| 194 | // '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. |
||
| 195 | // 'perms'=>'1', // Use 'perms'=>'$user->rights->api->level1->level2' if you want your menu with a permission rules |
||
| 196 | // 'target'=>'', |
||
| 197 | // 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both |
||
| 198 | // $r++; |
||
| 199 | |||
| 200 | |||
| 201 | // Exports |
||
| 202 | $r=1; |
||
| 203 | |||
| 204 | // Example: |
||
| 205 | // $this->export_code[$r]=$this->rights_class.'_'.$r; |
||
| 206 | // $this->export_label[$r]='CustomersInvoicesAndInvoiceLines'; // Translation key (used only if key ExportDataset_xxx_z not found) |
||
| 207 | // $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. |
||
| 208 | // $this->export_permission[$r]=array(array("facture","facture","export")); |
||
| 209 | // $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'); |
||
| 210 | // $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'); |
||
| 211 | // $this->export_sql_start[$r]='SELECT DISTINCT '; |
||
| 212 | // $this->export_sql_end[$r] =' FROM ('.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'facturedet as fd, '.MAIN_DB_PREFIX.'societe as s)'; |
||
| 213 | // $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product as p on (fd.fk_product = p.rowid)'; |
||
| 214 | // $this->export_sql_end[$r] .=' WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture'; |
||
| 215 | // $this->export_sql_order[$r] .=' ORDER BY s.nom'; |
||
| 216 | // $r++; |
||
| 217 | } |
||
| 218 | |||
| 256 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.