<?php

  $title="Ganti Password";

//   $judul=$title;

  $url='gantipass';

  if ($session->get('level')=='User'){

    redirect(url('beranda'));

  }



if(isset($_POST['simpan'])){

 

  // cek validasi

  $validation=null;

  $username = $session->get('nm_pengguna');

  $oldpass = md5($_POST['oldpass']);

  $newpass1 = $_POST['newpass1'];

  $newpass2 = $_POST['newpass2'];

  // cek apakah oldpass sama

  $db->where('nm_pengguna',$_POST['username']);

  $row=$db->ObjectBuilder()->getOne('pengguna');

  

  if($oldpass!=$row->kt_sandi){

    $validation[]='Password lama salah!';

  }

  if($newpass1!=$newpass2){

    $validation[]='Password baru tidak sama!';

  }

  if($oldpass==''){

    $validation[]='Masukkan password lama';

  }

  if($newpass1==''){

    $validation[]='Password baru tidak boleh kosong';

  }

  if($newpass2==''){

    $validation[]='Ulangi Password baru tidak boleh kosong';

  }



  if($validation!=null){

    $setTemplate=false;

    $session->set('error_validation',$validation);

    $session->set('error_value',$_POST);

    redirect($_SERVER['HTTP_REFERER']);

    return false;

  }

  // cek validasi





  $data['kt_sandi']=md5($newpass1);

  $db->where('nm_pengguna',$username);

  $exec=$db->update("pengguna",$data);

  $info='<div class="alert alert-success alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Sukses!</h4> Password berhasil diubah </div>';





  if($exec){

    $session->set('info',$info);

  }

  else{

      $session->set("info",'<div class="alert alert-danger alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Error!</h4> Proses gagal dilakukan <br>'.$db->getLastError().'

              </div>');

  }

  redirect(url($url));

}

?>



<?=content_open('Form Ganti Password')?>

    <form method="post">

      <?php



        echo $session->pull("info");

        // menampilkan error validasi

        if($session->get('error_validation')){

          foreach ($session->pull('error_validation') as $key => $value) {

            echo '<div class="alert alert-danger">'.$value.'</div>';

          }

        }





        $username = $session->get('nm_pengguna');

        $oldpass="";

        $newpass1="";

        $newpass2="";

      ?>

      <?=input_hidden('username',$username)?>

      <div class="form-group">

        <label>Password Lama</label>

        <div class="row">

          <div class="col-md-6">

            <?=input_password('oldpass',$oldpass)?>

          </div>

        </div>

      </div>

      <div class="form-group">

        <label>Password Baru</label>

        <div class="row">

          <div class="col-md-6">

            <?=input_password('newpass1',$newpass1)?>

          </div>

        </div>

      </div>

      <div class="form-group">

        <label>Ulangi Password Baru</label>

        <div class="row">

          <div class="col-md-6">

            <?=input_password('newpass2',$newpass2)?>

          </div>

        </div>

      </div>

      <div class="form-group">

        <button type="submit" name="simpan" class="btn btn-info"><i class="fa fa-save"></i> Simpan</button>

      <a href="<?=url('beranda')?>" class="btn btn-danger" ><i class="fa fa-reply"></i> Kembali</a>

      </div>

    </form>

<?=content_close()?>



