<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Konsultasi extends CI_Controller {
	public function detail(){
		$query = $this->model_utama->view_join_two('konsul','users','kategori_konsul','username','id_kategori_konsul',array('konsul.judul_seo' => $this->uri->segment(3),'konsul.status'=>'Y'),'id_konsul','DESC',0,1);
		if ($query->num_rows()<=0){
			redirect('main');
		}else{
			$row = $query->row_array();
			if ($this->session->username==$row['username'] OR $this->session->level=='admin' OR $this->session->level=='user'){
				$data['title'] = cetak($row['judul']);
				$data['description'] = cetak_meta($row['isi_konsul'],0,500);
				$data['keywords'] = cetak($row['judul']);
				$data['rows'] = $row;

				$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' => 26,
	                '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().'/detailkonsul',$data);
			}else{
				redirect('user/konsultasi');
			}
		}
	}

	function kirim_komentar(){
		if (isset($_POST['submit'])){
			$cek = $this->model_utama->view_where('konsul',array('id_konsul' => $this->input->post('a')));
			$row = $cek->row_array();
			if ($cek->num_rows()<=0){
				redirect('main');
			}else{
				if ($this->input->post() && (strtolower($this->input->post('secutity_code')) == strtolower($this->session->userdata('mycaptcha')))) {
					$cek = $this->db->query("SELECT * FROM users where username='".$this->session->username."'")->row_array();
					if ($cek['level']=='inovator'){ $level = ''; }elseif ($cek['level']=='admin'){ $level = 'Admin'; }else{ $level = 'Psikolog'; }
					$data = array('id_konsul'=>cetak($this->input->post('a')),
		                            'nama_komentar'=>$cek['nama_lengkap'].' ('.$level.')',
		                            'url'=>$cek['username'],
		                            'isi_komentar'=>cetak($this->input->post('d')),
		                            'tgl'=>date('Y-m-d'),
		                            'jam_komentar'=>date('H:i:s'),
		                            'aktif'=>'Y',
		                            'email'=>cetak($this->input->post('e')));
					$this->model_utama->insert('komentar_konsul',$data);
				}
			}
			redirect('konsultasi/detail/'.$row['judul_seo'].'#listcomment');

		}
	}
}
