Skip to content

Commit

Permalink
Merge pull request #194 from teabreakninja/master
Browse files Browse the repository at this point in the history
DXCC match on date as well as callsign. Table schema updates
  • Loading branch information
magicbug committed Feb 25, 2016
2 parents 1678d8c + 3459a81 commit bf6ae3c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion application/config/migration.php
Expand Up @@ -21,7 +21,7 @@
| be upgraded / downgraded to.
|
*/
$config['migration_version'] = 10;
$config['migration_version'] = 12;


/*
Expand Down
17 changes: 14 additions & 3 deletions application/controllers/update.php
Expand Up @@ -30,8 +30,10 @@ public function dxcc_entities() {

$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) {
Expand All @@ -48,6 +50,7 @@ public function dxcc_entities() {
'cont' => (string) $entity->cont,
'long' => (float) $entity->long,
'lat' => (float) $entity->lat,
'start' => $start_date,
'end' => $end_date,
);
}
Expand Down Expand Up @@ -113,6 +116,12 @@ public function dxcc_prefixes() {

$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,
Expand All @@ -122,6 +131,8 @@ public function dxcc_prefixes() {
'cont' => (string) $record->cont,
'long' => (float) $record->long,
'lat' => (float) $record->lat,
'start' => $start_date,
'end' => $end_date,
);

$this->db->insert('dxcc_prefixes', $data);
Expand Down Expand Up @@ -206,9 +217,9 @@ private function fix_migrations(){
}
}

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);

}

Expand Down
23 changes: 23 additions & 0 deletions application/migrations/011_add_dxcc_prefixes_start_end.php
@@ -0,0 +1,23 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_add_dxcc_prefixes_start_end extends CI_Migration {

public function up(){
$fields = (array(
'start' => 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');
}
}
19 changes: 19 additions & 0 deletions application/migrations/012_add_dxcc_entities_start.php
@@ -0,0 +1,19 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_add_dxcc_entities_start extends CI_Migration {

public function up(){
$fields = (array(
'start' => array(
'type' => 'date',
'Null' => TRUE
),
));

$this->dbforge->add_column('dxcc_entities', $fields);
}

public function down(){
$this->dbforge->drop_table('dxcc_entities');
}
}
34 changes: 25 additions & 9 deletions application/models/logbook_model.php
Expand Up @@ -967,15 +967,22 @@ function import($record) {
}


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();
Expand All @@ -986,24 +993,33 @@ private function check_dxcc_table($call){
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;
$this->db->trans_start();
//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<br/>", $row['COL_PRIMARY_KEY'], $d[1], $d[0]);
printf("Updating %s to %s and %s\n<br/>", $row['COL_PRIMARY_KEY'], ucwords(strtolower($d[1])), $d[0]);
$count++;
}
}
Expand Down
1 change: 1 addition & 0 deletions application/views/update/index.php
Expand Up @@ -9,6 +9,7 @@
</div>
<br/>
<a href="update/check_missing_dxcc">Check missing DXCC/Countries values</a>
<a href="update/check_missing_dxcc/all">[Re-Check ALL]</a>
</div>

<style>
Expand Down

0 comments on commit bf6ae3c

Please sign in to comment.