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 @@ - + - -
+ +

@@ -45,18 +45,10 @@ - + - - - - - - - + } ?>
station_profile_name;?> station_callsign;?> station_country; if ($row->end != NULL) { echo ' '.lang('gen_hamradio_deleted_dxcc').''; } ?>
diff --git a/assets/js/sections/stationsetup.js b/assets/js/sections/stationsetup.js index a6cd09348..8c0a8c0d7 100644 --- a/assets/js/sections/stationsetup.js +++ b/assets/js/sections/stationsetup.js @@ -209,10 +209,6 @@ $(document).ready(function () { onshown: function(dialog) { }, buttons: [{ - label: 'Save', - cssClass: 'btn-primary btn-sm', - }, - { label: lang_admin_close, cssClass: 'btn-sm', id: 'closeButton', @@ -505,3 +501,61 @@ function loadLocationTable(rows) { $('[data-bs-toggle="tooltip"]').tooltip(); } +function linkLocations() { + var locationid = $('#StationLocationSelect').val(); + var containerid = $('#station_logbook_id').val(); + var locationtext = $('#StationLocationSelect option:selected').text(); + var locationarray = locationtext.split(" "); + + if (locationid == null) return; + + $.ajax({ + url: base_url + 'index.php/stationsetup/linkLocations', + type: 'post', + data: { + containerid: containerid, + locationid: locationid + }, + success: function(data) { + jdata=JSON.parse(data); + if (jdata.success == 1) { + $("#StationLocationSelect").find('[value="'+ locationid +'"]').remove(); + // add to table + $('#station_logbooks_linked_table').append($('') + .append($('').append(locationarray[0])) + .append($('').append(locationarray[2])) + .append($('').append(locationarray[4].slice(0, -1))) + .append($('').append('')) + ) + } else { + $("#flashdata").html(jdata.flashdata); + } + }, + error: function(e) { + $("#flashdata").html("An unknown Error occured"); + } + }); +} + +function unLinkLocations(containerid, locationid) { + $.ajax({ + url: base_url + 'index.php/stationsetup/unLinkLocations', + type: 'post', + data: { + containerid: containerid, + locationid: locationid + }, + success: function (data) { + jdata=JSON.parse(data); + if (jdata.success == 1) { + + } else { + $("#flashdata").data(jdata.flashdata); + } + }, + error: function(e) { + $("#flashdata").html("An unknown Error occured"); + } + }); +} +