From ba2c450bd27e072fbe576156072e8b9c8e155095 Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Wed, 7 Aug 2024 15:43:20 +0200 Subject: [PATCH] uses the codeigniter querybuilder to construct the active ampeln query --- .../controllers/api/frontend/v1/Ampeln.php | 2 +- application/models/content/Ampel_model.php | 74 ++++++++++++++----- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/application/controllers/api/frontend/v1/Ampeln.php b/application/controllers/api/frontend/v1/Ampeln.php index 5560d1c69..e0ffb4df5 100644 --- a/application/controllers/api/frontend/v1/Ampeln.php +++ b/application/controllers/api/frontend/v1/Ampeln.php @@ -75,7 +75,7 @@ class Ampeln extends FHCAPI_Controller $userAmpeln = array(); // fetch active ampeln - $activeAmpeln = $this->AmpelModel->active(false, $this->uid); + $activeAmpeln = $this->AmpelModel->openActive($this->uid, false); $activeAmpeln = $this->getDataOrTerminateWithError($activeAmpeln); diff --git a/application/models/content/Ampel_model.php b/application/models/content/Ampel_model.php index f5404ecb2..1f6cf9f93 100755 --- a/application/models/content/Ampel_model.php +++ b/application/models/content/Ampel_model.php @@ -21,10 +21,10 @@ class Ampel_model extends DB_Model public function active($email = false, $uid = null) { $userLanguage = getUserLanguage(); - - $bestaetigt = ''; + $selectStatement='*,beschreibung[('.$this->getLanguageIndex($this->escape($userLanguage)).')] as beschreibung_trans, buttontext[('.$this->getLanguageIndex($this->escape($userLanguage)).')] as buttontext_trans'; + if($uid != null ){ - $bestaetigt = ', + $selectStatement .= ', COALESCE(( SELECT true FROM public.tbl_ampel_benutzer_bestaetigt a @@ -32,21 +32,59 @@ class Ampel_model extends DB_Model AND uid = ' . $this->escape($uid) . ' LIMIT 1), false) as bestaetigt'; } - $parametersArray = []; - $query = ' - SELECT *, beschreibung[('.$this->getLanguageIndex($this->escape($userLanguage)).')] as beschreibung_trans, buttontext[('.$this->getLanguageIndex($this->escape($userLanguage)).')] as buttontext_trans - '.$bestaetigt - .' - FROM public.tbl_ampel - WHERE'; - - if ($email === true) - { - $parametersArray['email'] = $email; - $query .= ' email = ? AND'; + $this->addSelect($selectStatement); + $whereStatement=''; + + if ($email === true) { + $whereStatement .= ' email = '.$this->escape($email).' AND'; } - $query .= '( + $whereStatement .= + '( + ( + (NOW()<(deadline+(COALESCE(verfallszeit,0) || \' days\')::interval)::date) + OR (verfallszeit IS NULL) + ) + AND + ( + (NOW()>(deadline-(COALESCE(vorlaufzeit,0) || \' days\')::interval)::date) + OR (vorlaufzeit IS NULL AND NOW() < deadline) + ) + )'; + + $this->addOrder('deadline', 'DESC'); + return $this->loadWhere($whereStatement); + + } + + public function openActive($uid, $email = false) + { + $userLanguage = getUserLanguage(); + $selectStatement = '*,beschreibung[(' . $this->getLanguageIndex($this->escape($userLanguage)) . ')] as beschreibung_trans, buttontext[(' . $this->getLanguageIndex($this->escape($userLanguage)) . ')] as buttontext_trans'; + + + $selectStatement .= ', + COALESCE(( + SELECT true + FROM public.tbl_ampel_benutzer_bestaetigt a + WHERE a.ampel_id = ' . $this->dbTable . '.ampel_id + AND uid = ' . $this->escape($uid) . ' LIMIT 1), false) as bestaetigt'; + + $this->addSelect($selectStatement); + $whereStatement = ''; + + if ($email === true) { + $whereStatement .= ' email = ' . $this->escape($email) . ' AND'; + } + + $whereStatement .= + ' + (COALESCE(( + SELECT true + FROM public.tbl_ampel_benutzer_bestaetigt a + WHERE a.ampel_id = ' . $this->dbTable . '.ampel_id + AND uid = ' . $this->escape($uid) . ' LIMIT 1), false) = FALSE) AND + ( ( (NOW()<(deadline+(COALESCE(verfallszeit,0) || \' days\')::interval)::date) OR (verfallszeit IS NULL) @@ -58,9 +96,9 @@ class Ampel_model extends DB_Model ) )'; - $query .= ' ORDER BY deadline DESC'; + $this->addOrder('deadline', 'DESC'); + return $this->loadWhere($whereStatement); - return $this->execQuery($query, $parametersArray); } /**