Linking locations works

This commit is contained in:
Andreas Kristiansen
2024-03-17 21:20:24 +01:00
parent 521933c5fe
commit 714833a9b8
4 changed files with 191 additions and 18 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}
}
?>

View File

@@ -21,11 +21,11 @@
</select>
</div>
<input type="hidden" class="form-control" name="station_logbook_id" value="<?php echo $station_logbook_details->logbook_id; ?>" required>
<input type="hidden" class="form-control" id="station_logbook_id" name="station_logbook_id" value="<?php echo $station_logbook_details->logbook_id; ?>" required>
<button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-link"></i> <?php echo lang('station_logbooks_link_loc'); ?></button>
</form>
<br />
<button class="btn btn-sm btn-primary" onclick="linkLocations();"><i class="fas fa-link"></i> <?php echo lang('station_logbooks_link_loc'); ?></button>
<br /><br />
<table id="station_logbooks_linked_table" class="table table-hover table-sm table-striped">
<thead class="thead-light">
@@ -45,18 +45,10 @@
<td style="text-align: center; vertical-align: middle;"><?php echo $row->station_profile_name;?></td>
<td style="text-align: center; vertical-align: middle;"><?php echo $row->station_callsign;?></td>
<td style="text-align: center; vertical-align: middle;"><?php echo $row->station_country; if ($row->end != NULL) { echo ' <span class="badge text-bg-danger">'.lang('gen_hamradio_deleted_dxcc').'</span>'; } ?></td>
<td style="text-align: center; vertical-align: middle;"><a href="<?php echo site_url('logbooks/delete_relationship/'); ?><?php echo $station_logbook_details->logbook_id; ?>/<?php echo $row->station_id;?>" class="btn btn-sm btn-danger"><i class="fas fa-unlink"></i></a></td>
<td style="text-align: center; vertical-align: middle;"><button class="btn btn-sm btn-danger" onclick="unLinkLocations('<?php echo $station_logbook_details->logbook_id; ?>','<?php echo $row->station_id;?>')"><i class="fas fa-unlink"></i></button>
</tr>
<?php
}
} else {
?>
<tr>
<td style="text-align: center; vertical-align: middle;" colspan="2"><?php echo lang('station_logbooks_no_linked_loc'); ?></td>
<td style="text-align: center; vertical-align: middle;"></td>
<td style="text-align: center; vertical-align: middle;"></td>
<td style="text-align: center; vertical-align: middle;"></td>
</tr>
<?php } ?>
} ?>
</tbody>
</table>

View File

@@ -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($('<tr>')
.append($('<td style="text-align: center; vertical-align: middle;">').append(locationarray[0]))
.append($('<td style="text-align: center; vertical-align: middle;">').append(locationarray[2]))
.append($('<td style="text-align: center; vertical-align: middle;">').append(locationarray[4].slice(0, -1)))
.append($('<td style="text-align: center; vertical-align: middle;">').append('<button class="btn btn-sm btn-danger" onclick="unLinkLocations('+containerid+','+locationid+');"><i class="fas fa-unlink"></i></button>'))
)
} 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");
}
});
}