From 1783f1dd251b46b2cc1aa9aec50071d38dee999b Mon Sep 17 00:00:00 2001 From: Hameedullah Pardess Date: Wed, 23 Mar 2016 03:07:02 +0430 Subject: [PATCH] Can upload profile photo Now profile photos can be uploaded and replaced by a new one. --- application/controllers/Prisoner.php | 155 ++++++++++++++++++++++++--- application/views/prisoner_list.php | 88 +++++++++++---- 2 files changed, 205 insertions(+), 38 deletions(-) diff --git a/application/controllers/Prisoner.php b/application/controllers/Prisoner.php index 7be6648..7a578ee 100644 --- a/application/controllers/Prisoner.php +++ b/application/controllers/Prisoner.php @@ -14,13 +14,7 @@ public function __construct() $this->load->model('district_model'); $this->load->model('marital_status_model'); - // File upload config - $config['upload_path'] = './photos/'; - $config['allowed_types'] = 'gif|jpg|png'; - $config['max_size'] = '100'; - $config['max_width'] = '1024'; - $config['max_height'] = '768'; - $this->load->library('upload', $config); + } public function index() @@ -104,6 +98,19 @@ public function delete($id) // add new record public function add() { + // $attachment_file=$_FILES["attachment_file"]; + // $output_dir = "upload/"; + // $fileName = $_FILES["attachment_file"]["name"]; + // move_uploaded_file($_FILES["attachment_file"]["tmp_name"],$output_dir.$fileName); + // echo "File uploaded successfully"; + + $response['success'] = TRUE; + $response['message'] = ''; + $response['result'] = ''; + + // start of transaction + $this->db->trans_begin(); + $criminal_history = $this->input->post('criminalHistory'); $data = array( @@ -117,17 +124,68 @@ public function add() 'permanent_province_id' => $this->input->post('permanentProvince'), 'permanent_district_id' => $this->input->post('permanentDistrict'), 'present_province_id' => $this->input->post('presentProvince'), - 'present_district_id' => $this->input->post('presentDistrict'), - 'profile_pic' => $this->input->post('profilePic') + 'present_district_id' => $this->input->post('presentDistrict') + // 'profile_pic' => $this->input->post('profilePic') ); - $insert = $this->prisoner_model->create($data); - // log_message('debug', 'insert: ' . $insert); - echo json_encode(array("status" => TRUE)); + $record_id = $this->prisoner_model->create($data); + log_message('debug', 'insert ID: ' . $insert_id); + + if ($this->db->trans_status() === FALSE) + { + $response['success'] = FALSE; + $response['message'] = 'Falied to save the data.'; + $this->db->trans_rollback(); + } + else + { + $extension = 'jpg'; + if($_FILES['profilePic'] && $_FILES['profilePic']['size'] > 0) + { + $image = $_FILES['profilePic']; + log_message('debug', 'file name: ' . $image['name']); + $path_info = pathinfo($image['name']); + $extension = $path_info['extension']; + + $file_new_name = $record_id . '.' . $extension; + log_message('debug', 'new file name: ' . $file_new_name); + + if(!$this->upload_photo('profilePic', $file_new_name)) + { + $response['success'] = FALSE; + $response['message'] = $this->upload->display_errors(); + // rollback transaction + $this->db->trans_rollback(); + } + else + { + $dataUpdate = array( + 'profile_pic' => $file_new_name + ); + $this->prisoner_model->update_by_id($record_id, $dataUpdate); + + // commit transaction + $this->db->trans_commit(); + } + } + else + { + // commit transaction + $this->db->trans_commit(); + } + } + echo json_encode($response); } // update exisitn record public function update() { + $response['success'] = TRUE; + $response['message'] = ''; + $response['result'] = ''; + + // start of transaction + $this->db->trans_begin(); + $criminal_history = $this->input->post('criminalHistory'); $data = array( @@ -141,11 +199,78 @@ public function update() 'permanent_province_id' => $this->input->post('permanentProvince'), 'permanent_district_id' => $this->input->post('permanentDistrict'), 'present_province_id' => $this->input->post('presentProvince'), - 'present_district_id' => $this->input->post('presentDistrict'), - 'profile_pic' => $this->input->post('profilePic') + 'present_district_id' => $this->input->post('presentDistrict') + // 'profile_pic' => $this->input->post('profilePic') ); $affected_rows = $this->prisoner_model->update(array('id' => $this->input->post('id')), $data); // log_message('debug', 'affected rows: ' . $affected_rows); - echo json_encode(array("status" => TRUE)); + + $record_id = $this->input->post('id'); + + if ($this->db->trans_status() === FALSE) + { + $response['success'] = FALSE; + $response['message'] = 'Falied to save the data.'; + $this->db->trans_rollback(); + } + else + { + $extension = 'jpg'; + if($_FILES['profilePic'] && $_FILES['profilePic']['size'] > 0) + { + $image = $_FILES['profilePic']; + log_message('debug', 'file name: ' . $image['name']); + $path_info = pathinfo($image['name']); + $extension = $path_info['extension']; + + $file_new_name = $record_id . '.' . $extension; + log_message('debug', 'new file name: ' . $file_new_name); + + if(!$this->upload_photo('profilePic', $file_new_name)) + { + $response['success'] = FALSE; + $response['message'] = $this->upload->display_errors(); + // rollback transaction + $this->db->trans_rollback(); + } + else + { + $dataUpdate = array( + 'profile_pic' => $file_new_name + ); + $this->prisoner_model->update_by_id($record_id, $dataUpdate); + + // commit transaction + $this->db->trans_commit(); + } + } + else + { + // commit transaction + $this->db->trans_commit(); + } + } + echo json_encode($response); + } + + private function upload_photo($field, $file_new_name) { + // File upload config + $config['upload_path'] = './photos/'; + $config['allowed_types'] = 'gif|jpg|png'; + $config['max_size'] = '2000'; + $config['max_width'] = '1024'; + $config['max_height'] = '768'; + $config['overwrite'] = TRUE; + $config['file_name'] = $file_new_name; + $this->load->library('upload', $config); + + if(!$this->upload->do_upload($field)) + { + log_message('debug', 'file upload: ' . var_export($this->upload->display_errors(), true)); + return FALSE; + } + + log_message('debug', 'file upload: ' . var_export($this->upload->data(), true)); + return TRUE; } } diff --git a/application/views/prisoner_list.php b/application/views/prisoner_list.php index 54abf72..4a167f1 100644 --- a/application/views/prisoner_list.php +++ b/application/views/prisoner_list.php @@ -45,6 +45,7 @@ var oTable; var provincesList = ; var districtsList = ; + var photos_directory = ""; $(document).ready(function () { $("li#prisoners", ".navbar-nav").addClass("active"); @@ -132,7 +133,16 @@ function view_record(id) $('p#permanentDistrict', '#modal_form_view').html(data.permanent_district_id); $('p#presentProvince', '#modal_form_view').html(data.present_province_id); $('p#presentDistrict', '#modal_form_view').html(data.present_district_id); - $('img#profilePic', '#modal_form_view').attr("src", data.profile_pic); + + if(data.profile_pic !== '' && data.profile_pic !== null) + { + $('img#profilePic', '#modal_form_view').attr("src", photos_directory + '/' + data.profile_pic); + $('img#profilePic', '#modal_form_view').attr("alt", 'Failed to display the photo.'); + } + else + { + $('img#profilePic', '#modal_form_view').attr("alt", 'Profile photo is not uploaded.'); + } $('#modal_form_view').modal('show'); // show bootstrap modal when complete loaded }, @@ -171,29 +181,24 @@ function edit_record(id) var permanentDistrictsSelectEl = $('[name="permanentDistrict"]', '#modal_form_edit'); render_district_list(data.permanentDistricts, permanentDistrictsSelectEl); - // $.each(data.permanentDistricts, function(index, value) { - // // if (data.prisoner.permanent_district_id === value.id) { - // // $('