Skip to content

Commit

Permalink
Update api_contracts.class.php with pagination (Dolibarr#30681)
Browse files Browse the repository at this point in the history
Co-authored-by: Laurent Destailleur <[email protected]>
  • Loading branch information
ptibogxiv and eldy authored Aug 20, 2024
1 parent 465b7dc commit f613752
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions htdocs/contrat/class/api_contracts.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public function get($id)
return $this->_cleanObjectDatas($this->contract);
}



/**
* List contracts
*
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit f613752

Please sign in to comment.