Merge pull request #7551 from hregis/develop_bug_restapi

Fix: refactorization of "getListForItem" function
This commit is contained in:
Laurent Destailleur 2017-10-04 10:59:55 +02:00 committed by GitHub
commit e7ac828cc7
7 changed files with 302 additions and 210 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
/* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr> /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
* Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -19,6 +20,7 @@ use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php'; require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
/** /**
* API class for members * API class for members
@ -357,4 +359,38 @@ class Members extends DolibarrApi
return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date); return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
} }
/**
* Get categories for a member
*
* @param int $id ID of member
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
*
* @return mixed
*
* @url GET {id}/categories
*/
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
{
if (! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
$categories = new Categorie($this->db);
$result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
if (empty($result)) {
throw new RestException(404, 'No category found');
}
if ($result < 0) {
throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
}
return $result;
}
} }

View File

@ -23,13 +23,13 @@
/** /**
* API class for categories * API class for categories
* *
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class Categories extends DolibarrApi class Categories 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( static $FIELDS = array(
'label', 'label',
@ -44,7 +44,7 @@ class Categories extends DolibarrApi
4 => 'contact', 4 => 'contact',
5 => 'account', 5 => 'account',
); );
/** /**
* @var Categorie $category {@type Categorie} * @var Categorie $category {@type Categorie}
*/ */
@ -67,20 +67,20 @@ class Categories extends DolibarrApi
* *
* @param int $id ID of category * @param int $id ID of category
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @throws RestException * @throws RestException
*/ */
function get($id) function get($id)
{ {
if(! DolibarrApiAccess::$user->rights->categorie->lire) { if(! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->category->fetch($id); $result = $this->category->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'category not found'); throw new RestException(404, 'category not found');
} }
if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) { if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -90,7 +90,7 @@ class Categories extends DolibarrApi
/** /**
* List categories * List categories
* *
* Get a list of categories * Get a list of categories
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
@ -105,13 +105,13 @@ class Categories extends DolibarrApi
*/ */
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $type = '', $sqlfilters = '') { function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $type = '', $sqlfilters = '') {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
if(! DolibarrApiAccess::$user->rights->categorie->lire) { if(! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401); throw new RestException(401);
} }
$sql = "SELECT t.rowid"; $sql = "SELECT t.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as t"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie as t";
$sql.= ' WHERE t.entity IN ('.getEntity('category').')'; $sql.= ' WHERE t.entity IN ('.getEntity('category').')';
@ -120,7 +120,7 @@ class Categories extends DolibarrApi
$sql.= ' AND t.type='.array_search($type,Categories::$TYPES); $sql.= ' AND t.type='.array_search($type,Categories::$TYPES);
} }
// Add sql filters // Add sql filters
if ($sqlfilters) if ($sqlfilters)
{ {
if (! DolibarrApi::_checkFilters($sqlfilters)) if (! DolibarrApi::_checkFilters($sqlfilters))
{ {
@ -129,93 +129,6 @@ class Categories extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
} }
$sql.= $db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
{
$page = 0;
}
$offset = $limit * $page;
$sql.= $db->plimit($limit + 1, $offset);
}
$result = $db->query($sql);
if ($result)
{
$i=0;
$num = $db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
while ($i < $min)
{
$obj = $db->fetch_object($result);
$category_static = new Categorie($db);
if($category_static->fetch($obj->rowid)) {
$obj_ret[] = $this->_cleanObjectDatas($category_static);
}
$i++;
}
}
else {
throw new RestException(503, 'Error when retrieve category list : '.$db->lasterror());
}
if( ! count($obj_ret)) {
throw new RestException(404, 'No category found');
}
return $obj_ret;
}
/**
* List categories of an entity
*
* Note: This method is not directly exposed in the API, it is used
* in the GET /xxx/{id}/categories requests.
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @param int $item Id of the item to get categories for
* @return array Array of category objects
*
* @access private
*/
function getListForItem($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $type='customer', $item = 0) {
global $db, $conf;
$obj_ret = array();
if(! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
//if ($type == "") {
//$type="product";
//}
$sub_type = $type;
$subcol_name = "fk_".$type;
if ($type=="customer" || $type=="supplier") {
$sub_type="societe";
$subcol_name="fk_soc";
}
if ($type=="contact") {
$subcol_name="fk_socpeople";
}
$sql = "SELECT s.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as s";
$sql.= " , ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub ";
$sql.= ' WHERE s.entity IN ('.getEntity('category').')';
$sql.= ' AND s.type='.array_search($type,Categories::$TYPES);
$sql.= ' AND s.rowid = sub.fk_categorie';
$sql.= ' AND sub.'.$subcol_name.' = '.$item;
$nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
{
$result = $db->query($sql);
$nbtotalofrecords = $db->num_rows($result);
}
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
@ -250,13 +163,12 @@ class Categories extends DolibarrApi
if( ! count($obj_ret)) { if( ! count($obj_ret)) {
throw new RestException(404, 'No category found'); throw new RestException(404, 'No category found');
} }
return $obj_ret; return $obj_ret;
} }
/** /**
* Create category object * Create category object
* *
* @param array $request_data Request data * @param array $request_data Request data
* @return int ID of category * @return int ID of category
*/ */
@ -268,7 +180,7 @@ class Categories extends DolibarrApi
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->category->$field = $value; $this->category->$field = $value;
} }
@ -280,22 +192,22 @@ class Categories extends DolibarrApi
/** /**
* Update category * Update category
* *
* @param int $id Id of category to update * @param int $id Id of category to update
* @param array $request_data Datas * @param array $request_data Datas
* @return int * @return int
*/ */
function put($id, $request_data = NULL) function put($id, $request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->categorie->creer) { if(! DolibarrApiAccess::$user->rights->categorie->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->category->fetch($id); $result = $this->category->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'category not found'); throw new RestException(404, 'category not found');
} }
if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) { if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -304,13 +216,13 @@ class Categories extends DolibarrApi
if ($field == 'id') continue; if ($field == 'id') continue;
$this->category->$field = $value; $this->category->$field = $value;
} }
if($this->category->update(DolibarrApiAccess::$user)) if($this->category->update(DolibarrApiAccess::$user))
return $this->get ($id); return $this->get ($id);
return false; return false;
} }
/** /**
* Delete category * Delete category
* *
@ -326,15 +238,15 @@ class Categories extends DolibarrApi
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'category not found'); throw new RestException(404, 'category not found');
} }
if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) { if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if (! $this->category->delete(DolibarrApiAccess::$user)) { if (! $this->category->delete(DolibarrApiAccess::$user)) {
throw new RestException(401,'error when delete category'); throw new RestException(401,'error when delete category');
} }
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
@ -342,8 +254,8 @@ class Categories extends DolibarrApi
) )
); );
} }
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
@ -351,9 +263,9 @@ class Categories extends DolibarrApi
* @return array Array of cleaned object properties * @return array Array of cleaned object properties
*/ */
function _cleanObjectDatas($object) { function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object); $object = parent::_cleanObjectDatas($object);
// Remove fields not relevent to categories // Remove fields not relevent to categories
unset($object->country); unset($object->country);
unset($object->country_id); unset($object->country_id);
@ -394,16 +306,16 @@ class Categories extends DolibarrApi
unset($object->fk_project); unset($object->fk_project);
unset($object->note); unset($object->note);
unset($object->statut); unset($object->statut);
return $object; return $object;
} }
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array|null $data Data to validate * @param array|null $data Data to validate
* @return array * @return array
* *
* @throws RestException * @throws RestException
*/ */
function _validate($data) function _validate($data)

View File

@ -879,6 +879,100 @@ class Categorie extends CommonObject
} }
} }
/**
* List categories of an element id
*
* @param int $item Id of element
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @return array Array of categories
*/
function getListForItem($id, $type='customer', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
{
global $conf;
$categories = array();
$sub_type = $type;
$subcol_name = "fk_".$type;
if ($type=="customer" || $type=="supplier") {
$sub_type="societe";
$subcol_name="fk_soc";
}
if ($type=="contact") {
$subcol_name="fk_socpeople";
}
$sql = "SELECT s.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as s";
$sql.= " , ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub ";
$sql.= ' WHERE s.entity IN ('.getEntity('category').')';
$sql.= ' AND s.type='.array_search($type, self::$MAP_ID_TO_CODE);
$sql.= ' AND s.rowid = sub.fk_categorie';
$sql.= ' AND sub.'.$subcol_name.' = '.$id;
$nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
{
$result = $this->db->query($sql);
$nbtotalofrecords = $this->db->num_rows($result);
}
$sql.= $this->db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
{
$page = 0;
}
$offset = $limit * $page;
$sql.= $this->db->plimit($limit + 1, $offset);
}
$result = $this->db->query($sql);
if ($result)
{
$i=0;
$num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
while ($i < $min)
{
$obj = $this->db->fetch_object($result);
$category_static = new Categorie($this->db);
if ($category_static->fetch($obj->rowid))
{
$categories[$i]['id'] = $category_static->id;
$categories[$i]['fk_parent'] = $category_static->fk_parent;
$categories[$i]['label'] = $category_static->label;
$categories[$i]['description'] = $category_static->description;
$categories[$i]['color'] = $category_static->color;
$categories[$i]['socid'] = $category_static->socid;
$categories[$i]['visible'] = $category_static->visible;
$categories[$i]['type'] = $category_static->type;
$categories[$i]['entity'] = $category_static->entity;
$categories[$i]['array_options'] = $category_static->array_options;
// multilangs
if (! empty($conf->global->MAIN_MULTILANGS)) {
$categories[$i]['multilangs'] = $category_static->multilangs;
}
}
$i++;
}
}
else {
$this->error = $this->db->lasterror();
return -1;
}
if ( ! count($categories)) {
return 0;
}
return $categories;
}
/** /**
* Return childs of a category * Return childs of a category
* *

View File

@ -319,7 +319,7 @@ else
// List of products or services (type is type of category) // List of products or services (type is type of category)
if ($object->type == Categorie::TYPE_PRODUCT) if ($type == Categorie::TYPE_PRODUCT)
{ {
$prods = $object->getObjectsInCateg("product"); $prods = $object->getObjectsInCateg("product");
if ($prods < 0) if ($prods < 0)
@ -391,7 +391,7 @@ if ($object->type == Categorie::TYPE_PRODUCT)
} }
} }
if ($object->type == Categorie::TYPE_SUPPLIER) if ($type == Categorie::TYPE_SUPPLIER)
{ {
$socs = $object->getObjectsInCateg("supplier"); $socs = $object->getObjectsInCateg("supplier");
if ($socs < 0) if ($socs < 0)
@ -440,7 +440,7 @@ if ($object->type == Categorie::TYPE_SUPPLIER)
} }
} }
if($object->type == Categorie::TYPE_CUSTOMER) if($type == Categorie::TYPE_CUSTOMER)
{ {
$socs = $object->getObjectsInCateg("customer"); $socs = $object->getObjectsInCateg("customer");
if ($socs < 0) if ($socs < 0)
@ -494,7 +494,7 @@ if($object->type == Categorie::TYPE_CUSTOMER)
} }
// List of members // List of members
if ($object->type == Categorie::TYPE_MEMBER) if ($type == Categorie::TYPE_MEMBER)
{ {
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
@ -547,7 +547,7 @@ if ($object->type == Categorie::TYPE_MEMBER)
} }
// Categorie contact // Categorie contact
if($object->type == Categorie::TYPE_CONTACT) if ($type == Categorie::TYPE_CONTACT)
{ {
$contacts = $object->getObjectsInCateg("contact"); $contacts = $object->getObjectsInCateg("contact");
if ($contacts < 0) if ($contacts < 0)
@ -600,7 +600,7 @@ if($object->type == Categorie::TYPE_CONTACT)
} }
// List of accounts // List of accounts
if ($object->type == Categorie::TYPE_ACCOUNT) if ($type == Categorie::TYPE_ACCOUNT)
{ {
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
@ -653,7 +653,7 @@ if ($object->type == Categorie::TYPE_ACCOUNT)
} }
// List of Project // List of Project
if ($object->type == Categorie::TYPE_PROJECT) if ($type == Categorie::TYPE_PROJECT)
{ {
require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';

View File

@ -16,24 +16,24 @@
*/ */
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
/** /**
* API class for products * API class for products
* *
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class Products extends DolibarrApi class Products 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( static $FIELDS = array(
'ref', 'ref',
'label' 'label'
); );
/** /**
@ -53,30 +53,30 @@ class Products extends DolibarrApi
/** /**
* Get properties of a product object * Get properties of a product object
* *
* Return an array with product informations * Return an array with product informations
* *
* @param int $id ID of product * @param int $id ID of product
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @throws RestException * @throws RestException
* TODO implement getting a product by ref or by $ref_ext * TODO implement getting a product by ref or by $ref_ext
*/ */
function get($id) function get($id)
{ {
if(! DolibarrApiAccess::$user->rights->produit->lire) { if(! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->product->fetch($id); $result = $this->product->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Product not found'); throw new RestException(404, 'Product not found');
} }
if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) { if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$this->product->load_stock(); $this->product->load_stock();
return $this->_cleanObjectDatas($this->product); return $this->_cleanObjectDatas($this->product);
@ -84,9 +84,9 @@ class Products extends DolibarrApi
/** /**
* List products * List products
* *
* Get a list of products * Get a list of products
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
@ -98,9 +98,9 @@ class Products extends DolibarrApi
*/ */
function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $category=0, $sqlfilters = '') { function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $category=0, $sqlfilters = '') {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
$sql = "SELECT t.rowid, t.ref, t.ref_ext"; $sql = "SELECT t.rowid, t.ref, t.ref_ext";
@ -121,7 +121,7 @@ class Products extends DolibarrApi
// Show services // Show services
if ($mode == 2) $sql.= " AND t.fk_product_type = 1"; if ($mode == 2) $sql.= " AND t.fk_product_type = 1";
// Add sql filters // Add sql filters
if ($sqlfilters) if ($sqlfilters)
{ {
if (! DolibarrApi::_checkFilters($sqlfilters)) if (! DolibarrApi::_checkFilters($sqlfilters))
{ {
@ -130,7 +130,7 @@ class Products extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
} }
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
if ($page < 0) if ($page < 0)
@ -165,10 +165,10 @@ class Products extends DolibarrApi
} }
return $obj_ret; return $obj_ret;
} }
/** /**
* Create product object * Create product object
* *
* @param array $request_data Request data * @param array $request_data Request data
* @return int ID of product * @return int ID of product
*/ */
@ -179,35 +179,35 @@ class Products extends DolibarrApi
} }
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->product->$field = $value; $this->product->$field = $value;
} }
if ($this->product->create(DolibarrApiAccess::$user) < 0) { if ($this->product->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors)); throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors));
} }
return $this->product->id; return $this->product->id;
} }
/** /**
* Update product * Update product
* *
* @param int $id Id of product to update * @param int $id Id of product to update
* @param array $request_data Datas * @param array $request_data Datas
* @return int * @return int
*/ */
function put($id, $request_data = NULL) function put($id, $request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->produit->creer) { if(! DolibarrApiAccess::$user->rights->produit->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->product->fetch($id); $result = $this->product->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Product not found'); throw new RestException(404, 'Product not found');
} }
if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) { if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -216,16 +216,16 @@ class Products extends DolibarrApi
if ($field == 'id') continue; if ($field == 'id') continue;
$this->product->$field = $value; $this->product->$field = $value;
} }
if($this->product->update($id, DolibarrApiAccess::$user,1,'update')) if($this->product->update($id, DolibarrApiAccess::$user,1,'update'))
return $this->get ($id); return $this->get ($id);
return false; return false;
} }
/** /**
* Delete product * Delete product
* *
* @param int $id Product ID * @param int $id Product ID
* @return array * @return array
*/ */
@ -238,18 +238,18 @@ class Products extends DolibarrApi
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Product not found'); throw new RestException(404, 'Product not found');
} }
if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) { if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
// The Product::delete() method uses the global variable $user. // The Product::delete() method uses the global variable $user.
global $user; global $user;
$user = DolibarrApiAccess::$user; $user = DolibarrApiAccess::$user;
return $this->product->delete(DolibarrApiAccess::$user); return $this->product->delete(DolibarrApiAccess::$user);
} }
/** /**
* Get categories for a product * Get categories for a product
* *
@ -263,9 +263,25 @@ class Products extends DolibarrApi
* *
* @url GET {id}/categories * @url GET {id}/categories
*/ */
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
$categories = new Categories(); {
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'product', $id); if (! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
$categories = new Categorie($this->db);
$result = $categories->getListForItem($id, 'product', $sortfield, $sortorder, $limit, $page);
if (empty($result)) {
throw new RestException(404, 'No category found');
}
if ($result < 0) {
throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
}
return $result;
} }
/** /**
@ -275,17 +291,17 @@ class Products extends DolibarrApi
* @return array Array of cleaned object properties * @return array Array of cleaned object properties
*/ */
function _cleanObjectDatas($object) { function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object); $object = parent::_cleanObjectDatas($object);
unset($object->regeximgext); unset($object->regeximgext);
return $object; return $object;
} }
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Datas to validate * @param array $data Datas to validate
* @return array * @return array
* @throws RestException * @throws RestException

View File

@ -18,6 +18,7 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
//require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; //require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
/** /**
* API class for contacts * API class for contacts
@ -333,9 +334,25 @@ class Contacts extends DolibarrApi
* *
* @url GET {id}/categories * @url GET {id}/categories
*/ */
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
$categories = new Categories(); {
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'contact', $id); if (! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
$categories = new Categorie($this->db);
$result = $categories->getListForItem($id, 'contact', $sortfield, $sortorder, $limit, $page);
if (empty($result)) {
throw new RestException(404, 'No category found');
}
if ($result < 0) {
throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
}
return $result;
} }
/** /**

View File

@ -17,19 +17,20 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
/** /**
* API class for thirdparties * API class for thirdparties
* *
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
* *
*/ */
class Thirdparties extends DolibarrApi class Thirdparties 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( static $FIELDS = array(
'name' 'name'
@ -48,7 +49,7 @@ class Thirdparties extends DolibarrApi
global $db, $conf; global $db, $conf;
$this->db = $db; $this->db = $db;
$this->company = new Societe($this->db); $this->company = new Societe($this->db);
if (! empty($conf->global->SOCIETE_EMAIL_MANDATORY)) { if (! empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
static::$FIELDS[] = 'email'; static::$FIELDS[] = 'email';
} }
@ -61,20 +62,20 @@ class Thirdparties extends DolibarrApi
* *
* @param int $id ID of thirdparty * @param int $id ID of thirdparty
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @throws RestException * @throws RestException
*/ */
function get($id) function get($id)
{ {
if(! DolibarrApiAccess::$user->rights->societe->lire) { if(! DolibarrApiAccess::$user->rights->societe->lire) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->company->fetch($id); $result = $this->company->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Thirdparty not found'); throw new RestException(404, 'Thirdparty not found');
} }
if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -84,14 +85,14 @@ class Thirdparties extends DolibarrApi
/** /**
* List thirdparties * List thirdparties
* *
* Get a list of thirdparties * Get a list of thirdparties
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
* @param int $page Page number * @param int $page Page number
* @param int $mode Set to 1 to show only customers * @param int $mode Set to 1 to show only customers
* Set to 2 to show only prospects * Set to 2 to show only prospects
* Set to 3 to show only those are not customer neither prospect * Set to 3 to show only those are not customer neither prospect
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
@ -99,12 +100,12 @@ class Thirdparties extends DolibarrApi
*/ */
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $sqlfilters = '') { function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $sqlfilters = '') {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
// case of external user, we force socids // case of external user, we force socids
$socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
// If the internal user must only see his customers, force searching by him // If the internal user must only see his customers, force searching by him
$search_sale = 0; $search_sale = 0;
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
@ -112,7 +113,7 @@ class Thirdparties extends DolibarrApi
$sql = "SELECT t.rowid"; $sql = "SELECT t.rowid";
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
$sql.= " FROM ".MAIN_DB_PREFIX."societe as t"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as t";
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
$sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st"; $sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
$sql.= " WHERE t.fk_stcomm = st.id"; $sql.= " WHERE t.fk_stcomm = st.id";
@ -130,7 +131,7 @@ class Thirdparties extends DolibarrApi
$sql .= " AND sc.fk_user = ".$search_sale; $sql .= " AND sc.fk_user = ".$search_sale;
} }
// Add sql filters // Add sql filters
if ($sqlfilters) if ($sqlfilters)
{ {
if (! DolibarrApi::_checkFilters($sqlfilters)) if (! DolibarrApi::_checkFilters($sqlfilters))
{ {
@ -139,7 +140,7 @@ class Thirdparties extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
} }
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
@ -175,7 +176,7 @@ class Thirdparties extends DolibarrApi
} }
return $obj_ret; return $obj_ret;
} }
/** /**
* Create thirdparty object * Create thirdparty object
* *
@ -189,13 +190,13 @@ class Thirdparties extends DolibarrApi
} }
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->company->$field = $value; $this->company->$field = $value;
} }
if ($this->company->create(DolibarrApiAccess::$user) < 0) if ($this->company->create(DolibarrApiAccess::$user) < 0)
throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors)); throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
return $this->company->id; return $this->company->id;
} }
@ -203,20 +204,20 @@ class Thirdparties extends DolibarrApi
* Update thirdparty * Update thirdparty
* *
* @param int $id Id of thirdparty to update * @param int $id Id of thirdparty to update
* @param array $request_data Datas * @param array $request_data Datas
* @return int * @return int
*/ */
function put($id, $request_data = NULL) function put($id, $request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->societe->creer) { if(! DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->company->fetch($id); $result = $this->company->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Thirdparty not found'); throw new RestException(404, 'Thirdparty not found');
} }
if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -225,13 +226,13 @@ class Thirdparties extends DolibarrApi
if ($field == 'id') continue; if ($field == 'id') continue;
$this->company->$field = $value; $this->company->$field = $value;
} }
if($this->company->update($id, DolibarrApiAccess::$user,1,'','','update')) if($this->company->update($id, DolibarrApiAccess::$user,1,'','','update'))
return $this->get ($id); return $this->get ($id);
return false; return false;
} }
/** /**
* Delete thirdparty * Delete thirdparty
* *
@ -252,7 +253,7 @@ class Thirdparties extends DolibarrApi
} }
return $this->company->delete($id); return $this->company->delete($id);
} }
/** /**
* Get categories for a thirdparty * Get categories for a thirdparty
* *
@ -266,9 +267,25 @@ class Thirdparties extends DolibarrApi
* *
* @url GET {id}/categories * @url GET {id}/categories
*/ */
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
$categories = new Categories(); {
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'customer', $id); if (! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
$categories = new Categorie($this->db);
$result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
if (empty($result)) {
throw new RestException(404, 'No category found');
}
if ($result < 0) {
throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
}
return $result;
} }
/** /**
@ -318,24 +335,24 @@ class Thirdparties extends DolibarrApi
* @return array Array of cleaned object properties * @return array Array of cleaned object properties
*/ */
function _cleanObjectDatas($object) { function _cleanObjectDatas($object) {
$object = parent::_cleanObjectDatas($object); $object = parent::_cleanObjectDatas($object);
unset($object->total_ht); unset($object->total_ht);
unset($object->total_tva); unset($object->total_tva);
unset($object->total_localtax1); unset($object->total_localtax1);
unset($object->total_localtax2); unset($object->total_localtax2);
unset($object->total_ttc); unset($object->total_ttc);
return $object; return $object;
} }
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Datas to validate * @param array $data Datas to validate
* @return array * @return array
* *
* @throws RestException * @throws RestException
*/ */
function _validate($data) function _validate($data)