From f05463b239b23ca38b10e3878b4bb462c83cf7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Mon, 4 Nov 2019 21:01:30 +0100 Subject: [PATCH 1/2] API Get the list of ordering methods --- htdocs/api/class/api_setup.class.php | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 50102b76b93..2201ccf5bae 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -44,6 +44,68 @@ class Setup extends DolibarrApi global $db; $this->db = $db; } + + /** + * Get the list of ordering methods. + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Number of items per page + * @param int $page Page number {@min 0} + * @param int $active Payment type is active or not {@min 0} {@max 1} + * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'CHQ')" + * + * @url GET dictionary/ordering_methods + * + * @return array [List of ordering methods] + * + * @throws 400 RestException + * @throws 200 OK + */ + public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '') + { + $list = array(); + + $sql = "SELECT rowid, code, libelle as label, module"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_input_method as t"; + $sql.= " WHERE t.active = ".$active; + // Add sql filters + if ($sqlfilters) + { + if (! DolibarrApi::_checkFilters($sqlfilters)) + { + throw new RestException(400, '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++) { + $list[] = $this->db->fetch_object($result); + } + } else { + throw new RestException(400, $this->db->lasterror()); + } + + return $list; + } /** * Get the list of payments types. From 50e51852979f15376d8dcbecdc513c04cbd564a7 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 4 Nov 2019 20:02:11 +0000 Subject: [PATCH 2/2] Fixing style errors. --- htdocs/api/class/api_setup.class.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 2201ccf5bae..f91eb5395d6 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -44,7 +44,7 @@ class Setup extends DolibarrApi global $db; $this->db = $db; } - + /** * Get the list of ordering methods. * @@ -65,7 +65,7 @@ class Setup extends DolibarrApi public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '') { $list = array(); - + $sql = "SELECT rowid, code, libelle as label, module"; $sql.= " FROM ".MAIN_DB_PREFIX."c_input_method as t"; $sql.= " WHERE t.active = ".$active; @@ -79,21 +79,21 @@ class Setup extends DolibarrApi $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)); @@ -103,7 +103,7 @@ class Setup extends DolibarrApi } else { throw new RestException(400, $this->db->lasterror()); } - + return $list; }