diff --git a/application/controllers/Logbookadvanced.php b/application/controllers/Logbookadvanced.php new file mode 100644 index 000000000..7b4598143 --- /dev/null +++ b/application/controllers/Logbookadvanced.php @@ -0,0 +1,175 @@ +load->helper(array('form', 'url', 'psr4_autoloader')); + + $this->load->model('user_model'); + if (!$this->user_model->authorize(2)) { + $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); + redirect('dashboard'); + } + } + + function index() { + $this->load->model('stations'); + $this->load->model('logbookadvanced_model'); + $this->load->model('logbook_model'); + $this->load->model('bands'); + $this->load->model('iota'); + $this->load->model('dxcc'); + + $stationIds = []; + + $deOptions = []; + foreach ($this->stations->all_of_user()->result() as $station) { + $deOptions[$station->station_callsign] = 1; + $stationIds[] = $station->station_id; + } + ksort($deOptions); + $deOptions = array_keys($deOptions); + + $modes = []; + if ($stationIds !== []) { + foreach ($this->logbookadvanced_model->get_worked_modes($stationIds) as $mode) { + $key = $mode['mode']; + if ($mode['submode'] !== null) { + $key .= "|" . $mode['submode']; + } + if ($mode['submode'] == null) { + $modes[$key] = $mode['mode']; + } else { + $modes[$key] = $mode['submode']; + } + } + } + + $data = []; + $data['page_title'] = "Advanced logbook"; + $data['hasDatePicker'] = true; + + $pageData = []; + $pageData['datePlaceholder'] = 'DD/MM/YYYY'; + $pageData['deOptions'] = $deOptions; + $pageData['modes'] = $modes; + $pageData['dxccarray'] = $this->logbook_model->fetchDxcc(); + $pageData['iotaarray'] = $this->logbook_model->fetchIota(); + + $pageData['bands'] = $this->bands->get_worked_bands(); + + $footerData = []; + $footerData['scripts'] = [ + 'assets/js/moment.min.js', + 'assets/js/tempusdominus-bootstrap-4.min.js', + 'assets/js/sections/logbookadvanced.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/logbookadvanced.js")) + ]; + + $this->load->view('interface_assets/header', $data); + $this->load->view('logbookadvanced/index', $pageData); + $this->load->view('interface_assets/footer', $footerData); + } + + public function search() { + $this->load->model('logbookadvanced_model'); + + $searchCriteria = array( + 'user_id' => (int)$this->session->userdata('user_id'), + 'dateFrom' => xss_clean($this->input->post('dateFrom')), + 'dateTo' => xss_clean($this->input->post('dateTo')), + 'de' => xss_clean($this->input->post('de')), + 'dx' => xss_clean($this->input->post('dx')), + 'mode' => xss_clean($this->input->post('mode')), + 'band' => xss_clean($this->input->post('band')), + 'qslSent' => xss_clean($this->input->post('qslSent')), + 'qslReceived' => xss_clean($this->input->post('qslReceived')), + 'iota' => xss_clean($this->input->post('iota')), + 'dxcc' => xss_clean($this->input->post('dxcc')), + 'propmode' => xss_clean($this->input->post('propmode')), + 'gridsquare' => xss_clean($this->input->post('gridsquare')), + 'state' => xss_clean($this->input->post('state')), + 'qsoresults' => xss_clean($this->input->post('qsoresults')), + ); + + $qsos = []; + foreach ($this->logbookadvanced_model->searchQsos($searchCriteria) as $qso) { + $qsos[] = $qso->toArray(); + } + + header("Content-Type: application/json"); + print json_encode($qsos); + } + + public function updateFromCallbook() { + $this->load->model('logbook_model'); + $this->load->model('logbookadvanced_model'); + + $qsoID = xss_clean($this->input->post('qsoID')); + $qso = $this->logbook_model->qso_info($qsoID)->row_array(); + if ($qso === null) { + header("Content-Type: application/json"); + echo json_encode([]); + return; + } + + $callbook = $this->logbook_model->loadCallBook($qso['COL_CALL'], $this->config->item('use_fullname')); + + if ($callbook['callsign'] !== "") { + $this->logbookadvanced_model->updateQsoWithCallbookInfo($qsoID, $qso, $callbook); + $qso['COL_NAME'] = trim($callbook['name']); + if (isset($callbook['qslmgr'])) { + $qso['COL_QSL_VIA'] = trim($callbook['qslmgr']); + } + } + + $qsoObj = new QSO($qso); + + header("Content-Type: application/json"); + echo json_encode($qsoObj->toArray()); + } + + function export_to_adif() { + $this->load->model('logbookadvanced_model'); + + $ids = xss_clean($this->input->post('id')); + $user_id = (int)$this->session->userdata('user_id'); + + $data['qsos'] = $this->logbookadvanced_model->getQsosForAdif($ids, $user_id); + + $this->load->view('adif/data/exportall', $data); + } + + function update_qsl() { + $this->load->model('logbookadvanced_model'); + + $ids = xss_clean($this->input->post('id')); + $user_id = (int)$this->session->userdata('user_id'); + $method = xss_clean($this->input->post('method')); + $sent = xss_clean($this->input->post('sent')); + + $status = $this->logbookadvanced_model->updateQsl($ids, $user_id, $method, $sent); + + $data = $this->logbookadvanced_model->getQsosForAdif($ids, $user_id); + + $results = $data->result('array'); + + $qsos = []; + foreach ($results as $data) { + $qsos[] = new QSO($data); + } + + $q = []; + foreach ($qsos as $qso) { + $q[] = $qso->toArray(); + } + + header("Content-Type: application/json"); + print json_encode($q); + } +} \ No newline at end of file diff --git a/application/helpers/psr4_autoloader_helper.php b/application/helpers/psr4_autoloader_helper.php new file mode 100644 index 000000000..74a00521d --- /dev/null +++ b/application/helpers/psr4_autoloader_helper.php @@ -0,0 +1,21 @@ +load->model('logbooks_model'); + + $ids = "'".implode("','",$stationIds)."'"; + + $sql = " + SELECT distinct `COL_MODE`, `COL_SUBMODE` + FROM `" . $this->config->item('table_name') . "` qsos + WHERE qsos.station_id IN (".$ids.") + ORDER BY COL_MODE, COL_SUBMODE"; + + $data = $this->db->query($sql); + + $results = []; + foreach ($data->result() as $row) { + $results[] = [ + 'mode' => $row->COL_MODE, + 'submode' => $row->COL_SUBMODE + ]; + } + return $results; + } + + /* + * @param array $searchCriteria + * @return array + */ + public function searchQsos($searchCriteria) : array { + $conditions = []; + $binding = [$searchCriteria['user_id']]; + + if ($searchCriteria['dateFrom'] !== '') { + $from = DateTime::createFromFormat('d/m/Y', $searchCriteria['dateFrom']); + $from = $from->format('Y-m-d'); + $conditions[] = "date(COL_TIME_ON) >= ?"; + $binding[] = $from; + } + if ($searchCriteria['dateTo'] !== '') { + $to = DateTime::createFromFormat('d/m/Y', $searchCriteria['dateTo']); + $to = $to->format('Y-m-d'); + $conditions[] = "date(COL_TIME_ON) <= ?"; + $binding[] = $to; + } + if ($searchCriteria['de'] !== '') { + $conditions[] = "COL_STATION_CALLSIGN = ?"; + $binding[] = trim($searchCriteria['de']); + } + if ($searchCriteria['dx'] !== '') { + $conditions[] = "COL_CALL LIKE ?"; + $binding[] = '%' . trim($searchCriteria['dx']) . '%'; + } + if ($searchCriteria['mode'] !== '') { + list ($mode, $subMode) = explode("|", trim($searchCriteria['mode'])); + $mode = trim($mode); + $subMode = trim($subMode); + $conditions[] = "COL_MODE = ?"; + $binding[] = $mode; + if ($subMode !== '') { + $conditions[] = "COL_SUBMODE = ?"; + $binding[] = $subMode; + } + } + if ($searchCriteria['band'] !== '') { + if($searchCriteria['band'] != "SAT") { + $conditions[] = "COL_BAND = ? and COL_PROP_MODE != 'SAT'"; + $binding[] = trim($searchCriteria['band']); + } else { + $conditions[] = "COL_PROP_MODE = 'SAT'"; + } + } + if ($searchCriteria['qslSent'] !== '') { + $conditions[] = "COL_QSL_SENT = ?"; + $binding[] = $searchCriteria['qslSent']; + } + if ($searchCriteria['qslReceived'] !== '') { + $conditions[] = "COL_QSL_RCVD = ?"; + $binding[] = $searchCriteria['qslReceived']; + } + + if ($searchCriteria['iota'] !== '') { + $conditions[] = "COL_IOTA = ?"; + $binding[] = $searchCriteria['iota']; + } + + if ($searchCriteria['dxcc'] !== '') { + $conditions[] = "COL_DXCC = ?"; + $binding[] = $searchCriteria['dxcc']; + } + + if ($searchCriteria['state'] !== '') { + $conditions[] = "COL_STATE = ?"; + $binding[] = $searchCriteria['state']; + } + + if ($searchCriteria['gridsquare'] !== '') { + $conditions[] = "(COL_GRIDSQUARE like ? or COL_VUCC_GRIDS like ?)"; + $binding[] = '%' . $searchCriteria['gridsquare'] . '%'; + $binding[] = '%' . $searchCriteria['gridsquare'] . '%'; + } + + if ($searchCriteria['propmode'] !== '') { + $conditions[] = "COL_PROP_MODE = ?"; + $binding[] = $searchCriteria['propmode']; + } + + $where = trim(implode(" AND ", $conditions)); + if ($where != "") { + $where = "AND $where"; + } + + $limit = $searchCriteria['qsoresults']; + + $sql = " + SELECT * + FROM " . $this->config->item('table_name') . " qsos + INNER JOIN station_profile ON qsos.station_id=station_profile.station_id + WHERE station_profile.user_id = ? + $where + ORDER BY qsos.COL_TIME_ON desc + LIMIT $limit + "; + + $data = $this->db->query($sql, $binding); + + $results = $data->result('array'); + + $qsos = []; + foreach ($results as $data) { + $qsos[] = new QSO($data); + } + + return $qsos; + } + + public function getQsosForAdif($ids, $user_id) : object { + $binding = [$user_id]; + $conditions[] = "COL_PRIMARY_KEY in ?"; + $binding[] = json_decode($ids, true); + + $where = trim(implode(" AND ", $conditions)); + if ($where != "") { + $where = "AND $where"; + } + + $sql = " + SELECT * + FROM " . $this->config->item('table_name') . " qsos + INNER JOIN station_profile ON qsos.station_id = station_profile.station_id + WHERE station_profile.user_id = ? + $where + ORDER BY qsos.COL_TIME_ON desc + "; + + return $this->db->query($sql, $binding); + } + + public function updateQsl($ids, $user_id, $method, $sent) { + $this->load->model('user_model'); + + if(!$this->user_model->authorize(2)) { + return array('message' => 'Error'); + } else { + $data = array( + 'COL_QSLSDATE' => date('Y-m-d H:i:s'), + 'COL_QSL_SENT' => $sent, + 'COL_QSL_SENT_VIA' => $method + ); + $this->db->where_in('COL_PRIMARY_KEY', json_decode($ids, true)); + $this->db->update($this->config->item('table_name'), $data); + + return array('message' => 'OK'); + } + } + + public function updateQsoWithCallbookInfo($qsoID, $qso, $callbook) { + $updatedData = array(); + if (!empty($callbook['name']) && empty($qso['COL_NAME'])) { + $updatedData['COL_NAME'] = $callbook['name']; + } + if (!empty($callbook['gridsquare']) && empty($qso['COL_GRIDSQUARE']) && empty($qso['COL_VUCC_GRIDS'] )) { + if (strpos(trim($callbook['gridsquare']), ',') === false) { + $updatedData['COL_GRIDSQUARE'] = strtoupper(trim($callbook['gridsquare'])); + } else { + $updatedData['COL_VUCC_GRIDS'] = strtoupper(trim($callbook['gridsquare'])); + } + } + if (!empty($callbook['city']) && empty($qso['COL_QTH'])) { + $updatedData['COL_QTH'] = $callbook['city']; + } + if (!empty($callbook['lat']) && empty($qso['COL_LAT'])) { + $updatedData['COL_LAT'] = $callbook['lat']; + } + if (!empty($callbook['long']) && empty($qso['COL_LON'])) { + $updatedData['COL_LON'] = $callbook['long']; + } + if (!empty($callbook['iota']) && empty($qso['COL_IOTA'])) { + $updatedData['COL_IOTA'] = $callbook['iota']; + } + if (!empty($callbook['state']) && empty($qso['COL_STATE'])) { + $updatedData['COL_STATE'] = $callbook['state']; + } + if (!empty($callbook['us_county']) && empty($qso['COL_USACA_COUNTIES'])) { + $updatedData['COL_USACA_COUNTIES'] = $callbook['us_county']; + } + if (!empty($callbook['qslmgr']) && empty($qso['COL_QSL_VIA'])) { + $updatedData['COL_QSL_VIA'] = $callbook['qslmgr']; + } + + if (count($updatedData) > 0) { + $this->db->where('COL_PRIMARY_KEY', $qsoID); + $this->db->update($this->config->item('table_name'), $updatedData); + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/application/views/interface_assets/footer.php b/application/views/interface_assets/footer.php index 66e919e23..30042f80b 100644 --- a/application/views/interface_assets/footer.php +++ b/application/views/interface_assets/footer.php @@ -3187,5 +3187,14 @@ className: 'buttons-clear', + + + diff --git a/application/views/interface_assets/header.php b/application/views/interface_assets/header.php index 0918e5726..b63c3e68d 100644 --- a/application/views/interface_assets/header.php +++ b/application/views/interface_assets/header.php @@ -36,7 +36,7 @@ - uri->segment(1) == "adif") { ?> + uri->segment(1) == "adif" || (isset($hasDatePicker) && $hasDatePicker)) { ?> @@ -58,7 +58,14 @@