diff --git a/application/config/migration.php b/application/config/migration.php index 6a1ed952..a700f85d 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE; | */ -$config['migration_version'] = 133; +$config['migration_version'] = 134; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Labels.php b/application/controllers/Labels.php index ef12376b..9a81d396 100644 --- a/application/controllers/Labels.php +++ b/application/controllers/Labels.php @@ -43,6 +43,8 @@ class Labels extends CI_Controller { $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 = []; @@ -69,6 +71,9 @@ class Labels extends CI_Controller { $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'); @@ -88,6 +93,43 @@ class Labels extends CI_Controller { } + /* + |-------------------------------------------------------------------------- + | 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'); + + 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')); @@ -114,24 +156,38 @@ class Labels extends CI_Controller { function prepareLabel($qsos, $jscall = false, $offset = 1) { $this->load->model('labels_model'); $label = $this->labels_model->getDefaultLabel(); - $label->font='DejaVuSans'; // Fix font to DejaVuSans try { if ($label) { - $pdf = new PDF_Label(array( - 'paper-size' => $label->paper_type, - '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 - )); + $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 ?? '') != '') { + $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' => $ptype->width, + 'pgY' => $ptype->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'); @@ -154,11 +210,11 @@ class Labels extends CI_Controller { } define('FPDF_FONTPATH', './src/Label/font/'); - $pdf->AddPage(); + $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); + $pdf->SetFont($label->font,''); } else { $pdf->AddFont($label->font); $pdf->SetFont($label->font); @@ -166,9 +222,9 @@ class Labels extends CI_Controller { if ($qsos->num_rows() > 0) { if ($label->qsos == 1) { - $this->makeMultiQsoLabel($qsos->result(), $pdf, 1, $offset); + $this->makeMultiQsoLabel($qsos->result(), $pdf, 1, $offset, $ptype->orientation); } else { - $this->makeMultiQsoLabel($qsos->result(), $pdf, $label->qsos, $offset); + $this->makeMultiQsoLabel($qsos->result(), $pdf, $label->qsos, $offset, $ptype->orientation); } } else { $this->session->set_flashdata('message', '0 QSOs found for print!'); @@ -177,7 +233,7 @@ class Labels extends CI_Controller { $pdf->Output(); } - function makeMultiQsoLabel($qsos, $pdf, $numberofqsos, $offset) { + function makeMultiQsoLabel($qsos, $pdf, $numberofqsos, $offset, $orientation) { $text = ''; $current_callsign = ''; $current_sat = ''; @@ -186,7 +242,7 @@ class Labels extends CI_Controller { $qso_data = []; if ($offset !== 1) { for ($i = 1; $i < $offset; $i++) { - $pdf->Add_Label(''); + $pdf->Add_Label('',$orientation); } } foreach($qsos as $qso) { @@ -194,7 +250,7 @@ class Labels extends CI_Controller { ( ($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); + $this->finalizeData($pdf, $current_callsign, $qso_data, $numberofqsos, $orientation); $qso_data = []; } $current_callsign = $qso->COL_CALL; @@ -216,7 +272,7 @@ class Labels extends CI_Controller { ]; } if (!empty($qso_data)) { - $this->finalizeData($pdf, $current_callsign, $qso_data, $numberofqsos); + $this->finalizeData($pdf, $current_callsign, $qso_data, $numberofqsos, $orientation); } } // New begin @@ -224,7 +280,7 @@ class Labels extends CI_Controller { 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) { + function finalizeData($pdf, $current_callsign, &$preliminaryData, $qso_per_label,$orientation) { $tableData = []; $count_qso = 0; @@ -244,7 +300,7 @@ class Labels extends CI_Controller { if($count_qso == $qso_per_label){ - $this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso); + $this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso,$orientation); $tableData = []; // reset the data $count_qso = 0; // reset the counter } @@ -252,12 +308,12 @@ class Labels extends CI_Controller { } // generate label for remaining QSOs if($count_qso > 0){ - $this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso); + $this->generateLabel($pdf, $current_callsign, $tableData,$count_qso,$qso,$orientation); $preliminaryData = []; // reset the data } } - function generateLabel($pdf, $current_callsign, $tableData,$numofqsos,$qso){ + function generateLabel($pdf, $current_callsign, $tableData,$numofqsos,$qso,$orientation){ $builder = new \AsciiTable\Builder(); $builder->addRows($tableData); $text = "Confirming QSO".($numofqsos>1 ? 's' : '')." with "; @@ -275,7 +331,7 @@ class Labels extends CI_Controller { } $text .= "\nThanks for the QSO".($numofqsos>1 ? 's' : ''); $text .= " | ".($qso['qsl_recvd'] == 'Y' ? 'TNX' : 'PSE')." QSL"; - $pdf->Add_Label($text); + $pdf->Add_Label($text,$orientation); } // New End @@ -285,7 +341,9 @@ class Labels extends CI_Controller { $cleanid = $this->security->xss_clean($id); - $data['label'] = $this->labels_model->getLabel($cleanid); + $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"; @@ -318,4 +376,43 @@ class Labels extends CI_Controller { $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'); + 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'); + } } diff --git a/application/controllers/Qslprint.php b/application/controllers/Qslprint.php index 72b08c19..4f7f0b23 100644 --- a/application/controllers/Qslprint.php +++ b/application/controllers/Qslprint.php @@ -17,7 +17,7 @@ class QSLPrint extends CI_Controller { } } - public function index() + public function index($station_id = 'All') { $this->load->model('user_model'); @@ -28,10 +28,15 @@ class QSLPrint extends CI_Controller { redirect('user/login'); } $this->load->model('stations'); + $data['station_id'] = $this->security->xss_clean($station_id); $data['station_profile'] = $this->stations->all_of_user(); $this->load->model('qslprint_model'); - $data['qsos'] = $this->qslprint_model->get_qsos_for_print(); + if ( ($station_id != 'All') && ($this->stations->check_station_is_accessible($station_id)) ) { + $data['qsos'] = $this->qslprint_model->get_qsos_for_print($station_id); + } else { + $data['qsos'] = $this->qslprint_model->get_qsos_for_print(); + } $data['page_title'] = "Print Requested QSLs"; diff --git a/application/migrations/134_create_label_paper_types_table.php b/application/migrations/134_create_label_paper_types_table.php new file mode 100644 index 00000000..6159628e --- /dev/null +++ b/application/migrations/134_create_label_paper_types_table.php @@ -0,0 +1,91 @@ +db->table_exists('paper_types')) { + $this->dbforge->add_field(array( + 'paper_id' => array( + 'type' => 'INT', + 'constraint' => 5, + 'unsigned' => TRUE, + 'auto_increment' => TRUE + ), + + 'user_id' => array( + 'type' => 'INT', + 'constraint' => 5, + ), + + 'paper_name' => array( + 'type' => 'VARCHAR', + 'constraint' => '250', + ), + + 'metric' => array( + 'type' => 'VARCHAR', + 'constraint' => '10', + ), + + 'width' => array( + 'type' => 'DECIMAL', + 'constraint' => '6,3', + 'null' => TRUE, + ), + + 'orientation' => array( + 'type' => 'VARCHAR', + 'constraint' => '1', + 'null' => TRUE, + ), + + 'height' => array( + 'type' => 'DECIMAL', + 'constraint' => '6,3', + 'null' => TRUE, + ), + + 'last_modified' => array( + 'type' => 'timestamp', + 'null' => TRUE, + ), + )); + + $this->dbforge->add_key('paper_id', TRUE); + $this->dbforge->add_key('user_id', TRUE); + + $this->dbforge->create_table('paper_types'); + $this->db->query("ALTER TABLE label_types ADD COLUMN paper_type_id INT(5) NOT NULL;"); + $this->db->query("CREATE UNIQUE INDEX idx_paper_types_user_id_paper_name ON paper_types (user_id, paper_name) ALGORITHM DEFAULT LOCK DEFAULT;"); + $this->db->query("insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('1','-1','A4','mm','210.000','P','297.000');"); + $this->db->query("insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('2','-1','A5','mm','148.000','P','210.000');"); + $this->db->query("insert into paper_types (paper_id,user_id,paper_name,metric,width,orientation,height) values ('3','-1','letter','mm','215.900','P','279.400');"); + $this->db->query("insert ignore paper_types (user_id,paper_name,metric,width,orientation,height) SELECT u.user_id, pt.paper_name, pt.metric, pt.width, pt.orientation,pt.height FROM paper_types pt inner join users u where pt.user_id = -1;"); + $this->db->query("update label_types l set l.paper_type_id=(select p.paper_id from paper_types p where upper(p.paper_name)=upper(l.paper_type) and p.user_id=l.user_id limit 1) where l.paper_type_id=0;"); + $this->db->query("update label_types l set l.paper_type_id = (select p.paper_id from paper_types p where p.user_id = l.user_id limit 1) where l.paper_type_id = 0;"); + $this->db->query("alter table label_types drop column paper_type;"); + } + + } + + public function down(){ + if ($this->db->table_exists('paper_types')) { + $this->dbforge->drop_table('paper_types'); + } + + if ($this->db->field_exists('paper_type_id', 'label_types')) { + $this->dbforge->drop_column('label_types', 'paper_type_id'); + } + + if (!$this->db->field_exists('paper_type', 'label_types')) { + $fields = array( + 'paper_type varchar(250)', + ); + + $this->dbforge->add_column('label_types', $fields); + $this->db->query("update label_types set paper_type = 'a4';"); + } + } +} diff --git a/application/models/Labels_model.php b/application/models/Labels_model.php index 41db763a..ad80dff3 100644 --- a/application/models/Labels_model.php +++ b/application/models/Labels_model.php @@ -5,7 +5,7 @@ class Labels_model extends CI_Model { $data = array( 'user_id' => $this->session->userdata('user_id'), 'label_name' => xss_clean($this->input->post('label_name', true)), - 'paper_type' => xss_clean($this->input->post('paper_type', true)), + 'paper_type_id' => xss_clean($this->input->post('paper_type_id', true)), 'metric' => xss_clean($this->input->post('measurementType', true)), 'marginleft' => xss_clean($this->input->post('marginLeft', true)), 'margintop' => xss_clean($this->input->post('marginTop', true)), @@ -25,19 +25,34 @@ class Labels_model extends CI_Model { } - function getLabel($id) { - $this->db->where('user_id', $this->session->userdata('user_id')); - $this->db->where('id', $id); - $query = $this->db->get('label_types'); - - return $query->row(); - } + function addPaper() { + $data = array( + 'user_id' => $this->session->userdata('user_id'), + 'paper_name' => xss_clean($this->input->post('paper_name', true)), + 'metric' => xss_clean($this->input->post('measurementType', true)), + 'width' => xss_clean($this->input->post('width', true)), + 'height' => xss_clean($this->input->post('height', true)), + 'orientation' => xss_clean($this->input->post('orientation', true)), + 'last_modified' => date('Y-m-d H:i:s'), + ); + + $this->db->insert('paper_types', $data); + + } + + function getLabel($id,$user_id) { + $sql="SELECT l.id, l.user_id,l.label_name, p.paper_name, p.paper_id,l.paper_type_id,l.metric, l.marginleft, l.margintop, l.nx, l.ny, l.spacex, l.spacey, l.width, l.height, l.font_size, l.font, l.qsos, l.useforprint, l.last_modified FROM cloudlog.label_types l left outer join paper_types p on (p.user_id=l.user_id and p.paper_id=l.paper_type_id) where l.user_id=? and l.id=?;"; + $query=$this->db->query($sql,array($user_id,$id)); + $result=$query->result(); + return $result[0]; + } + function updateLabel($id) { $data = array( 'user_id' => $this->session->userdata('user_id'), 'label_name' => xss_clean($this->input->post('label_name', true)), - 'paper_type' => xss_clean($this->input->post('paper_type', true)), + 'paper_type_id' => xss_clean($this->input->post('paper_type_id', true)), 'metric' => xss_clean($this->input->post('measurementType', true)), 'marginleft' => xss_clean($this->input->post('marginLeft', true)), 'margintop' => xss_clean($this->input->post('marginTop', true)), @@ -63,26 +78,31 @@ class Labels_model extends CI_Model { function deleteLabel($id) { $cleanid = xss_clean($id); - $this->db->delete('label_types', array('id' => $cleanid, 'user_id' => $this->session->userdata('user_id'))); + $this->db->delete('label_types', array('id' => $cleanid, 'user_id' => $this->session->userdata('user_id'))); } function fetchLabels($user_id) { - $this->db->where('user_id', $user_id); - $query = $this->db->get('label_types'); - + $sql="SELECT l.id, l.user_id,l.label_name, p.paper_name, l.metric, l.marginleft, l.margintop, l.nx, l.ny, l.spacex, l.spacey, l.width, l.height, l.font_size, l.font, l.qsos, l.useforprint, l.last_modified FROM cloudlog.label_types l left outer join paper_types p on (p.user_id=l.user_id and p.paper_id=l.paper_type_id) where l.user_id=?;"; + $query=$this->db->query($sql,$user_id); return $query->result(); } + function fetchPapertypes($user_id) { + $sql="SELECT p.paper_id,p.user_id,p.paper_name,p.metric,p.width,p.height,p.last_modified, p.orientation,COUNT(DISTINCT l.id) AS lbl_cnt FROM paper_types p LEFT OUTER JOIN label_types l ON (p.paper_id = l.paper_type_id and p.user_id=l.user_id) WHERE p.user_id = ? group by p.paper_id,p.user_id,p.paper_name,p.metric,p.width,p.height,p.last_modified;"; + $query = $this->db->query($sql, $this->session->userdata('user_id')); + return $query->result(); + } + function fetchQsos($user_id) { $qsl = "select count(*) count, station_profile.station_profile_name, station_profile.station_callsign, station_profile.station_id, station_profile.station_gridsquare - from ". $this->config->item('table_name') . " as l + from ". $this->config->item('table_name') . " as l join station_profile on l.station_id = station_profile.station_id where l.COL_QSL_SENT in ('R', 'Q') and station_profile.user_id = " . $user_id . " group by station_profile.station_profile_name, station_profile.station_callsign, station_profile.station_id, station_profile.station_gridsquare order by station_profile.station_callsign"; - + $query = $this->db->query($qsl); return $query->result(); @@ -92,7 +112,15 @@ class Labels_model extends CI_Model { $this->db->where('user_id', $this->session->userdata('user_id')); $this->db->where('useforprint', '1'); $query = $this->db->get('label_types'); - + + return $query->row(); + } + + function getPaperType($ptype_id) { + $this->db->where('user_id', $this->session->userdata('user_id')); + $this->db->where('paper_id',$ptype_id); + $query = $this->db->get('paper_types'); + return $query->row(); } @@ -145,4 +173,48 @@ class Labels_model extends CI_Model { return $query; } + + function updatePaper($id) { + $data = array( + 'user_id' => $this->session->userdata('user_id'), + 'paper_name' => xss_clean($this->input->post('paper_name', true)), + 'metric' => xss_clean($this->input->post('measurementType', true)), + 'width' => xss_clean($this->input->post('width', true)), + 'height' => xss_clean($this->input->post('height', true)), + 'orientation' => xss_clean($this->input->post('orientation', true)), + 'last_modified' => date('Y-m-d H:i:s'), + ); + + $cleanid = $this->security->xss_clean($id); + + $this->db->where('user_id', $this->session->userdata('user_id')); + $this->db->where('paper_id', $cleanid); + $this->db->update('paper_types', $data); + } + + function label_cnt_with_paper($paper_id) { + $clean_paper_id=xss_clean($paper_id); + $sql="select count(distinct l.id) as CNT from label_types l inner join paper_types p on (p.paper_id=l.paper_type_id) where l.user_id=? and p.user_id=? and l.paper_type_id=?"; + $query = $this->db->query($sql, array($this->session->userdata('user_id'), this->session->userdata('user_id'), $clean_paper_id)); + $row = $query->row(); + if (isset($row)) { + return($row->CNT); + } else { + return 0; + } + } + + function deletePaper($id) { + $cleanid = xss_clean($id); + + $this->db->delete('paper_types', array('paper_id' => $cleanid, 'user_id' => $this->session->userdata('user_id'))); + } + + function getPaper($id) { + $this->db->where('user_id', $this->session->userdata('user_id')); + $this->db->where('paper_id', $id); + $query = $this->db->get('paper_types'); + + return $query->row(); + } } diff --git a/application/models/User_model.php b/application/models/User_model.php index b1f1511d..fe84a0c9 100644 --- a/application/models/User_model.php +++ b/application/models/User_model.php @@ -173,6 +173,7 @@ class User_Model extends CI_Model { $this->db->insert($this->config->item('auth_table'), $data); $insert_id = $this->db->insert_id(); $this->db->query("insert into bandxuser (bandid, userid, active, cq, dok, dxcc, iota, pota, sig, sota, uscounties, was, wwff, vucc) select bands.id, " . $insert_id . ", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 from bands;"); + $this->db->query("insert into paper_types (user_id,paper_name,metric,width,orientation,height) SELECT ".$insert_id.", paper_name, metric, width, orientation,height FROM paper_types where user_id = -1;"); return OK; } else { return EUSERNAMEEXISTS; diff --git a/application/views/labels/create.php b/application/views/labels/create.php index daf88f55..8b0fc017 100644 --- a/application/views/labels/create.php +++ b/application/views/labels/create.php @@ -25,11 +25,14 @@