Skip to content

Commit

Permalink
Can upload profile photo
Browse files Browse the repository at this point in the history
Now profile photos can be uploaded and replaced by a new one.
  • Loading branch information
hpardess committed Mar 22, 2016
1 parent 9a8a4f0 commit 1783f1d
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 38 deletions.
155 changes: 140 additions & 15 deletions application/controllers/Prisoner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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;
}
}
88 changes: 65 additions & 23 deletions application/views/prisoner_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
var oTable;
var provincesList = <?= json_encode($provincesList) ?>;
var districtsList = <?= json_encode($districtsList) ?>;
var photos_directory = "<?= base_url('photos/') ?>";

$(document).ready(function () {
$("li#prisoners", ".navbar-nav").addClass("active");
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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) {
// // $('<option>').attr('value', value.id).attr('selected', true).html(value.group_name).appendTo(permanentDistrictsSelectEl);
// // } else {
// $('<option>').attr('value', value.id).html(value.name).appendTo(permanentDistrictsSelectEl);
// // }
// });

$('[name="permanentDistrict"]', '#modal_form_edit').val(data.prisoner.permanent_district_id);
$('[name="presentProvince"]', '#modal_form_edit').val(data.prisoner.present_province_id);

var presentDistrictsSelectEl = $('[name="presentDistrict"]', '#modal_form_edit');
render_district_list(data.presentDistricts, presentDistrictsSelectEl);
// $.each(data.presentDistricts, function(index, value) {
// // if (data.prisoner.present_district_id === value.id) {
// // $('<option>').attr('value', value.id).attr('selected', true).html(value.group_name).appendTo(presentDistrictsSelectEl);
// // } else {
// $('<option>').attr('value', value.id).html(value.name).appendTo(presentDistrictsSelectEl);
// // }
// });

$('[name="presentDistrict"]', '#modal_form_edit').val(data.prisoner.present_district_id);
$('[name="profilePic"]', '#modal_form_edit').attr("src", data.prisoner.profile_pic);

if(data.prisoner.profile_pic !== '' && data.prisoner.profile_pic !== null)
{
$('img#profilePicDisplay', '#modal_form_edit').attr("src", photos_directory + '/' + data.prisoner.profile_pic);
$('img#profilePicDisplay', '#modal_form_edit').attr("alt", 'Failed to display the photo.');
}
else
{
$('img#profilePicDisplay', '#modal_form_edit').attr("alt", 'Profile photo is not uploaded.');
}

$('#modal_form_edit').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title', '#modal_form_edit').text('Edit User'); // Set Title to Bootstrap modal title
Expand Down Expand Up @@ -246,23 +251,53 @@ function save_record()
url = "<?php echo site_url('prisoner/update')?>";
}

var formData = new FormData($('#form', '#modal_form_edit')[0]);

// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form', '#modal_form_edit').serialize(),
dataType: "JSON",
data: formData,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form_edit').modal('hide');
reload_table();
data = JSON.parse(data);
if(data.success === true)
{
$('#modal_form_edit').modal('hide');
reload_table();
}
else
{
alert(data.message);
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});

// ajax adding data to database
// $.ajax({
// url : url,
// type: "POST",
// data: $('#form', '#modal_form_edit').serialize(),
// dataType: "JSON",
// success: function(data)
// {
// //if success close modal and reload ajax table
// $('#modal_form_edit').modal('hide');
// reload_table();
// },
// error: function (jqXHR, textStatus, errorThrown)
// {
// alert('Error adding / update data');
// }
// });
}
</script>

Expand Down Expand Up @@ -348,7 +383,14 @@ function save_record()
<p class="form-control-static" id="presentDistrict"></p>
</div>
</div>
<img id="profilePic" alt="Profile Photo" class="img-rounded">
<div class="form-group">
<!-- <label class="control-label col-sm-4">Profile Photo</label> -->
<div class="col-sm-12">
<div class="thumbnail">
<img id="profilePic" alt="Profile Photo not exist" class="img-rounded">
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
Expand Down Expand Up @@ -474,7 +516,7 @@ function save_record()
<!-- <label class="control-label col-sm-4">Profile Photo</label> -->
<div class="col-sm-12">
<div class="thumbnail">
<img name="profilePicc" src="<?php echo base_url('photos/profile_pic01.jpg')?>" alt="Profile Photo" class="img-rounded">
<img id="profilePicDisplay" alt="Profile Photo" class="img-rounded">
</div>
</div>
</div>
Expand Down

0 comments on commit 1783f1d

Please sign in to comment.