From 3459a81098c49f0288a79a0cd55e97088a472652 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 23 Feb 2016 22:00:35 +0000 Subject: [PATCH] DXCC match on date as well as callsign. Table schema updates --- application/config/migration.php | 2 +- application/controllers/update.php | 17 ++++++++-- .../011_add_dxcc_prefixes_start_end.php | 23 +++++++++++++ .../012_add_dxcc_entities_start.php | 19 +++++++++++ application/models/logbook_model.php | 34 ++++++++++++++----- application/views/update/index.php | 1 + 6 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 application/migrations/011_add_dxcc_prefixes_start_end.php create mode 100644 application/migrations/012_add_dxcc_entities_start.php diff --git a/application/config/migration.php b/application/config/migration.php index aabac391..947334c3 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 10; +$config['migration_version'] = 12; /* diff --git a/application/controllers/update.php b/application/controllers/update.php index 17c5d53d..5896c2e7 100644 --- a/application/controllers/update.php +++ b/application/controllers/update.php @@ -30,8 +30,10 @@ class Update extends CI_Controller { $count = 0; foreach ($xml_data->entities->entity as $entity) { - $endinfo = strtotime($entity->end); + $startinfo = strtotime($record->start); + $endinfo = strtotime($record->end); + $start_date = ($startinfo) ? date('Y-m-d H:i:s',$startinfo) : ""; $end_date = ($endinfo) ? date('Y-m-d H:i:s',$endinfo) : ""; if(!$entity->cqz) { @@ -48,6 +50,7 @@ class Update extends CI_Controller { 'cont' => (string) $entity->cont, 'long' => (float) $entity->long, 'lat' => (float) $entity->lat, + 'start' => $start_date, 'end' => $end_date, ); } @@ -113,6 +116,12 @@ class Update extends CI_Controller { $count = 0; foreach ($xml_data->prefixes->prefix as $record) { + $startinfo = strtotime($record->start); + $endinfo = strtotime($record->end); + + $start_date = ($startinfo) ? date('Y-m-d H:i:s',$startinfo) : ""; + $end_date = ($endinfo) ? date('Y-m-d H:i:s',$endinfo) : ""; + $data = array( 'record' => (int) $record->attributes()->record, 'call' => (string) $record->call, @@ -122,6 +131,8 @@ class Update extends CI_Controller { 'cont' => (string) $record->cont, 'long' => (float) $record->long, 'lat' => (float) $record->lat, + 'start' => $start_date, + 'end' => $end_date, ); $this->db->insert('dxcc_prefixes', $data); @@ -206,9 +217,9 @@ class Update extends CI_Controller { } } - public function check_missing_dxcc(){ + public function check_missing_dxcc($all = false){ $this->load->model('logbook_model'); - $this->logbook_model->check_missing_dxcc_id(); + $this->logbook_model->check_missing_dxcc_id($all); } diff --git a/application/migrations/011_add_dxcc_prefixes_start_end.php b/application/migrations/011_add_dxcc_prefixes_start_end.php new file mode 100644 index 00000000..db01bd41 --- /dev/null +++ b/application/migrations/011_add_dxcc_prefixes_start_end.php @@ -0,0 +1,23 @@ + array( + 'type' => 'date', + 'Null' => TRUE + ), + 'end' => array( + 'type' => 'date', + 'Null' => TRUE + ), + )); + + $this->dbforge->add_column('dxcc_prefixes', $fields); + } + + public function down(){ + $this->dbforge->drop_table('dxcc_prefixes'); + } +} diff --git a/application/migrations/012_add_dxcc_entities_start.php b/application/migrations/012_add_dxcc_entities_start.php new file mode 100644 index 00000000..f73b2a57 --- /dev/null +++ b/application/migrations/012_add_dxcc_entities_start.php @@ -0,0 +1,19 @@ + array( + 'type' => 'date', + 'Null' => TRUE + ), + )); + + $this->dbforge->add_column('dxcc_entities', $fields); + } + + public function down(){ + $this->dbforge->drop_table('dxcc_entities'); + } +} diff --git a/application/models/logbook_model.php b/application/models/logbook_model.php index 8a1ea66a..3a628f35 100644 --- a/application/models/logbook_model.php +++ b/application/models/logbook_model.php @@ -941,15 +941,22 @@ class Logbook_model extends CI_Model { } - private function check_dxcc_table($call){ - global $con; + private function check_dxcc_table($call, $date){ $len = strlen($call); // query the table, removing a character from the right until a match for ($i = $len; $i > 0; $i--){ //printf("searching for %s\n", substr($call, 0, $i)); - $dxcc_result = $this->db->query("select `call`, `entity`, `adif` from dxcc_prefixes where `call` = '".substr($call, 0, $i) ."'"); + $dxcc_result = $this->db->select('`call`, `entity`, `adif`') + ->where('call', substr($call, 0, $i)) + ->where('(start <= ', $date) + ->or_where("start = '0000-00-00')", NULL, false) + ->where('(end >= ', $date) + ->or_where("end = '0000-00-00')", NULL, false) + ->get('dxcc_prefixes'); + //$dxcc_result = $this->db->query("select `call`, `entity`, `adif` from dxcc_prefixes where `call` = '".substr($call, 0, $i) ."'"); + //print $this->db->last_query(); if ($dxcc_result->num_rows() > 0){ $row = $dxcc_result->row_array(); @@ -960,10 +967,15 @@ class Logbook_model extends CI_Model { return array("Not Found", "Not Found"); } - public function check_missing_dxcc_id(){ + public function check_missing_dxcc_id($all){ // get all records with no COL_DXCC - $this->db->select("COL_PRIMARY_KEY, COL_CALL"); - $this->db->where("COL_DXCC is NULL"); + $this->db->select("COL_PRIMARY_KEY, COL_CALL, COL_TIME_ON, COL_TIME_OFF"); + + // check which to update - records with no dxcc or all records + if (! isset($all)){ + $this->db->where("COL_DXCC is NULL"); + } + $r = $this->db->get($this->config->item('table_name')); $count = 0; @@ -971,13 +983,17 @@ class Logbook_model extends CI_Model { //query dxcc_prefixes if ($r->num_rows() > 0){ foreach($r->result_array() as $row){ - $d = $this->check_dxcc_table($row['COL_CALL']); + $qso_date = $row['COL_TIME_OFF']=='' ? $row['COL_TIME_ON'] : $row['COL_TIME_ON']; + $qso_date = strftime("%Y-%m-%d", strtotime($qso_date)); + + $d = $this->check_dxcc_table($row['COL_CALL'], $qso_date); + if ($d[0] != 'Not Found'){ $sql = sprintf("update %s set COL_COUNTRY = '%s', COL_DXCC='%s' where COL_PRIMARY_KEY=%d", - $this->config->item('table_name'), addslashes($d[1]), $d[0], $row['COL_PRIMARY_KEY']); + $this->config->item('table_name'), addslashes(ucwords(strtolower($d[1]))), $d[0], $row['COL_PRIMARY_KEY']); $this->db->query($sql); //print($sql."\n"); - printf("Updating %s to %s and %s\n
", $row['COL_PRIMARY_KEY'], $d[1], $d[0]); + printf("Updating %s to %s and %s\n
", $row['COL_PRIMARY_KEY'], ucwords(strtolower($d[1])), $d[0]); $count++; } } diff --git a/application/views/update/index.php b/application/views/update/index.php index d1c06992..f8a64a7f 100644 --- a/application/views/update/index.php +++ b/application/views/update/index.php @@ -9,6 +9,7 @@
Check missing DXCC/Countries values + [Re-Check ALL]