mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
Allow params "limit" and "page" in REST-API
PR #25, code author Dennis Lassiter, many thanks ! Was commited to develop first but decided to retrofit this on support/2.5 (N°1449) (cherry picked from commitfd55bdf9a8) (cherry picked from commit49e31ddb3d)
This commit is contained in:
@@ -436,8 +436,10 @@ class CoreServices implements iRestServiceProvider
|
||||
$key = RestUtils::GetMandatoryParam($aParams, 'key');
|
||||
$aShowFields = RestUtils::GetFieldList($sClass, $aParams, 'output_fields');
|
||||
$bExtendedOutput = (RestUtils::GetOptionalParam($aParams, 'output_fields', '*') == '*+');
|
||||
$iLimit = (int)RestUtils::GetOptionalParam($aParams, 'limit', 0);
|
||||
$iPage = (int)RestUtils::GetOptionalParam($aParams, 'page', 1);
|
||||
|
||||
$oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key);
|
||||
$oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key, $iLimit, self::getOffsetFromLimitAndPage($iLimit, $iPage));
|
||||
$sTargetClass = $oObjectSet->GetFilter()->GetClass();
|
||||
|
||||
if (UserRights::IsActionAllowed($sTargetClass, UR_ACTION_READ) != UR_ALLOWED_YES)
|
||||
@@ -450,6 +452,11 @@ class CoreServices implements iRestServiceProvider
|
||||
$oResult->code = RestResult::UNAUTHORIZED;
|
||||
$oResult->message = "The current user does not have enough permissions for exporting data of class $sTargetClass";
|
||||
}
|
||||
elseif ($iPage < 1)
|
||||
{
|
||||
$oResult->code = RestResult::INVALID_PAGE;
|
||||
$oResult->message = "The request page number is not valid. It must be an integer greater than 0";
|
||||
}
|
||||
else
|
||||
{
|
||||
while ($oObject = $oObjectSet->Fetch())
|
||||
@@ -774,4 +781,15 @@ class CoreServices implements iRestServiceProvider
|
||||
$oResult->message = $sRes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iLimit
|
||||
* @param int $iPage
|
||||
*
|
||||
* @return int Offset for a given page number
|
||||
*/
|
||||
protected static function getOffsetFromLimitAndPage($iLimit, $iPage)
|
||||
{
|
||||
return $iLimit * max(0, $iPage - 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user