Files
wavelog/application/controllers/Labels.php
Fabian Berg c70c2ec5cd Clubstations for Wavelog (#1334)
* feat[clubstations]: New DB structure

* feat[clubstations]: Add clubstationstable in user managment

* feat[clubstations]: Show last operator

* feat[clubstations]: Better solution for last operator. tnx for the hint @int2001

* feat[clubstations]: New Club Model and Controller

* feat[clubstations]: Add "Add User" and "Edit User" functionality

* docs[clubstations]: move comment

* feat[clubstations]: Add "Delete Member" functionality

* feat[clubstations]: some enhancements and javascript

* fix[clubstations]: Wrong message class for flashmessages

* feat[clubstations]: Added Switch in the Header menu (not functional yet)

* feat[clubstations]: clubswitch modal

* fix[clubstations]: Load encryption library if not already loaded

* fix[clubstations]: Prevent direct login attempts to clubstations and enhance impersonation authorization

* fix[clubstations]: Typo

* feat[clubstations]: Only show the operator dialog if there is something fishy

* fix[user]: little UI bug

* feat[impersonate]: Add source uid to session data

* fix[impersonate]: logic adjustment

* feat[clubstations]: Add manage button in header menu for club officers

* fix[clubstations]: typo in permission level check

* fix[clubstations]: Full rights for the admin

* feat[impersonate]: Custom sessiondata

* feat[impersonate]: Implement stop impersonation feature with modal confirmation; "the way back"

* fix(modal): Fix bug where modal was hidden when mouse leaved the browser content

* docs(config): Adjust config description for special callsigns and clubstations

* feat(club): Add club access check helper

* typo

* fix[impersonation]: Better text

* feat(club): Selectize for a efficient user search

* feat(clubstations): Restrict clubstations based on users permission level part 1/x

* adjustments for dev merge

* Adjusted club right for the advanced logbook

* feat[user]: Refactoring of the Action Buttons in the user table

* fix[club_permissions]: normal button instead small one for club permissions

* remove unnecessary line break in modal body

* feat[clubstations]: Add Club Mode badge to the header

* fix[clubstations]: fix maintenance mode

* allow switch back on http

* feat(simplefle): display operator input based on club_access

* small UI adjustments

* small UI adjustments

* moved api page to a index.php file and added support for clubstations

* removed unused stuff

* typo

* radios and api keys

* missed one binding

* fix qso view, even officers do just see their own radios in QSO logging

* omit the need for a relogin to see the changes as an admin

* Omit the need for relogin after club changes in general. It's a question of UX. It's better to accept a little higher DB load (if clubstations are enabled) then the need of an user to relogin. There is some room for improvement by changing user_model->get_by_id() and adding a join there. This can be done later if we see that the load is too high

* If the user is not the creator of the API key, it's likely a clubstation. In this case the callsign of the clubstation can not be the same as the callsign of the user (operator call provided by the user). If this is the case, we need to use the callsign of the creator of the API key

* remove debug messages

* better UI in header

* found a typo

* full access in clubstations for admins (if accessed via admin usertable)

* adjusted text

* adjusted text

* adjust text

* reduce required chars

* bugfix: missing the correct authentication in case the admin was not member of the club. he wasn't able to switch back

* reduce debug messages

* fixed UI bug related to tooltips

* load js in controller

* upps..

* some UI adjustments

* corrected permissions

* if user gets delete we need to remove data in club_permissions and also api keys which were created by this user

* Notify members about new memberships or changes in permission level

* add spinner to save button

* make login/logout process more bulletproof

* remove the relogin cookie after the attempt

* better strategy

* bug where switch back failed if user is no admin

* make api keys more secure

* mask not owned api keys

* removed annoying link

* if a user gets removed from a club we also should delete the corresponding api keys and cat radios

* adjusted wiki link

* Auto creation of logbook and location when new user is created

* store and display locator in uppercase

* same for callsign

* fixed a bug in user/club creation

* Revert "Auto creation of logbook and location when new user is created"
We found another solution to which will be addressed in a second PR
This reverts commit f05f4b7bf0.

* Optimized SQL for stats at userlist

* Source query for lastop "out", because mysql<9.0 can't handle Windowed functions

* adjust migration

* add new columns to users table to get created_at and modified_at

* added a partial down function

* add operator dropdown for clubstations

* fix mig version

* Add some backend restrictions in case a user wants to try something funny with the club

---------

Co-authored-by: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com>
Co-authored-by: int2001 <joerg@dj7nt.de>
2025-01-02 10:22:23 +01:00

496 lines
18 KiB
PHP

<?php
require_once './src/Label/vendor/autoload.php';
use Wavelog\Label\PDF_Label;
use Wavelog\Label\tfpdf;
use Wavelog\Label\font\unifont\ttfonts;
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Labels extends CI_Controller {
/*
|--------------------------------------------------------------------------
| Controller: Labels
|--------------------------------------------------------------------------
|
| This Controller handles all things Labels, creating, editing and printing
|
|
*/
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'psr4_autoloader'));
$this->load->model('user_model');
if(!$this->user_model->authorize(2) || !clubaccess_check(9)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
}
/*
|--------------------------------------------------------------------------
| Function: index
|--------------------------------------------------------------------------
|
| Nothing fancy just shows the main display of how many labels are waiting
| to be printed per station profile.
|
*/
public function index() {
$data['page_title'] = __("QSL Card Labels");
$this->load->model('labels_model');
$data['labels'] = $this->labels_model->fetchLabels($this->session->userdata('user_id'));
$data['papertypes'] = $this->labels_model->fetchPapertypes($this->session->userdata('user_id'));
$data['qsos'] = $this->labels_model->fetchQsos($this->session->userdata('user_id'));
$footerData = [];
$footerData['scripts'] = [
'assets/js/sections/labels.js',
];
$this->load->view('interface_assets/header', $data);
$this->load->view('labels/index');
$this->load->view('interface_assets/footer', $footerData);
}
/*
|--------------------------------------------------------------------------
| Function: create
|--------------------------------------------------------------------------
|
| Shows the form used to create a label type.
|
*/
public function create() {
$data['page_title'] = __("Create Label Type");
$this->load->library('form_validation');
$this->load->model('labels_model');
$data['papertypes'] = $this->labels_model->fetchPapertypes($this->session->userdata('user_id'));
$this->form_validation->set_rules('label_name', __('Label Name'), 'required');
$this->form_validation->set_rules('paper_type_id', __('Paper Type'), 'required');
$this->form_validation->set_rules('measurementType', __('Measurement'), 'required');
$this->form_validation->set_rules('marginTop', __('Top Margin'), 'required');
$this->form_validation->set_rules('marginLeft', __('Left Margin'), 'required');
$this->form_validation->set_rules('NX', __('QSLs Horizontally'), 'required');
$this->form_validation->set_rules('NY', __('QSLs Vertically'), 'required');
$this->form_validation->set_rules('SpaceX', __('Horizontal Space'), 'required');
$this->form_validation->set_rules('SpaceY', __('Vertical Space'), 'required');
$this->form_validation->set_rules('width', __('Label width'), 'required');
$this->form_validation->set_rules('height', __('Label height'), 'required');
$this->form_validation->set_rules('font_size', __('Size of Font'), 'required');
$this->form_validation->set_rules('label_qsos', __('Number of QSOs on label'), 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('interface_assets/header', $data);
$this->load->view('labels/create');
$this->load->view('interface_assets/footer');
} else {
$this->load->model('labels_model');
$this->labels_model->addLabel();
redirect('labels');
}
}
/*
|--------------------------------------------------------------------------
| Function: createpaper
|--------------------------------------------------------------------------
|
| Shows the form used to create a paper type.
|
*/
public function createpaper() {
$data['page_title'] = __("Create Paper Type");
$this->load->library('form_validation');
$this->form_validation->set_rules('paper_name', __('Paper Name'), 'required');
$this->form_validation->set_rules('width', __('Paper Width'), 'required');
$this->form_validation->set_rules('height', __('Paper Height'), 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('interface_assets/header', $data);
$this->load->view('labels/createpaper');
$this->load->view('interface_assets/footer');
} else {
$this->load->model('labels_model');
try {
$this->labels_model->addPaper();
} catch (\Throwable $th) {
$this->session->set_flashdata('error', __('Your paper could not be saved. Remember that it can\'t have the same name as existing paper types.'));
redirect('labels/createpaper');
}
redirect('labels');
}
}
public function printids() {
$ids = xss_clean(json_decode($this->input->post('id')));
$offset = xss_clean($this->input->post('startat'));
$grid = $this->input->post('grid') === "true" ? 1 : 0;
$via = $this->input->post('via') === "true" ? 1 : 0;
$reference = $this->input->post('reference') == "true" ? 1 : 0;
$this->load->model('labels_model');
$result = $this->labels_model->export_printrequestedids($ids);
$this->prepareLabel($result, true, $offset, $grid, $via, $reference);
}
public function print($station_id) {
$clean_id = xss_clean($station_id);
$offset = xss_clean($this->input->post('startat'));
$grid = xss_clean($this->input->post('grid') ?? 0);
$via = xss_clean($this->input->post('via') ?? 0);
$reference = xss_clean($this->input->post('reference') ?? 0);
$this->load->model('stations');
if ($this->stations->check_station_is_accessible($station_id)) {
$this->load->model('labels_model');
$result = $this->labels_model->export_printrequested($clean_id);
$this->prepareLabel($result, false, $offset, $grid, $via, $reference);
} else {
redirect('labels');
}
}
function prepareLabel($qsos, $jscall = false, $offset = 1, $grid = false, $via = false, $reference = false) {
$this->load->model('labels_model');
$label = $this->labels_model->getDefaultLabel();
try {
if ($label) {
$label->font='DejaVuSans'; // Fix font to DejaVuSans
$ptype=$this->labels_model->getPaperType($label->paper_type_id); // fetch papersize out of paper-table
if (($ptype->paper_id ?? '') != '') {
if ($ptype->metric == 'in') { // convert papersize to mm if given in inch
$paper_width=$ptype->width*25.4;
$paper_height=$ptype->height*25.4;
} else {
$paper_width=$ptype->width;
$paper_height=$ptype->height;
}
$pdf = new PDF_Label(array(
'paper-size' => 'custom', // $label->paper_type, // The only Type left is "custom" because A4 and so on are also defined at paper_types
'metric' => $label->metric,
'marginLeft' => $label->marginleft,
'marginTop' => $label->margintop,
'NX' => $label->nx,
'NY' => $label->ny,
'SpaceX' => $label->spacex,
'SpaceY' => $label->spacey,
'width' => $label->width,
'height' => $label->height,
'font-size' => $label->font_size,
'pgX' => $paper_width,
'pgY' => $paper_height
));
} else {
if ($jscall) {
header('Content-Type: application/json');
echo json_encode(array('message' => __('You need to assign a paperType to the label before printing')));
return;
} else {
$this->session->set_flashdata('error', __('You need to assign a paperType to the label before printing'));
redirect('labels');
}
}
} else {
if ($jscall) {
header('Content-Type: application/json');
echo json_encode(array('message' => __('You need to create a label and set it to be used for print.')));
return;
} else {
$this->session->set_flashdata('error', __('You need to create a label and set it to be used for print.'));
redirect('labels');
}
}
} catch (\Throwable $th) {
if ($jscall) {
header('Content-Type: application/json');
echo json_encode(array('message' => __('Something went wrong! The label could not be generated. Check label size and font size.')));
return;
} else {
$this->session->set_flashdata('error', __('Something went wrong! The label could not be generated. Check label size and font size.'));
redirect('labels');
}
}
define('FPDF_FONTPATH', './src/Label/font/');
$pdf->AddPage($ptype->orientation);
if ($label->font == 'DejaVuSans') { // leave this here, for future Use
$pdf->AddFont($label->font,'','DejaVuSansMono.ttf',true);
$pdf->SetFont($label->font,'');
} else {
$pdf->AddFont($label->font);
$pdf->SetFont($label->font);
}
if ($qsos->num_rows() > 0) {
if ($label->qsos == 1) {
$this->makeMultiQsoLabel($qsos->result(), $pdf, 1, $offset, $ptype->orientation, $grid, $via, $reference);
} else {
$this->makeMultiQsoLabel($qsos->result(), $pdf, $label->qsos, $offset, $ptype->orientation, $grid, $via, $reference);
}
} else {
$this->session->set_flashdata('message', __('0 QSOs found for print!'));
redirect('labels');
}
$pdf->Output();
}
function makeMultiQsoLabel($qsos, $pdf, $numberofqsos, $offset, $orientation, $grid, $via, $reference) {
$text = '';
$current_callsign = '';
$current_sat = '';
$current_sat_mode = '';
$current_sat_bandrx = '';
$qso_data = [];
if ($offset !== 1) {
for ($i = 1; $i < $offset; $i++) {
$pdf->Add_Label('',$orientation);
}
}
foreach($qsos as $qso) {
if (($this->pretty_sat_mode($qso->COL_SAT_MODE) !== $current_sat_mode) || ($qso->COL_SAT_NAME !== $current_sat) || ($qso->COL_CALL !== $current_callsign) || // Call, SAT or SAT-Mode differs?
( ($qso->COL_BAND_RX !== $current_sat_bandrx) && ($this->pretty_sat_mode($qso->COL_SAT_MODE) !== '')) ) {
// ((($qso->COL_SAT_NAME ?? '' !== $current_sat) || ($qso->COL_CALL !== $current_callsign)) && ($qso->COL_SAT_NAME ?? '' !== '') && ($col->COL_BAND_RX ?? '' !== $current_sat_bandrx))) {
if (!empty($qso_data)) {
$this->finalizeData($pdf, $current_callsign, $qso_data, $numberofqsos, $orientation, $grid, $via, $reference);
$qso_data = [];
}
$current_callsign = $qso->COL_CALL;
$current_sat = $qso->COL_SAT_NAME;
$current_sat_mode = $this->pretty_sat_mode($qso->COL_SAT_MODE);
$current_sat_bandrx = $qso->COL_BAND_RX ?? '';
}
$qso_data[] = [
'time' => $qso->COL_TIME_ON,
'band' => $qso->COL_BAND,
'mode' => (($qso->COL_SUBMODE ?? '') == '') ? $qso->COL_MODE : $qso->COL_SUBMODE,
'rst' => $qso->COL_RST_SENT,
'mygrid' => $qso->station_gridsquare,
'via' => $qso->COL_QSL_VIA,
'sat' => $qso->COL_SAT_NAME,
'sat_mode' => $this->pretty_sat_mode($qso->COL_SAT_MODE ?? ''),
'sat_band_rx' => ($qso->COL_BAND_RX ?? ''),
'qsl_recvd' => $qso->COL_QSL_RCVD,
'mycall' => $qso->COL_STATION_CALLSIGN,
'sig' => $qso->station_sig ?? '',
'sig_info' => $qso->station_sig_info ?? '',
'sota' => $qso->station_sota ?? '',
'iota' => $qso->station_iota ?? '',
'pota' => $qso->station_pota ?? '',
'wwff' => $qso->station_wwff ?? ''
];
}
if (!empty($qso_data)) {
$this->finalizeData($pdf, $current_callsign, $qso_data, $numberofqsos, $orientation, $grid, $via, $reference);
}
}
// New begin
function pretty_sat_mode($sat_mode) {
return(strlen($sat_mode ?? '') == 2 ? (strtoupper($sat_mode[0]).'/'.strtoupper($sat_mode[1])) : strtoupper($sat_mode ?? ''));
}
function finalizeData($pdf, $current_callsign, &$preliminaryData, $qso_per_label,$orientation, $grid, $via, $reference) {
$tableData = [];
$count_qso = 0;
$qso=[];
foreach ($preliminaryData as $key => $row) {
$qso=$row;
$time = strtotime($qso['time']);
$myFormatForView = date("d.m.y H:i", $time);
$rowData = [
'DD.MM.YY UTC' => $myFormatForView,
'Band' => $row['band'],
'Mode' => $row['mode'],
'RST' => $row['rst'],
];
$tableData[] = $rowData;
$count_qso++;
if($count_qso == $qso_per_label){
$this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso,$orientation, $grid, $via, $reference);
$tableData = []; // reset the data
$count_qso = 0; // reset the counter
}
unset($preliminaryData[$key]);
}
// generate label for remaining QSOs
if($count_qso > 0){
$this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso,$orientation, $grid, $via, $reference);
$preliminaryData = []; // reset the data
}
}
function generateLabel($pdf, $current_callsign, $tableData,$numofqsos,$qso,$orientation,$grid=true, $via=false, $reference = false){
$builder = new \AsciiTable\Builder();
$builder->addRows($tableData);
$toradio = "To Radio: ";
$toradio .= $current_callsign;
if (($via) && ($qso['via'] ?? '' != '')) {
$toradio.=' via '.substr($qso['via'],0,8);
}
$builder->setTitle($toradio);
$additionalText = "Confirming QSO".($numofqsos>1 ? 's' : '');
$builder->setAdditionalText($additionalText);
$text = $builder->renderTable();
if($qso['sat'] != "") {
if (($qso['sat_mode'] == '') && ($qso['sat_band_rx'] !== '')) {
$text .= "\n".'Satellite: '.$qso['sat'].' Band RX: '.$qso['sat_band_rx'];
} elseif (($qso['sat_mode'] == '') && ($qso['sat_band_rx'] == '')) {
$text .= "\n".'Satellite: '.$qso['sat'];
} else {
$text .= "\n".'Satellite: '.$qso['sat'].' Mode: '.$qso['sat_mode'];
}
}
$text.="\n";
if ($grid) { $text .= "My call: ".$qso['mycall']." Grid: ".$qso['mygrid']."\n"; }
if ($reference) {
$ref_text = "";
$ref_avail = false;
if (!empty($qso['sig']) && !empty($qso['sig_info'])) { $ref_text .= $qso['sig'].":".$qso['sig_info']." "; $ref_avail = true;}
if (!empty($qso['sota'])) { $ref_text .= "SOTA:".$qso['sota']." "; $ref_avail = true;}
if (!empty($qso['iota'])) { $ref_text .= "IOTA:".$qso['iota']." "; $ref_avail = true;}
if (!empty($qso['pota'])) { $ref_text .= "POTA:".$qso['pota']." "; $ref_avail = true;}
if (!empty($qso['wwff'])) { $ref_text .= "WWFF:".$qso['wwff']; $ref_avail = true;}
if ($ref_avail == true) {$text .= $ref_text."\n";}
}
$text .= "Thanks for the QSO".($numofqsos>1 ? 's' : '');
$text .= " | ".($qso['qsl_recvd'] == 'Y' ? 'TNX' : 'PSE')." QSL";
$pdf->Add_Label($text,$orientation); }
// New End
public function edit($id) {
$this->load->model('labels_model');
$cleanid = $this->security->xss_clean($id);
$data['label'] = $this->labels_model->getLabel($cleanid,$this->session->userdata('user_id'));
$data['papertypes'] = $this->labels_model->fetchPapertypes($this->session->userdata('user_id'));
$data['page_title'] = __("Edit Label");
$this->load->view('interface_assets/header', $data);
$this->load->view('labels/edit');
$this->load->view('interface_assets/footer');
}
public function updateLabel($id) {
$this->load->model('labels_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('label_name', __('Label Name'), 'required');
$this->form_validation->set_rules('paper_type_id', __('Paper Type'), 'required');
$this->form_validation->set_rules('measurementType', __('Measurement'), 'required');
$this->form_validation->set_rules('marginTop', __('Top Margin'), 'required');
$this->form_validation->set_rules('marginLeft', __('Left Margin'), 'required');
$this->form_validation->set_rules('NX', __('QSLs Horizontally'), 'required');
$this->form_validation->set_rules('NY', __('QSLs Vertically'), 'required');
$this->form_validation->set_rules('SpaceX', __('Horizontal Space'), 'required');
$this->form_validation->set_rules('SpaceY', __('Vertical Space'), 'required');
$this->form_validation->set_rules('width', __('Label width'), 'required');
$this->form_validation->set_rules('height', __('Label height'), 'required');
$this->form_validation->set_rules('font_size', __('Size of Font'), 'required');
$this->form_validation->set_rules('label_qsos', __('Number of QSOs on label'), 'required');
if ($this->form_validation->run() == FALSE) {
$this->edit($id);
} else {
$this->labels_model->updateLabel($id);
$this->session->set_flashdata('message', __('Label was saved.'));
redirect('labels');
}
}
public function delete($id) {
$this->load->model('labels_model');
$this->labels_model->deleteLabel($id);
$this->session->set_flashdata('warning', __('Label was deleted.'));
redirect('labels');
}
public function saveDefaultLabel() {
$id = $this->input->post('id');
$this->load->model('labels_model');
$this->labels_model->saveDefaultLabel($id);
}
public function startAtLabel() {
$data['stationid'] = xss_clean($this->input->post('stationid'));
$this->load->view('labels/startatform', $data);
}
public function editPaper($id) {
$this->load->model('labels_model');
$cleanid = $this->security->xss_clean($id);
$data['paper'] = $this->labels_model->getPaper($cleanid);
$data['page_title'] = __("Edit Paper");
$this->load->view('interface_assets/header', $data);
$this->load->view('labels/editpaper');
$this->load->view('interface_assets/footer');
}
public function updatePaper($id) {
$this->load->model('labels_model');
$this->load->library('form_validation');
$this->form_validation->set_rules('paper_name', __('Paper Name'), 'required');
$this->form_validation->set_rules('width', __('Paper Width'), 'required');
$this->form_validation->set_rules('height', __('Paper Height'), 'required');
if ($this->form_validation->run() == FALSE) {
$this->editPaper($id);
} else {
try {
$this->labels_model->updatePaper($id);
} catch (\Throwable $th) {
$this->session->set_flashdata('error', __('Your paper could not be saved. Remember that it can\'t have the same name as existing paper types.'));
$cleanid = $this->security->xss_clean($id);
redirect('labels/editpaper/'.$cleanid);
}
$this->session->set_flashdata('message', __('Paper was saved.'));
redirect('labels');
}
}
function label_cnt_with_paper($paper_id) {
$this->load->model('labels_model');
return $this->labels_model->label_cnt_with_paper($paper_id);
}
public function deletePaper($id) {
$this->load->model('labels_model');
$this->labels_model->deletePaper($id);
$this->session->set_flashdata('warning', __('Paper was deleted.'));
redirect('labels');
}
}