Fix: refactorization of "getListForItem" function
This commit is contained in:
parent
ed5dca652d
commit
4537e5472b
@ -20,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
|
||||||
@ -362,8 +363,8 @@ class Members extends DolibarrApi
|
|||||||
* Get categories for a member
|
* Get categories for a member
|
||||||
*
|
*
|
||||||
* @param int $id ID of member
|
* @param int $id ID of member
|
||||||
* @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
|
||||||
*
|
*
|
||||||
@ -373,11 +374,19 @@ class Members extends DolibarrApi
|
|||||||
*/
|
*/
|
||||||
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 Categorie($this->db);
|
||||||
|
|
||||||
$categories = new Categories();
|
$result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
|
||||||
|
|
||||||
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'member', $id);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
*
|
*
|
||||||
|
|||||||
@ -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
|
||||||
*
|
*
|
||||||
|
|||||||
@ -265,11 +265,19 @@ class Products extends DolibarrApi
|
|||||||
*/
|
*/
|
||||||
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 Categorie($this->db);
|
||||||
|
|
||||||
$categories = new Categories();
|
$result = $categories->getListForItem($id, 'product', $sortfield, $sortorder, $limit, $page);
|
||||||
|
|
||||||
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'product', $id);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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
|
||||||
@ -335,11 +336,19 @@ class Contacts extends DolibarrApi
|
|||||||
*/
|
*/
|
||||||
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 Categorie($this->db);
|
||||||
|
|
||||||
$categories = new Categories();
|
$result = $categories->getListForItem($id, 'contact', $sortfield, $sortorder, $limit, $page);
|
||||||
|
|
||||||
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'contact', $id);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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
|
||||||
@ -268,11 +269,19 @@ class Thirdparties extends DolibarrApi
|
|||||||
*/
|
*/
|
||||||
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 Categorie($this->db);
|
||||||
|
|
||||||
$categories = new Categories();
|
$result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
|
||||||
|
|
||||||
return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'customer', $id);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user