This commit is contained in:
Paminger
2016-05-01 11:53:15 +02:00
parent 2dada5200c
commit 8cb8782586
16 changed files with 326 additions and 187 deletions
+102 -12
View File
@@ -2,28 +2,118 @@
class DB_Model extends FHC_Model
{
protected $dbTable = NULL; // Name of the DB-Table for CI-Insert, -Update, ...
protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ...
protected $pk; // Name of the PrimaryKey for DB-Update, Load, ...
protected $acl; // Name of the PrimaryKey for DB-Update, Load, ...
// Addon ID, stored to let to check the permissions
private $_addonID;
function __construct()
function __construct($dbTable = null, $pk = null)
{
parent::__construct();
$this->dbTable = $dbTable;
$this->pk = $pk;
$this->load->database();
$this->lang->load('fhc_db');
$this->acl = $this->config->item('fhc_acl');
}
/** ---------------------------------------------------------------
* Insert Data into DB-Table
*
* @param array $data DataArray for Insert
* @return array
*/
public function insert($data)
{
if(!is_null($this->dbTable))
{
$this->db->insert($this->dbTable, $data);
return TRUE;
}
// Check Class-Attributes
if(is_null($this->dbTable))
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
// Check rights
if (! $this->fhc_db_acl->isBerechtigt((string)($this->acl[$this->dbTable]), 'i'))
return $this->_error(lang('fhc_'.FHC_NORIGHT), FHC_MODEL_ERROR);
// DB-INSERT
if ($this->db->insert($this->dbTable, $data))
return $this->_success($this->db->insert_id());
else
{
return FALSE;
}
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
* Replace Data in DB-Table
*
* @param array $data DataArray for Replacement
* @return array
*/
public function replace($data)
{
// Check Class-Attributes
if(is_null($this->dbTable))
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
// Check rights
if (! $this->fhc_db_acl->isBerechtigt((string)($this->acl[$this->dbTable]), 'ui'))
return $this->_error(lang('fhc_'.FHC_NORIGHT), FHC_MODEL_ERROR);
// DB-REPLACE
if ($this->db->replace($this->dbTable, $data))
return $this->_success($this->db->insert_id());
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
* Update Data in DB-Table
*
* @param string $id PK for DB-Table
* @param array $data DataArray for Insert
* @return array
*/
public function update($id, $data)
{
// Check Class-Attributes
if(is_null($this->dbTable))
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
if(is_null($this->pk))
return $this->_error(lang('fhc_'.FHC_NOPK), FHC_MODEL_ERROR);
// Check rights
if (! $this->fhc_db_acl->isBerechtigt((string)($this->acl[$this->dbTable]), 'u'))
return $this->_error(lang('fhc_'.FHC_NORIGHT), FHC_MODEL_ERROR);
// DB-UPDATE
$this->db->where($this->pk, $id);
if ($this->db->update($this->dbTable, $data))
return $this->_success($id);
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
* Replace Data in DB-Table
*
* @param array $data DataArray for Replacement
* @return array
*/
public function load($id)
{
// Check Class-Attributes
if(is_null($this->dbTable))
return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR);
if(is_null($this->pk))
return $this->_error(lang('fhc_'.FHC_NOPK), FHC_MODEL_ERROR);
// Check rights
if (! $this->fhc_db_acl->isBerechtigt((string)($this->acl[$this->dbTable]), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT), FHC_MODEL_ERROR);
// DB-SELECT
if ($this->db->get_where($this->dbTable, array($this->pk => $id)))
return $this->_success($id);
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
/** ---------------------------------------------------------------
@@ -61,4 +151,4 @@ class DB_Model extends FHC_Model
{
return $this->_addonID;
}
}
}