- Added method loadList to model DB_model

- Added method getStudienplan to model Studiengang_model
- Changes in method getStudiengangStudienplan of controller Studiengang2
This commit is contained in:
bison
2016-08-26 14:21:04 +02:00
parent 59b5d76e44
commit 7e71681d26
3 changed files with 170 additions and 130 deletions
+134 -19
View File
@@ -136,7 +136,6 @@ class DB_Model extends FHC_Model
// Check rights only if this method is called from a model
//var_dump(get_called_class());
if (substr(get_called_class(), -6) == '_model')
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR);
@@ -174,21 +173,139 @@ class DB_Model extends FHC_Model
// Check rights
// Check rights only if this method is called from a model
//var_dump(get_called_class());
if (substr(get_called_class(), -6) == '_model')
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR);
// DB-SELECT
if (is_null($where))
$result = $this->db->get($this->dbTable);
else
$result = $this->db->get_where($this->dbTable, $where);
// Execute query
$result = $this->db->get_where($this->dbTable, $where);
if ($result)
return $this->_success($result->result());
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
* Load data and convert a record into a list of data from the main table,
* and linked to every element, the data from the side tables
*
* TODO:
* - Adding support for composed primary key
* - Adding support for cascading side tables
*
* @return array
*/
public function loadList($mainTable, $sideTables, $where = null, $sideTablesAliases = null)
{
// Check Class-Attributes
if (is_null($this->dbTable))
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
// Check rights
// Check rights only if this method is called from a model
if (substr(get_called_class(), -6) == '_model')
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR);
// List of tables on which it will work
$tables = array_merge(array($mainTable), $sideTables);
// Array that will contain the number of columns of each table
$tableColumnsCountArray = array();
// Generates the select clause based on the columns of each table
$select = "";
for ($t = 0; $t < count($tables); $t++)
{
$fields = $this->db->list_fields($tables[$t]); // list of the columns of the current table
for ($f = 0; $f < count($fields); $f++)
{
// To avoid overwriting of the properties within the object returned by CI
// will be given an alias to every column, that will be composed with the following schema
// <table name>.<column name> AS <table_name>_<column name>
$select .= $tables[$t] . "." . $fields[$f] . " AS " . $tables[$t] . "_" . $fields[$f];
if ($f < count($fields) - 1) $select .= ", ";
}
if ($t < count($tables) - 1) $select .= ", ";
$tableColumnsCountArray[$t] = count($fields);
}
// Adds the select clause
$this->addSelect($select);
// Execute the query
$resultDB = $this->db->get_where($this->dbTable, $where);
// If everything went ok...
if ($resultDB)
{
// Converts the object that contains data, from the returned CI's object to an array
$resultArray = $resultDB->result();
// Array that will contain all the mainTable records, and to each record the linked data
// of a side table
$returnArray = array();
$returnArrayCounter = 0; // Array counter
$prevPK = null; // Previous primary key
// Iterates the array that contains data from DB
for ($i = 0; $i < count($resultArray); $i++)
{
// Converts an object properties to an associative array
$objectVars = get_object_vars($resultArray[$i]);
// Temporary array that will contain a representation of every records returned from DB
// every element is an associative array that contains all the data of each table
$objTmpArray = array();
// Gets all the data of a single table from the returned record, and creates an object filled with these data
for ($f = 0; $f < count($tableColumnsCountArray); $f++)
{
$objTmpArray[$f] = new stdClass(); // Object that will represent a data set of a table
foreach (array_slice($objectVars, $f == 0 ? 0 : $tableColumnsCountArray[$f - 1], $tableColumnsCountArray[$f]) as $key => $value)
{
$objTmpArray[$f]->{str_replace($tables[$f] . "_", "", $key)} = $value;
}
}
// Object that represents data of the main table
$mainTableObj = $objTmpArray[0];
// Fill $returnArray with all data from mainTable, and for each element will link the data from the side tables
for ($t = 1; $t < count($tables); $t++)
{
// Object that represents data of the side table
$sideTableObj = $objTmpArray[$t];
$sideTableProperty = $tables[$t];
if (is_array($sideTablesAliases))
{
$sideTableProperty = $sideTablesAliases[$t -1];
}
if ($prevPK == $mainTableObj->{$this->pk})
{
array_push($returnArray[$returnArrayCounter - 1]->{$sideTableProperty}, $sideTableObj);
}
else
{
$mainTableObj->{$sideTableProperty} = array($sideTableObj);
$returnArray[$returnArrayCounter++] = $mainTableObj;
}
$prevPK = $mainTableObj->{$this->pk};
}
}
// Sets result with the standard success object that contains all the studiengang
$result = $this->_success($returnArray);
}
else
{
$result = $this->_error($resultDB);
}
return $result;
}
/** ---------------------------------------------------------------
* Add a table to join with
@@ -203,14 +320,13 @@ class DB_Model extends FHC_Model
// Check rights for joined table
// Check rights only if this method is called from a model
//var_dump(get_called_class());
if (substr(get_called_class(), -6) == '_model')
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($joinTable), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($joinTable), FHC_MODEL_ERROR);
// if (substr(get_called_class(), -6) == '_model')
// if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($joinTable), 's'))
// return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($joinTable), FHC_MODEL_ERROR);
$this->db->join($joinTable, $cond, $type);
return $this->_success(TRUE);
return $this->_success(true);
}
/** ---------------------------------------------------------------
@@ -226,7 +342,7 @@ class DB_Model extends FHC_Model
$this->db->order_by($field, $type);
return $this->_success(TRUE);
return $this->_success(true);
}
/** ---------------------------------------------------------------
@@ -234,15 +350,15 @@ class DB_Model extends FHC_Model
*
* @return void
*/
public function addSelect($select)
public function addSelect($select, $escape = true)
{
// Check Class-Attributes and parameters
if (is_null($select) || $select == '')
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
$this->db->select($select);
$this->db->select($select, $escape);
return $this->_success(TRUE);
return $this->_success(true);
}
/** ---------------------------------------------------------------
@@ -275,7 +391,7 @@ class DB_Model extends FHC_Model
$this->db->limit($start);
}
return $this->_success(TRUE);
return $this->_success(true);
}
/** ---------------------------------------------------------------
@@ -293,7 +409,6 @@ class DB_Model extends FHC_Model
return $this->_error(lang('fhc_'.FHC_NOPK), FHC_MODEL_ERROR);
// Check rights only if this method is called from a model
//var_dump(get_called_class());
if (substr(get_called_class(), -6) == '_model')
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 'd'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR);
@@ -392,7 +507,7 @@ class DB_Model extends FHC_Model
else
if (!$string && ($ch=='"' || $ch=="'"))
{
$string = TRUE;
$string = true;
$quote = $ch;
}
else