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

@ -166,94 +166,6 @@ class Categories extends DolibarrApi
return $obj_ret; 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);
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;
}
/** /**
* Create category object * Create category object
* *

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

@ -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;
} }
/** /**

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,6 +17,7 @@
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
@ -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;
} }
/** /**