From 714833a9b8585a5252c348a3b1d56507fc78f550 Mon Sep 17 00:00:00 2001
From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com>
Date: Sun, 17 Mar 2024 21:20:24 +0100
Subject: [PATCH] Linking locations works
---
application/controllers/Stationsetup.php | 26 ++++-
application/models/Stationsetup_model.php | 103 ++++++++++++++++++
.../views/stationsetup/linkedlocations.php | 18 +--
assets/js/sections/stationsetup.js | 62 ++++++++++-
4 files changed, 191 insertions(+), 18 deletions(-)
diff --git a/application/controllers/Stationsetup.php b/application/controllers/Stationsetup.php
index 914d057ad..f4a9215f2 100644
--- a/application/controllers/Stationsetup.php
+++ b/application/controllers/Stationsetup.php
@@ -381,8 +381,32 @@ class Stationsetup extends CI_Controller {
$data['flashdata']='Not allowed';
}
echo json_encode($data);
-
}
+ public function unLinkLocations() {
+ $containerid = xss_clean($this->input->post('containerid',true));
+ $locationid = xss_clean($this->input->post('locationid',true));
+ $this->load->model('stationsetup_model');
+ $this->stationsetup_model->unLinkLocations($containerid, $locationid);
+ $data['success']=1;
+ echo json_encode($data);
+ }
+
+ public function linkLocations() {
+ $containerid = xss_clean($this->input->post('containerid',true));
+ $locationid = xss_clean($this->input->post('locationid',true));
+
+ $this->load->model('stationsetup_model');
+
+ if(!$this->stationsetup_model->relationship_exists($containerid, $locationid)) {
+ // If no link exists, create
+ $this->stationsetup_model->create_logbook_location_link($containerid, $locationid);
+ $data['success']=1;
+ } else {
+ $data['success']=0;
+ $data['flashdata']='Error';
+ }
+ echo json_encode($data);
+ }
}
diff --git a/application/models/Stationsetup_model.php b/application/models/Stationsetup_model.php
index b9a42dec5..b778b5170 100644
--- a/application/models/Stationsetup_model.php
+++ b/application/models/Stationsetup_model.php
@@ -44,6 +44,109 @@ class Stationsetup_model extends CI_Model {
$this->db->where('logbook_id', $id);
$this->db->update('station_logbooks', $data);
}
+
+ function unLinkLocations($logbook_id, $station_id) {
+
+ // be sure that logbook belongs to user
+ if (!$this->check_logbook_is_accessible($logbook_id)) {
+ return;
+ }
+
+ // be sure that station belongs to user
+ $this->load->model('Stations');
+ if (!$this->Stations->check_station_is_accessible($station_id)) {
+ return;
+ }
+
+ // Delete relationship
+ $this->db->where('station_logbook_id', $logbook_id);
+ $this->db->where('station_location_id', $station_id);
+ $this->db->delete('station_logbooks_relationship');
+ }
+
+ public function check_logbook_is_accessible($id) {
+ // check if logbook belongs to user
+ $this->db->select('logbook_id');
+ $this->db->where('user_id', $this->session->userdata('user_id'));
+ $this->db->where('logbook_id', $id);
+ $query = $this->db->get('station_logbooks');
+ if ($query->num_rows() == 1) {
+ return true;
+ }
+ return false;
+ }
+
+ // Creates relationship between a logbook and a station location
+ function create_logbook_location_link($logbook_id, $location_id) {
+ // be sure that logbook belongs to user
+ if (!$this->check_logbook_is_accessible($logbook_id)) {
+ return;
+ }
+
+ // be sure that station belongs to user
+ $this->load->model('Stations');
+ if (!$this->Stations->check_station_is_accessible($location_id)) {
+ return;
+ }
+
+ // Create data array with field values
+ $data = array(
+ 'station_logbook_id' => $logbook_id,
+ 'station_location_id' => $location_id,
+ );
+
+ // Insert Record
+ $this->db->insert('station_logbooks_relationship', $data);
+ }
+
+ function relationship_exists($logbook_id, $location_id) {
+ $this->db->where('station_logbook_id', $logbook_id);
+ $this->db->where('station_location_id', $location_id);
+ $query = $this->db->get('station_logbooks_relationship');
+
+ if ($query->num_rows() > 0){
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function public_slug_exists($slug) {
+ $this->db->where('public_slug', $this->security->xss_clean($slug));
+ $query = $this->db->get('station_logbooks');
+
+ if ($query->num_rows() > 0){
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function public_slug_exists_logbook_id($slug) {
+ $this->db->where('public_slug', $this->security->xss_clean($slug));
+ $query = $this->db->get('station_logbooks');
+
+ if ($query->num_rows() > 0){
+ foreach ($query->result() as $row) {
+ return $row->logbook_id;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ function is_public_slug_available($slug) {
+ // Clean public_slug
+ $clean_slug = $this->security->xss_clean($slug);
+ $this->db->where('public_slug', $clean_slug);
+ $query = $this->db->get('station_logbooks');
+
+ if ($query->num_rows() > 0){
+ return false;
+ } else {
+ return true;
+ }
+ }
}
?>
diff --git a/application/views/stationsetup/linkedlocations.php b/application/views/stationsetup/linkedlocations.php
index d92a7a8c0..f888cf56e 100644
--- a/application/views/stationsetup/linkedlocations.php
+++ b/application/views/stationsetup/linkedlocations.php
@@ -21,11 +21,11 @@
-
+
-
-
+
+
| station_profile_name;?> | station_callsign;?> | station_country; if ($row->end != NULL) { echo ' '.lang('gen_hamradio_deleted_dxcc').''; } ?> | -+ | - |
| - | - | - | - | |