From 227bca1867a0283a79da7eddd8ba779b2932294f Mon Sep 17 00:00:00 2001 From: Jon Bendtsen Date: Wed, 7 Aug 2024 03:00:51 +0200 Subject: [PATCH] NEW API /setup endpoint for getting action triggers (#30538) * New API /setup endpoint for getting action triggers. This makes it easier to know which ID number to specify to enable PROPAL_VALIDATE notifications * introducing page and limit, also because phan was complaining --------- Co-authored-by: Jon Bendtsen --- htdocs/api/class/api_setup.class.php | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 5b1eba9519e..20852515aec 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -51,6 +51,72 @@ class Setup extends DolibarrApi $this->db = $db; } + /** + * Get the list of Action Triggers. + * + * @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 string $elementtype Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...) + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'SO-%')" + * @return array List of extra fields + * + * @url GET actiontriggers + * + * @throws RestException 400 Bad value for sqlfilters + * @throws RestException 503 Error when retrieving list of action triggers + */ + public function getListOfActionTriggers($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $elementtype = '', $sqlfilters = '') + { + $list = array(); + + if ($elementtype == 'thirdparty') { + $elementtype = 'societe'; + } + if ($elementtype == 'contact') { + $elementtype = 'socpeople'; + } + + $sql = "SELECT t.rowid as id, t.elementtype, t.code, t.contexts, t.label, t.description, t.rang"; + $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as t"; + if (!empty($elementtype)) { + $sql .= " WHERE t.elementtype = '".$this->db->escape($elementtype)."'"; + } + // Add sql filters + if ($sqlfilters) { + $errormessage = ''; + $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); + if ($errormessage) { + throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); + } + } + + $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(503, 'Error when retrieving list of action triggers : '.$this->db->lasterror()); + } + + return $list; + } + /** * Get the list of ordering methods. *