Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class User extends CI_Controller {
- public function index()
- {
- $this->load->model('user_model');
- $getData=$this->user_model->getPost();
- $data = array(
- 'getdata' => $getData,
- );
- $this->slice->view('user.view_user', $data);
- }
- public function edit_user()
- {
- $this->load->model('user_model');
- $getData=$this->user_model->getPost();
- $data = array(
- 'getdata' => $getData,
- );
- print_r($getData);
- $this->slice->view('user.edit_user', $data);
- }
- // function show_user_id() {
- // $id = $this->uri->segment(3);
- // $data['tblUser'] = $this->user_model->show_user();
- // $data['tblUser'] = $this->user_model->show_user_id($id);
- // $this->load->view('user.edit_user', $data);
- // }
- public function tambah()
- {
- $file_name = date('Ymdhis').".jpg";
- $config['upload_path']='./uploads/';
- $config['allowed_types']='jpg';
- $config['overwrite']=TRUE;
- $config['max_size']='1000000';
- $config['file_name']=$file_name;
- $this->load->library('upload', $config);
- if(!$this->upload->do_upload('fotouser')){
- $error = $this->upload->display_errors('','');
- }
- $this->load->library('form_validation');
- $this->form_validation->set_rules(
- 'namauser', 'nama',
- 'required|min_length[5]|max_length[15]|is_unique[tblUser.nama]',
- array(
- 'required' => 'You have not provided %s.',
- 'is_unique' => 'This %s already exists.'
- )
- );
- $this->form_validation->set_rules('emailuser', 'Email', 'required|valid_email|is_unique[tbluser.email]');
- $this->form_validation->set_rules('fnamauser','Fullname','required');
- $this->form_validation->set_rules('passworduser', 'Password', 'trim|required|min_length[6]',
- array('required' => 'You must provide a %s.')
- );
- $this->form_validation->set_rules('passworduser2', 'Password Confirmation', 'trim|required|matches[passworduser]');
- $this->form_validation->set_rules('tipeuser','tipe','required');
- $this->form_validation->set_rules('datepicker1','tgl_daftar','required');
- $this->form_validation->set_error_delimiters('', '');
- if($this->form_validation->run() != false){
- $data = array(
- 'fullname' => $this->input->post('fnamauser'),
- 'nama' => $this->input->post('namauser'),
- 'email' => $this->input->post('emailuser'),
- 'password' => $this->input->post('passworduser'),
- 'tgl_daftar'=> date('Y-m-d',strtotime($this->input->post('datepicker1'))),
- 'tipe' => $this->input->post('tipeuser'),
- 'foto' => $file_name,
- );
- $insert = $this->db->insert('tbluser', $data);
- $this->createJSON($insert, ["User baru berhasil ditambahkan.".$this->input->post('tgl_daftar'), "User baru gagal ditambahkan."]);
- }
- else
- {
- $this->createJSON(false, ["User baru berhasil.", $data['errors'] = validation_errors()]);
- }
- // return 0;
- }
- public function edit()
- {
- $file_name = $this->input->post('file_name');
- $config['upload_path']='./uploads/';
- $config['allowed_types']='jpg';
- $config['overwrite']=TRUE;
- $config['max_size']='1000000';
- $config['file_name']= $file_name;
- $this->load->library('upload', $config);
- if(!$this->upload->do_upload('fotouser')){
- $error = $this->upload->display_errors('','');
- }
- $this->load->library('form_validation');
- $this->form_validation->set_rules('fnamauser','Fullname');
- $this->form_validation->set_rules(
- 'namauser', 'nama',
- 'min_length[5]|max_length[15]|is_unique[tblUser.nama]');
- $this->form_validation->set_rules('emailuser', 'Email', 'valid_email|is_unique[tbluser.email]');
- $this->form_validation->set_rules('passworduser', 'Password', 'trim|min_length[6]');
- $this->form_validation->set_rules('passworduser2', 'Password Confirmation', 'trim|matches[passworduser');
- $this->form_validation->set_rules('tipeuser','tipe');
- $this->form_validation->set_rules('datepicker1','tgl_daftar');
- $this->form_validation->set_error_delimiters('', '');
- if($this->form_validation->run() != false){
- // $this->createJSON(true, ["User berhasil dihapus.", "User gagal dihapus"]);
- $data = array(
- 'id_user' => $this->input->post('id'),
- 'fullname' => $this->input->post('fnamauser'),
- 'nama' => $this->input->post('namauser'),
- 'email' => $this->input->post('emailuser'),
- 'password' => $this->input->post('passworduser'),
- 'tgl_daftar'=> date('Y-m-d',strtotime($this->input->post('datepicker1'))),
- 'tipe' => $this->input->post('tipeuser'),
- 'foto' => $file_name,
- );
- $this->db->where('id_user', $this->input->post('id_user'));
- $update = $this->db->update('tblUser', $data, array('id_user' => $id));
- $this->createJSON(true, ["User baru berhasil Diedit.".$this->input->post('fnamauser'), "Kategori baru gagal ditambahkan."]);
- }
- else
- {
- $this->createJSON(false, ["User baru berhasil.", $data['errors'] = validation_errors()]);
- }
- }
- public function delete()
- {
- $this->db->delete('tblUser',['id_user'=>$this->input->post('id')]);
- $this->createJSON(true, ["User berhasil dihapus.", "User gagal dihapus"]);
- }
- /* BEGIN HELPER FUNCTION */
- private function createJSON($condition, $text, $push = [])
- {
- if ($condition) :
- $json = array(
- 'status' => 'success',
- 'title' => 'God Job!',
- 'text' => $text[0]
- );
- if ($push) :
- foreach ($push as $key => $val) :
- $json[$key] = $val;
- endforeach;
- endif;
- else :
- $json = array(
- 'status' => 'error',
- 'title' => 'Oops!',
- 'text' => $text[1]
- );
- endif;
- header('Content-Type: application/json');
- echo json_encode($json, JSON_PRETTY_PRINT);
- }
- /* END HELPER FUNCTION */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement