diff --git a/application/controllers/system/Messages.php b/application/controllers/system/Messages.php index 5fac9c95c..472f818d5 100755 --- a/application/controllers/system/Messages.php +++ b/application/controllers/system/Messages.php @@ -40,14 +40,13 @@ class Messages extends VileSci_Controller } // Get variables - $this->load->model('crm/Prestudent_model', 'PrestudentModel'); - $prestudent = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudent_id); - if ($prestudent->error) + $this->load->model('system/Message_model', 'MessageModel'); + $msgVarsDataByPrestudentId = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudent_id); + if ($msgVarsDataByPrestudentId->error) { - show_error($prestudent->retval); + show_error($msgVarsDataByPrestudentId->retval); } - $this->load->model('system/Message_model', 'MessageModel'); if (!hasData($variables = $this->MessageModel->getMessageVars())) { unset($variables); @@ -65,21 +64,33 @@ class Messages extends VileSci_Controller array_shift($variables->retval); // Remove person_id array_shift($variables->retval); // Remove prestudent_id - // - $oe_kurzbz = null; + // Organisation units + $oe_kurzbz = array(); // A person can have more organisation units $this->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel'); $benutzerResult = $this->BenutzerfunktionModel->getByPersonId($sender_id); if (hasData($benutzerResult)) { - $oe_kurzbz = $benutzerResult->retval[0]->oe_kurzbz; + foreach($benutzerResult->retval as $val) + { + $oe_kurzbz[] = $val->oe_kurzbz; + } + } + + // Admin or commoner? + $this->load->model('system/Benutzerrolle_model', 'BenutzerrolleModel'); + $isAdmin = $this->BenutzerrolleModel->isAdminByPersonId($sender_id); + if (isError($isAdmin)) + { + show_error($isAdmin->retval); } $data = array ( 'sender_id' => $sender_id, - 'receivers' => $prestudent->retval, + 'receivers' => $msgVarsDataByPrestudentId->retval, 'message' => $msg, 'variables' => $variablesArray, - 'oe_kurzbz' => $oe_kurzbz + 'oe_kurzbz' => $oe_kurzbz, + 'isAdmin' => $isAdmin->retval ); $v = $this->load->view('system/messageWrite', $data); diff --git a/application/models/person/Benutzerfunktion_model.php b/application/models/person/Benutzerfunktion_model.php index c76dbd00d..e75540d92 100644 --- a/application/models/person/Benutzerfunktion_model.php +++ b/application/models/person/Benutzerfunktion_model.php @@ -13,7 +13,7 @@ class Benutzerfunktion_model extends DB_Model } /** - * + * Get the Benutzerfunktion using the person_id */ public function getByPersonId($person_id) { diff --git a/application/models/system/Benutzerrolle_model.php b/application/models/system/Benutzerrolle_model.php index 95ca90e71..13ebf57b6 100644 --- a/application/models/system/Benutzerrolle_model.php +++ b/application/models/system/Benutzerrolle_model.php @@ -1,7 +1,7 @@ dbTable = 'system.tbl_benutzerrolle'; $this->pk = 'benutzerberechtigung_id'; } -} + + /** + * Checks if the given user is an admin + */ + public function isAdminByPersonId($person_id) + { + // Join with the table tbl_benutzer + $this->addJoin('public.tbl_benutzer', 'uid'); + + $result = $this->loadWhere(array('person_id' => $person_id, 'rolle_kurzbz' => 'admin')); + + if (!isError($result)) + { + if (hasData($result)) + { + $result = success(true); + } + else if (!hasData($result)) + { + $result = success(false); + } + } + + return $result; + } +} \ No newline at end of file diff --git a/application/views/system/messageWrite.php b/application/views/system/messageWrite.php index 384d2a8db..4f4ea7427 100644 --- a/application/views/system/messageWrite.php +++ b/application/views/system/messageWrite.php @@ -93,7 +93,7 @@ widgetlib->widget( 'Vorlage_widget', - array('oe_kurzbz' => $oe_kurzbz), + array('oe_kurzbz' => $oe_kurzbz, 'isAdmin' => $isAdmin), array('name' => 'vorlage', 'id' => 'vorlageDnD') ); ?> diff --git a/application/widgets/vorlage_widget.php b/application/widgets/vorlage_widget.php index 50065d1c7..91cba301f 100644 --- a/application/widgets/vorlage_widget.php +++ b/application/widgets/vorlage_widget.php @@ -4,23 +4,25 @@ class Vorlage_widget extends DropdownWidget { public function display($widgetData) { - // Loads - $this->load->library('OrganisationseinheitLib'); + // All organization units to which the user belongs + $oe_kurzbz = $widgetData['oe_kurzbz']; + $idAdmin = $widgetData['isAdmin']; - $vorlage = $this->organisationseinheitlib->treeSearchEntire( - '( - SELECT v.vorlage_kurzbz, v.bezeichnung, vs.version, vs.oe_kurzbz, vs.aktiv, vs.subject, vs.text, v.mimetype - FROM tbl_vorlagestudiengang vs INNER JOIN tbl_vorlage v USING(vorlage_kurzbz) - ) templates', - 'templates', - array("templates.vorlage_kurzbz AS id", "UPPER(templates.oe_kurzbz) || ' - ' || templates.bezeichnung || ' - V' || templates.version AS description"), - 'templates.aktiv = TRUE - AND templates.subject IS NOT NULL - AND templates.text IS NOT NULL - AND templates.mimetype = \'text/html\'', - "description ASC", - $widgetData['oe_kurzbz'] - ); + $vorlage = null; + + // If the user is an admin + if ($idAdmin === true) + { + // Get all the vorlage with mimetype = text/html + $vorlage = $this->_getAllHTMLVorlage(); + } + else + { + // Get all the vorlage that belongs to the organisation units of the user + // and the parents of those organisation units until the root of the + // organisation unit tree + $vorlage = $this->_getUserVorlage($oe_kurzbz); + } $this->setElementsArray( $vorlage, @@ -31,4 +33,96 @@ class Vorlage_widget extends DropdownWidget $this->loadDropDownView($widgetData); } + + /** + * Get all the vorlage with mimetype = text/html + */ + private function _getAllHTMLVorlage() + { + $this->load->model('system/Vorlage_model', 'VorlageModel'); + $this->VorlageModel->addOrder('vorlage_kurzbz'); + + $this->addSelectToModel($this->VorlageModel, 'vorlage_kurzbz', 'bezeichnung'); + + return $this->VorlageModel->loadWhere(array('mimetype' => 'text/html')); + } + + /** + * Get all the vorlage that belongs to the organisation units of the user + * and the parents of those organisation units until the root of the + * organisation unit tree + */ + private function _getUserVorlage($oe_kurzbz) + { + // Loads library OrganisationseinheitLib + $this->load->library('OrganisationseinheitLib'); + + $vorlage = success(array()); // Default value + + $table = '( + SELECT v.vorlage_kurzbz, v.bezeichnung, vs.version, vs.oe_kurzbz, vs.aktiv, vs.subject, vs.text, v.mimetype + FROM tbl_vorlagestudiengang vs INNER JOIN tbl_vorlage v USING(vorlage_kurzbz) + ) templates'; + $alias = 'templates'; + $fields = array("templates.vorlage_kurzbz AS id", "UPPER(templates.oe_kurzbz) || ' - ' || templates.bezeichnung || ' - V' || templates.version AS description"); + $where = 'templates.aktiv = TRUE + AND templates.subject IS NOT NULL + AND templates.text IS NOT NULL + AND templates.mimetype = \'text/html\''; + $order_by = 'description ASC'; + + if (!is_array($oe_kurzbz)) + { + $vorlage = $this->organisationseinheitlib->treeSearchEntire( + $table, + $alias, + $fields, + $where, + $order_by, + $oe_kurzbz + ); + } + else // is an array + { + // Get the vorlage for each organisation unit + foreach($oe_kurzbz as $val) + { + $tmpVorlage = $this->organisationseinheitlib->treeSearchEntire( + $table, + $alias, + $fields, + $where, + $order_by, + $val + ); + + // Everything is ok and data are inside + if (hasData($tmpVorlage)) + { + // If it's the first vorlage copy it + if (count($vorlage->retval) == 0) + { + $vorlage->retval = $tmpVorlage->retval; + } + else // checks for duplicates, if it's not already present push it into the array $vorlage->retval + { + for ($i = 0; $i < count($vorlage->retval); $i++) + { + for ($j = 0; $j < count($tmpVorlage->retval); $j++) + { + if ($vorlage->retval[$i]->_pk != $tmpVorlage->retval[$j]->_pk + && $vorlage->retval[$i]->_ppk != $tmpVorlage->retval[$j]->_ppk + && $vorlage->retval[$i]->_jtpk != $tmpVorlage->retval[$j]->_jtpk) + { + array_push($vorlage->retval, $tmpVorlage->retval[$j]); + } + } + } + } + } + } + } + + return $vorlage; + } } \ No newline at end of file