Merge branch 'fronkdev' into 'master'

Added Country table and import script

See merge request fronk/thetool!254
This commit is contained in:
Frank Schubert
2024-02-20 14:42:38 +00:00
4 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
<?php
class Country extends mfBaseModel {
}

View File

@@ -0,0 +1,181 @@
<?php
class CountryModel {
public $isocode;
public $name;
public $is_eu;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Country();
foreach($data as $field => $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;
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddCountryTable extends AbstractMigration
{
public function up(): void
{
if($this->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") {
}
}
}

52
scripts/import-countries.php Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->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();
}