<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

	//load model
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		//proteksi halaman
		$this->simple_login->check_login();
		//ptoteksi admin
		if($this->session->userdata('level') !='Administrator') 
		{			
			//kalau bukan adamin, lempar ke login
			$this->session->set_flashdata('warning', 'Hak akses anda tidak diijinkan mengakses menu pengguna');
			redirect(base_url('login'),'refresh');				
		}
    }
	//Data user
	public function index()
	{
		$user = $this->user_model->listing();
		$total = $this->user_model->total();

		//validasi input
		$valid = $this->form_validation;
		//check nama
		$valid->set_rules('nama','Nama Lengkap','required',
			array( 'required'		=> '%s harus diisi...'));
		//check email
		$valid->set_rules('email','Email','required|valid_email',
			array(  'required'		=> '%s harus diisi',
					'valid_email'	=> '%s tidak valid. Masukan email yang benar'));
		//check username
		$valid->set_rules('userid','Userid','required|is_unique[user2.userid]',
			array(  'required'		=> '%s harus diisi',
					'is_unique'		=> '%s sudah ada. Buat userid baru'));
		//check password
		$valid->set_rules('password','Password','required|min_length[6]|max_length[32]',
			array(  'required'		=> '%s harus diisi',
					'min_length'	=> '%s minimal 6 karakter',
					'max_length'	=> '%s maksimal 32 karakter'));
		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		//end validasi

			$data = array(	'title'		=> 'Data Pengguna User [ '.$total->total.' ]',
						  	'user'		=> $user,
						  	'content' 	=> 'user/index'
						);
			$this->load->view('layout/wrapper', $data, FALSE);	
		//jika validasi oke, masuk database
		}else{
			$inp  = $this->input;
			$data = array(	'nama'			=> $inp->post('nama'),
							'email'			=> $inp->post('email'),
							'userid'		=> $inp->post('userid'),
							'password'		=> MD5($inp->post('password')),
							'level'			=> $inp->post('level'),
							'gambar'		=> $inp->post('gambar'),
						);
			//proses oleh model
			$this->user_model->tambah($data);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Data user telah ditambah');
			redirect(base_url('user'),'refresh');
		}
		//end masuk database
	}

	//Edit user
	public function edit($id)
	{
		//panggil data user yang akan diedit
		$user = $this->user_model->detail($id);
		

		//validasi input
		$valid = $this->form_validation;
		//check nama
		$valid->set_rules('nama','Nama Lengkap','required',
			array( 'required'		=> '%s harus diisi'));
		//check email
		$valid->set_rules('email','Email','required|valid_email',
			array(  'required'		=> '%s harus diisi',
					'valid_email'	=> '%s tidak valid. Masukan email yang benar'));
		//check password
		$valid->set_rules('password','Password','required|min_length[6]|max_length[32]',
			array(  'required'		=> '%s harus diisi',
					'min_length'	=> '%s minimal 6 karakter',
					'max_length'	=> '%s maksimal 32 karakter'));
		
		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		//end validasi

			$data = array(	'title'		=> 'Edit Data User : '.$user->nama,
						  	'user'		=> $user,
						  	'content' 	=> 'user/edit'
						);
			$this->load->view('layout/wrapper', $data, FALSE);	
		//jika validasi oke, masuk database
		}else{
			$inp  = $this->input;
			//check panjang password, jika lebih 6 karakter maka password diganti
			//jika password lebih dari 32 maka password tidak diganti 
			if(strlen($inp->post('password')) >= 6 || strlen($inp->post('password')) <= 32) {
				//password diganti
				$data = array(	'id'			=> $id,
								'nama'			=> $inp->post('nama'),
								'email'			=> $inp->post('email'),						
								'password'		=> MD5($inp->post('password')),
								'level'			=> $inp->post('level'),
						);
			}else{
				//jika kurang dari 6 atau lebih dari 32 maka tidak diganti
				$data = array(	'id'			=> $id,
								'nama'			=> $inp->post('nama'),
								'email'			=> $inp->post('email'),					
								'level'			=> $inp->post('level'),
						);
			}			
			//proses oleh model
			$this->user_model->edit($data);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Data user '.$user->nama.' telah diedit');
			redirect(base_url('user'),'refresh');
		}
		//end masuk database
	}

	//Delete user
	public function delete($id)
	{
		$data = array('id' => $id);
		//proses hapus
		$this->user_model->delete($data);
		//notifikasi
		$this->session->set_flashdata('sukses', 'Data user telah dihapus');
		redirect(base_url('user'),'refresh');
	}


}

/* End of file User.php */
/* Location: ./application/controllers/User.php */