From 63611ab717bcc3e10190572b89f40528be2284fe Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 11 Jan 2022 20:24:23 +0100 Subject: [PATCH] NEW add setup/establishments API (need for recruitment API) --- htdocs/api/class/api_setup.class.php | 62 +++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index f4e17f42556..d169c8220ec 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -4,7 +4,7 @@ * Copyright (C) 2017 Regis Houssin * Copyright (C) 2017 Neil Orley * Copyright (C) 2018-2021 Frédéric France - * Copyright (C) 2018-2021 Thibault FOUCART + * Copyright (C) 2018-2022 Thibault FOUCART * * * This program is free software; you can redistribute it and/or modify @@ -26,7 +26,7 @@ use Luracast\Restler\RestException; require_once DOL_DOCUMENT_ROOT.'/main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php'; - +require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php'; /** * API class for dictionaries @@ -1721,6 +1721,64 @@ class Setup extends DolibarrApi return $this->_cleanObjectDatas($mysoc); } + /** + * Get the list of establishments. + * + * @return array List of establishments + * + * @url GET /establishments + * + * @throws RestException + */ + public function getEstablishments() + { + $list = array(); + + $sql = "SELECT e.rowid, e.rowid as ref, e.label, e.address, e.zip, e.town, e.status"; + $sql .= " FROM ".MAIN_DB_PREFIX."establishment as e"; + $sql .= " WHERE e.entity IN (".getEntity('establishment').')'; + // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'"; + // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'"; + // Add sql filters + + $result = $this->db->query($sql); + + if ($result) { + $num = $this->db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + for ($i = 0; $i < $min; $i++) { + $list[] = $this->db->fetch_object($result); + } + } else { + throw new RestException(503, 'Error when retrieving list of establishments : '.$this->db->lasterror()); + } + + return $list; + } + + /** + * Get establishment by ID. + * + * @param int $id ID of establishment + * @return array Array of cleaned object properties + * + * @url GET establishments/{id} + * + * @throws RestException + */ + public function getEtablishmentByID($id) + { + $establishment = new Establishment($this->db); + + $result = $establishment->fetch($id); + if ($result < 0) { + throw new RestException(503, 'Error when retrieving state : '.$establishment->error); + } elseif ($result == 0) { + throw new RestException(404, 'Establishment not found'); + } + + return $this->_cleanObjectDatas($establishment); + } /** * Get value of a setup variables