Fix: missing include api_categories class

This commit is contained in:
Regis Houssin 2017-10-03 13:07:07 +02:00
parent db8d27f0fe
commit f8f4ff6d21
5 changed files with 151 additions and 116 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
@ -357,4 +358,26 @@ 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)
{
require_once DOL_DOCUMENT_ROOT.'/categories/class/api_categories.class.php';
$categories = new Categories();
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'member', $id);
}
} }

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,7 +129,7 @@ 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); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
if ($page < 0) if ($page < 0)
@ -168,7 +168,7 @@ class Categories extends DolibarrApi
/** /**
* List categories of an entity * List categories of an entity
* *
* Note: This method is not directly exposed in the API, it is used * Note: This method is not directly exposed in the API, it is used
* in the GET /xxx/{id}/categories requests. * in the GET /xxx/{id}/categories requests.
* *
@ -184,9 +184,9 @@ class Categories extends DolibarrApi
*/ */
function getListForItem($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $type='customer', $item = 0) { function getListForItem($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $type='customer', $item = 0) {
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);
} }
@ -250,13 +250,13 @@ 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 +268,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 +280,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 +304,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 +326,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 +342,8 @@ class Categories extends DolibarrApi
) )
); );
} }
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
@ -351,9 +351,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 +394,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

@ -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,13 @@ 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); require_once DOL_DOCUMENT_ROOT.'/categories/class/api_categories.class.php';
$categories = new Categories();
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'product', $id);
} }
/** /**
@ -275,17 +279,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

@ -333,8 +333,12 @@ 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)
{
require_once DOL_DOCUMENT_ROOT.'/categories/class/api_categories.class.php';
$categories = new Categories(); $categories = new Categories();
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'contact', $id); return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'contact', $id);
} }

View File

@ -21,15 +21,15 @@
/** /**
* 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 +48,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 +61,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 +84,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 +99,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 +112,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 +130,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 +139,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 +175,7 @@ class Thirdparties extends DolibarrApi
} }
return $obj_ret; return $obj_ret;
} }
/** /**
* Create thirdparty object * Create thirdparty object
* *
@ -189,13 +189,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 +203,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 +225,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 +252,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,8 +266,12 @@ 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)
{
require_once DOL_DOCUMENT_ROOT.'/categories/class/api_categories.class.php';
$categories = new Categories(); $categories = new Categories();
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'customer', $id); return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'customer', $id);
} }
@ -318,24 +322,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)