Advertisement
Guest User

Account.php

a guest
Nov 22nd, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <?php
  2. class Account extends OperatorController
  3. {
  4.     public function index()
  5.     {
  6.         $id = $this->session->userdata('userID');
  7.         $account = $this->account->find($id);
  8.         if (!$account) {
  9.             redirect('logout','refresh');
  10.         }
  11.  
  12.         $mainView = 'account/index';
  13.         $heading = 'Account';
  14.         $this->load->view('template', compact(
  15.             'mainView',
  16.             'heading',
  17.             'account'
  18.         ));
  19.     }
  20.     public function edit()
  21.     {
  22.         $id = $this->session->userdata('userID');
  23.         $account = $this->account->find($id);
  24.         if (!$account) {
  25.             redirect('logout', 'refresh');
  26.         }
  27.  
  28.         $input = (object) $this->input->post(null, true);
  29.         if (!$_POST) {
  30.             $input = (object) $account;
  31.             $input->password = '';
  32.         }
  33.  
  34.         $validate = $this->account->validate();
  35.         if (!$validate) {
  36.             $mainView   = 'account/form';
  37.             $heading    = 'Account > Edit';
  38.             $formAction = 'account/edit';
  39.             $buttonText = 'Update';
  40.             $this->load->view('template', compact(
  41.                 'mainView',
  42.                 'heading',
  43.                 'formAction',
  44.                 'input',
  45.                 'buttonText'
  46.             ));
  47.             return;
  48.         }
  49.  
  50.         // enkripsi password
  51.         if (!empty($input->password)) {
  52.             $input->password = md5($input->password);
  53.         } else {
  54.             unset($input->password);
  55.         }
  56.         // hapus passConf, gaperlu disimpen ke database
  57.         unset($input->passConf);
  58.         $update = $this->account->update($id, $input);
  59.         if (!$udpate) {
  60.             flashMessage('error', 'Data gagal diupdate!');
  61.         } else {
  62.             flashMessage('success', 'Data berhasil diupdate.');
  63.             $this->session->set_userdata(
  64.                 'username',
  65.                 $input->username
  66.             );
  67.         }
  68.        
  69.         redirect('account', 'refresh');
  70.     }
  71.  
  72.     public function isUsernameUnik()
  73.     {
  74.         $username = $this->input->post('username');
  75.         $id       = $this->input->post('ID');
  76.  
  77.         $this->db->where('username', $username);
  78.         !$id || $this->db->where('ID !=', $id);
  79.         $kembar = $this->db->get('user')->result();
  80.  
  81.         if (count($kembar) > 0) {
  82.             $this->form_validation->set_message(
  83.                 'isUsernameUnik',
  84.                 '%s sudah digunakan.'
  85.             );
  86.             return false;
  87.         }
  88.         return true;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement