<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
	public function index(){
		redirect('user/login');
	}

	public function login(){
		if (isset($_POST['submit'])){
			$username = $this->input->post('a');
			$password = hash("sha512", md5($this->input->post('b')));
			$cek = $this->model_app->cek_login_users($username,$password,'users');
		    $row = $cek->row_array();
		    $total = $cek->num_rows();
			if ($total > 0){
				$this->session->set_userdata('upload_image_file_manager',true);
				$this->session->set_userdata(array('username'=>$row['username'],
								   'level'=>$row['level'],
                                   'id_session'=>$row['id_session']));
				redirect('user/profile');
			}else{
				echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center>Maaf, Username dan Password Salah!!</center></div>');
				redirect('user/login');
			}
		}else{
			$data['title'] = "Login members";
			$data['description'] = description();
			$data['keywords'] = keywords();
			$this->template->load(template().'/template',template().'/login',$data);
		}
	}

	function pendaftaran(){
		if (isset($_POST['submit'])){
			if ($this->input->post() && (strtolower($this->input->post('secutity_code')) == strtolower($this->session->userdata('mycaptcha')))) {
				$cek = $this->db->query("SELECT * FROM users where username='".cetak($this->input->post('a'))."' OR email='".cetak($this->input->post('d'))."'");
				if ($cek->num_rows()>=1){
					echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center><b>GAGAL</b> - Maaf Username atau Email Sudah terdaftar di system!!</center></div>');
					redirect('user/pendaftaran');

				}elseif($this->input->post('b')!=$this->input->post('bb')){
					echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center><b>GAGAL</b> - Maaf Password dan Confirm Password Harus sama!</center></div>');
						redirect('user/pendaftaran');
				}else{
					$config['upload_path'] = 'asset/foto_user/';
		            $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
		            $config['max_size'] = '10000'; // kb
		            $this->load->library('upload', $config);
		            $this->upload->do_upload('f');
		            $hasil=$this->upload->data();
			            $nama_lengkap = $this->input->post('c').' '.$this->input->post('cc');
			            if ($hasil['file_name']==''){
			                $data = array('username'=>cetak($this->input->post('a')),
		                                    'password'=>hash("sha512", md5(cetak($this->input->post('b')))),
		                                    'nama_lengkap'=>cetak($nama_lengkap),
		                                    'email'=>cetak($this->input->post('d')),
		                                    'no_telp'=>cetak($this->input->post('e')),
		                                    'jenis_kelamin'=>cetak($this->input->post('kelamin')),
		                                    'alamat_lengkap'=>cetak($this->input->post('alamat')),
		                                    'tempat_lahir'=>cetak($this->input->post('tempat_lahir')),
		                                    'tanggal_lahir'=>cetak($this->input->post('tanggal_lahir')),
		                                    'status_kawin'=>cetak($this->input->post('status')),
		                                    'agama'=>cetak($this->input->post('agama')),
		                                    'perangkat_daerah'=>cetak($this->input->post('perangkat_daerah')),
		                                    'level'=>'inovator',
		                                    'blokir'=>'Y',
		                                    'id_session'=>md5(cetak($this->input->post('a'))).'-'.date('YmdHis'));
			            }else{
		                    $data = array('username'=>cetak($this->input->post('a')),
		                                    'password'=>hash("sha512", md5(cetak($this->input->post('b')))),
		                                    'nama_lengkap'=>cetak($nama_lengkap),
		                                    'email'=>cetak($this->input->post('d')),
		                                    'no_telp'=>cetak($this->input->post('e')),
		                                    'jenis_kelamin'=>cetak($this->input->post('kelamin')),
		                                    'alamat_lengkap'=>cetak($this->input->post('alamat')),
		                                    'tempat_lahir'=>cetak($this->input->post('tempat_lahir')),
		                                    'tanggal_lahir'=>cetak($this->input->post('tanggal_lahir')),
		                                    'status_kawin'=>cetak($this->input->post('status')),
		                                    'agama'=>cetak($this->input->post('agama')),
		                                    'perangkat_daerah'=>cetak($this->input->post('perangkat_daerah')),
		                                    'foto'=>$hasil['file_name'],
		                                    'level'=>'inovator',
		                                    'blokir'=>'Y',
		                                    'id_session'=>md5(cetak($this->input->post('a'))).'-'.date('YmdHis'));
			            }

			            $this->model_app->insert('users',$data);
			            $datam = array('id_session'=>md5(cetak($this->input->post('a'))).'-'.date('YmdHis'),
			                           'id_modul'=>18);
			            $this->model_app->insert('users_modul',$datam);
			            $this->session->set_userdata('upload_image_file_manager',true);
						$this->session->set_userdata(array('username'=>cetak($this->input->post('a')),
										   'level'=>'inovator',
		                                   'id_session'=>md5(cetak($this->input->post('a'))).'-'.date('YmdHis')));
					redirect('user/profile');
				}
			}else{
				echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center><b>GAGAL</b> - Kode Keamanan yang anda masukkan salah!!!</center></div>');
					redirect('user/pendaftaran');
			}
		}else{
			$data['title'] = "Registrasi / Pendaftaran Users";
			$data['description'] = description();
			$data['keywords'] = keywords();

			$this->load->helper('captcha');
			$vals = array(
	            'img_path'	 => './captcha/',
	            'img_url'	 => base_url().'captcha/',
	            'font_path' => './asset/Tahoma.ttf',
	            'font_size'     => 14,
	                'img_width'	 => '90',
	                'img_height' => 32,
	            'border' => 0, 
	            'word_length'   => 5,
	            'expiration' => 7200
	        );

	        $cap = create_captcha($vals);
	        $data['image'] = $cap['image'];
	        $this->session->set_userdata('mycaptcha', $cap['word']);
			$this->template->load(template().'/template',template().'/register',$data);
		}
	}

	public function profile(){
		cek_session_user();
		$profile = $this->db->query("SELECT * FROM users where username='".$this->session->username."'")->row_array();
		$data['title'] = $profile['nama_lengkap'];
		$data['description'] = description();
		$data['keywords'] = keywords();
		$data['row'] = $this->db->query("SELECT * FROM users where username='".$this->session->username."'")->row_array();
		$this->template->load(template().'/template',template().'/profile',$data);
	}

	public function edit_profile(){
		cek_session_user();
		if (isset($_POST['submit'])){
			if ($this->input->post() && (strtolower($this->input->post('secutity_code')) == strtolower($this->session->userdata('mycaptcha')))) {
				$nama_lengkap = $this->input->post('c').' '.$this->input->post('cc');
	            if ($this->input->post('b') ==''){
	                $data = array('username'=>$this->db->escape_str($this->input->post('a')),
	                                'nama_lengkap'=>$nama_lengkap,
	                                'email'=>$this->db->escape_str($this->input->post('d')),
	                                'no_telp'=>$this->db->escape_str($this->input->post('e')),
	                                'jenis_kelamin'=>$this->db->escape_str($this->input->post('kelamin')),
	                                'alamat_lengkap'=>$this->db->escape_str($this->input->post('alamat')),
	                                'tempat_lahir'=>cetak($this->input->post('tempat_lahir')),
                                    'tanggal_lahir'=>cetak($this->input->post('tanggal_lahir')),
                                    'status_kawin'=>cetak($this->input->post('status')),
                                    'agama'=>cetak($this->input->post('agama')),
                                    'perangkat_daerah'=>cetak($this->input->post('perangkat_daerah')));
	            }else{
	                $data = array('username'=>$this->db->escape_str($this->input->post('a')),
	                                'password'=>hash("sha512", md5($this->input->post('b'))),
	                                'nama_lengkap'=>$nama_lengkap,
	                                'email'=>$this->db->escape_str($this->input->post('d')),
	                                'no_telp'=>$this->db->escape_str($this->input->post('e')),
	                                'foto'=>$hasil['file_name'],
	                                'jenis_kelamin'=>$this->db->escape_str($this->input->post('kelamin')),
	                                'alamat_lengkap'=>$this->db->escape_str($this->input->post('alamat')),
	                                'tempat_lahir'=>cetak($this->input->post('tempat_lahir')),
                                    'tanggal_lahir'=>cetak($this->input->post('tanggal_lahir')),
                                    'status_kawin'=>cetak($this->input->post('status')),
                                    'agama'=>cetak($this->input->post('agama')),
                                    'perangkat_daerah'=>cetak($this->input->post('perangkat_daerah')));
	            }
	            $where = array('username' => $this->session->username);
	            $this->model_app->update('users', $data, $where);
	        }else{
	        	echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center>Gagal, Terjadi Kesalahan dengan Security Code!!</center></div>');
	        }
            redirect('user/profile');
		}else{
			$profile = $this->db->query("SELECT * FROM users where username='".$this->session->username."'")->row_array();
			$data['title'] = "Edit Profile";
			$data['description'] = description();
			$data['keywords'] = keywords();

			$this->load->helper('captcha');
			$vals = array(
	            'img_path'	 => './captcha/',
	            'img_url'	 => base_url().'captcha/',
	            'font_path' => './asset/Tahoma.ttf',
	            'font_size'     => 14,
	                'img_width'	 => '90',
	                'img_height' => 32,
	            'border' => 0, 
	            'word_length'   => 5,
	            'expiration' => 7200
	        );

	        $cap = create_captcha($vals);
	        $data['image'] = $cap['image'];
	        $this->session->set_userdata('mycaptcha', $cap['word']);
			$data['row'] = $this->db->query("SELECT * FROM users where username='".$this->session->username."'")->row_array();
			$this->template->load(template().'/template',template().'/profile_edit',$data);
		}
	}

	function foto(){
		cek_session_user();
		if (isset($_POST['submit'])){
	        $config['upload_path'] = 'asset/foto_user/';
            $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG';
            $config['max_size'] = '3000'; // kb
            $this->load->library('upload', $config);
            $this->upload->do_upload('f');
            $hasil=$this->upload->data();
            if ($hasil['file_name']!=''){
            	$data = array('foto'=>$hasil['file_name']);
            	$where = array('username'=>$this->session->username);
            	$this->model_app->update('users', $data, $where);
	        }
			redirect('user/profile');
		}else{
			redirect('user/profile');
		}
	}

	function konsultasi(){
		cek_session_user();
		$data['title'] = "List/Data Konsultasi";
		$data['description'] = description();
		$data['keywords'] = keywords();
		$this->template->load(template().'/template',template().'/konsultasi',$data);
	}

	function konsultasi_tambah(){
		cek_session_user();
		$id = $this->uri->segment(3);
		if (isset($_POST['submit'])){
			if ($this->input->post() && (strtolower($this->input->post('secutity_code')) == strtolower($this->session->userdata('mycaptcha')))) {
                $data = array('id_kategori_konsul'=>$this->db->escape_str($this->input->post('a')),
                                'username'=>$this->session->username,
                                'judul'=>$this->db->escape_str($this->input->post('b')),
                                'judul_seo'=>seo_title($this->input->post('b')),
                                'isi_konsul'=>$this->input->post('h'),
                                'hari'=>hari_ini(date('w')),
                                'tanggal'=>date('Y-m-d'),
                                'jam'=>date('H:i:s'),
                                'status'=>'Y');
	            $this->model_app->insert('konsul',$data);
	            echo $this->session->set_flashdata('message', '<div class="alert alert-success"><center>Success - Data Konsultasi anda sukses terkirim!!</center></div>');
	        }else{
	        	echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center>Gagal, Terjadi Kesalahan dengan Security Code!!</center></div>');
	        }
			redirect('user/konsultasi');
		}else{
			$data['title'] = "Tambah List/Data konsultasi";
			$data['description'] = description();
			$data['keywords'] = keywords();

			$this->load->helper('captcha');
			$vals = array(
	            'img_path'	 => './captcha/',
	            'img_url'	 => base_url().'captcha/',
	            'font_path' => './asset/Tahoma.ttf',
	            'font_size'     => 14,
	            'img_width'	 => '90',
	            'img_height' => 32,
	            'border' => 0, 
	            'word_length'   => 5,
	            'expiration' => 7200
	        );

	        $cap = create_captcha($vals);
	        $data['image'] = $cap['image'];
	        $this->session->set_userdata('mycaptcha', $cap['word']);
			$data['kategori'] = $this->model_app->view_ordering('kategori_konsul','id_kategori_konsul','ASC');
			$this->template->load(template().'/template',template().'/konsultasi_tambah',$data);
		}
	}

	function konsultasi_edit(){
		cek_session_user();
		$id = $this->uri->segment(3);
		if (isset($_POST['submit'])){
			if ($this->input->post() && (strtolower($this->input->post('secutity_code')) == strtolower($this->session->userdata('mycaptcha')))) {
	                $data = array('id_kategori_konsul'=>$this->db->escape_str($this->input->post('a')),
                                'username'=>$this->session->username,
                                'judul'=>$this->db->escape_str($this->input->post('b')),
                                'judul_seo'=>seo_title($this->input->post('b')),
                                'isi_konsul'=>$this->input->post('h'));
	            $where = array('id_konsul' => $this->input->post('id'),'username'=>$this->session->username);
				$this->model_app->update('konsul', $data, $where);
	            echo $this->session->set_flashdata('message', '<div class="alert alert-success"><center>Success - Data Konsultasi anda sukses disimpan!!</center></div>');
	        }else{
	        	echo $this->session->set_flashdata('message', '<div class="alert alert-danger"><center>Gagal, Terjadi Kesalahan dengan Security Code!!</center></div>');
	        }
			redirect('user/konsultasi');
		}else{
			$data['title'] = "Edit List/Data konsultasi";
			$data['description'] = description();
			$data['keywords'] = keywords();

			$this->load->helper('captcha');
			$vals = array(
	            'img_path'	 => './captcha/',
	            'img_url'	 => base_url().'captcha/',
	            'font_path' => './asset/Tahoma.ttf',
	            'font_size'     => 14,
	            'img_width'	 => '90',
	            'img_height' => 32,
	            'border' => 0, 
	            'word_length'   => 5,
	            'expiration' => 7200
	        );

	        $cap = create_captcha($vals);
	        $data['image'] = $cap['image'];
	        $this->session->set_userdata('mycaptcha', $cap['word']);
			$data['kategori'] = $this->model_app->view_ordering('kategori_konsul','id_kategori_konsul','ASC');
	        $data['rows'] = $this->model_app->edit('konsul', array('id_konsul' => $id))->row_array();
			$this->template->load(template().'/template',template().'/konsultasi_edit',$data);
		}
	}

	function konsultasi_delete(){
        cek_session_user();
		$id = array('id_konsul' => $this->uri->segment(3),'username'=>$this->session->username);
		$this->model_app->delete('konsul',$id);
		redirect('user/konsultasi');
	}

	private function set_upload_options(){
        $config = array();
        $config['upload_path'] = 'asset/files_inovasi/';
        $config['allowed_types'] = 'gif|jpg|png|zip|rar|pdf|doc|docx|ppt|pptx|xls|xlsx|txt|jpeg';
		$config['max_size']	= '30000'; // kb
		$config['encrypt_name'] = FALSE;
        $this->load->library('upload', $config);
	  return $config;
	}

	function logout(){
		$this->session->sess_destroy();
		redirect('main');
	}
}
