diff --git a/application/Country/Country.php b/application/Country/Country.php new file mode 100644 index 000000000..ac79bb1f6 --- /dev/null +++ b/application/Country/Country.php @@ -0,0 +1,5 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $model ->$field = $value; + } + } + + $me = new User(); + $me->loadMe(); + + if($model->create_by === null) { + $model->create_by = $me->id; + } + if($model->edit_by === null) { + $model->edit_by = $me->id; + } + + return $model; + } + + public static function getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("Country", "*", "1 = 1 ORDER BY owner_id,`create`"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new Country($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT Country.* FROM Country + WHERE $where + LIMIT 1"; + //var_dump($sql);exit; + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new Country($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function count($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT COUT(*) as cnt FROM Country + WHERE $where + "; + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + return $data->cnt; + } + return 0; + } + + public static function searchActive($filter, $limit = false) { + //var_dump($filter);exit; + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT Country.* FROM Country + WHERE $where + ORDER BY Country.isocode"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[$data->id] = new Country($data); + } + } + + return $items; + } + + public static function search($filter, $limit = false) { + //var_dump($filter);exit; + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT Country.* FROM Country + WHERE $where + ORDER BY Country.isocode"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[$data->id] = new Country($data); + } + } + + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + $db = FronkDB::singleton(); + + if(array_key_exists("name", $filter)) { + $name = $db->escape($filter['name']); + if($name) { + $where .= " AND Country.`name` like '%$name%'"; + } + } + + if(array_key_exists("isocode", $filter)) { + $isocode = $db->escape($filter['isocode']); + if($isocode) { + $where .= " AND Country.`isocode` like '%$isocode%'"; + } + } + + if(array_key_exists("is_eu", $filter)) { + $is_eu = $filter['is_eu']; + if($is_eu) { + $where .= " AND Country.`is_eu` = 1"; + } else { + $where .= " AND Country.`is_eu` = 0"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} diff --git a/db/migrations/20240220140716_add_country_table.php b/db/migrations/20240220140716_add_country_table.php new file mode 100644 index 000000000..9e77d223b --- /dev/null +++ b/db/migrations/20240220140716_add_country_table.php @@ -0,0 +1,39 @@ +getEnvironment() == "thetool") { + $table = $this->table("Country"); + $table->addColumn("isocode", "string", ["null" => false, "limit" => 16]); + $table->addColumn("name", "string", ["null" => false, "limit" => 255]); + $table->addColumn("is_eu", "integer", ["null" => false, "limit" => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY]); + $table->addColumn("create_by", "integer", ["null" => false]); + $table->addColumn("edit_by", "integer", ["null" => false]); + $table->addColumn("create", "integer", ["null" => false]); + $table->addColumn("edit", "integer", ["null" => false]); + $table->addIndex("isocode"); + $table->addIndex("name", ["limit" => 4]); + $table->create(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table("Country")->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/scripts/import-countries.php b/scripts/import-countries.php new file mode 100755 index 000000000..1bf5f089d --- /dev/null +++ b/scripts/import-countries.php @@ -0,0 +1,52 @@ +#!/usr/bin/php +id); +define("INTERNAL_USER_USERNAME", $me->username); + +$folder = __DIR__."/import/"; +$csvname = "iso_codes.csv"; +$filename = $folder.$csvname; + +$db = FronkDB::singleton(); +$log = mfLoghandler::singleton(); + +$input = fopen($filename, "r"); + +$l = 0; +$c = 0; +while($csv = fgetcsv($input, 0, ";")) { + $l++; + if($l == 1) continue; + + if(!trim($csv[0])) { + continue; + } + + $name = trim($csv[0]); + $isocode = trim($csv[1]); + $is_eu = trim($csv[2]); + + if(CountryModel::getFirst(["name" => $name]) || CountryModel::getFirst(["isocode" => $isocode])) { + continue; + } + + $country = CountryModel::create([ + "name" => $name, + "isocode" => $isocode, + "is_eu" => ($is_eu) ? 1 : 0 + ]); + + $country->save(); +} \ No newline at end of file