From dddc656e5ab6d7eb7754ab1948531f00a7231b8e Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Fri, 22 Jul 2011 01:08:47 +0100 Subject: [PATCH] Improved MVC layout of code --- application/controllers/notes.php | 43 +++++++----------- application/controllers/qso.php | 68 +++++++--------------------- application/controllers/search.php | 15 ------ application/models/logbook_model.php | 67 +++++++++++++++++++++++++++ application/models/note.php | 48 ++++++++++++++++++++ 5 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 application/models/note.php diff --git a/application/controllers/notes.php b/application/controllers/notes.php index 0c0d5b21..7eb33338 100644 --- a/application/controllers/notes.php +++ b/application/controllers/notes.php @@ -5,8 +5,8 @@ class Notes extends CI_Controller { /* Displays all notes in a list */ public function index() { - - $data['notes'] = $this->db->get('notes'); + $this->load->model('note'); + $data['notes'] = $this->note->list_all(); $this->load->view('layout/header'); $this->load->view('notes/main', $data); @@ -16,6 +16,8 @@ class Notes extends CI_Controller { /* Provides function for adding notes to the system. */ function add() { + $this->load->model('note'); + $this->load->library('form_validation'); $this->form_validation->set_rules('title', 'Note Title', 'required'); @@ -29,14 +31,8 @@ class Notes extends CI_Controller { $this->load->view('layout/footer'); } else - { - $data = array( - 'cat' => $this->input->post('category'), - 'title' => $this->input->post('title'), - 'note' => $this->input->post('content') - ); - - $this->db->insert('notes', $data); + { + $this->note->add(); redirect('notes'); } @@ -44,9 +40,9 @@ class Notes extends CI_Controller { /* View Notes */ function view($id) { - // Get Note - $this->db->where('id', $id); - $data['note'] = $this->db->get('notes'); + $this->load->model('note'); + + $data['note'] = $this->note->view($id); // Display $this->load->view('layout/header'); @@ -56,10 +52,10 @@ class Notes extends CI_Controller { /* Edit Notes */ function edit($id) { - + $this->load->model('note'); $data['id'] = $id; - $this->db->where('id', $id); - $data['note'] = $this->db->get('notes'); + + $data['note'] = $this->note->view($id); $this->load->library('form_validation'); @@ -75,22 +71,17 @@ class Notes extends CI_Controller { } else { - $data = array( - 'cat' => $this->input->post('category'), - 'title' => $this->input->post('title'), - 'note' => $this->input->post('content') - ); - - $this->db->where('id', $this->input->post('id')); - $this->db->update('notes', $data); - + $this->note->edit(); + redirect('notes'); } } /* Delete Note */ function delete($id) { - $this->db->delete('notes', array('id' => $id)); + $this->load->model('note'); + $this->note->delete($id); + redirect('notes'); } } \ No newline at end of file diff --git a/application/controllers/qso.php b/application/controllers/qso.php index e35c0def..0c658224 100644 --- a/application/controllers/qso.php +++ b/application/controllers/qso.php @@ -12,12 +12,11 @@ class QSO extends CI_Controller { public function index() { + $this->load->model('logbook_model'); + $data['notice'] = false; - $this->db->select('COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY'); - $this->db->order_by("COL_TIME_ON", "desc"); - $this->db->limit(10); - $data['query'] = $this->db->get($this->config->item('table_name')); + $data['query'] = $this->logbook_model->last_ten(); $this->load->library('form_validation'); @@ -33,41 +32,23 @@ class QSO extends CI_Controller { } else { - // Join date+time - $datetime = date('Y-m-d') ." ". $this->input->post('start_time'); - // Create array with QSO Data - $data = array( - 'COL_TIME_ON' => $datetime, - 'COL_TIME_OFF' => $datetime, - 'COL_CALL' => strtoupper($this->input->post('callsign')), - 'COL_BAND' => $this->input->post('band'), - 'COL_FREQ' => $this->input->post('freq'), - 'COL_MODE' => $this->input->post('mode'), - 'COL_RST_RCVD' => $this->input->post('rst_recv'), - 'COL_RST_SENT' => $this->input->post('rst_sent'), - 'COL_COMMENT' => $this->input->post('comment'), - 'COL_SAT_NAME' => $this->input->post('sat_name'), - 'COL_SAT_MODE' => $this->input->post('sat_mode'), - 'COL_GRIDSQUARE' => $this->input->post('locator'), - 'COL_COUNTRY' => $this->input->post('country'), - 'COL_MY_RIG' => $this->input->post('equipment'), - ); - - // Add QSO to database - $this->db->insert($this->config->item('table_name'), $data); + // Add QSO + $this->logbook_model->add(); + + // Store Basic QSO Info for reuse $this->session->set_userdata('band', $this->input->post('band')); $this->session->set_userdata('freq', $this->input->post('freq')); $this->session->set_userdata('mode', $this->input->post('mode')); $this->session->set_userdata('sat_name', $this->input->post('sat_name')); $this->session->set_userdata('sat_mode', $this->input->post('sat_mode')); + + // Get last Ten QSOs + $data['query'] = $this->logbook_model->last_ten(); - $this->db->select('COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY'); - $this->db->order_by("COL_TIME_ON", "desc"); - $this->db->limit(10); - $data['query'] = $this->db->get($this->config->item('table_name')); - + // Set Any Notice Messages $data['notice'] = "QSO Added"; + // Load view to create another contact $this->load->view('layout/header'); $this->load->view('qso/index', $data); @@ -76,8 +57,9 @@ class QSO extends CI_Controller { } function edit() { - $this->db->where('COL_PRIMARY_KEY', $this->uri->segment(3)); - $query = $this->db->get($this->config->item('table_name')); + + $this->load->model('logbook_model'); + $query = $this->logbook_model->qso_info($this->uri->segment(3)); $this->load->library('form_validation'); @@ -86,6 +68,7 @@ class QSO extends CI_Controller { $this->form_validation->set_rules('callsign', 'Callsign', 'required'); $data = $query->row(); + if ($this->form_validation->run() == FALSE) { $this->load->view('layout/header'); @@ -94,24 +77,7 @@ class QSO extends CI_Controller { } else { - - $data = array( - 'COL_TIME_ON' => $this->input->post('time_on'), - 'COL_TIME_OFF' => $this->input->post('time_off'), - 'COL_CALL' => strtoupper($this->input->post('callsign')), - 'COL_BAND' => $this->input->post('band'), - 'COL_FREQ' => $this->input->post('freq'), - 'COL_MODE' => $this->input->post('mode'), - 'COL_RST_RCVD' => $this->input->post('rst_recv'), - 'COL_RST_SENT' => $this->input->post('rst_sent'), - 'COL_COMMENT' => $this->input->post('comment'), - 'COL_NAME' => $this->input->post('name'), - 'COL_SAT_NAME' => $this->input->post('sat_name'), - 'COL_SAT_MODE' => $this->input->post('sat_mode'), - ); - - $this->db->where('COL_PRIMARY_KEY', $this->input->post('id')); - $this->db->update($this->config->item('table_name'), $data); + $this->logbook_model->edit(); $this->session->set_flashdata('notice', 'Record Updated'); redirect('logbook'); } diff --git a/application/controllers/search.php b/application/controllers/search.php index 32db04bf..b3d57be4 100644 --- a/application/controllers/search.php +++ b/application/controllers/search.php @@ -2,21 +2,6 @@ class Search extends CI_Controller { - /** - * Index Page for this controller. - * - * Maps to the following URL - * http://example.com/index.php/welcome - * - or - - * http://example.com/index.php/welcome/index - * - or - - * Since this controller is set as the default controller in - * config/routes.php, it's displayed at http://example.com/ - * - * So any other public methods not prefixed with an underscore will - * map to /index.php/welcome/ - * @see http://codeigniter.com/user_guide/general/urls.html - */ public function index() { $this->load->view('layout/header'); diff --git a/application/models/logbook_model.php b/application/models/logbook_model.php index cd7c7e61..67560e8c 100644 --- a/application/models/logbook_model.php +++ b/application/models/logbook_model.php @@ -8,6 +8,73 @@ class Logbook_model extends CI_Model { parent::__construct(); } + /* Add QSO to Logbook */ + function add() { + // Join date+time + $datetime = date('Y-m-d') ." ". $this->input->post('start_time'); + + // Create array with QSO Data + $data = array( + 'COL_TIME_ON' => $datetime, + 'COL_TIME_OFF' => $datetime, + 'COL_CALL' => strtoupper($this->input->post('callsign')), + 'COL_BAND' => $this->input->post('band'), + 'COL_FREQ' => $this->input->post('freq'), + 'COL_MODE' => $this->input->post('mode'), + 'COL_RST_RCVD' => $this->input->post('rst_recv'), + 'COL_RST_SENT' => $this->input->post('rst_sent'), + 'COL_COMMENT' => $this->input->post('comment'), + 'COL_SAT_NAME' => $this->input->post('sat_name'), + 'COL_SAT_MODE' => $this->input->post('sat_mode'), + 'COL_GRIDSQUARE' => $this->input->post('locator'), + 'COL_COUNTRY' => $this->input->post('country'), + 'COL_MY_RIG' => $this->input->post('equipment'), + ); + + // Add QSO to database + $this->db->insert($this->config->item('table_name'), $data); + } + + /* Edit QSO */ + function edit() { + + $data = array( + 'COL_TIME_ON' => $this->input->post('time_on'), + 'COL_TIME_OFF' => $this->input->post('time_off'), + 'COL_CALL' => strtoupper($this->input->post('callsign')), + 'COL_BAND' => $this->input->post('band'), + 'COL_FREQ' => $this->input->post('freq'), + 'COL_MODE' => $this->input->post('mode'), + 'COL_RST_RCVD' => $this->input->post('rst_recv'), + 'COL_RST_SENT' => $this->input->post('rst_sent'), + 'COL_COMMENT' => $this->input->post('comment'), + 'COL_NAME' => $this->input->post('name'), + 'COL_SAT_NAME' => $this->input->post('sat_name'), + 'COL_SAT_MODE' => $this->input->post('sat_mode'), + ); + + $this->db->where('COL_PRIMARY_KEY', $this->input->post('id')); + $this->db->update($this->config->item('table_name'), $data); + + } + + /* Return last 10 QSOs */ + function last_ten() { + $this->db->select('COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY'); + $this->db->order_by("COL_TIME_ON", "desc"); + $this->db->limit(10); + + return $this->db->get($this->config->item('table_name')); + } + + /* Return QSO Info */ + function qso_info($id) { + $this->db->where('COL_PRIMARY_KEY', $id); + + return $this->db->get($this->config->item('table_name')); + } + + function get_qsos($num, $offset) { $this->db->select('COL_CALL, COL_BAND, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_NAME, COL_COUNTRY, COL_PRIMARY_KEY'); $this->db->order_by("COL_TIME_ON", "desc"); diff --git a/application/models/note.php b/application/models/note.php new file mode 100644 index 00000000..56c18340 --- /dev/null +++ b/application/models/note.php @@ -0,0 +1,48 @@ +db->get('notes'); + } + + function add() { + $data = array( + 'cat' => $this->input->post('category'), + 'title' => $this->input->post('title'), + 'note' => $this->input->post('content') + ); + + $this->db->insert('notes', $data); + } + + function edit() { + $data = array( + 'cat' => $this->input->post('category'), + 'title' => $this->input->post('title'), + 'note' => $this->input->post('content') + ); + + $this->db->where('id', $this->input->post('id')); + $this->db->update('notes', $data); + } + + function delete($id) { + $this->db->delete('notes', array('id' => $id)); + } + + function view($id) { + // Get Note + $this->db->where('id', $id); + return $this->db->get('notes'); + } + +} + +?> \ No newline at end of file