Welcome, guest! Login / Register - Why register?
[email protected] webmail now available. Want one? Go here.
You will not find us on Bing and you will get a bounce when you try registering with an outlook/hotmail/live email address here. And the irony is they spam us and we don't spam them. Not our fault. Blame the corporate bully. #deeznutsmicrosoft

Paste

Pasted as PHP by sheena ( 4 years ago )
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {
    function __construct(){
		parent::__construct();
		if ($this->session->userdata('role')!="admin") {
			show_404();
        }
    }
    
	function index(){
        $data['title'] = 'user';
		$data['side'] = 'index';
		$data['user'] = $this->db->query("SELECT id_user, nama, username, status FROM user WHERE role = 'users'")->result();
		$this->theme($data,'a_user');
    }

    function tambah(){
        $data['title'] = 'user';
		$data['side'] = 'tambah';
		$this->theme($data,'a_user_tambah');
    }


    function edit($id = null){
		if($id > 0){
			$data['side'] = 'user_index';
            $cek = $this->db->query("SELECT id_user, nama, username, password, status FROM user WHERE role = 'users' AND id_user = '$id'");
            if($cek->num_rows() == 1){
                $data['data'] = $cek->result()[0];
                $data['title'] = 'user';
                $data['side'] = 'index';
                $this->theme($data,'a_user_edit');
            }else{
                show_404();
            }
		}else{
			show_404();
		}
    }

    
    function add(){
		if($this->input->post('submit')){
            $nama = $this->input->post('nama');
            $username = $this->input->post('username');
            $password = md5($this->input->post('password'));
            $status = $this->input->post('status');
			
			$data_insert = array(
                'nama' => $nama,
                'username' => $username,
                'password' => $password,
                'role' => 'users',
				'status' => $status
			);
			$this->db->insert('user',$data_insert);

            $this->session->set_flashdata('item', array('message' => 'Berhasil menambahkan data!','color' => 'success'));
			redirect('user');
		}else{
			show_404();
		}
    }


    function update(){
		if($this->input->post('submit')){
            $id_user = $this->input->post('id');
            $old_password = md5($this->input->post('old_password'));
            $cek = $this->db->query("SELECT id_user, username, password, nama FROM user WHERE role = 'users' AND id_user = '$id_user' AND password='$old_password'");

            if($cek->num_rows() == 1){
                $nama = $this->input->post('nama');
                $username = $this->input->post('username');
                $new_password = md5($this->input->post('new_password'));
                $conf_password = md5($this->input->post('conf_password'));
                $status = $this->input->post('status');

                if(strlen($new_password) > 0){
                    if((!strcmp($password, $old_password)) && (!strcmp($new_password, $conf_password))){
                        $data_update = array(
                        'nama' => $nama,
                        'username' => $username,
                        'password' => $new_password,
                        'status' => $status
                        );

                    $this->db->where('id_user',$id_user);
                    $this->db->update('user',$data_update);
                        
                    $this->session->set_flashdata('item', array('message' => 'Berhasil mengubah data!','color' => 'info'));
                    redirect('user');
                    
                    }elseif($new_password != $conf_password){
                        echo '<script type="text/javascript">'; 
                        echo 'alert("Data gagal diubah, Password baru tidak sesuai!");';
                        echo 'window.location.href = "'.$_SERVER['HTTP_REFERER'].'";';
                        echo '</script>';
                    }else{
                        echo '<script type="text/javascript">'; 
                        echo 'alert("Data gagal diubah, Password lama tidak sesuai!");';
                        echo 'window.location.href = "'.$_SERVER['HTTP_REFERER'].'";';
                        echo '</script>';
                    }  


                }else{
                    $data_update = array(
                        'nama' => $nama,
                        'username' => $username,
                        'status' => $status
                    );
                    $this->db->where('id_user',$id_user);
                    $this->db->update('user',$data_update);

                    $this->session->set_flashdata('item', array('message' => 'Berhasil mengubah data!','color' => 'info'));
                }

                
            }else{
                echo('no');
            }
		}else{
			show_404();
		}
    }
    
    function aktivasi($id = null){
		if($id > 0){
            $cek = $this->db->query("SELECT status FROM user WHERE role = 'users' AND id_user = '$id'");
            if($cek->num_rows() == 1){
                if($cek->result()[0]->status == 0){
                    $status = '1';
                    $message = array('message' => 'Akun telah aktif!','color' => 'success');
                }else{
                    $status = '0';
                    $message = array('message' => 'Akun telah tidak aktif!','color' => 'danger');
                }
                $data_update = array(
                    'status' => $status
                );
                $this->db->where('id_user',$id);
                $this->db->update('user',$data_update);

                $this->session->set_flashdata('item', $message);
                redirect('user');
            }else{
                show_404();
            }
		}else{
			show_404();
		}
	}
    
    private function theme($data,$view){
        $this->load->view('template/headers',$data);
        $this->load->view('template/sidebar');
        $this->load->view("content/$view");
        $this->load->view('template/footer');
    }
}

 

Revise this Paste

Parent: 97297
Your Name: Code Language: