From 08c5733298009379f0d892c32177746ce7c83101 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 17 Apr 2018 16:00:19 +0200 Subject: [PATCH] - Added method loadPhrases to FHC_Conroller as a wrapper to load phrases - Added method getLanguage to Person_model to load the language for a user by the given UID - Added comments to method getPhrasesByCategoryAndLanguage of system/Phrase_model, ORDER BY and more fields to SQL statement --- .../controllers/api/v1/system/Phrase.php | 27 ++++++++------- application/core/FHC_Controller.php | 12 ++++++- application/models/person/Person_model.php | 33 +++++++++++++++++++ application/models/system/Phrase_model.php | 9 +++-- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/application/controllers/api/v1/system/Phrase.php b/application/controllers/api/v1/system/Phrase.php index 31decf540..457f75521 100644 --- a/application/controllers/api/v1/system/Phrase.php +++ b/application/controllers/api/v1/system/Phrase.php @@ -22,20 +22,22 @@ class Phrase extends APIv1_Controller public function __construct() { parent::__construct(); + + // Loads the phrases library $this->load->library('PhrasesLib'); } /** - * @return void + * */ public function getPhrase() { $phrase_id = $this->get('phrase_id'); - + if (isset($phrase_id)) { $result = $this->phraseslib->getPhrase($phrase_id); - + $this->response($result, REST_Controller::HTTP_OK); } else @@ -43,9 +45,9 @@ class Phrase extends APIv1_Controller $this->response(); } } - + /** - * @return void + * */ public function getPhrases() { @@ -55,11 +57,11 @@ class Phrase extends APIv1_Controller $orgeinheit_kurzbz = $this->get('orgeinheit_kurzbz'); $orgform_kurzbz = $this->get('orgform_kurzbz'); $blockTags = $this->get('blockTags'); - + if (isset($app) && isset($sprache)) { $result = $this->phraseslib->getPhrases($app, $sprache, $phrase, $orgeinheit_kurzbz, $orgform_kurzbz, $blockTags); - + $this->response($result, REST_Controller::HTTP_OK); } else @@ -69,7 +71,7 @@ class Phrase extends APIv1_Controller } /** - * @return void + * */ public function postPhrase() { @@ -83,7 +85,7 @@ class Phrase extends APIv1_Controller { $result = $this->PhraseModel->insert($this->post()); } - + $this->response($result, REST_Controller::HTTP_OK); } else @@ -91,9 +93,12 @@ class Phrase extends APIv1_Controller $this->response(); } } - + + /** + * + */ private function _validate($phrase = null) { return false; } -} \ No newline at end of file +} diff --git a/application/core/FHC_Controller.php b/application/core/FHC_Controller.php index 5e252998e..f6f138441 100644 --- a/application/core/FHC_Controller.php +++ b/application/core/FHC_Controller.php @@ -10,7 +10,17 @@ class FHC_Controller extends CI_Controller public function __construct() { parent::__construct(); - + $this->load->helper('fhcauth'); } + + /** + * Wrapper to load phrases using the PhrasesLib + * NOTE: The library is loaded with the alias 'p', so must me used with this alias in the rest of the code. + * EX: $this->p->t(, ) + */ + public function loadPhrases($categories, $language = null) + { + $this->load->library('PhrasesLib', array($categories, $language), 'p'); + } } diff --git a/application/models/person/Person_model.php b/application/models/person/Person_model.php index 790f1b4db..2647d6105 100644 --- a/application/models/person/Person_model.php +++ b/application/models/person/Person_model.php @@ -213,4 +213,37 @@ class Person_model extends DB_Model return $this->loadWhere(array('uid' => $uid)); } + + /** + * Retrives the language of the user by the UID + * Gets all the persons related to the given UID and starting from the most recent person in DB + * tries to find a valid language (!= null), if found is returned, otherwise is returned the + * default global language of the system + */ + public function getLanguage($uid) + { + $language = DEFAULT_LANGUAGE; + + $this->addJoin('public.tbl_benutzer', 'person_id'); + $this->addOrder('public.tbl_person.updateamum', 'DESC'); + $this->addOrder('public.tbl_person.insertvon', 'DESC'); + + $persons = $this->loadWhere(array('uid' => $uid)); + + if (hasData($persons)) + { + for ($i = 0; $i < count($persons->retval); $i++) + { + $person = $persons->retval[$i]; + + if (!empty($person->sprache)) + { + $language = $person->sprache; + break; + } + } + } + + return $language; + } } diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index 7d2856932..a0363eb5a 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -62,15 +62,18 @@ class Phrase_model extends DB_Model } /** - * + * Loads phrases using category(s) and language as keys + * The retrived fields are category, phrase, orgeinheit_kurzbz, orgform_kurzbz and text + * They are ordered by p.category, p.phrase, pt.orgeinheit_kurzbz DESC and pt.orgform_kurzbz DESC' */ public function getPhrasesByCategoryAndLanguage($categories, $language) { - $query = 'SELECT p.category, p.phrase, pt.text + $query = 'SELECT p.category, p.phrase, pt.orgeinheit_kurzbz, pt.orgform_kurzbz, pt.text FROM system.tbl_phrase p INNER JOIN system.tbl_phrasentext pt USING(phrase_id) WHERE p.category IN ? - AND pt.sprache = ?'; + AND pt.sprache = ? + ORDER BY p.category, p.phrase, pt.orgeinheit_kurzbz DESC, pt.orgform_kurzbz DESC'; return $this->execQuery($query, array($categories, $language)); }