Update api_knowledgemanagement.class.php with pagination (#30664)

This commit is contained in:
ptibogxiv 2024-08-18 16:20:45 +02:00 committed by GitHub
parent c9151a235e
commit 4e2842ca27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -128,13 +128,14 @@ class KnowledgeManagement extends DolibarrApi
* @param int $category Use this param to filter list by category
* @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
* @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
*
* @url GET /knowledgerecords/
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '')
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
{
$obj_ret = array();
$tmpobject = new KnowledgeRecord($this->db);
@ -187,6 +188,9 @@ class KnowledgeManagement extends DolibarrApi
}
}
//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) {
@ -213,6 +217,23 @@ class KnowledgeManagement extends DolibarrApi
throw new RestException(503, 'Error when retrieving knowledgerecord 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;
}