0, 'Active' => 1, 'Ping Failure' => 2, 'GPS Telnet Failure' => 3, 'Other Failure' => 4, ]; public function __construct() { parent::__construct(); // Load model $this->load->model('phone_farm_phone_model', 'phone_farm_phone'); $this->load->helper('pagination'); } public function index() { $this->load->helper(array('url')); //get query string filter $filterData = $this->input->get(); $queryString = $this->input->server('QUERY_STRING'); // remove empty value $filterData = array_filter($filterData, function ($v, $k) { return $v != ''; }, ARRAY_FILTER_USE_BOTH); $pagingUrl = empty($queryString) ? uri_string() : uri_string() . '?' . $queryString; $pagingUrl = remove_querystring_var($pagingUrl, 'page'); $data = $this->phone_farm_phone->getPhones($filterData); $this->viewData['list'] = $data['list']; $this->viewData['statusOptions'] = $this->statusOptions; $this->viewData['filterData'] = $filterData; $this->viewData['pagination'] = initPagination($this->pagePerItem, $data['totalItems'], $data['page'], $pagingUrl); $this->renderAdminPage('phone_farm_phones/list', $this->viewData); } public function create() { $this->load->helper(array('form', 'url')); $this->viewData['statusOptions'] = $this->statusOptions; $this->viewData['phone'] = null; $this->renderAdminPage('phone_farm_phones/create', $this->viewData); } public function store() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->viewData['phone'] = null; $this->viewData['statusOptions'] = $this->statusOptions; $this->setPhoneCreationRules(); if ($this->form_validation->run() == false) { $this->renderAdminPage('phone_farm_phones/create', $this->viewData); return; } $inputData = $this->getFormData(); $res = $this->phone_farm_phone->createPhone($inputData); if ($res['status'] == 'success') { $this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success.'); redirect('/phone_farm_phones'); } else { $this->session->set_flashdata('error', isSet($res['message']) ? $res['message'] : 'Create fail.'); $this->renderAdminPage('phone_farm_phones/create', $this->viewData); return; } } public function edit($id) { $this->load->helper(array('form', 'url')); $res= $this->phone_farm_phone->getPhoneById($id); $this->viewData['phone'] = isSet($res['data']) ? $res['data'] : []; $this->viewData['statusOptions'] = $this->statusOptions; $this->viewData['phoneId'] = $id; $this->renderAdminPage('phone_farm_phones/edit', $this->viewData); } public function update($id) { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->viewData['statusOptions'] = $this->statusOptions; $this->viewData['phone'] = null; $this->viewData['phoneId'] = $id; $this->setPhoneCreationRules(); if ($this->form_validation->run() == false) { $this->renderAdminPage('phone_farm_phones/edit', $this->viewData); return; } $inputData = $this->getFormData(); $inputData['id'] = (int) $id; $res = $this->phone_farm_phone->updatePhoneById($id, $inputData); if ($res['status'] == 'success') { $this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success'); redirect('/phone_farm_phones'); } else { $this->session->set_flashdata('error', $res['message']); $this->renderAdminPage('phone_farm_phones/edit', $this->viewData); return; } } public function remove($param) { $res = $this->phone_farm_phone->removePhoneById($param); if (!$res['success'] != 'success') { $this->viewData['errMsg'] = isSet($res['message']) ? $res['message'] : 'Something went wrong. Please try again later.'; $this->renderAdminPage('phone_farm_phones', $this->viewData); return; } redirect('/phone_farm_phones'); } protected function renderAdminPage($page_name, $data) { $this->load->view('admin/view_admin_header', $data); $this->load->view($page_name, $data); $this->load->view('admin/view_admin_footer', $data); } private function getFormData() { return [ 'phone_name' => $this->input->post('phone_name'), 'phone_model' => $this->input->post('phone_model'), 'phone_vendor' => $this->input->post('phone_vendor'), 'phone_serial' => $this->input->post('phone_serial'), 'ip_address' => $this->input->post('ip_address'), 'gps_emulator_port' => $this->input->post('gps_emulator_port'), 'phone_number' => $this->input->post('phone_number'), 'status' => (int) $this->input->post('status'), ]; } private function setPhoneCreationRules() { $config = [ [ 'field' => 'phone_name', 'label' => 'Name', 'rules' => 'required|max_length[12]', 'errors' => array( 'max_length' => 'Max length is 20.' ), ], [ 'field' => 'phone_model', 'label' => 'Model', 'rules' => 'required|max_length[40]', 'errors' => array( 'max_length' => 'Max length is 40.' ) ], [ 'field' => 'phone_vendor', 'label' => 'Vendor', 'rules' => 'required|max_length[40]', 'errors' => array( 'max_length' => 'Max length is 40.' ) ], [ 'field' => 'phone_serial', 'label' => 'Serial', 'rules' => 'required|max_length[20]', 'errors' => array( 'max_length' => 'Max length is 20.', ) ], [ 'field' => 'phone_number', 'label' => 'Phone number', 'rules' => 'required|max_length[20]', 'errors' => array( 'max_length' => 'Max length is 20.' ) ] ]; $this->form_validation->set_rules($config); } }