From 56e7827fe7ab8bc53f693d65258e404e8ba800c2 Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Tue, 7 Sep 2021 17:42:35 +0100 Subject: [PATCH] Adds code to create relationship between logbook and location --- application/controllers/Logbooks.php | 13 +++++--- application/models/Logbooks_model.php | 46 +++++++++++++++++++++++++++ application/views/logbooks/edit.php | 5 +-- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/application/controllers/Logbooks.php b/application/controllers/Logbooks.php index 78556e32..b1b68fde 100644 --- a/application/controllers/Logbooks.php +++ b/application/controllers/Logbooks.php @@ -59,6 +59,7 @@ class Logbooks extends CI_Controller { $station_logbook_id = $this->security->xss_clean($id); $station_logbook_details_query = $this->logbooks_model->logbook($station_logbook_id); + $data['station_locations_array'] = $this->logbooks_model->list_logbook_relationships($station_logbook_id); $data['station_logbook_details'] = $station_logbook_details_query->row(); $data['station_locations_list'] = $this->stations->all(); @@ -80,16 +81,18 @@ class Logbooks extends CI_Controller { $data['notice'] = "Station Logbooks ".$this->security->xss_clean($this->input->post('station_logbook_name', true))." Updated"; foreach ($this->input->post('SelectedStationLocations') as $selectedOption){ - echo $selectedOption."\n"; - // Check if theres already a link between logbook and location - - // If no link exisits create + if($this->logbooks_model->relationship_exists($this->input->post('station_logbook_id'), $selectedOption) != TRUE) { + // If no link exisits create + $this->logbooks_model->create_logbook_location_link($this->input->post('station_logbook_id'), $selectedOption); + } else { + echo "Already Linked"; + } // Delete link if removed } - //redirect('logbooks'); + redirect('logbooks'); } } diff --git a/application/models/Logbooks_model.php b/application/models/Logbooks_model.php index 9e013348..cb7aa401 100644 --- a/application/models/Logbooks_model.php +++ b/application/models/Logbooks_model.php @@ -53,5 +53,51 @@ class Logbooks_model extends CI_Model { $this->db->where('logbook_id', $clean_id); return $this->db->get('station_logbooks'); } + + + // Creates relationship between a logbook and a station location + function create_logbook_location_link($logbook_id, $location_id) { + // 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 list_logbook_relationships($logbook_id) { + + $relationships_array = array(); + + $this->db->where('station_logbook_id', $logbook_id); + $query = $this->db->get('station_logbooks_relationship'); + + if ($query->num_rows() > 0){ + foreach ($query->result() as $row) + { + array_push($relationships_array, $row->station_location_id); + } + + return $relationships_array; + } + else{ + return false; + } + } } ?> \ No newline at end of file diff --git a/application/views/logbooks/edit.php b/application/views/logbooks/edit.php index d459cb87..e8826962 100644 --- a/application/views/logbooks/edit.php +++ b/application/views/logbooks/edit.php @@ -45,7 +45,7 @@ Hold down the Ctrl (windows) or Command (Mac) button to select multiple options. @@ -55,9 +55,10 @@ + - + \ No newline at end of file