diff --git a/application/controllers/Cis/Profil.php b/application/controllers/Cis/Profil.php index dd2c1a311..1141808fe 100755 --- a/application/controllers/Cis/Profil.php +++ b/application/controllers/Cis/Profil.php @@ -244,23 +244,23 @@ class Profil extends Auth_Controller $this->GemeindeModel->addDistinct(); $this->GemeindeModel->addSelect(["name"]); if ($nation == "A") { - if (isset($zip) && $zip > 999 && $zip < 32000) { + if (isset($zip) && $zip > 999 && $zip < 32000) { - $gemeinde_res = $this->GemeindeModel->loadWhere(['plz' => $zip]); - if (isError($gemeinde_res)) { - show_error("error while trying to query bis.tbl_gemeinde"); + $gemeinde_res = $this->GemeindeModel->loadWhere(['plz' => $zip]); + if (isError($gemeinde_res)) { + show_error("error while trying to query bis.tbl_gemeinde"); + } + $gemeinde_res = hasData($gemeinde_res) ? getData($gemeinde_res) : null; + $gemeinde_res = array_map(function ($obj) { + return $obj->name; + }, $gemeinde_res); + echo json_encode($gemeinde_res); + + } else { + echo json_encode(error("ortschaftskennziffer code was not valid")); } - $gemeinde_res = hasData($gemeinde_res) ? getData($gemeinde_res) : null; - $gemeinde_res = array_map(function ($obj) { - return $obj->name; - }, $gemeinde_res); - echo json_encode($gemeinde_res); - - } else { - echo json_encode(error("ortschaftskennziffer code was not valid")); - } } else { - echo json_encode(error("Nation was not 'A' (Austria)")); + echo json_encode(error("Nation was not 'A' (Austria)")); } } diff --git a/application/controllers/Cis/ProfilUpdate.php b/application/controllers/Cis/ProfilUpdate.php index 97a86488f..4dce86573 100755 --- a/application/controllers/Cis/ProfilUpdate.php +++ b/application/controllers/Cis/ProfilUpdate.php @@ -525,8 +525,6 @@ class ProfilUpdate extends Auth_Controller public function acceptProfilRequest() { - - $_POST = json_decode($this->input->raw_input_stream, true); $id = $this->input->post('profil_update_id', true); $uid = $this->input->post('uid', true); diff --git a/application/controllers/api/frontend/v1/Profil.php b/application/controllers/api/frontend/v1/Profil.php new file mode 100644 index 000000000..ffb7449f8 --- /dev/null +++ b/application/controllers/api/frontend/v1/Profil.php @@ -0,0 +1,690 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +/** + * This controller operates between (interface) the JS (GUI) and the SearchBarLib (back-end) + * Provides data to the ajax get calls about the searchbar component + * This controller works with JSON calls on the HTTP GET and the output is always JSON + */ +class Profil extends FHCAPI_Controller +{ + + /** + * Object initialization + */ + public function __construct() + { + parent::__construct([ + 'getView' => self::PERM_LOGGED, + 'fotoSperre' => self::PERM_LOGGED, + 'getGemeinden' => self::PERM_LOGGED, + 'getAllNationen' => self::PERM_LOGGED, + 'isMitarbeiter' => self::PERM_LOGGED, + + ]); + + + $this->load->model('ressource/mitarbeiter_model', 'MitarbeiterModel'); + $this->load->model('crm/Student_model', 'StudentModel'); + $this->load->model('person/Benutzer_model', 'BenutzerModel'); + $this->load->model('person/Person_model', 'PersonModel'); + $this->load->model('person/Adresse_model', 'AdresseModel'); + $this->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel'); + $this->load->model('person/Benutzergruppe_model', 'BenutzergruppeModel'); + $this->load->model('ressource/Betriebsmittelperson_model', 'BetriebsmittelpersonModel'); + $this->load->model('person/Kontakt_model', 'KontaktModel'); + $this->load->model('person/Profil_update_model', 'ProfilUpdateModel'); + $this->load->model('content/DmsVersion_model', 'DmsVersionModel'); + + + //? put the uid and pid inside the controller for reusability + $this->uid = getAuthUID(); + $this->pid = getAuthPersonID(); + + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + + /** + * function that returns the data used for the corresponding view + * the client side parses the @param $uid and calls this function to get the data to the correct view + * @access public + * @param boolean $uid the userID used to identify which information should be retrieved for which view + * @return stdClass all the data corresponding to a view of a user + */ + public function getView($uid) + { + $res = new stdClass(); + + // if parsing the URL did not found a UID then the UID of the logged in user is used + if ($uid == "Profil" || $uid == $this->uid) { + $isMitarbeiter = $this->MitarbeiterModel->isMitarbeiter($this->uid); + if (isError($isMitarbeiter)) { + show_error("error while checking if UID: " . $this->uid . " is a mitarbeiter"); + } + $isMitarbeiter = getData($isMitarbeiter); + if ($isMitarbeiter) { + $res->view = "MitarbeiterProfil"; + $res->data = $this->mitarbeiterProfil(); + $res->data->pid = $this->pid; + } else { + $res->view = "StudentProfil"; + $res->data = $this->studentProfil(); + $res->data->pid = $this->pid; + } + } + // UID is availabe when accessing Profil/View/:uid + else { + $this->PersonModel->addSelect(["person_id"]); + $pid = $this->PersonModel->getByUid($uid); + if (isError($pid)) { + show_error("error while trying to update table public.tbl_person while searching for a person with UID: " . $uid); + } + $pid = hasData($pid) ? getData($pid)[0] : null; + if (!$pid) { + show_error("Person with UID: " . $uid . " does not exist"); + } + $isMitarbeiter = $this->MitarbeiterModel->isMitarbeiter($uid); + if (isError($isMitarbeiter)) { + show_error("error while checking if UID: " . $uid . " is a mitarbeiter"); + } + $isMitarbeiter = getData($isMitarbeiter); + if ($isMitarbeiter) { + $res->view = "ViewMitarbeiterProfil"; + $res->data = $this->viewMitarbeiterProfil($uid); + + } else { + $res->view = "ViewStudentProfil"; + $res->data = $this->viewStudentProfil($uid); + } + } + $this->terminateWithSuccess($res); + } + + /** + * update column foto_sperre in public.tbl_person + * @access public + * @param boolean $value new value for the column + * @return boolean the new value added to the column in public.tbl_person + */ + public function fotoSperre($value) + { + if(!isset($value)){ + $this->terminateWithError("Missing parameter", self::ERROR_TYPE_GENERAL); + } + + $res = $this->PersonModel->update($this->pid, ["foto_sperre" => $value]); + if (isError($res)) { + show_error("error while trying to update table public.tbl_person"); + } + $this->PersonModel->addSelect("foto_sperre"); + $res = $this->PersonModel->load($this->pid); + if (isError($res)) { + show_error("error while trying to query table public.tbl_person"); + } + + $res = $this->getDataOrTerminateWithError($res); + + $this->terminateWithSuccess(current($res)); + } + + /** + * gets all nations in the table bis.tbl_nation + * + * @access public + * @return array all the nations in table bis.tbl_nation + */ + public function getAllNationen() + { + // load the nationen from the database + $this->load->model('codex/Nation_model', "NationModel"); + $this->NationModel->addSelect(["nation_code as code", "langtext"]); + $nation_res = $this->NationModel->load(); + + if (isError($nation_res)) { + $this->terminateWithError("error while trying to query table codex.tbl_nation", self::ERROR_TYPE_GENERAL); + } + + $nation_res = $this->getDataOrTerminateWithError($nation_res); + + $this->terminateWithSuccess($nation_res); + } + + public function getGemeinden($nation, $zip) + { + if(!isset($nation) || !isset($zip)){ + echo json_encode(error("Missing parameters")); + return; + } + + $this->load->model('codex/Gemeinde_model', "GemeindeModel"); + + + $gemeinde_res = $this->GemeindeModel->getGemeindeByPlz($zip); + + if (isError($gemeinde_res)) { + $this->terminateWithError(getError($gemeinde_res),self::ERROR_TYPE_GENERAL); + } + $gemeinde_res = $this->getDataOrTerminateWithError($gemeinde_res); + + /* $gemeinde_res = array_map(function ($obj) { + return $obj->ortschaftsname; + }, $gemeinde_res); */ + + $this->terminateWithSuccess($gemeinde_res); + + } + + + // ----------------------------------------------------------------------------------------------------------------- + // Private methods + + /** + * function that returns the data used for viewing another mitarbeiter profile + * @access private + * @param integer $uid the userID to retrieve the mitarbeiter data + * @return stdClass restricted mitarbeiter data + */ + private function viewMitarbeiterProfil($uid) + { + $mailverteiler_res = $this->getMailverteiler($uid); + $benutzer_funktion_res = $this->getBenutzerFunktion($uid); + $benutzer_res = $this->getBenutzerAlias($uid); + $person_res = $this->getPersonInfo($uid); + $mitarbeiter_res = $this->getMitarbeiterInfo($uid); + $telefon_res = $this->getTelefonInfo($uid); + + $res = new stdClass(); + $res->username = $uid; + + //? Person Info + foreach ($person_res as $key => $val) { + $res->$key = $val; + } + + //? Mitarbeiter Info + foreach ($mitarbeiter_res as $key => $val) { + $res->$key = $val; + + } + + $intern_email = array(); + $intern_email["type"] = "intern"; + $intern_email["email"] = $uid . "@" . DOMAIN; + $extern_email = array(); + $extern_email["type"] = "alias"; + $extern_email["email"] = $benutzer_res->alias . "@" . DOMAIN; + $res->emails = array($intern_email, $extern_email); + + $res->funktionen = $benutzer_funktion_res; + $res->mailverteiler = $mailverteiler_res; + $res->standort_telefon = isset($telefon_res) ? $telefon_res->kontakt : null; + + return $res; + } + + /** + * function that returns the data used for viewing another student profile + * @access private + * @param integer $uid the userID to retrieve the student data + * @return stdClass restricted student data + */ + private function viewStudentProfil($uid) + { + $mailverteiler_res = $this->getMailverteiler($uid); + $person_res = $this->getPersonInfo($uid); + $student_res = $this->getStudentInfo($uid); + $matr_res = $this->getMatrikelNummer($uid); + + $res = new stdClass(); + $res->username = $uid; + + //? Person Information + foreach ($person_res as $key => $value) { + $res->$key = $value; + } + + //? Student Information + foreach ($student_res as $key => $value) { + $res->$key = $value; + } + + $intern_email = array(); + $intern_email["type"] = "intern"; + $intern_email["email"] = $uid . "@" . DOMAIN; + + $res->emails = [$intern_email]; + $res->matrikelnummer = $matr_res->matr_nr; + $res->mailverteiler = $mailverteiler_res; + + return $res; + } + + /** + * checks whether a specific userID is a mitarbeiter or not (foreword declaration of the function isMitarbeiter in Mitarbeiter_model.php) + * @access public + * @param $uid the userID used to check if it is a mitarbeiter + * @return boolean + */ + public function isMitarbeiter($uid) + { + + if(!$uid) $this->terminateWithError("No uid provided", self::ERROR_TYPE_GENERAL); + + + $result = $this->MitarbeiterModel->isMitarbeiter($uid); + + if (isError($result)) { + $this->terminateWithError("error when calling Mitarbeiter_model function isMitarbeiter with uid " . $uid, self::ERROR_TYPE_GENERAL); + } + + $result = $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess($result); + } + + /** + * function that returns the data used for the mitarbeiter profile + * @access private + * @return stdClass mitarbeiter data + */ + private function mitarbeiterProfil() + { + + $zutrittskarte_ausgegebenam = $this->getZutrittskarteDatum($this->uid); + $adresse_res = $this->getAdressenInfo($this->pid); + $kontakte_res = $this->getKontaktInfo($this->pid); + $mailverteiler_res = $this->getMailverteiler($this->uid); + $person_res = $this->getPersonInfo($this->uid, true); + $benutzer_funktion_res = $this->getBenutzerFunktion($this->uid); + $betriebsmittelperson_res = $this->getBetriebsmittelInfo($this->pid); + $profilUpdates = $this->getProfilUpdates($this->uid); + $telefon_res = $this->getTelefonInfo($this->uid); + $mitarbeiter_res = $this->getMitarbeiterInfo($this->uid); + + $res = new stdClass(); + $res->username = $this->uid; + + //? Person Information + foreach ($person_res as $key => $value) { + $res->$key = $value; + } + + //? Mitarbeiter Information + foreach ($mitarbeiter_res as $key => $value) { + $res->$key = $value; + } + + $res->adressen = $adresse_res; + $res->zutrittsdatum = $zutrittskarte_ausgegebenam; + $res->kontakte = $kontakte_res; + $res->mittel = $betriebsmittelperson_res; + $res->mailverteiler = $mailverteiler_res; + + $intern_email = array(); + $intern_email["type"] = "intern"; + $intern_email["email"] = $this->uid . "@" . DOMAIN; + $extern_email = array(); + $extern_email["type"] = "alias"; + $extern_email["email"] = $mitarbeiter_res->alias . "@" . DOMAIN; + $res->emails = [$intern_email, $extern_email]; + + $res->funktionen = $benutzer_funktion_res; + $res->standort_telefon = $telefon_res; + $res->profilUpdates = $profilUpdates; + + return $res; + } + + /** + * function that returns the data used for the student profile + * @access private + * @return stdClass student data + */ + private function studentProfil() + { + $betriebsmittelperson_res = $this->getBetriebsmittelInfo($this->pid); + $kontakte_res = $this->getKontaktInfo($this->pid); + $zutrittskarte_ausgegebenam = $this->getZutrittskarteDatum($this->uid); + $adresse_res = $this->getAdressenInfo($this->pid); + $mailverteiler_res = $this->getMailverteiler($this->uid); + $person_res = $this->getPersonInfo($this->uid, true); + $zutrittsgruppe_res = $this->getZutrittsgruppen($this->uid); + $student_res = $this->getStudentInfo($this->uid); + $matr_res = $this->getMatrikelNummer($this->uid); + $profilUpdates = $this->getProfilUpdates($this->uid); + + $res = new stdClass(); + $res->username = $this->uid; + + //? Person Information + foreach ($person_res as $key => $value) { + $res->$key = $value; + } + + //? Student Information + foreach ($student_res as $key => $value) { + $res->$key = trim($value); + } + + $intern_email = array(); + $intern_email["type"] = "intern"; + $intern_email["email"] = $this->uid . "@" . DOMAIN; + + $res->emails = [$intern_email]; + $res->adressen = $adresse_res; + $res->zutrittsdatum = $zutrittskarte_ausgegebenam; + $res->kontakte = $kontakte_res; + $res->mittel = $betriebsmittelperson_res; + $res->matrikelnummer = $matr_res->matr_nr; + $res->zuttritsgruppen = $zutrittsgruppe_res; + $res->mailverteiler = $mailverteiler_res; + $res->profilUpdates = $profilUpdates; + + return $res; + } + + + /** + * gets all the mailverteiler using the tables: tbl_benutzer, tbl_benutzergruppe, tbl_gruppe + * @access private + * @param integer $uid the userID used to retrieve the mailverteiler + * @return array returns the mailvertailer corresponding to a userID + */ + private function getMailverteiler($uid) + { + $this->PersonModel->addSelect('gruppe_kurzbz, beschreibung'); + $this->PersonModel->addJoin('tbl_benutzer', 'person_id'); + $this->PersonModel->addJoin('tbl_benutzergruppe', 'uid'); + $this->PersonModel->addJoin('tbl_gruppe', 'gruppe_kurzbz'); + + $mailverteiler_res = $this->PersonModel->loadWhere(array('mailgrp' => true, 'uid' => $uid)); + if (isError($mailverteiler_res)) { + show_error("was not able to query the table public.tbl_benutzer:" . getData($mailverteiler_res)); + } + $mailverteiler_res = hasData($mailverteiler_res) ? getData($mailverteiler_res) : null; + $mailverteiler_res = array_map(function ($element) { + $element->mailto = "mailto:" . $element->gruppe_kurzbz . "@" . DOMAIN; + return $element; + }, $mailverteiler_res); + return $mailverteiler_res; + } + + /** + * gets all the Benutzerfunktionen of a corresponding user + * @access private + * @param integer $uid the userID used to retrieve the Benutzerfunktionen + * @return array returns the Benutzerfunktionen corresponding to a userID + */ + private function getBenutzerFunktion($uid) + { + $this->BenutzerfunktionModel->addSelect(["tbl_benutzerfunktion.bezeichnung as Bezeichnung", "tbl_organisationseinheit.bezeichnung as Organisationseinheit", "datum_von as Gültig_von", "datum_bis as Gültig_bis", "wochenstunden as Wochenstunden"]); + $this->BenutzerfunktionModel->addJoin("tbl_organisationseinheit", "oe_kurzbz"); + + $benutzer_funktion_res = $this->BenutzerfunktionModel->loadWhere(array('uid' => $uid)); + if (isError($benutzer_funktion_res)) { + show_error("was not able to query the table public.tbl_benutzerfunktion:" . getData($benutzer_funktion_res)); + } + $benutzer_funktion_res = hasData($benutzer_funktion_res) ? getData($benutzer_funktion_res) : null; + return $benutzer_funktion_res; + } + + /** + * gets all the Betriebsmittel of a corresponding user + * @access private + * @param integer $uid the userID used to retrieve the Betriebsmittel + * @return array returns the Betriebsmittel corresponding to a userID + */ + private function getBetriebsmittelInfo($pid) + { + $this->BetriebsmittelpersonModel->addSelect(["CONCAT(betriebsmitteltyp, ' ' ,beschreibung) as Betriebsmittel", "nummer as Nummer", "ausgegebenam as Ausgegeben_am"]); + + //? betriebsmittel are not needed in a view + $betriebsmittelperson_res = $this->BetriebsmittelpersonModel->getBetriebsmittel($pid); + if (isError($betriebsmittelperson_res)) { + show_error("was not able to query the table public.tbl_betriebsmittelperson:" . getData($betriebsmittelperson_res)); + } + $betriebsmittelperson_res = hasData($betriebsmittelperson_res) ? getData($betriebsmittelperson_res) : null; + return $betriebsmittelperson_res; + } + + /** + * gets the alias of a corresponding user + * @access private + * @param integer $uid the userID used to get the alias + * @return string the alias of the userID + */ + private function getBenutzerAlias($uid) + { + $this->BenutzerModel->addSelect(["alias"]); + $benutzer_res = $this->BenutzerModel->load([$uid]); + if (isError($benutzer_res)) { + show_error("was not able to query the table public.tbl_benutzer:" . getData($benutzer_res)); + } else { + $benutzer_res = hasData($benutzer_res) ? getData($benutzer_res)[0] : null; + } + + return $benutzer_res; + } + + /** + * gets the person information corresponding to a user + * @access private + * @param integer $uid the userID used to get the person information + * @param integer $geburtsInfo flag wether to add the columns gebort, gebdatum, foto_sperre or not + * @return array all the person informaion corresponding to a userID + */ + private function getPersonInfo($uid, $geburtsInfo = null) + { + $selectClause = ["foto", "anrede", "titelpost as postnomen", "titelpre as titel", "vorname", "nachname"]; + /** @param integer $geburtsInfo */ + if ($geburtsInfo) { + array_push($selectClause, "gebort"); + array_push($selectClause, "gebdatum"); + array_push($selectClause, "foto_sperre"); + } + $this->BenutzerModel->addSelect($selectClause); + $this->BenutzerModel->addJoin("tbl_person", "person_id"); + + $person_res = $this->BenutzerModel->load([$uid]); + if (isError($person_res)) { + show_error("was not able to query the table public.tbl_benutzer:" . getData($person_res)); + } else { + $person_res = hasData($person_res) ? getData($person_res)[0] : null; + } + + return $person_res; + } + + /** + * gets the mitarbeiter information corresponding to a user + * @access private + * @param integer $uid the userID used to get the mitarbeiter information + * @return array all the mitarbeiter informaion corresponding to a userID + */ + private function getMitarbeiterInfo($uid) + { + $this->MitarbeiterModel->addSelect(["kurzbz", "telefonklappe", "alias", "ort_kurzbz"]); + $this->MitarbeiterModel->addJoin("tbl_benutzer", "tbl_benutzer.uid = tbl_mitarbeiter.mitarbeiter_uid"); + $mitarbeiter_res = $this->MitarbeiterModel->load($uid); + if (isError($mitarbeiter_res)) { + show_error("was not able to query the table public.tbl_mitarbeiter:" . getData($mitarbeiter_res)); + } else { + $mitarbeiter_res = hasData($mitarbeiter_res) ? getData($mitarbeiter_res)[0] : null; + } + + return $mitarbeiter_res; + } + + /** + * gets the telefon information corresponding to a user + * @access private + * @param integer $uid the userID used to get the telefon information + * @return array all the telefon informaion corresponding to a userID + */ + private function getTelefonInfo($uid) + { + $this->MitarbeiterModel->addSelect(["kontakt"]); + $this->MitarbeiterModel->addJoin("tbl_kontakt", "tbl_mitarbeiter.standort_id = tbl_kontakt.standort_id"); + $this->MitarbeiterModel->addLimit(1); + $telefon_res = $this->MitarbeiterModel->loadWhere(["mitarbeiter_uid" => $uid, "kontakttyp" => "telefon"]); + if (isError($telefon_res)) { + show_error("was not able to query the table public.tbl_mitarbeiter:" . getData($telefon_res)); + } + $telefon_res = hasData($telefon_res) ? getData($telefon_res)[0] : null; + return $telefon_res; + } + + /** + * gets the student information corresponding to a user + * @access private + * @param integer $uid the userID used to get the student information + * @return array all the student informaion corresponding to a userID + */ + private function getStudentInfo($uid) + { + $this->StudentModel->addSelect(['tbl_studiengang.bezeichnung as studiengang', 'tbl_student.semester', 'tbl_student.verband', 'tbl_student.gruppe', 'tbl_student.matrikelnr as personenkennzeichen']); + $this->StudentModel->addJoin('tbl_studiengang', "tbl_studiengang.studiengang_kz=tbl_student.studiengang_kz"); + + $student_res = $this->StudentModel->load([$uid]); + if (isError($student_res)) { + show_error("was not able to query the table public.tbl_student:" . getData($student_res)); + } + $student_res = hasData($student_res) ? getData($student_res)[0] : null; + return $student_res; + } + + /** + * gets the profil updates corresponding to a user + * @access private + * @param integer $uid the userID used to get the profil updates + * @return array all the profil updates corresponding to a userID + */ + private function getProfilUpdates($uid) + { + $profilUpdates = $this->ProfilUpdateModel->getProfilUpdatesWhere(['uid' => $uid]); + if (isError($profilUpdates)) { + show_error("was not able to query the table public.tbl_profil_update:" . getData($profilUpdates)); + } + $profilUpdates = hasData($profilUpdates) ? getData($profilUpdates) : null; + return $profilUpdates; + } + + /** + * gets the Matrikelnummer corresponding to a user + * @access private + * @param integer $uid the userID used to get the Matrikelnummer + * @return integer the Matrikelnummer corresponding to a userID + */ + private function getMatrikelNummer($uid) + { + $this->BenutzerModel->addSelect(["matr_nr"]); + $this->BenutzerModel->addJoin("tbl_person", "person_id"); + + $matr_res = $this->BenutzerModel->load([$uid]); + if (isError($matr_res)) { + show_error("was not able to query the table public.tbl_benutzer:" . getData($matr_res)); + } + $matr_res = hasData($matr_res) ? getData($matr_res)[0] : []; + return $matr_res; + } + + /** + * gets the Zutrittsgruppen corresponding to a user + * @access private + * @param integer $uid the userID used to get the Zutrittsgruppen + * @return array all the Zutrittsgruppen corresponding to a userID + */ + private function getZutrittsgruppen($uid) + { + $this->BenutzergruppeModel->addSelect(['bezeichnung']); + $this->BenutzergruppeModel->addJoin('tbl_gruppe', 'gruppe_kurzbz'); + + $zutrittsgruppe_res = $this->BenutzergruppeModel->loadWhere(array("uid" => $uid, "zutrittssystem" => true)); + if (isError($zutrittsgruppe_res)) { + show_error("was not able to query the table public.tbl_benutzergruppe:" . getData($zutrittsgruppe_res)); + } + $zutrittsgruppe_res = hasData($zutrittsgruppe_res) ? getData($zutrittsgruppe_res) : null; + return $zutrittsgruppe_res; + } + + /** + * gets the address information corresponding to a user + * @access private + * @param integer $uid the userID used to get the address information + * @return array all the address information corresponding to a userID + */ + private function getAdressenInfo($pid) + { + $adresse_res = $this->AdresseModel->addSelect(["adresse_id", "strasse", "tbl_adressentyp.bezeichnung as typ", "plz", "ort", "zustelladresse", "gemeinde", "nation"]); + $adresse_res = $this->AdresseModel->addOrder("zustelladresse", "DESC"); + $adresse_res = $this->AdresseModel->addJoin("tbl_adressentyp", "typ=adressentyp_kurzbz"); + + $adresse_res = $this->AdresseModel->loadWhere(["person_id" => $pid]); + if (isError($adresse_res)) { + show_error("was not able to query the table public.tbl_adresse:" . getData($adresse_res)); + } + $adresse_res = hasData($adresse_res) ? getData($adresse_res) : null; + return $adresse_res; + } + + /** + * gets the kontakt information corresponding to a user + * @access private + * @param integer $uid the userID used to get the kontakt information + * @return array all the kontakt information corresponding to a userID + */ + private function getKontaktInfo($pid) + { + $this->KontaktModel->addSelect(['kontakttyp', 'kontakt_id', 'kontakt', 'tbl_kontakt.anmerkung', 'tbl_kontakt.zustellung']); + $this->KontaktModel->addJoin('public.tbl_standort', 'standort_id', 'LEFT'); + $this->KontaktModel->addJoin('public.tbl_firma', 'firma_id', 'LEFT'); + $this->KontaktModel->addOrder('kontakttyp, kontakt, tbl_kontakt.updateamum, tbl_kontakt.insertamum'); + + $kontakte_res = $this->KontaktModel->loadWhere(['person_id' => $pid]); + if (isError($kontakte_res)) { + show_error("was not able to query the table public.tbl_kontakt:" . getData($kontakte_res)); + } + $kontakte_res = hasData($kontakte_res) ? getData($kontakte_res) : null; + return $kontakte_res; + } + + /** + * gets the date of issue of the FH access card corresponding to a user + * @access private + * @param integer $uid the userID used to get the date of issue of the FH access card + * @return string the date of issue of the FH access card corresponding to a userID + */ + private function getZutrittskarteDatum($uid) + { + $zutrittskarte_ausgegebenam = $this->BetriebsmittelpersonModel->getBetriebsmittelByUid($uid, "Zutrittskarte"); + if (isError($zutrittskarte_ausgegebenam)) { + show_error("was not able to query the table wavi.tbl_bentriebsmittelperson:" . getData($zutrittskarte_ausgegebenam)); + } + $zutrittskarte_ausgegebenam = hasData($zutrittskarte_ausgegebenam) ? getData($zutrittskarte_ausgegebenam)[0]->ausgegebenam : null; + + //? formats date from 01-01-2000 to 01.01.2000 + $zutrittskarte_ausgegebenam = str_replace("-", ".", $zutrittskarte_ausgegebenam); + return $zutrittskarte_ausgegebenam; + } +} + diff --git a/application/controllers/api/frontend/v1/ProfilUpdate.php b/application/controllers/api/frontend/v1/ProfilUpdate.php new file mode 100644 index 000000000..4b9dedb48 --- /dev/null +++ b/application/controllers/api/frontend/v1/ProfilUpdate.php @@ -0,0 +1,704 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +/** + * This controller operates between (interface) the JS (GUI) and the SearchBarLib (back-end) + * Provides data to the ajax get calls about the searchbar component + * This controller works with JSON calls on the HTTP GET and the output is always JSON + */ +class ProfilUpdate extends FHCAPI_Controller +{ + + public static $STATUS_PENDING = NULL; + public static $STATUS_ACCEPTED = NULL; + public static $STATUS_REJECTED = NULL; + + public static $TOPICS = []; + + /** + * Object initialization + */ + public function __construct() + { + parent::__construct([ + 'getStatus' => self::PERM_LOGGED, + 'getTopic' => self::PERM_LOGGED, + 'getProfilRequestFiles' => self::PERM_LOGGED, + 'denyProfilRequest' => ['student/stammdaten:rw', 'mitarbeiter/stammdaten:rw'], + 'acceptProfilRequest' => ['student/stammdaten:rw', 'mitarbeiter/stammdaten:rw'], + 'selectProfilRequest' => self::PERM_LOGGED, + 'insertProfilRequest' => self::PERM_LOGGED, + 'updateProfilRequest' => self::PERM_LOGGED, + 'deleteProfilRequest' => self::PERM_LOGGED, + 'insertFile' => self::PERM_LOGGED, + ]); + + // Load language phrases + $this->loadPhrases( + array( + 'ui', + 'global', + 'person', + 'profil', + 'profilUpdate' + ) + ); + + $this->load->model('person/Profil_update_model', 'ProfilUpdateModel'); + $this->load->model('person/Kontakt_model', 'KontaktModel'); + $this->load->model('person/Adresse_model', 'AdresseModel'); + $this->load->model('person/Adressentyp_model', 'AdressenTypModel'); + $this->load->model('person/Person_model', 'PersonModel'); + $this->load->model('ressource/mitarbeiter_model', 'MitarbeiterModel'); + $this->load->model('crm/Student_model', 'StudentModel'); + $this->load->model('person/Benutzer_model', 'BenutzerModel'); + $this->load->model('system/Sprache_model', 'SpracheModel'); + $this->load->model('person/Profil_update_status_model', 'ProfilUpdateStatusModel'); + $this->load->model('person/Profil_update_topic_model', 'ProfilUpdateTopicModel'); + + $this->load->library('DmsLib'); + $this->load->library('PermissionLib'); + + //? put the uid and pid inside the controller for reusability + $this->uid = getAuthUID(); + $this->pid = getAuthPersonID(); + + // setup the ProfilUpdate states + $this->ProfilUpdateStatusModel->addSelect(['status_kurzbz']); + $status_kurzbz = $this->ProfilUpdateStatusModel->load(); + if (hasData($status_kurzbz)) { + list($status_pending, $status_accepted, $status_rejected) = getData($status_kurzbz); + + self::$STATUS_PENDING = $status_pending->status_kurzbz; + self::$STATUS_ACCEPTED = $status_accepted->status_kurzbz; + self::$STATUS_REJECTED = $status_rejected->status_kurzbz; + } + // setup the ProfilUpdate topics + $this->ProfilUpdateTopicModel->addSelect(['topic_kurzbz']); + $topic_kurzbz = $this->ProfilUpdateTopicModel->load(); + + if (hasData($topic_kurzbz)) { + foreach (getData($topic_kurzbz) as $topic) { + self::$TOPICS[$topic->topic_kurzbz] = $topic->topic_kurzbz; + } + } + + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + public function getStatus() + { + $this->terminateWithSuccess([self::$STATUS_PENDING => self::$STATUS_PENDING, self::$STATUS_ACCEPTED => self::$STATUS_ACCEPTED, self::$STATUS_REJECTED => self::$STATUS_REJECTED]); + } + + + public function getTopic() + { + if(!count(self::$TOPICS)){ + $this->terminateWithError('No topics found'); + } + $this->terminateWithSuccess(self::$TOPICS); + } + + public function selectProfilRequest() + { + + $uid = $this->input->get('uid',true); + $id = $this->input->get('id',true); + $whereClause = ['uid' => $this->uid]; + + if (isset($uid)) + $whereClause['uid'] = $uid; + if (isset($id)) + $whereClause['id'] = $id; + + $res = $this->ProfilUpdateModel->getProfilUpdatesWhere($whereClause); + $res = $this->getDataOrTerminateWithError($res); + $this->terminateWithSuccess($res); + + } + + public function insertProfilRequest() + { + + $payload = $this->input->post('payload',true); + $topic = $this->input->post('topic',true); + $fileID = $this->input->post('fileID',true); + + if(!isset($payload) || !isset($topic)){ + $this->terminateWithError("required parameters are missing"); + } + + $identifier = array_key_exists("kontakt_id", $payload) ? "kontakt_id" : (array_key_exists("adresse_id", $payload) ? "adresse_id" : null); + + $data = ["topic" => $topic, "uid" => $this->uid, "requested_change" => json_encode($payload), "insertamum" => "NOW()", "insertvon" => $this->uid, "status" => self::$STATUS_PENDING ?: 'Pending']; + + //? insert fileID in the dataset if sent with post request + if (isset($fileID)) { + $data['attachment_id'] = $fileID; + } + + //? loops over all updateRequests from a user to validate if the new request is valid + $res = $this->ProfilUpdateModel->getProfilUpdatesWhere(["uid" => $this->uid]); + if (isError($res)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_loading_error')); + } + $res = $this->getDataOrTerminateWithError($res); + + //? the user cannot delete a zustelladresse/kontakt + if (isset($payload->delete) && $payload->{$identifier == "kontakt_id" ? "zustellung" : "zustelladresse"}) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_deleteZustellung_error')); + } + + //? if the user tries to delete a adresse, checks whether the adresse is a heimatadresse, if so an error is raised + if (isset($payload->delete) && $identifier == "adresse_id") { + $adr = $this->AdresseModel->load($payload->$identifier); + $adr = $this->getDataOrTerminateWithError($adr)[0]; + if ($adr->heimatadresse) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_deleteZustellung_error')); + } + } + + if ($res) { + $pending_changes = array_filter($res, function ($element) { + return $element->status == (self::$STATUS_PENDING ?: "Pending"); + }); + + foreach ($pending_changes as $update_request) { + $existing_change = $update_request->requested_change; + + //? the user can add as many new kontakte/adressen as he likes + if (!isset($payload->add) && property_exists($existing_change, $identifier) && property_exists($payload, $identifier) && $existing_change->$identifier == $payload->$identifier) { + //? the kontakt_id / adresse_id of a change has to be unique + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_changeTwice_error')); + } + + //? if it is not updating any kontakt/adresse, the topic has to be unique + elseif (!$identifier && $update_request->topic == $topic) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_changeTopicTwice_error', ['0' => $update_request->topic])); + } + } + } + + $insertID = $this->ProfilUpdateModel->insert($data); + + if (isError($insertID)) { + $this->terminateWithError(getError($insertID)); + } else { + + $insertID = hasData($insertID) ? getData($insertID) : null; + //? sends emails to the correspondents of the $uid + $this->sendEmail_onProfilUpdate_insertion($this->uid, $insertID, $topic); + $this->terminateWithSuccess(success($insertID)); + } + } + + public function updateProfilRequest() + { + $topic = $this->input->post('topic', true); + $payload = $this->input->post('payload', true); + $ID = $this->input->post('ID', true); + $fileID = $this->input->post('fileID', true);//optional + + if(!isset($topic) || !isset($payload) || !isset($ID)){ + $this->terminateWithError("required parameters are missing"); + } + + $updateData = ["requested_change" => json_encode($payload), "updateamum" => "NOW()", "updatevon" => $this->uid]; + if (isset($fileID)) { + $updateData['attachment_id'] = json_decode($fileID); + } + $updateID = $this->ProfilUpdateModel->update([$ID], $updateData); + //? insert fileID in the dataset if sent with post request + + if (isError($updateID)) { + $this->terminateWithError(getError($updateID)); + } + + $updateID = $this->getDataOrTerminateWithError($updateID)[0]; + + $this->terminateWithSuccess(success($updateID)); + } + + public function deleteProfilRequest() + { + + $requestID = $this->input->post('requestID', true); + $result = $this->ProfilUpdateModel->delete([$requestID]); + if (isError($result)) { + $this->terminateWithError(getError($result)); + } + $this->terminateWithSuccess($result); + } + + public function getProfilRequestFiles($id) + { + if(!$id){ + $this->terminateWithError("parameter id is missing"); + } + + $this->ProfilUpdateModel->addSelect(["attachment_id"]); + $attachmentID = $this->ProfilUpdateModel->load([$id]); + if (isError($attachmentID)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_loading_error'),self::ERROR_TYPE_GENERAL); + } + //? get the attachmentID + $dms_id = $this->getDataOrTerminateWithError($attachmentID)[0]->attachment_id; + + //? get the name to the file + $this->DmsVersionModel->addSelect(["name", "dms_id"]); + $attachment = $this->DmsVersionModel->load([$dms_id, 0]); + if (isError($attachment)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_dmsVersion_error'),self::ERROR_TYPE_GENERAL); + } + $attachment = $this->getDataOrTerminateWithError($attachment); + //? returns {name:..., dms_id:...} + $this->terminateWithSuccess($attachment); + } + + public function denyProfilRequest() + { + $id = $this->input->post('profil_update_id', true); + $uid = $this->input->post('uid', true); + $topic = $this->input->post('topic', true); + $status_message = $this->input->post('status_message', true); //optional + + if(!isset($id) || !isset($uid) || !isset($topic)){ + $this->terminateWithError("parameter id, uid, topic or status_message is missing"); + } + + $is_mitarbeiter = $this->MitarbeiterModel->isMitarbeiter($uid); + $is_mitarbeiter = $this->getDataOrTerminateWithError($is_mitarbeiter); + + $is_student = $this->StudentModel->isStudent($uid); + $is_student = $this->getDataOrTerminateWithError($is_student); + + if ( + $is_student && $this->permissionlib->isBerechtigt('student/stammdaten', "suid", $this->getOE_from_student($uid)) || + $is_mitarbeiter && $this->permissionlib->isBerechtigt('mitarbeiter/stammdaten', "suid") + ) { + $this->sendEmail_onProfilUpdate_response($uid, $topic, self::$STATUS_REJECTED); + $this->terminateWithSuccess($this->setStatusOnUpdateRequest($id, self::$STATUS_REJECTED, $status_message)); + } else { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_permission_error'),self::ERROR_TYPE_GENERAL); + } + } + + public function acceptProfilRequest() + { + $id = $this->input->post('profil_update_id', true); + $uid = $this->input->post('uid', true); + $topic = $this->input->post('topic', true); + $requested_change = $this->input->post('requested_change'); + $status_message = $this->input->post('status_message', true); //optional + + //? fetching person_id using UID + $personID = $this->PersonModel->getByUid($uid); + $personID = $this->getDataOrTerminateWithError($personID)[0]->person_id; + + //! check for required information + if (!isset($id) || !isset($uid) || !isset($personID) || !isset($requested_change) || !isset($topic)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_requiredInformation_error')); + } + + $is_mitarbeiter = $this->MitarbeiterModel->isMitarbeiter($uid); + $is_mitarbeiter = $this->getDataOrTerminateWithError($is_mitarbeiter); + + $is_student = $this->StudentModel->isStudent($uid); + $is_student = $this->getDataOrTerminateWithError($is_student); + + + //? check if the permissions are set correctly + if ( + $is_student && $this->permissionlib->isBerechtigt('student/stammdaten', "suid", $this->getOE_from_student($uid)) || + $is_mitarbeiter && $this->permissionlib->isBerechtigt('mitarbeiter/stammdaten', "suid") + ) { + + if (is_array($requested_change) && array_key_exists("adresse_id", $requested_change)) { + $insertID = $this->handleAdresse($requested_change, $personID); + $insertID = getData($insertID); + if (isset($insertID)) { + $requested_change['adresse_id'] = $insertID; + $update_res = $this->updateRequestedChange($id, $requested_change); + if (isError($update_res)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_address_error', [$insertID])); + } + } + + } else if (is_array($requested_change) && array_key_exists("kontakt_id", $requested_change)) { + $insertID = $this->handleKontakt($requested_change, $personID); + $insertID = getData($insertID); + if (isset($insertID)) { + $requested_change['kontakt_id'] = $insertID; + $update_res = $this->updateRequestedChange($id, $requested_change); + if (isError($update_res)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_kontakt_error', [$insertID])); + } + } + + + } else { + switch ($topic) { + // mapping phrasen to database columns to make the update with the correct column names + case self::$TOPICS['Titel']: + $topic = "titelpre"; + break; + case self::$TOPICS['Postnomen']: + $topic = "titelpost"; + break; + case self::$TOPICS['Vorname']: + $topic = "vorname"; + break; + case self::$TOPICS['Nachname']: + $topic = "nachname"; + break; + default: + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_topic_error', [$topic])); + } + + $result = $this->PersonModel->update($personID, [$topic => $requested_change["value"]]); + if (isError($result)) $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_insert_error')); + + } + $this->sendEmail_onProfilUpdate_response($uid, $topic, self::$STATUS_ACCEPTED); + + $this->terminateWithSuccess($this->setStatusOnUpdateRequest($id, self::$STATUS_ACCEPTED, $status_message, $requested_change)); + } else { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_permission_error')); + } + + + } + + public function insertFile($replace) + { + $replace = json_decode($replace); + + if (!count($_FILES)) { + $this->terminateWithError("No file available for upload"); + } + + //? if replace is set it contains the profil_update_id in which the attachment_id has to be replaced + if (isset($replace)) { + + $this->ProfilUpdateModel->addSelect(["attachment_id"]); + $profilUpdate = $this->ProfilUpdateModel->load([$replace]); + if (isError($profilUpdate)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_loading_error')); + } + //? get the attachmentID + $dms_id = $this->getDataOrTerminateWithError($profilUpdate)[0]->attachment_id; + + //? delete old dms_file of Profil Update + $deleteOldFile_result = $this->deleteOldVersionFile($dms_id); + if(!$deleteOldFile_result){ + $this->terminateWithError("error while deleting the old file"); + } + } + + + $files = $_FILES['files']; + $file_count = count($files['name']); + + $res = []; + + for ($i = 0; $i < $file_count; $i++) { + $_FILES['files']['name'] = $files['name'][$i]; + $_FILES['files']['type'] = $files['type'][$i]; + $_FILES['files']['tmp_name'] = $files['tmp_name'][$i]; + $_FILES['files']['error'] = $files['error'][$i]; + $_FILES['files']['size'] = $files['size'][$i]; + + $dms = [ + "kategorie_kurzbz" => "profil_aenderung", + "version" => 0, + "name" => $_FILES['files']['name'], + "mimetype" => $_FILES['files']['type'], + "beschreibung" => $this->uid . " Profil Änderung", + "insertvon" => $this->uid, + "insertamum" => "NOW()", + ]; + + $tmp_res = $this->dmslib->upload($dms, 'files', array("jpg", "png", "pdf")); + + if(isError($tmp_res)){ + $this->addError(getError($tmp_res)); + } + + $tmp_res = $this->getDataOrTerminateWithError($tmp_res); + array_push($res, $tmp_res); + } + + $this->terminateWithSuccess($res); + } + + //------------------------------------------------------------------------------------------------------------------ + // Private methods + + + private function sendEmail_onProfilUpdate_insertion($uid, $profil_update_id, $topic) + { + + $this->load->helper('hlp_sancho_helper'); + $emails = []; + + $is_mitarbeiter = $this->MitarbeiterModel->isMitarbeiter($uid); + if (isError($is_mitarbeiter)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_mitarbeiterCheck_error')); + } + $is_mitarbeiter = $this->getDataOrTerminateWithError($is_mitarbeiter); + + //! if the $uid is a mitarbeiter and student, only the hr is notified by email + if ($is_mitarbeiter) { + //? user is not a student therefore he is a mitarbeiter, send email to Personalverwaltung + //? use constant variable MAIL_GST to mail to the personalverwaltung + $this->MitarbeiterModel->addSelect([TRUE]); + $this->MitarbeiterModel->addJoin("public.tbl_benutzer", "public.tbl_benutzer.uid = public.tbl_mitarbeiter.mitarbeiter_uid"); + //? check if the the userID is a mitarbeiter and if the benutzer is active + $res = $this->MitarbeiterModel->loadWhere(["public.tbl_mitarbeiter.mitarbeiter_uid" => $uid, "public.tbl_benutzer.aktiv" => TRUE]); + if (isError($res)) { + $this->terminateWithError("was not able to query the mitarbeiter and benutzer by the uid: " . $uid); + } + if (hasData($res)) { + array_push($emails, MAIL_GST); + } else { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_mitarbeiterCheck_error')); + } + } else { + //? if it is not a mitarbeiter, check whether it is a student and send email to studiengang + $is_student = $this->StudentModel->isStudent($uid); + if (isError($is_student)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_studentCheck_error')); + } + $is_student = $this->getDataOrTerminateWithError($is_student); + if ($is_student) { + //? Send email to the Studiengangsassistentinnen + $this->StudentModel->addSelect(["public.tbl_studiengang.email"]); + $this->StudentModel->addJoin("public.tbl_benutzer", "public.tbl_benutzer.uid = public.tbl_student.student_uid"); + $this->StudentModel->addJoin("public.tbl_prestudent", "public.tbl_benutzer.person_id = public.tbl_prestudent.person_id"); + $this->StudentModel->addJoin("public.tbl_prestudentstatus", "public.tbl_prestudentstatus.prestudent_id = public.tbl_prestudent.prestudent_id"); + $this->StudentModel->addJoin("public.tbl_studiengang", "public.tbl_studiengang.studiengang_kz = public.tbl_prestudent.studiengang_kz"); + //* check if the benutzer itself is active + //* check if the student status is Student or Diplomand (active students) + $this->StudentModel->db->where_in("public.tbl_prestudentstatus.status_kurzbz", ['Student', 'Diplomand']); + $res = $this->StudentModel->loadWhere(["public.tbl_benutzer.aktiv" => TRUE, "public.tbl_student.student_uid" => $uid]); + if (isError($res)) { + $this->terminateWithError(getError($res)); + } else { + $res = $this->getDataOrTerminateWithError($res); + foreach ($res as $emailObj) { + array_push($emails, $emailObj->email); + } + } + } + } + $mail_res = []; + //? sending email + foreach ($emails as $email) { + array_push($mail_res, sendSanchoMail("profil_update", ['uid' => $uid, 'topic' => $topic, 'href' => APP_ROOT . 'Cis/ProfilUpdate/id/' . $profil_update_id], $email, ("Profil Änderung von " . $uid))); + } + foreach ($mail_res as $m_res) { + if (!$m_res) { + $this->addError($this->p->t('profilUpdate', 'profilUpdate_email_error')); + } + } + + } + + private function sendEmail_onProfilUpdate_response($uid, $topic, $status) + { + $this->load->helper('hlp_sancho_helper'); + $email = $uid . "@" . DOMAIN; + + + function languageQuery($language) + { + return "select index from public.tbl_sprache where sprache = '" + $language + "'"; + } + + $this->ProfilUpdateStatusModel->addSelect(["bezeichnung_mehrsprachig[(" . languageQuery('German') . ")] as status_de", "bezeichnung_mehrsprachig[(" . languageQuery('English') . ")] as status_en"]); + + $status_translation = $this->ProfilUpdateStatusModel->loadWhere(["status_kurzbz" => $status]); + + if (isError($status_translation)) { + $this->terminateWithError($this->p->t('profilUpdate', 'ProfilUpdateStatusTranslationError')); + } + + $status_translation = hasData($status_translation) ? getData($status_translation)[0] : null; + + if (isset($status_translation)) { + $mail_res = sendSanchoMail("profil_update_response", ['topic' => $topic, 'status_de' => $status_translation->status_de, 'status_en' => $status_translation->status_en, 'href' => APP_ROOT . 'Cis/Profil'], $email, ("Profil Änderung " . $this->p->t('profilUpdate', 'pending'))); + if (!$mail_res) { + $this->addError($this->p->t('profilUpdate', 'profilUpdate_email_error')); + } + } + } + + private function setStatusOnUpdateRequest($id, $status, $status_message) + { + return $this->ProfilUpdateModel->update([$id], ["status" => $status, "status_timestamp" => "NOW()", "status_message" => $status_message]); + } + + private function updateRequestedChange($id, $requested_change) + { + return $this->ProfilUpdateModel->update([$id], ['requested_change' => json_encode($requested_change)]); + } + + private function handleAdresse($requested_change, $personID) + { + $this->AdressenTypModel->addSelect(["adressentyp_kurzbz"]); + $adr_kurzbz = $this->AdressenTypModel->loadWhere(["bezeichnung" => $requested_change['typ']]); + $adr_kurzbz = $this->getDataOrTerminateWithError($adr_kurzbz)[0]->adressentyp_kurzbz; + + //? replace the address_typ with its correct kurzbz foreign key + $requested_change['typ'] = $adr_kurzbz; + + $adresse_id = $requested_change["adresse_id"]; + + //? removes the adresse_id because we don't want to update the kontakt_id in the database + unset($requested_change["adresse_id"]); + + //! ADD + if (array_key_exists('add', $requested_change) && $requested_change['add']) { + + //? removes add flag + unset($requested_change['add']); + $requested_change['insertamum'] = "NOW()"; + $requested_change['insertvon'] = getAuthUID(); + $requested_change['person_id'] = $personID; + //TODO: zustelladresse, heimatadresse, rechnungsadresse und nation werden nicht beachtet + $insertID = $this->AdresseModel->insert($requested_change); + $insert_adresse_id = $insertID; + if (isError($insert_adresse_id)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_insertAdresse_error')); + } + $insert_adresse_id = $this->getDataOrTerminateWithError($insert_adresse_id); + if ($insert_adresse_id) { + $this->handleDupplicateZustellAdressen($requested_change['zustelladresse'], $insert_adresse_id); + } + + } + //! DELETE + elseif (array_key_exists('delete', $requested_change) && $requested_change['delete']) { + $result = $this->AdresseModel->delete($adresse_id); + if(isError($result)){ + $this->terminateWithError(getError($result)); + } + } + //! UPDATE + else { + $requested_change['updateamum'] = "NOW()"; + $requested_change['updatevon'] = getAuthUID(); + $update_adresse_id = $this->AdresseModel->update($adresse_id, $requested_change); + if (isError($update_adresse_id)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_updateAdresse_error')); + } + $update_adresse_id = $this->getDataOrTerminateWithError($update_adresse_id); + $this->handleDupplicateZustellAdressen($requested_change['zustelladresse'], $update_adresse_id); + + } + return $insertID ?? null; + } + + + private function handleDupplicateZustellAdressen($zustellung, $adresse_id) + { + if ($zustellung) { + $this->PersonModel->addSelect("public.tbl_adresse.adresse_id"); + $this->PersonModel->addJoin("public.tbl_adresse", "public.tbl_adresse.person_id = public.tbl_person.person_id"); + $zustellAdressenArray = $this->PersonModel->loadWhere(["public.tbl_person.person_id" => $this->pid, "zustelladresse" => TRUE]); + if (isError($zustellAdressenArray)) { + $this->terminateWithError($this->p->t('profilUpdate', 'profilUpdate_loadingZustellAdressen_error')); + } + $zustellAdressenArray = $this->getDataOrTerminateWithError($zustellAdressenArray); + + if (count($zustellAdressenArray) > 0) { + + $zustellAdressenArray = array_filter($zustellAdressenArray, function ($adresse) use ($adresse_id) { + + return $adresse->adresse_id != $adresse_id; + }); + + // remove the zustelladresse from all other zustelladressen + foreach ($zustellAdressenArray as $adresse) { + $this->AdresseModel->update($adresse->adresse_id, ["zustelladresse" => FALSE]); + } + + } + } + } + + private function deleteOldVersionFile($dms_id) + { + // starting the transaction + $this->db->trans_start(); + + + if (!isset($dms_id)) { + return; + } + + //? delete the file from the profilUpdate first + $profilUpdateFileDelete = $this->ProfilUpdateModel->removeFileFromProfilUpdate($dms_id); + if(isError($profilUpdateFileDelete)){ + $this->terminateWithError(getError($profilUpdateFileDelete)); + } + + //? delete all the different versions of the dms_file + $dmsVersions = $this->DmsVersionModel->loadWhere(["dms_id" => $dms_id]); + $dmsVersions = $this->getDataOrTerminateWithError($dmsVersions); + + + + $dms_versions = array_map(function ($item) { + return $item->version; + }, $dmsVersions); + + + $test_array = array(); + foreach ($dms_versions as $version) { + + $delete_result = $this->dmslib->removeVersion($dms_id, $version); + array_push($test_array, $delete_result); + + if(isError($delete_result)){ + $this->addError(getError($delete_result)); + } + } + + // transaction complete + $this->db->trans_complete(); + + if ($this->db->trans_status() === FALSE) + { + return false; + } + else + { + return true; + } + + } + +} + + diff --git a/application/models/codex/Gemeinde_model.php b/application/models/codex/Gemeinde_model.php index c782346a0..92f0e0ca4 100755 --- a/application/models/codex/Gemeinde_model.php +++ b/application/models/codex/Gemeinde_model.php @@ -19,4 +19,28 @@ class Gemeinde_model extends DB_Model return $this->loadWhere(array("plz" => $plz)); } + + public function getGemeindeByNation($nation, $zip){ + + + $this->addSelect(["name"]); + + if ($nation == "A") { + if (isset($zip) && $zip > 999 && $zip < 32000) { + + $gemeinde_res = $this->GemeindeModel->loadWhere(['plz' => $zip]); + if (isError($gemeinde_res)) { + show_error("error while trying to query bis.tbl_gemeinde"); + } + $gemeinde_res = hasData($gemeinde_res) ? getData($gemeinde_res) : null; + $gemeinde_res = array_map(function ($obj) { + return $obj->name; + }, $gemeinde_res); + echo json_encode($gemeinde_res); + + } else { + echo json_encode(error("ortschaftskennziffer code was not valid")); + } + } else {} + } } \ No newline at end of file diff --git a/application/models/person/Profil_update_model.php b/application/models/person/Profil_update_model.php index 68394dd9a..ffb04b7e7 100755 --- a/application/models/person/Profil_update_model.php +++ b/application/models/person/Profil_update_model.php @@ -77,6 +77,21 @@ class Profil_update_model extends DB_Model } + //? remove File from the Profil Update + public function removeFileFromProfilUpdate($dms_id) + { + + if(!is_int($dms_id) || $dms_id < 0){ + return error("not valid dms_id"); + } + + return $this->execReadOnlyQuery(" + UPDATE public.tbl_profil_update + SET attachment_id = NULL + WHERE attachment_id = ?", [$dms_id]); + + } + /** * getProfilUpdateWithPermission diff --git a/application/views/Cis/Documents.php b/application/views/Cis/Documents.php index 191558d86..74d7b1349 100755 --- a/application/views/Cis/Documents.php +++ b/application/views/Cis/Documents.php @@ -12,9 +12,10 @@ $this->load->view('templates/CISHTML-Header', $includesArray);