redesign for mulituser
change validation concept (client and server) using entities
This commit is contained in:
@@ -3,41 +3,65 @@
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class mAccounts extends Model {
|
||||
protected $table = 'budget_accounts';
|
||||
private $defaultTableName='_budget_accounts';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
protected $returnType = 'object';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $useSoftDeletes = true;
|
||||
protected $deletedField ="deleted_at";
|
||||
|
||||
protected $allowedFields = ['parent_id','type','bookable','description','lkz'];
|
||||
protected $allowedFields = ['parent_id','bookable','description','account','monitor','deleted_at'];
|
||||
|
||||
protected $useTimestamps = false;
|
||||
private $mAccountList = array();
|
||||
private ?array $mAccountList = null;
|
||||
|
||||
protected function initialize()
|
||||
{
|
||||
$result = $this->findAll();
|
||||
foreach ($result as $value) {
|
||||
$this->mAccountList[$value->id] = $value;
|
||||
protected array $casts = [
|
||||
'bookable' => 'boolean',
|
||||
'monitor' => 'boolean',
|
||||
'account' => 'boolean',
|
||||
];
|
||||
|
||||
protected function initialize() {
|
||||
log_message('debug','mAccounts initialize');
|
||||
$this->table = auth()->user()->preFixDB.$this->defaultTableName;
|
||||
$this->checkOrCreateTable();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sicherstellen dass mAccountList gefüllt ist
|
||||
*/
|
||||
private function populateAccountList(): void {
|
||||
if ($this->mAccountList === null) {
|
||||
log_message('debug','mAccounts populateAccountList New');
|
||||
$result = $this->withDeleted()->findAll();
|
||||
// log_message('debug','mAccounts DB-Result '.json_encode($result));
|
||||
$this->mAccountList = [];
|
||||
foreach ($result as $value) {
|
||||
$this->mAccountList[$value->id] = $value;
|
||||
}
|
||||
//log_message('debug','mAccounts intern List '.json_encode($this->mAccountList));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Erstellt DropDownListe aus gegeben array
|
||||
* @param array liste der daten
|
||||
* @param bool wenn true wird auch ein 0-Eintrag erstellt
|
||||
* @param array $data liste der daten
|
||||
* @param bool $withnull wenn true wird auch ein 0-Eintrag erstellt
|
||||
* @param int $key index für null eintrag
|
||||
* @return mixed
|
||||
*/
|
||||
private function createDropDownList($data,$withnull = true,$key=0)
|
||||
{
|
||||
private function createDropDownList(array $data, bool $withnull = true, int $key=0) {
|
||||
$liste = array();
|
||||
if ($withnull)
|
||||
$liste[$key] = '-----';
|
||||
foreach ( $data as $row )
|
||||
{
|
||||
if ($withnull){
|
||||
if ($key > 0)
|
||||
$liste[$key] = '-----';
|
||||
// else
|
||||
// $liste[""] = '-----';
|
||||
}
|
||||
foreach ( $data as $row ) {
|
||||
$liste[$row->id] = $row->description;
|
||||
}
|
||||
asort( $liste );
|
||||
@@ -46,114 +70,114 @@ class mAccounts extends Model {
|
||||
|
||||
/**
|
||||
* holt Text vom gegebenen Element
|
||||
* @param int id des gewünschten Elements
|
||||
* @param int $id ID des gewünschten Elements
|
||||
* @return string Full Name of Entry
|
||||
*/
|
||||
public function getDropDownEntry($id){
|
||||
public function getDropDownEntry(int $id) {
|
||||
$this->populateAccountList();
|
||||
$element = $this->mAccountList[$id];
|
||||
$description = $element->description;
|
||||
$id = $element->parent_id;
|
||||
while ( $id > 0 )
|
||||
{
|
||||
while ( $id > 0 ) {
|
||||
$description = $this->mAccountList[$id]->description .'-'.$description;
|
||||
$id = $this->mAccountList[$id]->parent_id;
|
||||
}
|
||||
return $description;;
|
||||
return $description;
|
||||
}
|
||||
|
||||
private function filterIt(string $type = 'all', bool $bookableOnly = true, bool $withDeleted = false, ?int $defaultKey = null){
|
||||
return unserialize(serialize(array_filter($this->mAccountList, function($var) use($type, $bookableOnly, $withDeleted, $defaultKey) {
|
||||
if ($defaultKey !== null && $var->id == $defaultKey)
|
||||
return true;
|
||||
if (!($withDeleted) && $var->deleted_at)
|
||||
return false;
|
||||
if ($bookableOnly && !$var->bookable)
|
||||
return false;
|
||||
if ($type == 'Acc' && !$var->account)
|
||||
return false;
|
||||
elseif ($type == 'InOut' && $var->account)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
})));
|
||||
}
|
||||
|
||||
/**
|
||||
* liefert DropDown Liste mit gegeben Suchparametern
|
||||
* @param string Kontotyp "InOut,In,Out,Acc"
|
||||
* @param bool ob bookable gesetzt sein muss
|
||||
* @return bool ob lkz Flag gesetzt sein darf
|
||||
* @param string $type Kontotyp "InOut,Acc,all"
|
||||
* @param bool $bookableOnly ob bookable gesetzt sein muss
|
||||
* @param bool $withDeleted ob gelöschte auch
|
||||
* @return array fertige DropDownListe
|
||||
*/
|
||||
public function getDropDownList($type, $bookable = true, $lkz = false )
|
||||
{
|
||||
if ($bookable == true)
|
||||
$this->where('bookable',true);
|
||||
if ($lkz == false) $this->where('lkz',NULL);
|
||||
if ( $type == 'InOut' ) $this->where('type !=','account');
|
||||
elseif ( $type == 'Acc' ) $this->where('type','account');
|
||||
elseif ( $type == 'In' ) $this->where('type','input');
|
||||
elseif ( $type == 'Out' ) $this->where('type','output');
|
||||
$this->orderBy('description');
|
||||
$result = $this->findAll();
|
||||
$parent = array();
|
||||
foreach ( $result as &$element )
|
||||
{
|
||||
public function getDropDownList(string $type, bool $bookableOnly = true, bool $withDeleted = false, ?int $defaultKey = null) : array {
|
||||
$this->populateAccountList();
|
||||
$result = $this->filterIt($type,$bookableOnly,$withDeleted,$defaultKey);
|
||||
foreach ( $result as &$element ) {
|
||||
$id = $element->parent_id;
|
||||
while ( $id > 0 )
|
||||
{
|
||||
while ( $id > 0 ) {
|
||||
$element->description = $this->mAccountList[$id]->description .'-'.$element->description;
|
||||
if ($this->mAccountList[$id]->parent_id == 0){
|
||||
$parent[] = (object)['id' => $element->id,'description' => $element->description ];
|
||||
}
|
||||
$id = $this->mAccountList[$id]->parent_id;
|
||||
}
|
||||
}
|
||||
return $this->createDropDownList($result);
|
||||
}
|
||||
|
||||
/**
|
||||
public function getCurDDLists(string $type, bool $bookableOnly = true, bool $withDeleted = false, ?int $defaultKey = null ) : array {
|
||||
$this->populateAccountList();
|
||||
$list = $this->filterIt($type,$bookableOnly,$withDeleted, $defaultKey);
|
||||
$parent = $this->getParent($defaultKey);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* liefert 2 DropDown Listen mit gegeben Suchparametern
|
||||
* parent für Vorselektion, subs für Untereinträge
|
||||
* @param string Kontotyp "InOut,In,Out,Acc"
|
||||
* @param bool ob bookable gesetzt sein muss
|
||||
* @return bool ob lkz Flag gesetzt sein darf
|
||||
* @param string $type Kontotyp "InOut,In,Out,Acc"
|
||||
* @param bool $bookableOnly ob bookable gesetzt sein muss
|
||||
* @param bool $withDeleted ob lkz Flag gesetzt sein darf
|
||||
* @return array fertige DropDownListen mit parent und subs
|
||||
*/
|
||||
public function getDropDownLists($type, $bookable = true, $lkz = false )
|
||||
{
|
||||
if ($bookable == true) $this->where('bookable',true);
|
||||
if ($lkz == false) $this->where('lkz',null);
|
||||
if ( $type == 'InOut' ) $this->where('type !=','account');
|
||||
elseif ( $type == 'Acc' ) $this->where('type','account');
|
||||
elseif ( $type == 'In' ) $this->where('type','output');
|
||||
elseif ( $type == 'Out' ) $this->where('type','input');
|
||||
$this->orderBy('description');
|
||||
$result = $this->findAll();
|
||||
public function getDropDownLists(string $type, bool $bookableOnly = true, bool $withDeleted = false, ?int $defaultKey = null ) : array {
|
||||
$this->populateAccountList();
|
||||
$list = $this->filterIt($type,$bookableOnly,$withDeleted, $defaultKey);
|
||||
|
||||
$parent = array();
|
||||
$listen = array();
|
||||
foreach ( $result as &$element )
|
||||
{
|
||||
foreach ( $list as &$element ) {
|
||||
$id = $element->parent_id;
|
||||
while ( $id > 0 )
|
||||
{
|
||||
if ($this->mAccountList[$id]->lkz == 1) continue 2;
|
||||
while ( $id > 0 ) {
|
||||
if ($this->mAccountList[$id]->deleted_at) continue 2;
|
||||
if ($this->mAccountList[$id]->parent_id == 0){
|
||||
if (!array_key_exists($id, $parent))
|
||||
$parent[$id] = (object)['id' => $id,'description' => $this->mAccountList[$id]->description ];
|
||||
$listen[$id][$element->id] = (object)['id' => $element->id,'description' => $element->description ];
|
||||
}
|
||||
else{
|
||||
} else{
|
||||
$element->description = $this->mAccountList[$id]->description .'-'.$element->description;
|
||||
}
|
||||
$id = $this->mAccountList[$id]->parent_id;
|
||||
}
|
||||
}
|
||||
$subs = array();
|
||||
$subs[0] = $this->createDropDownList(array());
|
||||
$subs[0] = ["0"=>"----"];
|
||||
foreach ( $listen as $key => $element ){
|
||||
$subs[$key] = $this->createDropDownList($element,($this->mAccountList[$key]->bookable=="t"), $key);
|
||||
$subs[$key] = $this->createDropDownList($element,($this->mAccountList[$key]->bookable), $key);
|
||||
}
|
||||
return array('parent'=> $this->createDropDownList($parent),'subs'=>(object)$subs);
|
||||
}
|
||||
|
||||
/**
|
||||
* liefert parent id für category
|
||||
* @param int account id
|
||||
* @param int $sid account id
|
||||
* @return int parent id
|
||||
*/
|
||||
public function getParent($sid){
|
||||
public function getParent(int $sid): int{
|
||||
if ($sid == 0) return 0;
|
||||
$result = $sid;
|
||||
$id = $this->mAccountList[$sid]->parent_id;
|
||||
while ( $id > 0 )
|
||||
{
|
||||
$result = $id;
|
||||
$id = $this->mAccountList[$id]->parent_id;
|
||||
$this->populateAccountList();
|
||||
while ( $this->mAccountList[$sid]->parent_id > 0 ) {
|
||||
$sid = $this->mAccountList[$sid]->parent_id;
|
||||
}
|
||||
return $result;
|
||||
return $sid;
|
||||
}
|
||||
|
||||
|
||||
@@ -163,45 +187,71 @@ class mAccounts extends Model {
|
||||
*/
|
||||
public function getTreeData(){
|
||||
helper('array');
|
||||
$parents = array();
|
||||
$subs = array();
|
||||
foreach ( $this->mAccountList as $element )
|
||||
{
|
||||
if ($element->parent_id == 0)
|
||||
$parents[$element->id] = $element;
|
||||
}
|
||||
foreach ( $this->mAccountList as $element )
|
||||
{
|
||||
// if ($element->parent_id > 0)
|
||||
// echo "".$parents[$element->parent_id]->id;
|
||||
$this->populateAccountList();
|
||||
$parents = array_filter($this->mAccountList, function($var) {
|
||||
return ($var->parent_id == 0);
|
||||
});
|
||||
foreach ( $this->mAccountList as $element ) {
|
||||
if (($element->parent_id > 0) && array_key_exists($element->parent_id , $parents))
|
||||
$parents[$element->parent_id]->subs[$element->id] = $element;
|
||||
}
|
||||
array_sort_by_multiple_keys($parents, ['description' => SORT_ASC]);
|
||||
return ['parents'=>$parents,'subs'=>$subs, 'all'=>$this->mAccountList];
|
||||
return ['parents'=>$parents, 'all'=>$this->mAccountList];
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* erstellt oder updatet den gegeben Account
|
||||
* @var array formulardaten
|
||||
*/
|
||||
public function saveAccount($data){
|
||||
public function saveAccount(array $data) : bool{
|
||||
$this->set('parent_id',$data['parent']);
|
||||
$this->set('type', $data['type']);
|
||||
$this->set('bookable', array_key_exists('bookable', $data) ? true : false);
|
||||
$this->set('bookable', array_key_exists('bookable', $data));
|
||||
$this->set('monitor', array_key_exists('monitor', $data));
|
||||
$this->set('account', array_key_exists('account', $data));
|
||||
$this->set('description', $data['description']);
|
||||
$this->set('lkz', (array_key_exists('lkz', $data) ? 1 : null));
|
||||
$this->set('deleted_at', (array_key_exists('deleted_at', $data) ? 'LOCALTIMESTAMP' : null),(array_key_exists('deleted_at', $data)? false: true));
|
||||
if ($data['id'] == 0)
|
||||
$this->insert();
|
||||
$result = $this->insert();
|
||||
else
|
||||
$this->update($data['id']);
|
||||
$this->initialize();
|
||||
$result = $this->update($data['id']);
|
||||
log_message('debug',$this->db->getLastQuery());
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function checkType($id, $type){
|
||||
return ($this->mAccountList[$id]->type == $type);
|
||||
/**
|
||||
* prüft ob gegebene Kategorie ein Konot ist
|
||||
*
|
||||
* @param int $id
|
||||
* @return bool
|
||||
*/
|
||||
public function isAccount(int $id){
|
||||
$this->populateAccountList();
|
||||
return ($this->mAccountList[$id]->account);
|
||||
}
|
||||
|
||||
public function checkOrCreateTable(){
|
||||
log_message('debug',"Check or Create Account Table");
|
||||
if ($this->db->tableExists($this->table)) return;
|
||||
$forge = \Config\Database::forge();
|
||||
$fields = array(
|
||||
'id' => array( 'type' => 'serial'),
|
||||
'parent_id' => array( 'type' => 'integer','default'=>'0'),
|
||||
'bookable' => array( 'type' => 'boolean' ),
|
||||
'description' => array( 'type' => 'character varying','constraint' => '80'),
|
||||
'monitor' => array( 'type' => 'boolean','default'=>'false' ),
|
||||
'account' => array( 'type' => 'boolean','default'=>'false' ),
|
||||
'deleted_at'=> array( 'type' => 'timestamp without time zone','null'=>true),
|
||||
);
|
||||
$forge->addField( $fields );
|
||||
$forge->addPrimaryKey('id');
|
||||
|
||||
// $this->db->query("CREATE TABLE $this->table (LIKE $this->defaultTableName INCLUDING DEFAULTS INCLUDING CONSTRAINTS) PARTITION BY RANGE (datum);");
|
||||
$forge->createTable($this->table,true);
|
||||
// CREATE TABLE temperatures_y2025q1 PARTITION OF temperatures
|
||||
// FOR VALUES FROM ('2025-01-01') TO ('2025-04-01');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user