From f3b79cd731e3648c98be66c91c3956514b215da1 Mon Sep 17 00:00:00 2001 From: bison-paolo Date: Mon, 17 Oct 2016 17:07:51 +0200 Subject: [PATCH] - Added method chkRights to DB_Model - Added method toPhp to DB_Model to convert array and boolean types from PostgresSQL to php - Added method execQuery to DB_Model to execute a query (it calls toPhp) - Added method pgsqlArrayToPhpArray to convert a pgsql array to php - Updated DB_Model methods to using chkRights (and toPhp where it is needed) - Removed methods escapeArray and _pgsqlArrayToPhpArray from controller APIv1_Controller - Removed escapeArray from controllers Dokumentstudiengang and Dokument - Updated models to use execQuery (and chkRights where it is needed) --- .../controllers/api/v1/crm/Dokument.php | 3 - .../api/v1/crm/Dokumentstudiengang.php | 6 - application/core/APIv1_Controller.php | 56 +-- application/core/DB_Model.php | 348 +++++++++++++----- application/models/codex/Orgform_model.php | 12 +- application/models/crm/Akte_model.php | 14 +- .../models/crm/Dokumentprestudent_model.php | 14 +- .../models/crm/Prestudentstatus_model.php | 7 +- .../Organisationseinheit_model.php | 15 +- .../models/organisation/Studiengang_model.php | 4 +- .../organisation/Studiensemester_model.php | 31 +- application/models/person/Person_model.php | 60 ++- application/models/system/Message_model.php | 6 +- application/models/system/Phrase_model.php | 9 +- application/models/system/Recipient_model.php | 31 +- application/models/system/Vorlage_model.php | 15 +- .../models/system/Vorlagedokument_model.php | 14 +- 17 files changed, 327 insertions(+), 318 deletions(-) diff --git a/application/controllers/api/v1/crm/Dokument.php b/application/controllers/api/v1/crm/Dokument.php index cff810916..21c364cb6 100644 --- a/application/controllers/api/v1/crm/Dokument.php +++ b/application/controllers/api/v1/crm/Dokument.php @@ -37,9 +37,6 @@ class Dokument extends APIv1_Controller { $result = $this->DokumentModel->load($dokument_kurzbz); - // Workaround - $result = $this->escapeArrays($result, array('bezeichnung_mehrsprachig', 'dokumentbeschreibung_mehrsprachig')); - $this->response($result, REST_Controller::HTTP_OK); } else diff --git a/application/controllers/api/v1/crm/Dokumentstudiengang.php b/application/controllers/api/v1/crm/Dokumentstudiengang.php index b4b6c07c9..2d90c44e7 100644 --- a/application/controllers/api/v1/crm/Dokumentstudiengang.php +++ b/application/controllers/api/v1/crm/Dokumentstudiengang.php @@ -38,9 +38,6 @@ class Dokumentstudiengang extends APIv1_Controller { $result = $this->DokumentstudiengangModel->load(array($studiengang_kz, $dokument_kurzbz)); - // Workaround - $result = $this->escapeArrays($result, array('beschreibung_mehrsprachig')); - $this->response($result, REST_Controller::HTTP_OK); } else @@ -62,9 +59,6 @@ class Dokumentstudiengang extends APIv1_Controller { $result = $this->DokumentstudiengangModel->getDokumentstudiengangByStudiengang_kz($studiengang_kz, $onlinebewerbung, $pflicht); - // Workaround - $result = $this->escapeArrays($result, array('bezeichnung_mehrsprachig', 'dokumentbeschreibung_mehrsprachig')); - $this->response($result, REST_Controller::HTTP_OK); } else diff --git a/application/core/APIv1_Controller.php b/application/core/APIv1_Controller.php index b3cb37bd1..cb5196dd6 100644 --- a/application/core/APIv1_Controller.php +++ b/application/core/APIv1_Controller.php @@ -9,7 +9,7 @@ class APIv1_Controller extends REST_Controller parent::__construct(); // Loads return messages - $this->load->helper('message'); + $this->load->helper('Message'); } /** @@ -23,7 +23,7 @@ class APIv1_Controller extends REST_Controller { foreach($data as $key=>$value) { - if($value === "") + if($value === '') { $data[$key] = NULL; } @@ -36,60 +36,10 @@ class APIv1_Controller extends REST_Controller } else { - if($data == "") + if($data == '') { return NULL; } } } - - /** ---------------------------------------------------------------------------------------------------------------------------------- - * Workaround for converting a pgsql array to a php array - * To be dropped as soon as possible :D - */ - protected function escapeArrays($result, $fields_names) - { - if (is_object($result) && isset($result->retval) && is_array($result->retval)) - { - for ($i = 0; $i < count($result->retval); $i++) - { - foreach($fields_names as $field_name) - { - if (isset($result->retval[$i]->{$field_name})) - { - $result->retval[$i]->{$field_name} = $this->_pgsqlArrayToPhpArray($result->retval[$i]->{$field_name}); - } - } - } - } - - return $result; - } - - /** - * To be moved to DB_model - */ - private function _pgsqlArrayToPhpArray($string) - { - $result = array(); - - if (!empty($string)) - { - preg_match_all( - '/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(?hasSequence = $hasSequence; $this->load->database(); } - + /** --------------------------------------------------------------- * Insert Data into DB-Table * @@ -27,11 +34,10 @@ class DB_Model extends FHC_Model // Check Class-Attributes if (is_null($this->dbTable)) return error(FHC_MODEL_ERROR, FHC_NODBTABLE); - - // Check rights - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::INSERT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; - + + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::INSERT_RIGHT)) return $chkRights; + // DB-INSERT if ($this->db->insert($this->dbTable, $data)) { @@ -73,9 +79,8 @@ class DB_Model extends FHC_Model if (is_null($this->dbTable)) return error(FHC_MODEL_ERROR, FHC_NODBTABLE); - // Check rights - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::REPLACE_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::REPLACE_RIGHT)) return $chkRights; // DB-REPLACE if ($this->db->replace($this->dbTable, $data)) @@ -99,9 +104,8 @@ class DB_Model extends FHC_Model if (is_null($this->pk)) return error(FHC_MODEL_ERROR, FHC_NOPK); - // Check rights - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::UPDATE_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::UPDATE_RIGHT)) return $chkRights; // DB-UPDATE // Check for composite Primary Key @@ -119,6 +123,40 @@ class DB_Model extends FHC_Model else return error($this->db->error(), FHC_DB_ERROR); } + + /** --------------------------------------------------------------- + * Delete data from DB-Table + * + * @param string $id Primary Key for DELETE + * @return array + */ + public function delete($id) + { + // Check Class-Attributes + if (is_null($this->dbTable)) + return error(FHC_MODEL_ERROR, FHC_NODBTABLE); + if (is_null($this->pk)) + return error(FHC_MODEL_ERROR, FHC_NOPK); + + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::DELETE_RIGHT)) return $chkRights; + + // DB-DELETE + // Check for composite Primary Key + if (is_array($id)) + { + if (isset($id[0])) + $result = $this->db->delete($this->dbTable, $this->_arrayMergeIndex($this->pk, $id)); + else + $result = $this->db->delete($this->dbTable, $id); + } + else + $result = $this->db->delete($this->dbTable, array($this->pk => $id)); + if ($result) + return success($id); + else + return error($this->db->error(), FHC_DB_ERROR); + } /** --------------------------------------------------------------- * Load single data from DB-Table @@ -134,10 +172,8 @@ class DB_Model extends FHC_Model if (is_null($this->pk)) return error(FHC_MODEL_ERROR, FHC_NOPK); - // Check rights only if this method is called from a model - if (substr(get_called_class(), -6) == '_model') - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; // DB-SELECT // Check for composite Primary Key @@ -154,7 +190,7 @@ class DB_Model extends FHC_Model $result = $this->db->get_where($this->dbTable, array($this->pk => $id)); if ($result) - return success($result->result()); + return success($this->toPhp($result)); else return error($this->db->error(), FHC_DB_ERROR); } @@ -170,17 +206,14 @@ class DB_Model extends FHC_Model if (is_null($this->dbTable)) return error(FHC_MODEL_ERROR, FHC_NODBTABLE); - // Check rights - // Check rights only if this method is called from a model - if (substr(get_called_class(), -6) == '_model') - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; - + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; + // Execute query $result = $this->db->get_where($this->dbTable, $where); if ($result) - return success($result->result()); + return success($this->toPhp($result)); else return error($this->db->error(), FHC_DB_ERROR); } @@ -191,7 +224,7 @@ class DB_Model extends FHC_Model * * TODO: * - Adding support for composed primary key - * - Adding support for cascading side tables (?) + * - Adding support for cascading side tables (useful?) * * @return array */ @@ -201,11 +234,8 @@ class DB_Model extends FHC_Model if (is_null($this->dbTable)) return error(FHC_MODEL_ERROR, FHC_NODBTABLE); - // Check rights - // Check rights only if this method is called from a model - if (substr(get_called_class(), -6) == '_model') - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; // List of tables on which it will work $tables = array_merge(array($mainTable), $sideTables); @@ -241,7 +271,8 @@ class DB_Model extends FHC_Model if ($resultDB) { // Converts the object that contains data, from the returned CI's object to an array - $resultArray = $resultDB->result(); + // with the postgresql array and boolean types converterd + $resultArray = $this->toPhp($resultDB); // Array that will contain all the mainTable records, and to each record the linked data // of a side table $returnArray = array(); @@ -306,22 +337,6 @@ class DB_Model extends FHC_Model return $result; } - /** - * Used in loadTree - */ - private function findMainTable($mainTableObj, $mainTableArray) - { - for ($i = 0; $i < count($mainTableArray); $i++) - { - if ($mainTableObj->{$this->pk} == $mainTableArray[$i]->{$this->pk}) - { - return $i; - } - } - - return false; - } - /** --------------------------------------------------------------- * Add a table to join with * @@ -402,42 +417,6 @@ class DB_Model extends FHC_Model return success(true); } - - /** --------------------------------------------------------------- - * Delete data from DB-Table - * - * @param string $id Primary Key for DELETE - * @return array - */ - public function delete($id) - { - // Check Class-Attributes - if (is_null($this->dbTable)) - return error(FHC_MODEL_ERROR, FHC_NODBTABLE); - if (is_null($this->pk)) - return error(FHC_MODEL_ERROR, FHC_NOPK); - - // Check rights only if this method is called from a model - if (substr(get_called_class(), -6) == '_model') - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::DELETE_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; - - // DB-DELETE - // Check for composite Primary Key - if (is_array($id)) - { - if (isset($id[0])) - $result = $this->db->delete($this->dbTable, $this->_arrayMergeIndex($this->pk, $id)); - else - $result = $this->db->delete($this->dbTable, $id); - } - else - $result = $this->db->delete($this->dbTable, array($this->pk => $id)); - if ($result) - return success($id); - else - return error($this->db->error(), FHC_DB_ERROR); - } /** --------------------------------------------------------------- * Reset the query builder state @@ -467,14 +446,21 @@ class DB_Model extends FHC_Model * @param char $b PG-Char to convert * @return bool */ - public function pgBoolPhp($b) + public function pgBoolPhp($val) { - if (is_null($b)) - return null; - elseif ($b === 't') + // If true + if ($val == DB_Model::PGSQL_BOOLEAN_TRUE) + { return true; - else + } + // If false + else if ($val == DB_Model::PGSQL_BOOLEAN_FALSE) + { return false; + } + + // If it is null, let it be null + return $val; } /** --------------------------------------------------------------- @@ -530,7 +516,43 @@ class DB_Model extends FHC_Model } return $return; } - + + /** + * Converts from PostgreSQL array to php array + * It also takes care about array of booleans + */ + public function pgsqlArrayToPhpArray($string, $booleans = false) + { + // At least returns an empty array + $result = array(); + + // String that represents the pgsql array, better if not empty + if (!empty($string)) + { + // Magic convertion + preg_match_all( + '/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(?pgBoolPhp($tmp); + } + // Adds it to the result array + $result[] = $tmp; + } + } + + return $result; + } + /** --------------------------------------------------------------- * Invalid ID * @@ -546,4 +568,152 @@ class DB_Model extends FHC_Model $a[$i[$j]] = $v[$j]; return $a; } + + /** + * Executes a query and converts array and boolean data types from PgSql to php + * @return: boolean false on failure + * boolean if the query is of the write type (INSERT, UPDATE, DELETE...) + * array that represents DB data + */ + protected function execQuery($query, $parametersArray = null) + { + $result = null; + + // If the query is empty don't lose time + if (!empty($query)) + { + // If there are parameters to bind to the query + if (is_array($parametersArray) && count($parametersArray) > 0) + { + $resultDB = $this->db->query($query, $parametersArray); + } + else + { + $resultDB = $this->db->query($query); + } + + // If no errors occurred + if ($resultDB) + { + $result = success($this->toPhp($resultDB)); + } + else + { + $result = error($this->db->error(), FHC_DB_ERROR); + } + } + + return $result; + } + + /** + * Checks if the caller is entitled to perform this operation with this right + */ + protected function chkRights($permission) + { + // If the caller is _not_ a model _and_ tries to read data, then avoids to check permissions + // Otherwise checks always the permissions + if (($permission == PermissionLib::SELECT_RIGHT && + substr(get_called_class(), -6) == DB_Model::MODEL_POSTFIX) || + $permission != PermissionLib::SELECT_RIGHT) + { + if (($chkRights = $this->isEntitled($this->dbTable, $permission, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + { + return $chkRights; + } + } + } + + /** + * Converts array and boolean data types from PgSql to php + * NOTE: PostgreSQL php drivers returns: + * - A boolean value if the query is of the write type (INSERT, UPDATE, DELETE...) + * - A FALSE value on failure + * - Otherwise an object filled with data on success + */ + private function toPhp($result) + { + $toPhp = $result; // if there is nothing to convert then return the result from DB + + // If it's an object its fields will be parsed to find booleans and arrays types + if (is_object($result)) + { + $toBeConverterdArray = array(); // Fields to be converted + $metaDataArray = $result->field_data(); // Fields information + for($i = 0; $i < count($metaDataArray); $i++) // Looking for booleans and arrays + { + // If array type or boolean type + if (strpos($metaDataArray[$i]->type, DB_Model::PGSQL_ARRAY_TYPE) !== false || + $metaDataArray[$i]->type == DB_Model::PGSQL_BOOLEAN_TYPE) + { + // Name and type of the field to be converted + $toBeConverted = new stdClass(); + // Set the type of the field to be converted + $toBeConverted->type = $metaDataArray[$i]->type; + // Set the name of the field to be converted + $toBeConverted->name = $metaDataArray[$i]->name; + // Add the field to be converted to $toBeConverterdArray + array_push($toBeConverterdArray, $toBeConverted); + } + } + + // If there is something to convert, otherwhise don't lose time + if (count($toBeConverterdArray) > 0) + { + // Returns the array of objects, each of them represents a DB record + $resultsArray = $result->result(); + // Looping on results + for($i = 0; $i < count($resultsArray); $i++) + { + // Single element + $tmpResult = $resultsArray[$i]; + // Looping on fields to be converted + for($j = 0; $j < count($toBeConverterdArray); $j++) + { + // Single element + $toBeConverted = $toBeConverterdArray[$j]; + + // Array type + if (strpos($toBeConverted->type, DB_Model::PGSQL_ARRAY_TYPE) !== false) + { + $tmpResult->{$toBeConverted->name} = $this->pgsqlArrayToPhpArray( + $tmpResult->{$toBeConverted->name}, + $toBeConverted->type == DB_Model::PGSQL_BOOLEAN_ARRAY_TYPE + ); + } + // Boolean type + else if ($toBeConverted->type == DB_Model::PGSQL_BOOLEAN_TYPE) + { + $tmpResult->{$toBeConverted->name} = $this->pgBoolPhp($tmpResult->{$toBeConverted->name}); + } + } + } + // Returns DB data as an array + $toPhp = $resultsArray; + } + // And returns DB data as an array + else + { + $toPhp = $result->result(); + } + } + + return $toPhp; + } + + /** + * Used in loadTree to find the main tables + */ + private function findMainTable($mainTableObj, $mainTableArray) + { + for ($i = 0; $i < count($mainTableArray); $i++) + { + if ($mainTableObj->{$this->pk} == $mainTableArray[$i]->{$this->pk}) + { + return $i; + } + } + + return false; + } } \ No newline at end of file diff --git a/application/models/codex/Orgform_model.php b/application/models/codex/Orgform_model.php index 51afac4b2..6b5d1f0e7 100644 --- a/application/models/codex/Orgform_model.php +++ b/application/models/codex/Orgform_model.php @@ -14,20 +14,14 @@ class Orgform_model extends DB_Model public function getOrgformLV() { - // Checks if the operation is permitted by the API caller - if (($chkRights = $this->isEntitled($this->dbTable, PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; $query = "SELECT * FROM bis.tbl_orgform WHERE orgform_kurzbz NOT IN ('VBB', 'ZGS') ORDER BY orgform_kurzbz"; - $result = $this->db->query($query); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query); } } \ No newline at end of file diff --git a/application/models/crm/Akte_model.php b/application/models/crm/Akte_model.php index b55d944d4..828dee826 100644 --- a/application/models/crm/Akte_model.php +++ b/application/models/crm/Akte_model.php @@ -75,12 +75,7 @@ class Akte_model extends DB_Model $query .= ' ORDER BY erstelltam'; - $result = $this->db->query($query, $parametersArray); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } /** @@ -131,11 +126,6 @@ class Akte_model extends DB_Model $query .= ' GROUP BY a.akte_id ORDER BY a.erstelltam'; - $result = $this->db->query($query, $parametersArray); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } } \ No newline at end of file diff --git a/application/models/crm/Dokumentprestudent_model.php b/application/models/crm/Dokumentprestudent_model.php index 2e522f047..6e3afec00 100644 --- a/application/models/crm/Dokumentprestudent_model.php +++ b/application/models/crm/Dokumentprestudent_model.php @@ -35,13 +35,10 @@ class Dokumentprestudent_model extends DB_Model AND ds.studiengang_kz = ? )'; - $result = $this->db->query($query, array($prestudent_id, $studiengang_kz)); + $result = $this->execQuery($query, array($prestudent_id, $studiengang_kz)); } - if ($result) - return success($result); - else - return error($this->db->error(), FHC_DB_ERROR); + return $result; } public function setAcceptedDocuments($prestudent_id, $dokument_kurzbz) @@ -66,12 +63,9 @@ class Dokumentprestudent_model extends DB_Model ) )'; - $result = $this->db->query($query, array($prestudent_id, $dokument_kurzbz, $prestudent_id)); + $result = $this->execQuery($query, array($prestudent_id, $dokument_kurzbz, $prestudent_id)); } - if ($result) - return success($result); - else - return error($this->db->error(), FHC_DB_ERROR); + return $result; } } \ No newline at end of file diff --git a/application/models/crm/Prestudentstatus_model.php b/application/models/crm/Prestudentstatus_model.php index c91a40291..19b94f27f 100644 --- a/application/models/crm/Prestudentstatus_model.php +++ b/application/models/crm/Prestudentstatus_model.php @@ -49,11 +49,6 @@ class Prestudentstatus_model extends DB_Model $query .= ' ORDER BY datum DESC, insertamum DESC, ext_id DESC LIMIT 1'; - $result = $this->db->query($query, $parametersArray); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } } \ No newline at end of file diff --git a/application/models/organisation/Organisationseinheit_model.php b/application/models/organisation/Organisationseinheit_model.php index d33454e95..b3d13f336 100644 --- a/application/models/organisation/Organisationseinheit_model.php +++ b/application/models/organisation/Organisationseinheit_model.php @@ -35,12 +35,8 @@ class Organisationseinheit_model extends DB_Model if (!empty($typ)) $qry .= 'WHERE organisationseinheittyp_kurzbz IN ('.$typ.') '; $qry .= 'ORDER BY path;'; - - if ($res = $this->db->query($qry)) - return success($res); - else - return error($this->db->error()); + return $this->execQuery($query); } /** @@ -79,13 +75,6 @@ class Organisationseinheit_model extends DB_Model $query = sprintf($query, $table, $fields, $schema, $table, $where, $orderby); - if ($result = $this->db->query($query, array($oe_kurzbz))) - { - return success($result->result()); - } - else - { - return error($this->db->error()); - } + return $this->execQuery($query, array($oe_kurzbz)); } } \ No newline at end of file diff --git a/application/models/organisation/Studiengang_model.php b/application/models/organisation/Studiengang_model.php index d6c6e8736..49c1862db 100644 --- a/application/models/organisation/Studiengang_model.php +++ b/application/models/organisation/Studiengang_model.php @@ -96,9 +96,7 @@ class Studiengang_model extends DB_Model AND t1.aktiv IS TRUE ORDER BY typ, studiengangbezeichnung, tbl_lgartcode.bezeichnung ASC'; - $result = $this->db->query($allForBewerbungQuery); - - return success($result->result()); + return $this->execQuery($allForBewerbungQuery); } /** diff --git a/application/models/organisation/Studiensemester_model.php b/application/models/organisation/Studiensemester_model.php index 17fa706f9..f2763f036 100644 --- a/application/models/organisation/Studiensemester_model.php +++ b/application/models/organisation/Studiensemester_model.php @@ -14,9 +14,8 @@ class Studiensemester_model extends DB_Model public function getLastOrAktSemester($days = 60) { - // Checks if the operation is permitted by the API caller - if (($chkRights = $this->isEntitled('public.tbl_studiensemester', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; if (!is_numeric($days)) { @@ -29,19 +28,13 @@ class Studiensemester_model extends DB_Model ORDER BY start DESC LIMIT 1'; - $result = $this->db->query($query); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query); } public function getNextFrom($studiensemester_kurzbz) { - // Checks if the operation is permitted by the API caller - if (($chkRights = $this->isEntitled('public.tbl_studiensemester', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; $query = 'SELECT studiensemester_kurzbz, start, @@ -55,12 +48,7 @@ class Studiensemester_model extends DB_Model ORDER BY start LIMIT 1'; - $result = $this->db->query($query, array($studiensemester_kurzbz)); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, array($studiensemester_kurzbz)); } /** @@ -93,11 +81,6 @@ class Studiensemester_model extends DB_Model $query .= ' ORDER BY delta LIMIT 1'; - $result = $this->db->query($query); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query); } } \ No newline at end of file diff --git a/application/models/person/Person_model.php b/application/models/person/Person_model.php index bd4c42c38..64b328817 100644 --- a/application/models/person/Person_model.php +++ b/application/models/person/Person_model.php @@ -8,15 +8,15 @@ class Person_model extends DB_Model public function __construct() { parent::__construct(); - $this->dbTable = "public.tbl_person"; - $this->pk = "person_id"; + $this->dbTable = 'public.tbl_person'; + $this->pk = 'person_id'; } public function getPersonKontaktByZugangscode($zugangscode, $email) { - $this->addJoin("public.tbl_kontakt", "person_id"); + $this->addJoin('public.tbl_kontakt', 'person_id'); - return $this->loadWhere(array("zugangscode" => $zugangscode, "kontakt" => $email)); + return $this->loadWhere(array('zugangscode' => $zugangscode, 'kontakt' => $email)); } /** @@ -24,77 +24,71 @@ class Person_model extends DB_Model */ public function checkBewerbung($email, $studiensemester_kurzbz = null) { - if (($chkRights = $this->isEntitled("public.tbl_person", "s", FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + if (($chkRights = $this->isEntitled('public.tbl_person', 's', FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) return $chkRights; - if (($chkRights = $this->isEntitled("public.tbl_kontakt", "s", FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + if (($chkRights = $this->isEntitled('public.tbl_kontakt', 's', FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) return $chkRights; - if (($chkRights = $this->isEntitled("public.tbl_benutzer", "s", FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + if (($chkRights = $this->isEntitled('public.tbl_benutzer', 's', FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) return $chkRights; - if (($chkRights = $this->isEntitled("public.tbl_prestudent", "s", FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + if (($chkRights = $this->isEntitled('public.tbl_prestudent', 's', FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) return $chkRights; - if (($chkRights = $this->isEntitled("public.tbl_prestudentstatus", "s", FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + if (($chkRights = $this->isEntitled('public.tbl_prestudentstatus', 's', FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) return $chkRights; - $result = null; - $checkBewerbungQuery = ""; + $checkBewerbungQuery = ''; $parametersArray = array($email, $email, $email); if (is_null($studiensemester_kurzbz)) { - $checkBewerbungQuery = "SELECT DISTINCT p.person_id, p.zugangscode, p.insertamum + $checkBewerbungQuery = 'SELECT DISTINCT p.person_id, p.zugangscode, p.insertamum FROM public.tbl_person p JOIN public.tbl_kontakt k ON p.person_id = k.person_id LEFT JOIN public.tbl_benutzer b ON p.person_id = b.person_id - WHERE k.kontakttyp = 'email' - AND (kontakt = ? OR alias || '@" . DOMAIN . "' = ? OR uid || '@" . DOMAIN . "' = ?) + WHERE k.kontakttyp = \'email\' + AND (kontakt = ? OR alias || \'@' . DOMAIN . '\' = ? OR uid || \'@' . DOMAIN . '\' = ?) ORDER BY p.insertamum DESC - LIMIT 1"; + LIMIT 1'; } else { - $checkBewerbungQuery = "SELECT DISTINCT p.person_id, p.zugangscode, p.insertamum + $checkBewerbungQuery = 'SELECT DISTINCT p.person_id, p.zugangscode, p.insertamum FROM public.tbl_person p JOIN public.tbl_kontakt k ON p.person_id = k.person_id LEFT JOIN public.tbl_benutzer b ON p.person_id = b.person_id JOIN public.tbl_prestudent ps ON p.person_id = ps.person_id JOIN public.tbl_prestudentstatus pst ON pst.prestudent_id = ps.prestudent_id - WHERE k.kontakttyp = 'email' - AND (kontakt = ? OR alias || '@" . DOMAIN . "' = ? OR uid || '@" . DOMAIN . "' = ?) + WHERE k.kontakttyp = \'email\' + AND (kontakt = ? OR alias || \'@' . DOMAIN . '\' = ? OR uid || \'@' . DOMAIN . '\' = ?) AND studiensemester_kurzbz = ? ORDER BY p.insertamum DESC - LIMIT 1"; + LIMIT 1'; array_push($parametersArray, $studiensemester_kurzbz); } - $result = $this->db->query($checkBewerbungQuery, $parametersArray); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($checkBewerbungQuery, $parametersArray); } public function updatePerson($person) { - if (isset($person["svnr"]) && $person["svnr"] != "") + if (isset($person['svnr']) && $person['svnr'] != '') { - $this->PersonModel->addOrder("svnr", "DESC"); + $this->PersonModel->addOrder('svnr', 'DESC'); $result = $this->PersonModel->loadWhere(array( - "person_id != " => $person["person_id"], - "SUBSTRING(svnr FROM 1 FOR 10) = " => $person["svnr"]) + 'person_id != ' => $person['person_id'], + 'SUBSTRING(svnr FROM 1 FOR 10) = ' => $person['svnr']) ); if (hasData($result)) { - if (count($result->retval) == 1 && $result->retval[0]->svnr == $person["svnr"]) + if (count($result->retval) == 1 && $result->retval[0]->svnr == $person['svnr']) { - $person["svnr"] = $person["svnr"] . "v1"; + $person['svnr'] = $person['svnr'] . 'v1'; } else { - $person["svnr"] = $person["svnr"] . "v" . ($result->retval[0]->svnr{11} + 1); + $person['svnr'] = $person['svnr'] . 'v' . ($result->retval[0]->svnr{11} + 1); } } } - return $this->PersonModel->update($person["person_id"], $person); + return $this->PersonModel->update($person['person_id'], $person); } } \ No newline at end of file diff --git a/application/models/system/Message_model.php b/application/models/system/Message_model.php index 6fd630e18..8d4fad145 100644 --- a/application/models/system/Message_model.php +++ b/application/models/system/Message_model.php @@ -64,10 +64,6 @@ class Message_model extends DB_Model $sql = sprintf($sql, 'WHERE status >= 3'); } - $result = $this->db->query($sql, $parametersArray); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($sql, $parametersArray); } } \ No newline at end of file diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index 1433ef43b..952631171 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -57,12 +57,7 @@ class Phrase_model extends DB_Model $parametersArray['orgform_kurzbz'] = $orgform_kurzbz; $query .= ' AND orgform_kurzbz = ?'; } - - $result = $this->db->query($query, $parametersArray); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } -} +} \ No newline at end of file diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 1aaa2690b..772650d73 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -47,11 +47,7 @@ class Recipient_model extends DB_Model $parametersArray = array($message_id, $person_id); // Get data of the messages to sent - $result = $this->db->query($query, $parametersArray); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } /** @@ -85,11 +81,7 @@ class Recipient_model extends DB_Model WHERE r.token = ? LIMIT 1'; - $result = $this->db->query($sql, array(MSG_STATUS_DELETED, $token)); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($sql, array(MSG_STATUS_DELETED, $token)); } /** @@ -147,11 +139,7 @@ class Recipient_model extends DB_Model $sql = sprintf($sql, 'WHERE person_id = ? AND message_id NOT IN (SELECT message_id FROM public.tbl_msg_status WHERE status >= 3 AND person_id = ?)'); } - $result = $this->db->query($sql, $parametersArray); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($sql, $parametersArray); } /** @@ -204,11 +192,7 @@ class Recipient_model extends DB_Model if (! $all) $sql .= ' AND (status < 3 OR status IS NULL)'; - $result = $this->db->query($sql, array($uid)); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($sql, array($uid)); } /** @@ -273,11 +257,6 @@ class Recipient_model extends DB_Model array_push($parametersArray, $limit); } - // Get data of the messages to sent - $result = $this->db->query($query, $parametersArray); - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($query, $parametersArray); } } \ No newline at end of file diff --git a/application/models/system/Vorlage_model.php b/application/models/system/Vorlage_model.php index 45541f601..e5633d96b 100644 --- a/application/models/system/Vorlage_model.php +++ b/application/models/system/Vorlage_model.php @@ -1,7 +1,7 @@ chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; - if ($res = $this->db->query($qry)) - return success($res); - else - return error($this->db->error()); + $query = 'SELECT DISTINCT mimetype FROM public.tbl_vorlage ORDER BY mimetype'; + + return $this->execQuery($query); } -} +} \ No newline at end of file diff --git a/application/models/system/Vorlagedokument_model.php b/application/models/system/Vorlagedokument_model.php index 45ac287a1..924ff16a9 100644 --- a/application/models/system/Vorlagedokument_model.php +++ b/application/models/system/Vorlagedokument_model.php @@ -17,11 +17,8 @@ class Vorlagedokument_model extends DB_Model */ public function loadDokumenteFromVorlagestudiengang($vorlagestudiengang_id) { - // Checks if the operation is permitted by the API caller - if (($chkRights = $this->isEntitled('public.tbl_vorlagedokument', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) - return $chkRights; - - $result = null; + // Checks rights + if ($chkRights = $this->chkRights(PermissionLib::SELECT_RIGHT)) return $chkRights; $qry = 'SELECT vorlagedokument_id, sort, @@ -33,11 +30,6 @@ class Vorlagedokument_model extends DB_Model WHERE vorlagestudiengang_id = ? ORDER BY sort ASC'; - $result = $this->db->query($qry, array($vorlagestudiengang_id)); - - if (is_object($result)) - return success($result->result()); - else - return error($this->db->error(), FHC_DB_ERROR); + return $this->execQuery($qry, array($vorlagestudiengang_id)); } } \ No newline at end of file