'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 $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(array $data, bool $withnull = true, int $key=0) { $liste = array(); if ($withnull){ if ($key > 0) $liste[$key] = '-----'; // else // $liste[""] = '-----'; } foreach ( $data as $row ) { $liste[$row->id] = $row->description; } asort( $liste ); return $liste; } /** * holt Text vom gegebenen Element * @param int $id ID des gewünschten Elements * @return string Full Name of Entry */ public function getDropDownEntry(int $id) { $this->populateAccountList(); $element = $this->mAccountList[$id]; $description = $element->description; $id = $element->parent_id; while ( $id > 0 ) { $description = $this->mAccountList[$id]->description .'-'.$description; $id = $this->mAccountList[$id]->parent_id; } 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 $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(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 ) { $element->description = $this->mAccountList[$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 $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(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 ( $list as &$element ) { $id = $element->parent_id; 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{ $element->description = $this->mAccountList[$id]->description .'-'.$element->description; } $id = $this->mAccountList[$id]->parent_id; } } $subs = array(); $subs[0] = ["0"=>"----"]; foreach ( $listen as $key => $element ){ $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 $sid account id * @return int parent id */ public function getParent(int $sid): int{ if ($sid == 0) return 0; $this->populateAccountList(); while ( $this->mAccountList[$sid]->parent_id > 0 ) { $sid = $this->mAccountList[$sid]->parent_id; } return $sid; } /** * liefert array für jedes sub element und level * @return mixed array for each level element */ public function getTreeData(){ helper('array'); $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, 'all'=>$this->mAccountList]; } /** * erstellt oder updatet den gegeben Account * @var array formulardaten */ public function saveAccount(array $data) : bool{ $this->set('parent_id',$data['parent']); $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('deleted_at', (array_key_exists('deleted_at', $data) ? 'LOCALTIMESTAMP' : null),(array_key_exists('deleted_at', $data)? false: true)); if ($data['id'] == 0) $result = $this->insert(); else $result = $this->update($data['id']); log_message('debug',$this->db->getLastQuery()); return $result; } /** * 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'); } }; ?>