[DX Atlas Gridsquare Export] Got file export working.

This commit is contained in:
Andreas 2021-09-04 20:26:51 +02:00
parent d397e35d82
commit 84fbcaffb4
2 changed files with 205 additions and 52 deletions

View File

@ -11,9 +11,9 @@ class Dxatlas extends CI_Controller {
if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
$data['worked_bands'] = $this->dxcc->get_worked_bands(); // Used in the view for band select
$data['modes'] = $this->modes->active(); // Used in the view for mode select
$data['dxcc'] = $this->logbook_model->fetchDxcc(); // Used in the view for dxcc select
$data['worked_bands'] = $this->dxcc->get_worked_bands(); // Used in the view for band select
$data['modes'] = $this->modes->active(); // Used in the view for mode select
$data['dxcc'] = $this->logbook_model->fetchDxcc(); // Used in the view for dxcc select
$data['page_title'] = "DX Atlas Gridsquare Export";
@ -25,11 +25,6 @@ class Dxatlas extends CI_Controller {
public function export()
{
// Load Librarys
$this->load->library('qra');
$this->load->helper('file');
// Load Database connections
$this->load->model('dxatlas_model');
// Parameters
@ -42,63 +37,84 @@ class Dxatlas extends CI_Controller {
$todate = $this->input->post('todate');
// Get QSOs with Valid QRAs
$qsos = $this->dxatlas_model->get_gridsquares($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate);
$grids = $this->dxatlas_model->get_gridsquares($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate);
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$output .= "<kml xmlns=\"http://www.opengis.net/kml/2.2\">";
$this->generateFiles($grids['worked'], $grids['confirmed'], $band);
}
$output .= "<Document>";
function generateFiles($wkdArray, $cfmArray, $band) {
foreach ($qsos->result() as $row)
{
$output .= "<Placemark>";
$gridCfmArray = [];
$gridWkdArray = [];
$fieldCfmArray = [];
$fieldWkdArray = [];
if($row->COL_GRIDSQUARE != null) {
$stn_loc = $this->qra->qra2latlong($row->COL_GRIDSQUARE);
foreach ($cfmArray as $grid) {
$field = substr($grid, 0, 2);
if (!in_array($field, $fieldCfmArray)) {
$fieldCfmArray[] = $field;
}
$gridCfmArray[] = $grid;
}
$lat = $stn_loc[0];
$lng = $stn_loc[1];
} else {
$query = $this->db->query('
SELECT *
FROM dxcc_entities
WHERE prefix = SUBSTRING( \''.$row->COL_CALL.'\', 1, LENGTH( prefix ) )
ORDER BY LENGTH( prefix ) DESC
LIMIT 1
');
foreach ($query->result() as $dxcc) {
$lat = $dxcc->lat;
$lng = $dxcc->long;
foreach ($wkdArray as $grid) {
$field = substr($grid, 0, 2);
if (!in_array($field, $fieldCfmArray)) {
if (!in_array($field, $fieldWkdArray)) {
$fieldWkdArray[] = $field;
}
}
$timestamp = strtotime($row->COL_TIME_ON);
$output .= "<name>".$row->COL_CALL."</name>";
$output .= "<description><![CDATA[<p>Date/Time: ".date('Y-m-d H:i:s', ($timestamp))."<br/>Band: ".$row->COL_BAND."<br /></p>]]></description>";
$output .= "<Point>";
$output .= "<coordinates>".$lng.",".$lat.",0</coordinates>";
$output .= "</Point>";
$output .= "</Placemark>";
if (!in_array($grid, $gridCfmArray)) {
$gridWkdArray[] = $grid;
}
}
$output .= "</Document>";
$output .= "</kml>";
$gridWkdString = '';
$gridCfmString = '';
if (!file_exists('kml')) {
mkdir('kml', 0755, true);
asort($gridWkdArray);
asort($gridCfmArray);
asort($fieldWkdArray);
asort($fieldCfmArray);
foreach ($fieldWkdArray as $fields) {
$gridWkdString .= $fields . "\r\n";
}
if ( ! write_file('kml/qsos.kml', $output))
{
echo 'Unable to write the file. Make sure the folder KML has write permissions.';
}
else
{
header("Content-Disposition: attachment; filename=\"qsos.kml\"");
echo $output;
foreach ($gridWkdArray as $grids) {
$gridWkdString .= $grids . "\r\n";
}
foreach ($fieldCfmArray as $fields) {
$gridCfmString .= $fields . "\r\n";
}
foreach ($gridCfmArray as $grids) {
$gridCfmString .= $grids . "\r\n";
}
$this->makeZip($gridWkdString, $gridCfmString, $band);
}
function makeZip($gridWkdString, $gridCfmString, $band) {
$zipFileName = $this->session->userdata('user_callsign') . '_'. $band . '.zip';
// Prepare File
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
// Stuff with content
$zip->addFromString($band . '_grids.wkd', $gridWkdString);
$zip->addFromString($band . '_grids.cfm', $gridCfmString);
// Close and send to users
$zip->close();
$length = filesize($file);
header('Content-Type: application/zip');
header('Content-Length: ' . $length);
header('Content-Disposition: attachment; filename="' . $zipFileName . '"');
readfile($file);
unlink($file);
}
}

View File

@ -35,7 +35,7 @@ class Dxatlas_model extends CI_Model
/*
* Fetches worked and confirmed gridsquare on each band and total
*/
function get_gridsquares($data) {
function get_gridsquares($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate) {
$gridArray = $this->fetchGrids($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate);
if (isset($gridArray)) {
@ -45,5 +45,142 @@ class Dxatlas_model extends CI_Model
}
}
/*
* Builds the array to display worked/confirmed vucc on awward page
*/
function fetchGrids($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate) {
// Getting all the worked grids
$col_gridsquare_worked = $this->get_grids($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, 'none');
$workedGridArray = array();
foreach ($col_gridsquare_worked as $workedgrid) {
array_push($workedGridArray, $workedgrid['gridsquare']);
}
$col_vucc_grids_worked = $this->get_grids_col_vucc($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, 'none');
foreach ($col_vucc_grids_worked as $gridSplit) {
$grids = explode(",", $gridSplit['col_vucc_grids']);
foreach($grids as $key) {
$grid_four = strtoupper(substr(trim($key),0,4));
if(!in_array($grid_four, $workedGridArray)){
array_push($workedGridArray, $grid_four);
}
}
}
// Getting all the confirmed grids
$col_gridsquare_confirmed = $this->get_grids($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, 'both');
$confirmedGridArray = array();
foreach ($col_gridsquare_confirmed as $confirmedgrid) {
array_push($confirmedGridArray, $confirmedgrid['gridsquare']);
if(in_array($confirmedgrid['gridsquare'], $workedGridArray)){
$index = array_search($confirmedgrid['gridsquare'],$workedGridArray);
unset($workedGridArray[$index]);
}
}
$col_vucc_grids_confirmed = $this->get_grids_col_vucc($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, 'both');
foreach ($col_vucc_grids_confirmed as $gridSplit) {
$grids = explode(",", $gridSplit['col_vucc_grids']);
foreach($grids as $key) {
$grid_four = strtoupper(substr(trim($key),0,4));
if(!in_array($grid_four, $confirmedGridArray)){
array_push($confirmedGridArray, $grid_four);
}
if(in_array($grid_four, $workedGridArray)){
$index = array_search($grid_four,$workedGridArray);
unset($workedGridArray[$index]);
}
}
}
$vuccArray['worked'] = $workedGridArray;
$vuccArray['confirmed'] = $confirmedGridArray;
return $vuccArray;
}
/*
* Gets the grid from col_vucc_grids
* $band = the band chosen
* $confirmationMethod - qsl, lotw or both, use anything else to skip confirmed
*/
function get_grids_col_vucc($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, $confirmationMethod) {
$station_id = $this->get_station_id();
$sql = "select col_vucc_grids
from " . $this->config->item('table_name') .
" where station_id =" . $station_id .
" and col_vucc_grids <> '' ";
if ($confirmationMethod == 'both') {
$sql .= " and (col_qsl_rcvd='Y' or col_lotw_qsl_rcvd='Y')";
}
else if ($confirmationMethod == 'qsl') {
$sql .= " and col_qsl_rcvd='Y'";
}
else if ($confirmationMethod == 'lotw') {
$sql .= " and col_lotw_qsl_rcvd='Y'";
}
if ($band != 'All') {
if ($band == 'SAT') {
$sql .= " and col_prop_mode ='" . $band . "'";
} else {
$sql .= " and col_prop_mode !='SAT'";
$sql .= " and col_band ='" . $band . "'";
}
}
$query = $this->db->query($sql);
return $query->result_array();
}
/*
* Gets the grid from col_gridsquare
* $band = the band chosen
* $confirmationMethod - qsl, lotw or both, use anything else to skip confirmed
*/
function get_grids($band, $mode, $dxcc, $cqz, $propagation, $fromdate, $todate, $confirmationMethod) {
$station_id = $this->get_station_id();
$sql = "select distinct upper(substring(col_gridsquare, 1, 4)) gridsquare
from " . $this->config->item('table_name') .
" where station_id =" . $station_id .
" and col_gridsquare <> ''";
if ($confirmationMethod == 'both') {
$sql .= " and (col_qsl_rcvd='Y' or col_lotw_qsl_rcvd='Y')";
}
else if ($confirmationMethod == 'qsl') {
$sql .= " and col_qsl_rcvd='Y'";
}
else if ($confirmationMethod == 'lotw') {
$sql .= " and col_lotw_qsl_rcvd='Y'";
}
if ($band != 'All') {
if ($band == 'SAT') {
$sql .= " and col_prop_mode ='" . $band . "'";
} else {
$sql .= " and col_prop_mode !='SAT'";
$sql .= " and col_band ='" . $band . "'";
}
}
$query = $this->db->query($sql);
return $query->result_array();
}
function get_station_id() {
$CI =& get_instance();
$CI->load->model('Stations');
return $CI->Stations->find_active();
}
}
?>