From af1d2bfffdec4d176c710b802ed2f392a7c3a97c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 7 Oct 2017 14:11:01 +0200 Subject: [PATCH] Standardize code --- .../comm/propal/class/api_proposals.class.php | 9 +- .../class/api_deprecated_commande.class.php | 2 +- htdocs/commande/class/api_orders.class.php | 9 +- .../facture/class/api_invoices.class.php | 26 ++-- .../class/api_expensereports.class.php | 140 +++++++++--------- 5 files changed, 96 insertions(+), 90 deletions(-) diff --git a/htdocs/comm/propal/class/api_proposals.class.php b/htdocs/comm/propal/class/api_proposals.class.php index 7dda7031914..325fad1a326 100644 --- a/htdocs/comm/propal/class/api_proposals.class.php +++ b/htdocs/comm/propal/class/api_proposals.class.php @@ -370,8 +370,10 @@ class Proposals extends DolibarrApi * @url DELETE {id}/lines/{lineid} * * @return int + * @throws 401 + * @throws 404 */ - function delLine($id, $lineid) { + function deleteLine($id, $lineid) { if(! DolibarrApiAccess::$user->rights->propal->creer) { throw new RestException(401); } @@ -384,9 +386,10 @@ class Proposals extends DolibarrApi if( ! DolibarrApi::_checkAccessToResource('propal',$this->propal->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - $request_data = (object) $request_data; + + $request_data = (object) $request_data; $updateRes = $this->propal->deleteline($lineid); - if ($updateRes == 1) { + if ($updateRes > 0) { return $this->get($id); } return false; diff --git a/htdocs/commande/class/api_deprecated_commande.class.php b/htdocs/commande/class/api_deprecated_commande.class.php index 11d3fe7260a..d41fd9cee1d 100644 --- a/htdocs/commande/class/api_deprecated_commande.class.php +++ b/htdocs/commande/class/api_deprecated_commande.class.php @@ -389,7 +389,7 @@ class CommandeApi extends DolibarrApi * * @return int */ - function delLine($id, $lineid) { + function deleteLine($id, $lineid) { if(! DolibarrApiAccess::$user->rights->commande->creer) { throw new RestException(401); } diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index d4b4502d4ed..6ffca33a4ce 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -353,14 +353,16 @@ class Orders extends DolibarrApi * Delete a line to given order * * - * @param int $id Id of commande to update + * @param int $id Id of order to update * @param int $lineid Id of line to delete * * @url DELETE {id}/lines/{lineid} * * @return int + * @throws 401 + * @throws 404 */ - function delLine($id, $lineid) { + function deleteLine($id, $lineid) { if(! DolibarrApiAccess::$user->rights->commande->creer) { throw new RestException(401); } @@ -373,7 +375,8 @@ class Orders extends DolibarrApi if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - $request_data = (object) $request_data; + + $request_data = (object) $request_data; $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user,$lineid); if ($updateRes > 0) { return $this->get($id); diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 4531a0587cc..d8a5a231593 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -312,13 +312,13 @@ class Invoices extends DolibarrApi * @param int $id Id of invoice * @param int $lineid Id of the line to delete * - * @url DELETE {id}/deleteline/{lineid} + * @url DELETE {id}/lines/{lineid} * * @return array - * @throws 304 * @throws 400 * @throws 401 * @throws 404 + * @throws 405 */ function deleteLine($id, $lineid) { @@ -340,7 +340,7 @@ class Invoices extends DolibarrApi $result = $this->invoice->deleteline($lineid); if( $result < 0) { - throw new RestException(304); + throw new RestException(405, $this->invoice->error); } $result = $this->invoice->fetch($id); @@ -355,11 +355,11 @@ class Invoices extends DolibarrApi - + /** * Add a line to a given invoice - * - * Exemple of POST query : { "desc": "Desc", "subprice": "1.00000000", "qty": "1", "tva_tx": "20.000", "localtax1_tx": "0.000", "localtax2_tx": "0.000", "fk_product": "1", "remise_percent": "0", "date_start": "", "date_end": "", "fk_code_ventilation": 0, "info_bits": "0", "fk_remise_except": null, "product_type": "1", "rang": "-1", "special_code": "0", "fk_parent_line": null, "fk_fournprice": null, "pa_ht": "0.00000000", "label": "", "array_options": [], "situation_percent": "100", "fk_prev_id": null, "fk_unit": null } + * + * Exemple of POST query : { "desc": "Desc", "subprice": "1.00000000", "qty": "1", "tva_tx": "20.000", "localtax1_tx": "0.000", "localtax2_tx": "0.000", "fk_product": "1", "remise_percent": "0", "date_start": "", "date_end": "", "fk_code_ventilation": 0, "info_bits": "0", "fk_remise_except": null, "product_type": "1", "rang": "-1", "special_code": "0", "fk_parent_line": null, "fk_fournprice": null, "pa_ht": "0.00000000", "label": "", "array_options": [], "situation_percent": "100", "fk_prev_id": null, "fk_unit": null } * * @param int $id Id of invoice * @param array $request_data Invoiceline data @@ -381,14 +381,14 @@ class Invoices extends DolibarrApi if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - - $request_data = (object) $request_data; - + + $request_data = (object) $request_data; + // Reset fk_parent_line for no child products and special product if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) { $request_data->fk_parent_line = 0; - } - + } + $updateRes = $this->invoice->addline( $request_data->desc, $request_data->subprice, @@ -436,13 +436,13 @@ class Invoices extends DolibarrApi * @url POST {id}/settodraft * * @return array - * + * * @throws 200 * @throws 304 * @throws 401 * @throws 404 * @throws 500 - * + * */ function settodraft($id, $idwarehouse=-1) { diff --git a/htdocs/expensereport/class/api_expensereports.class.php b/htdocs/expensereport/class/api_expensereports.class.php index 58783ba69b3..d1f123b791f 100644 --- a/htdocs/expensereport/class/api_expensereports.class.php +++ b/htdocs/expensereport/class/api_expensereports.class.php @@ -1,7 +1,7 @@ * Copyright (C) 2016 Laurent Destailleur - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -23,14 +23,14 @@ /** * API class for Expense Reports * - * @access protected + * @access protected * @class DolibarrApiAccess {@requires user,external} */ class ExpenseReports extends DolibarrApi { /** - * @var array $FIELDS Mandatory fields, checked when create and update object + * @var array $FIELDS Mandatory fields, checked when create and update object */ static $FIELDS = array( 'socid' @@ -41,7 +41,7 @@ class ExpenseReports extends DolibarrApi */ public $expensereport; - + /** * Constructor */ @@ -56,36 +56,36 @@ class ExpenseReports extends DolibarrApi * Get properties of a Expense Report object * * Return an array with Expense Report informations - * + * * @param int $id ID of Expense Report * @return array|mixed Data without useless information * * @throws RestException */ function get($id) - { + { if(! DolibarrApiAccess::$user->rights->expensereport->lire) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'Expense report not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $this->expensereport->fetchObjectLinked(); return $this->_cleanObjectDatas($this->expensereport); } /** * List Expense Reports - * + * * Get a list of Expense Reports - * + * * @param string $sortfield Sort field * @param string $sortorder Sort order * @param int $limit Limit for list @@ -96,19 +96,19 @@ class ExpenseReports extends DolibarrApi */ function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') { global $db, $conf; - + $obj_ret = array(); // case of external user, $societe param is ignored and replaced by user's socid //$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe; - + $sql = "SELECT t.rowid"; $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as t"; $sql.= ' WHERE t.entity IN ('.getEntity('expensereport').')'; if ($user_ids) $sql.=" AND t.fk_user_author IN (".$user_ids.")"; - + // Add sql filters - if ($sqlfilters) + if ($sqlfilters) { if (! DolibarrApi::_checkFilters($sqlfilters)) { @@ -117,7 +117,7 @@ class ExpenseReports extends DolibarrApi $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; } - + $sql.= $db->order($sortfield, $sortorder); if ($limit) { if ($page < 0) @@ -130,7 +130,7 @@ class ExpenseReports extends DolibarrApi } $result = $db->query($sql); - + if ($result) { $num = $db->num_rows($result); @@ -181,7 +181,7 @@ class ExpenseReports extends DolibarrApi if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) { throw new RestException(500, "Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors)); } - + return $this->expensereport->id; } @@ -189,22 +189,22 @@ class ExpenseReports extends DolibarrApi * Get lines of an Expense Report * * @param int $id Id of Expense Report - * + * * @url GET {id}/lines - * - * @return int + * + * @return int */ -/* +/* function getLines($id) { if(! DolibarrApiAccess::$user->rights->expensereport->lire) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -216,28 +216,28 @@ class ExpenseReports extends DolibarrApi return $result; } */ - + /** * Add a line to given Expense Report * * @param int $id Id of Expense Report to update - * @param array $request_data Expense Report data - * + * @param array $request_data Expense Report data + * * @url POST {id}/lines - * - * @return int + * + * @return int */ -/* +/* function postLine($id, $request_data = NULL) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -277,29 +277,29 @@ class ExpenseReports extends DolibarrApi return false; } */ - + /** * Update a line to given Expense Report * * @param int $id Id of Expense Report to update * @param int $lineid Id of line to update - * @param array $request_data Expense Report data - * + * @param array $request_data Expense Report data + * * @url PUT {id}/lines/{lineid} - * - * @return object + * + * @return object */ /* function putLine($id, $lineid, $request_data = NULL) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -342,22 +342,22 @@ class ExpenseReports extends DolibarrApi * * @param int $id Id of Expense Report to update * @param int $lineid Id of line to delete - * + * * @url DELETE {id}/lines/{lineid} - * - * @return int + * + * @return int */ /* - function delLine($id, $lineid) { + function deleteLine($id, $lineid) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -369,26 +369,26 @@ class ExpenseReports extends DolibarrApi return false; } */ - + /** * Update Expense Report general fields (won't touch lines of expensereport) * * @param int $id Id of Expense Report to update - * @param array $request_data Datas - * - * @return int + * @param array $request_data Datas + * + * @return int */ /* function put($id, $request_data = NULL) { if(! DolibarrApiAccess::$user->rights->expensereport->creer) { throw new RestException(401); } - + $result = $this->expensereport->fetch($id); if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } @@ -396,19 +396,19 @@ class ExpenseReports extends DolibarrApi if ($field == 'id') continue; $this->expensereport->$field = $value; } - + if($this->expensereport->update(DolibarrApiAccess::$user)) return $this->get($id); - + return false; } */ - + /** * Delete Expense Report * * @param int $id Expense Report ID - * + * * @return array */ function delete($id) @@ -420,31 +420,31 @@ class ExpenseReports extends DolibarrApi if( ! $result ) { throw new RestException(404, 'Expense Report not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + if( ! $this->expensereport->delete(DolibarrApiAccess::$user)) { throw new RestException(500, 'Error when delete Expense Report : '.$this->expensereport->error); } - + return array( 'success' => array( 'code' => 200, 'message' => 'Expense Report deleted' ) ); - + } - + /** * Validate an Expense Report - * + * * @param int $id Expense Report ID - * + * * @url POST {id}/validate - * + * * @return array * FIXME An error 403 is returned if the request has an empty body. * Error message: "Forbidden: Content type `text/plain` is not supported." @@ -463,15 +463,15 @@ class ExpenseReports extends DolibarrApi if( ! $result ) { throw new RestException(404, 'expensereport not found'); } - + if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + if( ! $this->expensereport->valid(DolibarrApiAccess::$user, $idwarehouse)) { throw new RestException(500, 'Error when validate expensereport'); } - + return array( 'success' => array( 'code' => 200, @@ -479,12 +479,12 @@ class ExpenseReports extends DolibarrApi ) ); }*/ - + /** * Validate fields before create or update object - * + * * @param array $data Array with data to verify - * @return array + * @return array * @throws RestException */ function _validate($data) @@ -494,7 +494,7 @@ class ExpenseReports extends DolibarrApi if (!isset($data[$field])) throw new RestException(400, "$field field missing"); $expensereport[$field] = $data[$field]; - + } return $expensereport; }