Advertisement
tripsdoc

User.php

Dec 12th, 2016
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. Class User extends Admin_Controller
  5. {
  6.     public function __construct()
  7.     {
  8.         parent::__construct();
  9.         if(!$this->session->userdata('username'))
  10.         {
  11.             redirect('login');
  12.         }
  13.         $this->halaman = 'Data User';
  14.     }
  15.     public function index($page = null)
  16.     {
  17.        
  18.         $users = $this->user->getAll();
  19.         $halaman = $this->halaman;
  20.         $main_view = 'user/index';
  21.         $this->load->view('template', compact('halaman', 'main_view', 'users'));
  22.     }
  23.     public function create()
  24.     {
  25.         if(!$_POST)
  26.         {
  27.             $input = (object) $this->user->getDefaultValues();
  28.         }
  29.         else
  30.         {
  31.             $input = (object) $this->input->post(null, true);
  32.         }
  33.        
  34.         if(!$this->user->validate())
  35.         {
  36.             $halaman = $this->halaman;
  37.             $main_view = 'user/form';
  38.             $form_action = 'user/create';
  39.            
  40.             $this->load->view('template', compact('halaman', 'main_view', 'form_action', 'input'));
  41.             return;
  42.         }
  43.        
  44.         // Hash password
  45.         $input->password = md5($input->password);
  46.        
  47.         if($this->user->insert($input))
  48.         {
  49.             $this->session->set_flashdata('success', 'Data user berhasil disimpan.');
  50.         }
  51.         else
  52.         {
  53.             $this->session->set_flashdata('error', 'Data user gagal disimpan.');
  54.         }
  55.        
  56.         redirect('user');
  57.     }
  58.     public function is_password_required()
  59.     {
  60.         $edit = $this->uri->segment(2);
  61.        
  62.         if($edit != 'edit')
  63.         {
  64.             $password = $this->input->post('password', true);
  65.             if(empty($password))
  66.             {
  67.                 $this->form_validation->set_message('is_password_required', '%s harus diisi.');
  68.                 return false;
  69.             }
  70.         }
  71.         return true;
  72.     }
  73.     public function username_unik()
  74.     {
  75.         $username = $this->input->post('username');
  76.         $id_user = $this->input->post('id_user');
  77.        
  78.         $this->user->where('username', $username);
  79.         !$id_user || $this->user->where('id_user !=', $id_user);
  80.         $user = $this->user->get();
  81.        
  82.         if(count($user))
  83.         {
  84.             $this->form_validation->set_message('username_unik', '%s sudah digunakan.');
  85.             return false;
  86.         }
  87.         return true;
  88.     }
  89.     public function delete($id = null)
  90.     {
  91.         $user = $this->user->where('id_user', $id)->get();
  92.         if(!$user)
  93.         {
  94.             $this->session->set_flashdata('warning', 'Data user tidak ada.');
  95.             redirect('user');
  96.         }
  97.        
  98.         if($this->user->where('id_user', $id)->delete())
  99.         {
  100.             $this->session->set_flashdata('success', 'Data user berhasil dihapus.');
  101.         }
  102.         else
  103.         {
  104.             $this->session->set_flashdata('error', 'Data user gagal dihapus.');
  105.         }
  106.        
  107.         redirect('user');
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement