<?php
defined(&#039;BASEPATH&#039;) OR exit(&#039;No direct script access allowed&#039;);

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

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


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

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

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


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

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

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

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


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

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

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

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

Add a code snippet to your website: www.paste.org