From 2568d8f91252212d037008add6c8161a9b6498f6 Mon Sep 17 00:00:00 2001 From: paolo Date: Fri, 15 Jul 2016 13:38:09 +0200 Subject: [PATCH] - Removed the parameter "sprache" from controller Message - Added method escape to class DB_Model - Changed method sendMessageVorlage of MessageLib to send messages trying to use the same language of the receiver, and improved message errors - Added OrganisationseinheitLib to handle a recursive search using in the organisation tree - Removed from PhrasesLib the method loadVorlagetext because is already present in VorlageLib - Changed method loadVorlagetext of VorlageLib, now it searches the template using the organisation tree - Added method getOneLevel to model Organisationseinheit_model to get one level of the organisation tree --- .../controllers/api/v1/system/Message.php | 5 - application/core/DB_Model.php | 12 ++ application/libraries/MessageLib.php | 142 ++++++++++-------- .../libraries/OrganisationseinheitLib.php | 60 ++++++++ application/libraries/PhrasesLib.php | 21 +-- application/libraries/VorlageLib.php | 38 ++++- .../Organisationseinheit_model.php | 48 +++++- 7 files changed, 236 insertions(+), 90 deletions(-) create mode 100644 application/libraries/OrganisationseinheitLib.php diff --git a/application/controllers/api/v1/system/Message.php b/application/controllers/api/v1/system/Message.php index e64badb33..1bcb8f96b 100644 --- a/application/controllers/api/v1/system/Message.php +++ b/application/controllers/api/v1/system/Message.php @@ -126,7 +126,6 @@ class Message extends APIv1_Controller $this->post()['vorlage_kurzbz'], $this->post()['oe_kurzbz'], $this->post()['data'], - $this->post()['sprache'], isset($this->post()['relationmessage_id']) ? $this->post()['relationmessage_id'] : null, isset($this->post()['orgform_kurzbz']) ? $this->post()['orgform_kurzbz'] : null ); @@ -213,10 +212,6 @@ class Message extends APIv1_Controller { return $this->_error('data is not set'); } - if (!isset($message['sprache'])) - { - return $this->_error('sprache is not set'); - } return $this->_success('Input data are valid'); } diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 4c075deb9..954336898 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -324,6 +324,18 @@ class DB_Model extends FHC_Model { $this->db->reset_query(); } + + /** --------------------------------------------------------------- + * This method call the method escape from class CI_DB_driver, therefore: + * this method determines the data type so that it can escape only string data. + * It also automatically adds single quotes around the data so you don’t have to + * + * @return void + */ + public function escape($value) + { + return $this->db->escape($value); + } /** --------------------------------------------------------------- * Convert PG-Boolean to PHP-Boolean diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index fc43c72c8..b75a43a7a 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -268,88 +268,110 @@ class MessageLib * @param integer $priority * @return array */ - function sendMessageVorlage($sender_id, $receiver_id, $vorlage_kurzbz, $oe_kurzbz, $data, $sprache, $relationmessage_id = null, $orgform_kurzbz = null) + function sendMessageVorlage($sender_id, $receiver_id, $vorlage_kurzbz, $oe_kurzbz, $data, $relationmessage_id = null, $orgform_kurzbz = null) { if (!is_numeric($sender_id) || !is_numeric($receiver_id)) return $this->_invalid_id(MSG_ERR_INVALID_MSG_ID); - $result = $this->ci->vorlagelib->loadVorlagetext($vorlage_kurzbz, $oe_kurzbz, $orgform_kurzbz, $sprache); - if (is_object($result) && $result->error == EXIT_SUCCESS) + // Load reveiver data to get its relative language + $this->ci->load->model('person/Person_model', 'PersonModel'); + $result = $this->ci->PersonModel->load($receiver_id); + if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) { - if (is_array($result->retval) && count($result->retval) > 0 && - !empty($result->retval[0]->text) && !empty($result->retval[0]->subject)) + // Set the language with the global value + $sprache = DEFAULT_LEHREINHEIT_SPRACHE; + // If the receiver has a prefered language use this + if (isset($result->retval[0]->sprache) && $result->retval[0]->sprache != '') { - $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); - - $this->ci->db->trans_start(false); - //save Message - $msgData = array( - 'person_id' => $sender_id, - 'subject' => $result->retval[0]->subject, - 'body' => $parsedText, - 'priority' => PRIORITY_NORMAL, - 'relationmessage_id' => $relationmessage_id, - 'oe_kurzbz' => $oe_kurzbz - ); - - $result = $this->ci->MessageModel->insert($msgData); - if (is_object($result) && $result->error == EXIT_SUCCESS) + $sprache = $result->retval[0]->sprache; + } + + // Loads template data + $result = $this->ci->vorlagelib->loadVorlagetext($vorlage_kurzbz, $oe_kurzbz, $orgform_kurzbz, $sprache); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + // If the text and the subject of the template are not empty + if (is_array($result->retval) && count($result->retval) > 0 && + !empty($result->retval[0]->text) && !empty($result->retval[0]->subject)) { - $msg_id = $result->retval; - $recipientData = array( - 'person_id' => $receiver_id, - 'message_id' => $msg_id, - 'token' => generateToken() + // Parses template text + $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); + + $this->ci->db->trans_start(false); + // Save Message + $msgData = array( + 'person_id' => $sender_id, + 'subject' => $result->retval[0]->subject, + 'body' => $parsedText, + 'priority' => PRIORITY_NORMAL, + 'relationmessage_id' => $relationmessage_id, + 'oe_kurzbz' => $oe_kurzbz ); - $result = $this->ci->RecipientModel->insert($recipientData); + $result = $this->ci->MessageModel->insert($msgData); if (is_object($result) && $result->error == EXIT_SUCCESS) { - $statusData = array( - 'message_id' => $msg_id, + // Link the message with the receiver + $msg_id = $result->retval; + $recipientData = array( 'person_id' => $receiver_id, - 'status' => MSG_STATUS_UNREAD + 'message_id' => $msg_id, + 'token' => generateToken() ); - $result = $this->ci->MsgStatusModel->insert($statusData); + $result = $this->ci->RecipientModel->insert($recipientData); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + // Save message status + $statusData = array( + 'message_id' => $msg_id, + 'person_id' => $receiver_id, + 'status' => MSG_STATUS_UNREAD + ); + $result = $this->ci->MsgStatusModel->insert($statusData); + } + } + + $this->ci->db->trans_complete(); + + if ($this->ci->db->trans_status() === FALSE || (is_object($result) && $result->error != EXIT_SUCCESS)) + { + $this->ci->db->trans_rollback(); + return $this->_error($result->msg, EXIT_ERROR); + } + else + { + $this->ci->db->trans_commit(); + return $this->_success($msg_id); } - } - - $this->ci->db->trans_complete(); - - if ($this->ci->db->trans_status() === FALSE || (is_object($result) && $result->error != EXIT_SUCCESS)) - { - $this->ci->db->trans_rollback(); - return $this->_error($result->msg, EXIT_ERROR); } else { - $this->ci->db->trans_commit(); - return $this->_success($msg_id); + // Better message error + if (!is_array($result->retval) || (is_array($result->retval) && count($result->retval) == 0)) + { + $result = $this->_error('Vorlage not found', EXIT_ERROR); + } + else if (is_array($result->retval) && count($result->retval) > 0) + { + if (is_null($result->retval[0]->oe_kurzbz)) + { + $result = $this->_error('Vorlage not found', EXIT_ERROR); + } + else if (empty($result->retval[0]->text)) + { + $result = $this->_error('Vorlage has an empty text', EXIT_ERROR); + } + else if (empty($result->retval[0]->subject)) + { + $result = $this->_error('Vorlage has an empty subject', EXIT_ERROR); + } + } } } else { - // Better message error - if (!is_array($result->retval) || (is_array($result->retval) && count($result->retval) == 0)) - { - $result = $this->_error('Vorlage not found', EXIT_ERROR); - } - else if (is_array($result->retval) && count($result->retval) > 0) - { - if (empty($result->retval[0]->text)) - { - $result = $this->_error('Vorlage has an empty text', EXIT_ERROR); - } - else if (empty($result->retval[0]->subject)) - { - $result = $this->_error('Vorlage has an empty subject', EXIT_ERROR); - } - } + $result = $this->_error($result->retval, EXIT_ERROR); } } - else - { - $result = $this->_error($result->retval, EXIT_ERROR); - } return $result; } diff --git a/application/libraries/OrganisationseinheitLib.php b/application/libraries/OrganisationseinheitLib.php new file mode 100644 index 000000000..5686f3617 --- /dev/null +++ b/application/libraries/OrganisationseinheitLib.php @@ -0,0 +1,60 @@ +ci =& get_instance(); + $this->ci->load->model('organisation/Organisationseinheit_model', 'OrganisationseinheitModel'); + } + + /** + * treeSearch + * + * This method searchs through the organisation tree starting from the bottom + * to the top, starting from the given oe_kurzbz. It stops when it finds a + * match with the other table, which attributes are passed as parameters: + * schema name, table name, fields to be selected, where conditions, orderby clause + * + * @param string $schema REQUIRED + * @param string $table REQUIRED + * @param mixed $fields REQUIRED + * @param string $where REQUIRED + * @param string $orderby REQUIRED + * @param string $oe_kurzbz REQUIRED + * @return array + */ + public function treeSearch($schema, $table, $fields, $where, $orderby, $oe_kurzbz) + { + $select = ""; + if (is_array($fields)) + { + for ($i = 0; $i < count($fields); $i++) + { + $select .= $fields[$i]; + if ($i != count($fields) - 1) + { + $select .= ", "; + } + } + } + else + { + $select = $fields; + } + + $result = $this->ci->OrganisationseinheitModel->getOneLevel($schema, $table, $select, $where, $orderby, $oe_kurzbz); + + if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) + { + if ($result->retval[0]->_ppk != null && $result->retval[0]->oe_kurzbz == null) + { + return $this->treeSearch($schema, $table, $select, $where, $orderby, $result->retval[0]->_ppk); + } + } + + return $result; + } +} \ No newline at end of file diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index ebb8682e7..0fa924eb8 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -133,24 +133,7 @@ class PhrasesLib } /** - * loadVorlagetext() - will load the best fitting Template. - * - * @param string $vorlage_kurzbz REQUIRED - * @param string $oe_kurzbz OPTIONAL - * @param string $orgform_kurzbz OPTIONAL - * @return array - */ - function loadVorlagetext($vorlage_kurzbz, $oe_kurzbz=null, $orgform_kurzbz=null) - { - if (empty($vorlage_kurzbz)) - return $this->_error($this->ci->lang->line('fhc_'.FHC_INVALIDID, false)); - - $vorlage = $this->ci->VorlageStudiengangModel->getVorlageStudiengang($vorlage_kurzbz, $oe_kurzbz, $orgform_kurzbz); - return $vorlage; - } - - /** - * insertVorlagetext() - will load tbl_vorlagestudiengang for a spezific Template. + * insertPhraseinhalt() - will load tbl_vorlagestudiengang for a spezific Template. * * @param string $vorlage_kurzbz REQUIRED * @return array @@ -162,7 +145,7 @@ class PhrasesLib } /** - * loadVorlagetext() - will load tbl_vorlagestudiengang for a spezific Template. + * getVorlagetextById() - will load tbl_vorlagestudiengang for a spezific Template. * * @param string $vorlage_kurzbz REQUIRED * @return array diff --git a/application/libraries/VorlageLib.php b/application/libraries/VorlageLib.php index 531d1ffd0..5a0f6a9e8 100644 --- a/application/libraries/VorlageLib.php +++ b/application/libraries/VorlageLib.php @@ -18,6 +18,7 @@ class VorlageLib $this->ci =& get_instance(); $this->ci->load->library('parser'); + $this->ci->load->library('OrganisationseinheitLib'); $this->ci->load->model('system/Vorlage_model', 'VorlageModel'); $this->ci->load->model('system/Vorlagestudiengang_model', 'VorlageStudiengangModel'); @@ -99,12 +100,39 @@ class VorlageLib if (empty($vorlage_kurzbz)) return $this->_error($this->ci->lang->line('fhc_'.FHC_INVALIDID, false)); - $vorlage = $this->ci->VorlageStudiengangModel->loadWhere(array( - 'vorlage_kurzbz' => $vorlage_kurzbz, - 'oe_kurzbz' => $oe_kurzbz, - 'orgform_kurzbz' => $orgform_kurzbz, - 'sprache' => $sprache) + // Builds where clause + $where = ""; + if (is_null($orgform_kurzbz)) + { + $where .= "orgform_kurzbz IS NULL"; + } + else + { + $where .= "orgform_kurzbz = " . $this->ci->VorlageModel->escape($orgform_kurzbz); + } + + $where .= " AND "; + + if (is_null($sprache)) + { + $where .= "sprache IS NULL"; + } + else + { + $where .= "sprache = " . $this->ci->VorlageModel->escape($sprache); + } + + $vorlage = $this->ci->organisationseinheitlib->treeSearch( + 'public', + 'tbl_vorlagestudiengang', + array("vorlage_kurzbz", "studiengang_kz", "version", "text", "oe_kurzbz", + "vorlagestudiengang_id", "style", "berechtigung", "anmerkung_vorlagestudiengang", + "aktiv", "sprache", "subject", "orgform_kurzbz"), + $where, + "version DESC", + $oe_kurzbz ); + return $vorlage; } diff --git a/application/models/organisation/Organisationseinheit_model.php b/application/models/organisation/Organisationseinheit_model.php index 0ccf3deef..3e668c4af 100644 --- a/application/models/organisation/Organisationseinheit_model.php +++ b/application/models/organisation/Organisationseinheit_model.php @@ -42,4 +42,50 @@ class Organisationseinheit_model extends DB_Model else return $this->_error($this->db->error()); } -} + + /** + * getOneLevel + * + * This method get one level of the organisation tree, using the given parameters. + * It returns even the data from another table linked by the oe_kurzbz + * + * @param string $schema REQUIRED + * @param string $table REQUIRED + * @param mixed $fields REQUIRED + * @param string $where REQUIRED + * @param string $orderby REQUIRED + * @param string $oe_kurzbz REQUIRED + * @return array + */ + public function getOneLevel($schema, $table, $fields, $where, $orderby, $oe_kurzbz) + { + $query = "WITH RECURSIVE organizations(_pk, _ppk) AS + ( + SELECT o.oe_kurzbz, o.oe_parent_kurzbz + FROM public.tbl_organisationseinheit o + WHERE o.oe_parent_kurzbz IS NULL + UNION ALL + SELECT o.oe_kurzbz, o.oe_parent_kurzbz + FROM public.tbl_organisationseinheit o INNER JOIN organizations orgs ON (o.oe_parent_kurzbz = orgs._pk) + ) + SELECT orgs._pk, orgs._ppk, _joined_table.* + FROM organizations orgs LEFT JOIN ( + SELECT %s.oe_kurzbz as _pk, %s + FROM %s.%s + WHERE %s + ) _joined_table ON (orgs._pk = _joined_table._pk) + WHERE orgs._pk = ? + ORDER BY %s"; + + $query = sprintf($query, $table, $fields, $schema, $table, $where, $orderby); + + if ($result = $this->db->query($query, array($oe_kurzbz))) + { + return $this->_success($result->result()); + } + else + { + return $this->_error($this->db->error()); + } + } +} \ No newline at end of file