Update api_recruitments.class.php with pagination (#30677)

* Update api_recruitments.class.php with pagination

* Update api_recruitments.class.php

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
ptibogxiv 2024-08-20 09:44:07 +02:00 committed by GitHub
parent a83fb60074
commit 42a79c3dc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -123,7 +123,6 @@ class Recruitments extends DolibarrApi
return $this->_cleanObjectDatas($this->candidature);
}
/**
* List jobpositions
*
@ -135,13 +134,14 @@ class Recruitments extends DolibarrApi
* @param int $page Page number
* @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 /jobposition/
*/
public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
{
$obj_ret = array();
$tmpobject = new RecruitmentJobPosition($this->db);
@ -186,6 +186,9 @@ class Recruitments 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) {
@ -212,6 +215,23 @@ class Recruitments extends DolibarrApi
throw new RestException(503, 'Error when retrieving jobposition 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;
}
@ -225,13 +245,15 @@ class Recruitments extends DolibarrApi
* @param int $limit Limit for list
* @param int $page Page number
* @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 /candidature/
*/
public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
{
global $db, $conf;
@ -278,6 +300,9 @@ class Recruitments 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) {
@ -296,7 +321,7 @@ class Recruitments extends DolibarrApi
$obj = $this->db->fetch_object($result);
$tmp_object = new RecruitmentCandidature($this->db);
if ($tmp_object->fetch($obj->rowid)) {
$obj_ret[] = $this->_cleanObjectDatas($tmp_object);
$obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
}
$i++;
}
@ -304,6 +329,23 @@ class Recruitments extends DolibarrApi
throw new RestException(503, 'Error when retrieving candidature 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;
}