From 5566a7ebb56188075d9201ecfc2777f1c694ae09 Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Thu, 17 Oct 2024 11:28:52 +0700 Subject: [PATCH] Add Meta class for meta data attributes --- system/autoload/Meta.php | 118 +++++++++++++++++++++++++++++++++++++++ system/lan/english.json | 11 +++- 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 system/autoload/Meta.php diff --git a/system/autoload/Meta.php b/system/autoload/Meta.php new file mode 100644 index 00000000..58465298 --- /dev/null +++ b/system/autoload/Meta.php @@ -0,0 +1,118 @@ +set(1, 'point', '24'); + * it means tbl_plans with id 1 have point value 24, customer will get 24 point for loyalty if buy plan with id 1 + * You need to create the logic for that, Meta only hold the data + * + * Example to get data + * $point = Meta::for("tbl_plans")->get(1, 'point'); + * this will return the point value of plan with id 1 + * + * to get all key value + * $metas = Meta::for("tbl_plans")->getAll(1); + * + * to delete 1 data + * Meta::for("tbl_plans")->delete(1, 'point'); + * + * to delete all data + * Meta::for("tbl_plans")->deleteAll(1); + **/ + + +class Meta +{ + protected $table = ''; + + protected function __construct($table) + { + $this->table = $table; + } + + public static function for($table) + { + return new self($table); + } + + public function get($id, $key) + { + // get the Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->find_one()['value']; + } + + public function getAll($id) + { + //get all key Value + $metas = []; + $result = ORM::for_table('tbl_meta') + ->select('name') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->find_array(); + foreach ($result as $value) { + $metas[$value['name']] = $value['value']; + } + return $metas; + } + + public function set($id, $key, $value = '') + { + $meta = ORM::for_table('tbl_meta') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->find_one(); + if (!$meta) { + $meta = ORM::for_table('tbl_meta')->create(); + $meta->tbl = $this->table; + $meta->tbl_id = $id; + $meta->name = $key; + $meta->value = $value; + $meta->save(); + $result = $meta->id(); + if ($result) { + return $result; + } + } else { + $meta->value = $value; + $meta->save(); + return $meta['id']; + } + } + + public function delete($id, $key = '') + { + // get the Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->delete(); + } + + public function deleteAll($id) + { + //get all key Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->delete_many(); + } +} diff --git a/system/lan/english.json b/system/lan/english.json index 4bdcdcca..9df3b863 100644 --- a/system/lan/english.json +++ b/system/lan/english.json @@ -829,5 +829,14 @@ "Default___Remove_Customer": "Default - Remove Customer", "When_Expired__customer_will_be_move_to_selected_internet_package": "When Expired, customer will be move to selected internet package", "on_login___on_up": "on-login \/ on-up", - "on_logout___on_down": "on-logout \/ on-down" + "on_logout___on_down": "on-logout \/ on-down", + "Online_Status": "Online Status", + "Last_Seen": "Last Seen", + "Online": "Online", + "Offline": "Offline", + "Check_if_Mikrotik_Online_": "Check if Mikrotik is Online?", + "To_check_if_Mikrotik_is_Online_or_not__go_to_Settings__set_Router_Check_Enabled": "To check if Mikrotik is Online or not, go to Settings, set Router Check Enabled", + "via_SMS": "via SMS", + "Via_WhatsApp": "Via WhatsApp", + "Via_WhatsApp_and_SMS": "Via WhatsApp and SMS" } \ No newline at end of file