From f613752bd3ac0e043d004461d94e9c13c66a9752 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 20 Aug 2024 10:18:15 +0200 Subject: [PATCH] Update api_contracts.class.php with pagination (#30681) Co-authored-by: Laurent Destailleur --- htdocs/contrat/class/api_contracts.class.php | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/htdocs/contrat/class/api_contracts.class.php b/htdocs/contrat/class/api_contracts.class.php index 1503a214162f8..eaaeb04daa04b 100644 --- a/htdocs/contrat/class/api_contracts.class.php +++ b/htdocs/contrat/class/api_contracts.class.php @@ -82,8 +82,6 @@ public function get($id) return $this->_cleanObjectDatas($this->contract); } - - /** * List contracts * @@ -96,12 +94,13 @@ public function get($id) * @param string $thirdparty_ids Thirdparty ids to filter contracts of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} * @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 $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names - * @return array Array of contract objects + * @param bool $pagination_data If this parameter is set to true the response will include pagination data. Default value is false. Page starts from 0* + * @return array Array of order objects * * @throws RestException 404 Not found * @throws RestException 503 Error */ - public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '') + public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false) { global $db, $conf; @@ -143,6 +142,9 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, } } + //this query will return total orders with the filters given + $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql); + $sql .= $this->db->order($sortfield, $sortorder); if ($limit) { if ($page < 0) { @@ -172,6 +174,23 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, throw new RestException(503, 'Error when retrieve contrat list : '.$this->db->lasterror()); } + //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit) + if ($pagination_data) { + $totalsResult = $this->db->query($sqlTotals); + $total = $this->db->fetch_object($totalsResult)->total; + + $tmp = $obj_ret; + $obj_ret = []; + + $obj_ret['data'] = $tmp; + $obj_ret['pagination'] = [ + 'total' => (int) $total, + 'page' => $page, //count starts from 0 + 'page_count' => ceil((int) $total / $limit), + 'limit' => $limit + ]; + } + return $obj_ret; }