From 678a33d4521c6d7f737a6f77e71c9d495c5870ac Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Sat, 6 May 2023 08:17:23 +0200
Subject: [PATCH 1/7] [eQSL] Made CRON job for download, renamed to sync
---
 application/controllers/Eqsl.php         | 40 +++++++++++++++++++++---
 application/models/Eqslmethods_model.php | 17 ++++++++++
 application/models/Stations.php          | 12 -------
 3 files changed, 52 insertions(+), 17 deletions(-)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index e7a1a03b..e56599cf 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -13,8 +13,8 @@ class eqsl extends CI_Controller {
 		if(!$this->user_model->authorize(2)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
 
 		// Check if eQSL Nicknames have been defined
-		$this->load->model('stations');
-		$eqsl_locations = $this->stations->all_of_user_with_eqsl_nick_defined();
+		$this->load->model('eqslmethods_model');
+		$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined();
 		if($eqsl_locations->num_rows() == 0) {
 			show_error("eQSL Nicknames in Station Profiles aren't defined");
 			exit;
@@ -28,8 +28,6 @@ class eqsl extends CI_Controller {
 
 		$this->load->library('upload', $config);
 
-		$this->load->model('eqslmethods_model');
-
 		$eqsl_results = array();
 		if ($this->input->post('eqslimport') == 'fetch')
 		{
@@ -564,9 +562,41 @@ class eqsl extends CI_Controller {
 		}
 	}
 
-	function uploadUser($userid, $username, $password) {
+	/*
+	 * Used for CRON job
+	 */
+	public function sync() {
 		ini_set('memory_limit', '-1');
 		set_time_limit(0);
+		$this->load->model('eqslmethods_model');
+
+		$users = $this->eqslmethods_model->get_eqsl_users();
+
+		foreach ($users as $user) {
+			$this->uploadUser($user->user_id, $user->user_eqsl_name, $user->user_eqsl_password);
+			$this->downloadUser($user->user_id, $user->user_eqsl_name, $user->user_eqsl_password);
+		}
+	}
+
+	public function downloadUser($userid, $username, $password) {
+		$this->load->library('EqslImporter');
+		$this->load->model('eqslmethods_model');
+
+		$config['upload_path'] = './uploads/';
+		$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined();
+
+		foreach ($eqsl_locations->result_array() as $eqsl_location) {
+			$this->eqslimporter->from_callsign_and_QTH(
+				$eqsl_location['station_callsign'],
+				$eqsl_location['eqslqthnickname'],
+				$config['upload_path']
+			);
+
+			$eqsl_results[] = $this->eqslimporter->fetch($eqsl_password);
+		}
+	}
+
+	function uploadUser($userid, $username, $password) {
 		$data['user_eqsl_name'] = $this->security->xss_clean($username);
 		$data['user_eqsl_password'] = $this->security->xss_clean($password);
 		$clean_userid = $this->security->xss_clean($userid);
diff --git a/application/models/Eqslmethods_model.php b/application/models/Eqslmethods_model.php
index ebb82432..2a66562b 100644
--- a/application/models/Eqslmethods_model.php
+++ b/application/models/Eqslmethods_model.php
@@ -81,6 +81,23 @@ class Eqslmethods_model extends CI_Model {
         return "eQSL Sent";
     }
 
+    // Returns all the distinct callsign, eqsl nick pair for the current user/supplied user
+	function all_of_user_with_eqsl_nick_defined($userid = null) {
+        if ($userid == null) {
+            $this->db->where('user_id', $this->session->userdata('user_id'));
+        } else {
+            $this->db->where('user_id', $userid);
+        }
+
+		$this->db->where('eqslqthnickname IS NOT NULL');
+		$this->db->where('eqslqthnickname !=', '');
+		$this->db->from('station_profile');
+		$this->db->select('station_callsign, eqslqthnickname');
+		$this->db->distinct(TRUE);
+
+		return $this->db->get();
+	}
+
 }
 
 ?>
\ No newline at end of file
diff --git a/application/models/Stations.php b/application/models/Stations.php
index c8251dbc..364a6419 100644
--- a/application/models/Stations.php
+++ b/application/models/Stations.php
@@ -397,18 +397,6 @@ class Stations extends CI_Model {
 		return $query->num_rows();
     }
 
-	// Returns all the distinct callsing, eqsl nick pair for the current user
-	function all_of_user_with_eqsl_nick_defined() {
-		$this->db->where('user_id', $this->session->userdata('user_id'));
-		$this->db->where('eqslqthnickname IS NOT NULL');
-		$this->db->where('eqslqthnickname !=', '');
-		$this->db->from('station_profile');
-		$this->db->select('station_callsign, eqslqthnickname');
-		$this->db->distinct(TRUE);
-
-		return $this->db->get();
-	}
-
 	public function check_station_is_accessible($id) {
 		// check if station belongs to user
 		$this->db->select('station_id');
From 86312d16aabf2ec145ff1819060e4b4f9c054169 Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Sat, 6 May 2023 11:35:09 +0200
Subject: [PATCH 2/7] [eQSL] Refactored and moved stuff from logbook_model to
 eqslmethods_model
---
 application/libraries/EqslImporter.php   |  7 ++-
 application/models/Eqslmethods_model.php | 65 ++++++++++++++++++++++
 application/models/Logbook_model.php     | 70 ------------------------
 3 files changed, 69 insertions(+), 73 deletions(-)
diff --git a/application/libraries/EqslImporter.php b/application/libraries/EqslImporter.php
index a8dd6841..77cc8701 100644
--- a/application/libraries/EqslImporter.php
+++ b/application/libraries/EqslImporter.php
@@ -18,6 +18,7 @@ class EqslImporter
 
 		$this->CI->load->model('logbook_model');
 		$this->CI->load->library('adif_parser');
+		$this->CI->load->model('eqslmethods_model');
 	}
 
 	private function init($name, $adif_file) {
@@ -62,7 +63,7 @@ class EqslImporter
 		$eqsl_url = $q->eqsl_download_url;
 
 		// Query the logbook to determine when the last eQSL confirmation was
-		$eqsl_last_qsl_date = $this->CI->logbook_model->eqsl_last_qsl_rcvd_date($this->callsign, $this->qth_nickname);
+		$eqsl_last_qsl_date = $this->CI->eqslmethods_model->eqsl_last_qsl_rcvd_date($this->callsign, $this->qth_nickname);
 
 		// Build parameters for eQSL inbox file
 		$eqsl_params = http_build_query(array(
@@ -157,10 +158,10 @@ class EqslImporter
 			$qsoid = 0;
 			if ($status[0] == "Found") {
 				$qsoid = $status[1];
-				$dupe = $this->CI->logbook_model->eqsl_dupe_check($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
+				$dupe = $this->CI->eqslmethods_model->eqsl_dupe_check($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
 				if ($dupe == false) {
 					$updated += 1;
-					$eqsl_status = $this->CI->logbook_model->eqsl_update($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
+					$eqsl_status = $this->CI->eqslmethods_model->eqsl_update($time_on, $record['call'], $record['band'], $config['eqsl_rcvd_mark']);
 				} else {
 					$dupes += 1;
 					$eqsl_status = "Already received an eQSL for this QSO.";
diff --git a/application/models/Eqslmethods_model.php b/application/models/Eqslmethods_model.php
index 2a66562b..eb7546f5 100644
--- a/application/models/Eqslmethods_model.php
+++ b/application/models/Eqslmethods_model.php
@@ -98,6 +98,71 @@ class Eqslmethods_model extends CI_Model {
 		return $this->db->get();
 	}
 
+    // Get the last date we received an eQSL
+    function eqsl_last_qsl_rcvd_date($callsign, $nickname) {
+        $qso_table_name = $this->config->item('table_name');
+        $this->db->from($qso_table_name);
+
+        $this->db->join('station_profile',
+            'station_profile.station_id = '.$qso_table_name.'.station_id AND station_profile.eqslqthnickname != ""');
+        $this->db->where('station_profile.station_callsign', $callsign);
+        $this->db->where('station_profile.eqslqthnickname', $nickname);
+
+        $this->db->select("DATE_FORMAT(COL_EQSL_QSLRDATE,'%Y%m%d') AS COL_EQSL_QSLRDATE", FALSE);
+        $this->db->where('COL_EQSL_QSLRDATE IS NOT NULL');
+        $this->db->order_by("COL_EQSL_QSLRDATE", "desc");
+        $this->db->limit(1);
+
+        $query = $this->db->get();
+        $row = $query->row();
+
+        if (isset($row->COL_EQSL_QSLRDATE)){
+            return $row->COL_EQSL_QSLRDATE;
+        } else {
+            // No previous date (first time import has run?), so choose UNIX EPOCH!
+            // Note: date is yyyy/mm/dd format
+            return '1970/01/01';
+        }
+    }
+
+    // Update a QSO with eQSL QSL info
+    // We could also probably use this use this: http://eqsl.cc/qslcard/VerifyQSO.txt
+    // http://www.eqsl.cc/qslcard/ImportADIF.txt
+    function eqsl_update($datetime, $callsign, $band, $qsl_status) {
+        $data = array(
+            'COL_EQSL_QSLRDATE' => date('Y-m-d H:i:s'), // eQSL doesn't give us a date, so let's use current
+            'COL_EQSL_QSL_RCVD' => $qsl_status
+        );
+
+        $this->db->where('COL_TIME_ON >= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL -15 MINUTE )');
+        $this->db->where('COL_TIME_ON <= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL 15 MINUTE )');
+        $this->db->where('COL_CALL', $callsign);
+        $this->db->where('COL_BAND', $band);
+
+        $this->db->update($this->config->item('table_name'), $data);
+
+        return "Updated";
+    }
+
+    // Determine if we've already received an eQSL for this QSO
+    function eqsl_dupe_check($datetime, $callsign, $band, $qsl_status) {
+        $this->db->select('COL_EQSL_QSLRDATE');
+        $this->db->where('COL_TIME_ON >= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL -15 MINUTE )');
+        $this->db->where('COL_TIME_ON <= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL 15 MINUTE )');
+        $this->db->where('COL_CALL', $callsign);
+        $this->db->where('COL_BAND', $band);
+        $this->db->where('COL_EQSL_QSL_RCVD', $qsl_status);
+        $this->db->limit(1);
+    
+        $query = $this->db->get($this->config->item('table_name'));
+        $row = $query->row();
+    
+        if ($row != null) {
+            return true;
+        } 
+        return false;
+    }
+
 }
 
 ?>
\ No newline at end of file
diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php
index b301148f..692803aa 100755
--- a/application/models/Logbook_model.php
+++ b/application/models/Logbook_model.php
@@ -2553,76 +2553,6 @@ class Logbook_model extends CI_Model {
       return '1900-01-01 00:00:00.000';
     }
 
-//////////////////////////////
-  // Update a QSO with eQSL QSL info
-  // We could also probably use this use this: http://eqsl.cc/qslcard/VerifyQSO.txt
-  // http://www.eqsl.cc/qslcard/ImportADIF.txt
-  function eqsl_update($datetime, $callsign, $band, $qsl_status) {
-    $data = array(
-         'COL_EQSL_QSLRDATE' => date('Y-m-d H:i:s'), // eQSL doesn't give us a date, so let's use current
-         'COL_EQSL_QSL_RCVD' => $qsl_status
-    );
-
-    $this->db->where('COL_TIME_ON >= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL -15 MINUTE )');
-    $this->db->where('COL_TIME_ON <= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL 15 MINUTE )');
-    $this->db->where('COL_CALL', $callsign);
-    $this->db->where('COL_BAND', $band);
-
-    $this->db->update($this->config->item('table_name'), $data);
-
-    return "Updated";
-  }
-
-  // Get the last date we received an eQSL
-  function eqsl_last_qsl_rcvd_date($callsign, $nickname) {
-	  $qso_table_name = $this->config->item('table_name');
-	  $this->db->from($qso_table_name);
-
-	  $this->db->join('station_profile',
-		  'station_profile.station_id = '.$qso_table_name.'.station_id AND station_profile.eqslqthnickname != ""');
-	  $this->db->where('station_profile.station_callsign', $callsign);
-	  $this->db->where('station_profile.eqslqthnickname', $nickname);
-
-      $this->db->select("DATE_FORMAT(COL_EQSL_QSLRDATE,'%Y%m%d') AS COL_EQSL_QSLRDATE", FALSE);
-      $this->db->where('COL_EQSL_QSLRDATE IS NOT NULL');
-      $this->db->order_by("COL_EQSL_QSLRDATE", "desc");
-      $this->db->limit(1);
-
-      $query = $this->db->get();
-      $row = $query->row();
-
-      if (isset($row->COL_EQSL_QSLRDATE)){
-          return $row->COL_EQSL_QSLRDATE;
-        }else{
-            // No previous date (first time import has run?), so choose UNIX EPOCH!
-            // Note: date is yyyy/mm/dd format
-            return '1970/01/01';
-        }
-    }
-
-    // Determine if we've already received an eQSL for this QSO
-    function eqsl_dupe_check($datetime, $callsign, $band, $qsl_status) {
-      $this->db->select('COL_EQSL_QSLRDATE');
-      $this->db->where('COL_TIME_ON >= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL -15 MINUTE )');
-    $this->db->where('COL_TIME_ON <= DATE_ADD(DATE_FORMAT("'.$datetime.'", \'%Y-%m-%d %H:%i\' ), INTERVAL 15 MINUTE )');
-      $this->db->where('COL_CALL', $callsign);
-      $this->db->where('COL_BAND', $band);
-      $this->db->where('COL_EQSL_QSL_RCVD', $qsl_status);
-      $this->db->limit(1);
-
-      $query = $this->db->get($this->config->item('table_name'));
-      $row = $query->row();
-
-      if ($row != null)
-      {
-        return true;
-      }
-      else
-      {
-        return false;
-      }
-    }
-
     /*
      * $skipDuplicate - used in ADIF import to skip duplicate checking when importing QSOs
      * $markLoTW - used in ADIF import to mark QSOs as exported to LoTW when importing QSOs
From 2b68c7820f2796b9af6f302d3553effbb54a5891 Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Sat, 6 May 2023 11:40:55 +0200
Subject: [PATCH 3/7] [eQSL] Added some result from download CRON job
---
 application/controllers/Eqsl.php | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index e56599cf..9c712cfa 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -594,6 +594,16 @@ class eqsl extends CI_Controller {
 
 			$eqsl_results[] = $this->eqslimporter->fetch($eqsl_password);
 		}
+
+		echo 'Result from eQSL download:
';
+		foreach ($eqsl_results as $result) {
+			foreach ($result as $r) {
+			echo $r->name . '
';
+			echo $r->adif_file . '
';
+			echo $r->qsos . '
';
+			echo $r->status . '
';
+		}
+
 	}
 
 	function uploadUser($userid, $username, $password) {
From be4614773ddddfccf5201d2d51a6001794a04b2f Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Sun, 7 May 2023 09:20:05 +0200
Subject: [PATCH 4/7] [eQSL] Fixed a missing bracket
---
 application/controllers/Eqsl.php | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index 9c712cfa..18a3ca34 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -598,12 +598,12 @@ class eqsl extends CI_Controller {
 		echo 'Result from eQSL download:
';
 		foreach ($eqsl_results as $result) {
 			foreach ($result as $r) {
-			echo $r->name . '
';
-			echo $r->adif_file . '
';
-			echo $r->qsos . '
';
-			echo $r->status . '
';
+				echo $r->name . '
';
+				echo $r->adif_file . '
';
+				echo $r->qsos . '
';
+				echo $r->status . '
';
+			}
 		}
-
 	}
 
 	function uploadUser($userid, $username, $password) {
From 91e199ae1f20449dee969243dc5f02cf5a57f590 Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Sun, 7 May 2023 09:31:45 +0200
Subject: [PATCH 5/7] [eQSL] Added missing userid, and corrected password
 variable
---
 application/controllers/Eqsl.php | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index 18a3ca34..879f6cec 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -583,7 +583,9 @@ class eqsl extends CI_Controller {
 		$this->load->model('eqslmethods_model');
 
 		$config['upload_path'] = './uploads/';
-		$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined();
+		$eqsl_locations = $this->eqslmethods_model->all_of_user_with_eqsl_nick_defined($userid);
+
+		$eqsl_results = array();
 
 		foreach ($eqsl_locations->result_array() as $eqsl_location) {
 			$this->eqslimporter->from_callsign_and_QTH(
@@ -592,18 +594,11 @@ class eqsl extends CI_Controller {
 				$config['upload_path']
 			);
 
-			$eqsl_results[] = $this->eqslimporter->fetch($eqsl_password);
+			$eqsl_results[] = $this->eqslimporter->fetch($password);
 		}
 
 		echo 'Result from eQSL download:
';
-		foreach ($eqsl_results as $result) {
-			foreach ($result as $r) {
-				echo $r->name . '
';
-				echo $r->adif_file . '
';
-				echo $r->qsos . '
';
-				echo $r->status . '
';
-			}
-		}
+		var_dump($eqsl_results);
 	}
 
 	function uploadUser($userid, $username, $password) {
From 97317421564d76f55cd1ac6c2022331eaa832c13 Mon Sep 17 00:00:00 2001
From: phl0 
Date: Sun, 7 May 2023 18:03:50 +0200
Subject: [PATCH 6/7] Fix eQSL image download function
---
 application/controllers/Eqsl.php | 1 +
 1 file changed, 1 insertion(+)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index 879f6cec..8d7a22ac 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -470,6 +470,7 @@ class eqsl extends CI_Controller {
 
 		if($this->Eqsl_images->get_image($id) == "No Image") {
 			$this->load->model('logbook_model');
+			$this->load->model('user_model');
 			$qso_query = $this->logbook_model->get_qso($id);
 			$qso = $qso_query->row();
 			$qso_timestamp = strtotime($qso->COL_TIME_ON);
From 10212d741f7ca2ae229b23adedec39071175d44a Mon Sep 17 00:00:00 2001
From: Andreas <6977712+AndreasK79@users.noreply.github.com>
Date: Mon, 8 May 2023 07:48:02 +0200
Subject: [PATCH 7/7] [eQSL] Removed debug output. Also removed /upload, since
 this is all done in /sync
---
 application/controllers/Eqsl.php | 16 ----------------
 1 file changed, 16 deletions(-)
diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php
index 8d7a22ac..96dda6e0 100644
--- a/application/controllers/Eqsl.php
+++ b/application/controllers/Eqsl.php
@@ -550,19 +550,6 @@ class eqsl extends CI_Controller {
 		redirect('eqsl/tools');
 	}
 
-	/*
-	 * Used for CRON job
-	 */
-	public function upload() {
-		$this->load->model('eqslmethods_model');
-
-		$users = $this->eqslmethods_model->get_eqsl_users();
-
-		foreach ($users as $user) {
-			$this->uploadUser($user->user_id, $user->user_eqsl_name, $user->user_eqsl_password);
-		}
-	}
-
 	/*
 	 * Used for CRON job
 	 */
@@ -597,9 +584,6 @@ class eqsl extends CI_Controller {
 
 			$eqsl_results[] = $this->eqslimporter->fetch($password);
 		}
-
-		echo 'Result from eQSL download:
';
-		var_dump($eqsl_results);
 	}
 
 	function uploadUser($userid, $username, $password) {