From fd55bdf9a8874ebf5ee6590656f5d0669cc25133 Mon Sep 17 00:00:00 2001 From: Dennis Lassiter Date: Fri, 8 Feb 2019 21:48:52 -0500 Subject: [PATCH] Allow params "limit" and "page" in REST-API PR #25, code author Dennis Lassiter, many thanks ! --- application/applicationextension.inc.php | 10 ++++++++-- core/restservices.class.inc.php | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/application/applicationextension.inc.php b/application/applicationextension.inc.php index 030ab6f5e..90aeb4f8e 100644 --- a/application/applicationextension.inc.php +++ b/application/applicationextension.inc.php @@ -825,6 +825,10 @@ class RestResult * Result: the requested operation cannot be performed because it can cause data (integrity) loss */ const UNSAFE = 12; + /** + * Result: the request page number is not valid. It must be an integer greater than 0 + */ + const INVALID_PAGE = 13; /** * Result: the operation could not be performed, see the message for troubleshooting */ @@ -1091,10 +1095,12 @@ class RestUtils * * @param string $sClass Name of the class * @param mixed $key Either search criteria (substructure), or an object or an OQL string. + * @param int $limit The limit of results to return + * @param int $offset The offset of results to return * @return DBObjectSet The search result set * @throws Exception If the input structure is not valid */ - public static function GetObjectSetFromKey($sClass, $key) + public static function GetObjectSetFromKey($sClass, $key, $limit = 0, $offset = 0) { if (is_object($key)) { @@ -1128,7 +1134,7 @@ class RestUtils { throw new Exception("Wrong format for key"); } - $oObjectSet = new DBObjectSet($oSearch); + $oObjectSet = new DBObjectSet($oSearch, array(), array(), null, $limit, $offset); return $oObjectSet; } diff --git a/core/restservices.class.inc.php b/core/restservices.class.inc.php index 916dd5f72..38852b7fe 100644 --- a/core/restservices.class.inc.php +++ b/core/restservices.class.inc.php @@ -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', '*') == '*+'); + $limit = (int) RestUtils::GetOptionalParam($aParams, 'limit', 0); + $page = (int) RestUtils::GetOptionalParam($aParams, 'page', 1); - $oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key); + $oObjectSet = RestUtils::GetObjectSetFromKey($sClass, $key, $limit, self::getOffsetFromLimitAndPage($limit, $page)); $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 ($page < 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,16 @@ class CoreServices implements iRestServiceProvider $oResult->message = $sRes; } } + + /** + * Returns the Offset for a given page number + * + * @param int $limit + * @param int $page + * @return int + */ + protected static function getOffsetFromLimitAndPage($limit, $page) + { + return $limit * max(0, $page - 1); + } }