<?php

CLASS BasicPassword extends RasioProject
{
	protected $set;
	protected $enableCk = true;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'User';
		$this->set->dBaseH 		= 'sub';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kd_sub');
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("passlama", "passbaru", 'confpass');
		$this->set->button 		= array("Save", "Edit");
		$this->set->addClear 	= true;
		$this->set->funct 		= '_password';
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "Password Lama", "width" => 125), "input" => array("type" => 'password',	'attr' => array("name" => 'passlama', "size" => 25, "class" => 'text _password')));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Password Baru", "width" => 125), "input" => array("type" => 'password',	'attr' => array("name" => 'passbaru', "size" => 25, "class" => 'text _password')));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Conf Password Baru", "width" => 125), "input" => array("type" => 'password',	'attr' => array("name" => 'confpass', 'size' => 25, "class" => 'text _password')));
		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "&nbsp", "width" => 125), "input" => array("type" => 'text',	'attr' => array("name" => 'username', 'value' => $this->login['username'], 'size' => 25, "readonly" => true)));
		return $form;
	}
	
	
	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/index";
	}
	
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set, false);
		
		if(md5($_POST['passbaru']) != md5($_POST['confpass'])){
			echo "<script>$('#_displayErrorPopUp').html('Password Baru dan Conf Password harus sama')</script>";
			$error[] = false;
		}
		if(strlen($_POST['passbaru']) < 5){
			echo "<script>$('#_displayErrorPopUp').html('Password Baru min 5 Character')</script>";
			$error[] = false;
		}
		if(md5($_POST['passlama']) != $this->login['passwrod']){
			echo "<script>$('#_displayErrorPopUp').html('Password Lama salah')</script>";
			$error[] = false;
		}
		
		return $basic->getError($error);
	}
	
	public function proses()
	{
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			$basic = new Basic;
			$sql = $this->em->addUpdate($this->set->dBaseH);
			$sql = $this->em->addAttrUp(array("pass" => md5($_POST['passbaru'])));
			$sql = $this->em->getWhere("kd_sub = '".$this->login['kd_user']."'");

			$qb = array();
			$qb[] = $sql;
			$qb[] = $basic->getRecord($_POST, $this->set, array(), $qb);
			$save = $this->em->getFlush($qb);
			
			if($save != false){
				echo "<script>";
				echo "alert('Proses Sukses, Silahkan Login Kembali');";
				echo "window.location = '".$this->baseUrl."login/logout'";
				echo "</script>";
			}else{
				echo "<script>$('#_displayErrorPopUp').html('Error! Silahkan Coba Lagi')</script>";
			}
			
		}
		$this->layout = "null";
	}
}