Advertisement
Guest User

Untitled

a guest
May 27th, 2018
3,677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.78 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Person extends CI_Controller {     public function __construct()     {         parent::__construct();         $this->load->model('person_model','person');
  2.     }
  3.  
  4.     public function index()
  5.     {
  6.         $this->load->helper('url');
  7.         $this->load->view('person_view');
  8.     }
  9.  
  10.     public function ajax_list()
  11.     {
  12.         $this->load->helper('url');
  13.  
  14.         $list = $this->person->get_datatables();
  15.         $data = array();
  16.         $no = $_POST['start'];
  17.         foreach ($list as $person) {
  18.             $no++;
  19.             $row = array();
  20.             $row[] = $person->firstName;
  21.             $row[] = $person->lastName;
  22.             $row[] = $person->gender;
  23.             $row[] = $person->address;
  24.             $row[] = $person->dob;
  25.             if($person->photo)
  26.                 $row[] = '<a href="'.base_url('upload/'.$person->photo).'" target="_blank"><img src="'.base_url('upload/'.$person->photo).'" class="img-responsive" /></a>';
  27.             else
  28.                 $row[] = '(No photo)';
  29.  
  30.             //add html for action
  31.             $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$person->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
  32.                  <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$person->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
  33.          
  34.             $data[] = $row;
  35.         }
  36.  
  37.         $output = array(
  38.                         "draw" => $_POST['draw'],
  39.                         "recordsTotal" => $this->person->count_all(),
  40.                         "recordsFiltered" => $this->person->count_filtered(),
  41.                         "data" => $data,
  42.                 );
  43.         //output to json format
  44.         echo json_encode($output);
  45.     }
  46.  
  47.     public function ajax_edit($id)
  48.     {
  49.         $data = $this->person->get_by_id($id);
  50.         $data->dob = ($data->dob == '0000-00-00') ? '' : $data->dob; // if 0000-00-00 set tu empty for datepicker compatibility
  51.         echo json_encode($data);
  52.     }
  53.  
  54.     public function ajax_add()
  55.     {
  56.         $this->_validate();
  57.          
  58.         $data = array(
  59.                 'firstName' => $this->input->post('firstName'),
  60.                 'lastName' => $this->input->post('lastName'),
  61.                 'gender' => $this->input->post('gender'),
  62.                 'address' => $this->input->post('address'),
  63.                 'dob' => $this->input->post('dob'),
  64.             );
  65.  
  66.         if(!empty($_FILES['photo']['name']))
  67.         {
  68.             $upload = $this->_do_upload();
  69.             $data['photo'] = $upload;
  70.         }
  71.  
  72.         $insert = $this->person->save($data);
  73.  
  74.         echo json_encode(array("status" => TRUE));
  75.     }
  76.  
  77.     public function ajax_update()
  78.     {
  79.         $this->_validate();
  80.         $data = array(
  81.                 'firstName' => $this->input->post('firstName'),
  82.                 'lastName' => $this->input->post('lastName'),
  83.                 'gender' => $this->input->post('gender'),
  84.                 'address' => $this->input->post('address'),
  85.                 'dob' => $this->input->post('dob'),
  86.             );
  87.  
  88.         if($this->input->post('remove_photo')) // if remove photo checked
  89.         {
  90.             if(file_exists('upload/'.$this->input->post('remove_photo')) && $this->input->post('remove_photo'))
  91.                 unlink('upload/'.$this->input->post('remove_photo'));
  92.             $data['photo'] = '';
  93.         }
  94.  
  95.         if(!empty($_FILES['photo']['name']))
  96.         {
  97.             $upload = $this->_do_upload();
  98.              
  99.             //delete file
  100.             $person = $this->person->get_by_id($this->input->post('id'));
  101.             if(file_exists('upload/'.$person->photo) && $person->photo)
  102.                 unlink('upload/'.$person->photo);
  103.  
  104.             $data['photo'] = $upload;
  105.         }
  106.  
  107.         $this->person->update(array('id' => $this->input->post('id')), $data);
  108.         echo json_encode(array("status" => TRUE));
  109.     }
  110.  
  111.     public function ajax_delete($id)
  112.     {
  113.         //delete file
  114.         $person = $this->person->get_by_id($id);
  115.         if(file_exists('upload/'.$person->photo) && $person->photo)
  116.             unlink('upload/'.$person->photo);
  117.          
  118.         $this->person->delete_by_id($id);
  119.         echo json_encode(array("status" => TRUE));
  120.     }
  121.  
  122.     private function _do_upload()
  123.     {
  124.         $config['upload_path']          = 'upload/';
  125.         $config['allowed_types']        = 'gif|jpg|png';
  126.         $config['max_size']             = 100; //set max size allowed in Kilobyte
  127.         $config['max_width']            = 1000; // set max width image allowed
  128.         $config['max_height']           = 1000; // set max height allowed
  129.         $config['file_name']            = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
  130.  
  131.         $this->load->library('upload', $config);
  132.  
  133.         if(!$this->upload->do_upload('photo')) //upload and validate
  134.         {
  135.             $data['inputerror'][] = 'photo';
  136.             $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
  137.             $data['status'] = FALSE;
  138.             echo json_encode($data);
  139.             exit();
  140.         }
  141.         return $this->upload->data('file_name');
  142.     }
  143.  
  144.     private function _validate()
  145.     {
  146.         $data = array();
  147.         $data['error_string'] = array();
  148.         $data['inputerror'] = array();
  149.         $data['status'] = TRUE;
  150.  
  151.         if($this->input->post('firstName') == '')
  152.         {
  153.             $data['inputerror'][] = 'firstName';
  154.             $data['error_string'][] = 'First name is required';
  155.             $data['status'] = FALSE;
  156.         }
  157.  
  158.         if($this->input->post('lastName') == '')
  159.         {
  160.             $data['inputerror'][] = 'lastName';
  161.             $data['error_string'][] = 'Last name is required';
  162.             $data['status'] = FALSE;
  163.         }
  164.  
  165.         if($this->input->post('dob') == '')
  166.         {
  167.             $data['inputerror'][] = 'dob';
  168.             $data['error_string'][] = 'Date of Birth is required';
  169.             $data['status'] = FALSE;
  170.         }
  171.  
  172.         if($this->input->post('gender') == '')
  173.         {
  174.             $data['inputerror'][] = 'gender';
  175.             $data['error_string'][] = 'Please select gender';
  176.             $data['status'] = FALSE;
  177.         }
  178.  
  179.         if($this->input->post('address') == '')
  180.         {
  181.             $data['inputerror'][] = 'address';
  182.             $data['error_string'][] = 'Addess is required';
  183.             $data['status'] = FALSE;
  184.         }
  185.  
  186.         if($data['status'] === FALSE)
  187.         {
  188.             echo json_encode($data);
  189.             exit();
  190.         }
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement