From c5fa6eac585caf1ae8794ecfdb1c5c8733faa1aa Mon Sep 17 00:00:00 2001 From: bahfir abbes Date: Sun, 13 Sep 2020 20:15:25 +0100 Subject: [PATCH 1/6] allow access to adherent_extrafields table fields in printFieldListWhere hook; --- htdocs/adherents/subscription/list.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/htdocs/adherents/subscription/list.php b/htdocs/adherents/subscription/list.php index 509f00d2705..4d0e2ec7439 100644 --- a/htdocs/adherents/subscription/list.php +++ b/htdocs/adherents/subscription/list.php @@ -147,11 +147,13 @@ $sql .= " c.rowid as crowid, c.fk_type, c.subscription,"; $sql .= " c.dateadh, c.datef, c.datec as date_creation, c.tms as date_update,"; $sql .= " c.fk_bank as bank, c.note,"; $sql .= " b.fk_account"; -$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d, ".MAIN_DB_PREFIX."subscription as c"; +$sql .= " FROM ".MAIN_DB_PREFIX."adherent as d"; +$sql .= " JOIN ".MAIN_DB_PREFIX."subscription as c on d.rowid = c.fk_adherent"; +$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ef on (d.rowid = ef.fk_object)"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON c.fk_bank=b.rowid"; -$sql .= " WHERE d.rowid = c.fk_adherent"; -$sql .= " AND d.entity IN (".getEntity('adherent').")"; -if (isset($date_select) && $date_select != '') { +$sql .= " WHERE d.entity IN (".getEntity('adherent').")"; +if (isset($date_select) && $date_select != '') +{ $sql .= " AND c.dateadh >= '".$date_select."-01-01 00:00:00'"; $sql .= " AND c.dateadh < '".($date_select + 1)."-01-01 00:00:00'"; } From 557a6863ba140825d2fef44bc43c4efe3b7bc0a1 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Mon, 14 Sep 2020 10:45:20 +0200 Subject: [PATCH 2/6] NEW get state by REST API --- htdocs/api/class/api_setup.class.php | 128 ++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 58b8954762c..e7ae6a39528 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -1,10 +1,11 @@ * Copyright (C) 2016 Laurent Destailleur * Copyright (C) 2017 Regis Houssin * Copyright (C) 2017 Neil Orley * Copyright (C) 2018 Frédéric France - * Copyright (C) 2018-2019 Thibault FOUCART + * Copyright (C) 2018-2020 Thibault FOUCART * * * This program is free software; you can redistribute it and/or modify @@ -24,6 +25,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'; /** @@ -167,6 +169,107 @@ class Setup extends DolibarrApi return $list; } + + /** + * Get the list of states. + * + * The names of the states will be translated to the given language if + * the $lang parameter is provided. The value of $lang must be a language + * code supported by Dolibarr, for example 'en_US' or 'fr_FR'. + * The returned list is sorted by state ID. + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Number of items per page + * @param int $page Page number (starting from zero) + * @param string $filter To filter the countries by name + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)" + * @return array List of countries + * + * @url GET dictionary/states + * + * @throws RestException + */ + public function getListOfStates($sortfield = "code_departement", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $sqlfilters = '') + { + $list = array(); + + // Note: The filter is not applied in the SQL request because it must + // be applied to the translated names, not to the names in database. + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_departements as t"; + $sql .= " WHERE 1 = 1"; + // Add sql filters + if ($sqlfilters) + { + if (!DolibarrApi::_checkFilters($sqlfilters)) + { + throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); + } + $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; + $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + + $sql .= $this->db->order($sortfield, $sortorder); + + if ($limit) { + if ($page < 0) { + $page = 0; + } + $offset = $limit * $page; + + $sql .= $this->db->plimit($limit, $offset); + } + + $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++) { + $obj = $this->db->fetch_object($result); + $state = new Cstate($this->db); + if ($state->fetch($obj->rowid) > 0) { + if (empty($filter) || stripos($state->label, $filter) !== false) { + $list[] = $this->_cleanObjectDatas($state); + } + } + } + } else { + throw new RestException(503, 'Error when retrieving list of states'); + } + + return $list; + } + + /** + * Get state by ID. + * + * @param int $id ID of state + * @return array Array of cleaned object properties + * + * @url GET dictionary/states/{id} + * + * @throws RestException + */ + public function getStateByID($id) + { + return $this->_fetchCstate($id, ''); + } + + /** + * Get state by Code. + * + * @param string $code Code of state + * @return array Array of cleaned object properties + * + * @url GET dictionary/states/byCode/{code} + * + * @throws RestException + */ + public function getStateByCode($code) + { + return $this->_fetchCstate('', $code); + } /** * Get the list of countries. @@ -294,6 +397,29 @@ class Setup extends DolibarrApi { return $this->_fetchCcountry('', '', $iso, $lang); } + + /** + * Get state. + * + * @param int $id ID of state + * @param string $code Code of state + * @return array Array of cleaned object properties + * + * @throws RestException + */ + private function _fetchCstate($id, $code = '') + { + $state = new Cstate($this->db); + + $result = $state->fetch($id, $code); + if ($result < 0) { + throw new RestException(503, 'Error when retrieving state : '.$state->error); + } elseif ($result == 0) { + throw new RestException(404, 'State not found'); + } + + return $this->_cleanObjectDatas($state); + } /** * Get country. From e98ae029e3a484bceff95397c09021734f5aecb5 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Mon, 14 Sep 2020 10:46:19 +0200 Subject: [PATCH 3/6] Update cstate.class.php --- htdocs/core/class/cstate.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/cstate.class.php b/htdocs/core/class/cstate.class.php index 34ef707b674..c107601ba98 100644 --- a/htdocs/core/class/cstate.class.php +++ b/htdocs/core/class/cstate.class.php @@ -50,7 +50,7 @@ class Cstate // extends CommonObject public $id; public $code_departement; - + public $code; /** * @var string * @deprecated @@ -167,8 +167,10 @@ class Cstate // extends CommonObject $obj = $this->db->fetch_object($resql); $this->id = $obj->rowid; - $this->code_departement = $obj->code_departement; - $this->nom = $obj->nom; + $this->code_departement = $obj->code_departement; //deprecated + $this->code = $obj->code_departement; + $this->nom = $obj->nom; //deprecated + $this->name = $obj->nom; $this->active = $obj->active; } $this->db->free($resql); From 75a96780864a56bdfa67b3c705a1855239bf1f39 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 14 Sep 2020 08:48:04 +0000 Subject: [PATCH 4/6] Fixing style errors. --- htdocs/api/class/api_setup.class.php | 6 +++--- htdocs/core/class/cstate.class.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index e7ae6a39528..1bb3995954e 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -169,7 +169,7 @@ class Setup extends DolibarrApi return $list; } - + /** * Get the list of states. * @@ -240,7 +240,7 @@ class Setup extends DolibarrApi return $list; } - + /** * Get state by ID. * @@ -397,7 +397,7 @@ class Setup extends DolibarrApi { return $this->_fetchCcountry('', '', $iso, $lang); } - + /** * Get state. * diff --git a/htdocs/core/class/cstate.class.php b/htdocs/core/class/cstate.class.php index c107601ba98..80c47591fbb 100644 --- a/htdocs/core/class/cstate.class.php +++ b/htdocs/core/class/cstate.class.php @@ -168,9 +168,9 @@ class Cstate // extends CommonObject $this->id = $obj->rowid; $this->code_departement = $obj->code_departement; //deprecated - $this->code = $obj->code_departement; + $this->code = $obj->code_departement; $this->nom = $obj->nom; //deprecated - $this->name = $obj->nom; + $this->name = $obj->nom; $this->active = $obj->active; } $this->db->free($resql); From f09655ec6c36fcd1c8eda778055090e8dac0afe2 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Mon, 14 Sep 2020 10:53:38 +0200 Subject: [PATCH 5/6] Update api_setup.class.php --- htdocs/api/class/api_setup.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 1bb3995954e..ba36d4c638e 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -1,5 +1,4 @@ * Copyright (C) 2016 Laurent Destailleur * Copyright (C) 2017 Regis Houssin From 584a02bc9ae74e7d1071087384f7d964144744de Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 16 Sep 2020 01:11:47 +0200 Subject: [PATCH 6/6] Update api_setup.class.php --- htdocs/api/class/api_setup.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index ba36d4c638e..f0812262e4b 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -170,7 +170,7 @@ class Setup extends DolibarrApi } /** - * Get the list of states. + * Get the list of states/provinces. * * The names of the states will be translated to the given language if * the $lang parameter is provided. The value of $lang must be a language