<?php 
class Model_pengguna extends CI_model{
    function logo(){
        return $this->db->query("SELECT * FROM logo");
    }

    function logo_update(){
        $config['upload_path'] = 'asset/logo/';
        $config['allowed_types'] = 'gif|jpg|png|JPG';
        $config['max_size'] = '2000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('logo');
        $hasil=$this->upload->data();
            $datadb = array('gambar'=>$hasil['file_name']);
        $this->db->where('id_logo',$this->input->post('id'));
        $this->db->update('logo',$datadb);
    }

    function identitas(){
        return $this->db->query("SELECT * FROM identitas ORDER BY id_identitas DESC LIMIT 1");
    }
	 function sambutan_list(){
        return $this->db->query("SELECT * FROM tb_sambutan ORDER BY id DESC LIMIT 1");
    } 
	function sambutan(){
        return $this->db->query("SELECT * FROM tb_sambutan");
    }
	function bantuan(){
        return $this->db->query("SELECT * FROM tb_panduan ORDER BY id ASC"); 
    }
	function pengumuman_akademik(){
        return $this->db->query("SELECT * FROM pengumuman ORDER BY id_pengumuman ASC"); 
    }
	 function hitung_pengumuman_akademik(){
        return $this->db->query("SELECT * FROM pengumuman");
    }
	 function semua_pengumuman_akademik($start, $limit){
        return $this->db->query("SELECT * FROM pengumuman ORDER BY id_pengumuman DESC LIMIT $start,$limit");
    }
	 function hari(){
        return $this->db->query("SELECT * FROM tb_hari ORDER BY hari_id DESC");
    }
    function semua_berita_cari($start, $limit, $keyword){
        return $this->db->query("SELECT * FROM berita a JOIN users b on a.username=b.username where status='Y' AND judul LIKE '%$keyword%' ORDER BY id_berita DESC LIMIT $start,$limit");
    }
	function fakultas(){
        return $this->db->query("SELECT * FROM tb_fakultas ORDER BY fakultas_id DESC LIMIT 1");
    }
	function fakultas_update(){
					$datadb = array('nama_fakultas'=>$this->db->escape_str($this->input->post('a')),
						'aktif'=>$this->db->escape_str($this->input->post('b')),
						'diperbaharui_tanggal'=>date('Y-m-d H:i:s'),
						'diperbaharui_oleh'=>$this->session->username);
			 $this->db->where('fakultas_id',1);
            $this->db->update('tb_fakultas',$datadb);			
	 }
	 function recordekan(){
        return $this->db->query("SELECT * FROM msdos WHERE STDOSMSDOS = 'A' ORDER BY NMDOSMSDOS ASC");
    }
	 function jenjang_pendidikan(){
        return $this->db->query("SELECT * FROM tb_jenjang_pendidikan ORDER BY kode_jenjang ASC");
    }
	 function program_study(){
        return $this->db->query("SELECT * FROM mspst ORDER BY IDPSTMSPST DESC LIMIT 1");
    }
	function program_study_update(){
			//$exx = explode('-',$this->input->post('TGLSKMSPST'));
			//	$TGLSKMSPST = $exx[2].'-'.$exx[0].'-'.$exx[1];
			//$tgl2 = explode('-',$this->input->post('TGLAKMSPST'));
			//	$tgl_berlaku = $tgl2[2].'-'.$tgl2[0].'-'.$tgl2[1];
		
					$datadb = array('KDJENMSPST'=>$this->input->post('KDJENMSPST'),
						'fakultas_id'=>$this->input->post('fakultas_id'),
						'NMPSTMSPST'=>$this->input->post('NMPSTMSPST'),
						'nama_jurusan'=>$this->input->post('nama_jurusan'),
						'NOMSKMSPST'=>$this->db->escape_str($this->input->post('NOMSKMSPST')),
						'TGLSKMSPST'=>$this->input->post('TGLSKMSPST'),
						'TGLAKMSPST'=>$this->input->post('TGLAKMSPST'),
						'SKSTTMSPST'=>$this->db->escape_str($this->input->post('SKSTTMSPST')),
						'STATUMSPST'=>$this->db->escape_str($this->input->post('STATUMSPST')),
						'EMAILMSPST'=>$this->db->escape_str($this->input->post('EMAILMSPST')),
						'TGAWLMSPST'=>$this->db->escape_str($this->input->post('TGAWLMSPST')),
						'SMAWLMSPST'=>$this->db->escape_str($this->input->post('SMAWLMSPST')),
						'MLSEMMSPST'=>$this->db->escape_str($this->input->post('MLSEMMSPST')),
						'NOMBAMSPST'=>$this->db->escape_str($this->input->post('NOMBAMSPST')),
						'TGLBAMSPST'=>$this->db->escape_str($this->input->post('TGLBAMSPST')),
						'TGLABMSPST'=>$this->db->escape_str($this->input->post('TGLABMSPST')),
						'KDSTAMSPST'=>$this->db->escape_str($this->input->post('KDSTAMSPST')),
						'NOKPSMSPST'=>$this->db->escape_str($this->input->post('NOKPSMSPST')),
						'modified_date'=>date('Y-m-d H:i:s'),
						'modified_userid'=>$this->session->username);
			 $this->db->where('IDPSTMSPST',$this->input->post('id'));
            $this->db->update('mspst',$datadb);			
	 }
	function tautan(){
        return $this->db->query("SELECT * FROM identitas ORDER BY id_identitas DESC LIMIT 1");
    }
    function identitas_update(){
            $config['upload_path'] = 'asset/images/';
            $config['allowed_types'] = 'gif|jpg|png|ico';
            $config['max_size'] = '500'; // kb
            $this->load->library('upload', $config);
            $this->upload->do_upload('i');
            $hasil=$this->upload->data();
			$exxujian = explode('-',$this->input->post('tanggal_pendirian'));
			$tanggal_pendirian = $exxujian[2].'-'.$exxujian[1].'-'.$exxujian[0];
            if ($hasil['file_name']==''){
                    $datadb = array('nama_website'=>$this->db->escape_str($this->input->post('a')),
                                    'email'=>$this->db->escape_str($this->input->post('b')),
									'nama_yayasan'=>$this->db->escape_str($this->input->post('nama_yayasan')),
                                    'url'=>$this->db->escape_str($this->input->post('c')),
									'tanggal_pendirian'=>$tanggal_pendirian,
                                    'facebook'=>$this->db->escape_str($this->input->post('d')),
                                    'keterangan'=>$this->db->escape_str($this->input->post('ket')),
                                    'no_telp'=>$this->db->escape_str($this->input->post('f')),
                                    'meta_deskripsi'=>$this->db->escape_str($this->input->post('g')),
                                    'meta_keyword'=>$this->db->escape_str($this->input->post('h')),
                                    'maps'=>$this->db->escape_str($this->input->post('j')));
            }else{
                    $datadb = array('nama_website'=>$this->db->escape_str($this->input->post('a')),
                                    'email'=>$this->db->escape_str($this->input->post('b')),
										 'nama_yayasan'=>$this->db->escape_str($this->input->post('nama_yayasan')),
									  'kode_badan_hukum'=>$this->db->escape_str($this->input->post('kode_badan_hukum')),
									   'tanggal_pendirian'=>$tanggal_pendirian,
									    'url'=>$this->db->escape_str($this->input->post('c')),
                                    'facebook'=>$this->db->escape_str($this->input->post('d')),
                                    'keterangan'=>$this->db->escape_str($this->input->post('ket')),
                                    'no_telp'=>$this->db->escape_str($this->input->post('f')),
                                    'meta_deskripsi'=>$this->db->escape_str($this->input->post('g')),
                                    'meta_keyword'=>$this->db->escape_str($this->input->post('h')),
                                    'favicon'=>$hasil['file_name'],
                                    'maps'=>$this->db->escape_str($this->input->post('j')));
            }
            $this->db->where('id_identitas',1);
            $this->db->update('identitas',$datadb);
    }
	function visi_update(){
			$exx = explode('-',$this->input->post('tanggal_kuliah'));
			$tanggal_kuliah = $exx[2].'-'.$exx[1].'-'.$exx[0];
			$exxspp = explode('-',$this->input->post('tanggal_spp'));
			$tanggal_spp = $exxspp[2].'-'.$exxspp[1].'-'.$exxspp[0];
			$exxujian = explode('-',$this->input->post('tanggal_ujian'));
			$tanggal_ujian = $exxujian[2].'-'.$exxujian[1].'-'.$exxujian[0];

           $datadb = array('mulai_kuliah'=>$tanggal_kuliah,
									'batas_spp'=>$tanggal_spp,
									'ujian_semester'=>$tanggal_ujian,
                                    'visi_misi'=>$this->input->post('visi'),
                                    'tujuan'=>$this->input->post('tujuan'));
            
            $this->db->where('id_identitas',1);
            $this->db->update('identitas',$datadb);
    }
    function pesan_masuk(){
        return $this->db->query("SELECT * FROM hubungi ORDER BY id_hubungi DESC");
    }

    function pesan_baru($limit){
        return $this->db->query("SELECT * FROM hubungi ORDER BY id_hubungi DESC LIMIT $limit");
    }

    function pesan_masuk_view($id){
        return $this->db->query("SELECT * FROM hubungi where id_hubungi='$id'");
    }

    function pesan_masuk_kirim(){
        $nama           = $this->input->post('a');
        $email           = $this->input->post('b');
        $subject         = $this->input->post('c');
        $message         = $this->input->post('isi')." <br><hr><br> ".$this->input->post('d');
        
        $this->email->from('robby.prihandaya@gmail.com', 'PHPMU.COM');
        $this->email->to($email);
        $this->email->cc('');
        $this->email->bcc('');

        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->set_mailtype("html");
        $this->email->send();
        
        $config['protocol'] = 'sendmail';
        $config['mailpath'] = '/usr/sbin/sendmail';
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $this->email->initialize($config);

    }

    function grafik_kunjungan(){
        return $this->db->query("SELECT count(*) as jumlah, tanggal FROM statistik GROUP BY tanggal ORDER BY tanggal DESC LIMIT 10");
    }

    function kunjungan(){
        $ip      = $_SERVER['REMOTE_ADDR'];
        $tanggal = date("Y-m-d");
        $waktu   = time(); 
        $cekk = $this->db->query("SELECT * FROM statistik WHERE ip='$ip' AND tanggal='$tanggal'");
        $rowh = $cekk->row_array();
        if($cekk->num_rows() == 0){
            $datadb = array('ip'=>$ip, 'tanggal'=>$tanggal, 'hits'=>'1', 'online'=>$waktu);
            $this->db->insert('statistik',$datadb);
        }else{
            $hitss = $rowh['hits'] + 1;
            $datadb = array('ip'=>$ip, 'tanggal'=>$tanggal, 'hits'=>$hitss, 'online'=>$waktu);
            $array = array('ip' => $ip, 'tanggal' => $tanggal);
            $this->db->where($array);
            $this->db->update('statistik',$datadb);
        }
    }

    function kirim_Pesan(){
        if ($this->input->post('cek')==''){
            $nama       = strip_tags($this->input->post('a'));
            $email      = strip_tags($this->input->post('b'));
            $subjek     = strip_tags($this->input->post('c'));
            $pesan      = strip_tags($this->input->post('d'));
                $datadb = array('nama'=>$nama,
                                'email'=>$email,
                                'subjek'=>$subjek,
                                'pesan'=>$pesan,
                                'tanggal'=>date('Y-m-d'),
                                'jam'=>date('H:i:s'),
                                'dibaca'=>'N');
            $this->db->insert('hubungi',$datadb);
        }
    }
	function iklan(){
        return $this->db->query("SELECT * FROM iklan");
    }

    function iklan_update(){
        $config['upload_path'] = 'asset/img_iklan/';
        $config['allowed_types'] = 'gif|jpg|png|JPG';
        $config['max_size'] = '2000'; // kb
        $this->load->library('upload', $config);
        $this->upload->do_upload('logo');
        $hasil=$this->upload->data();
		 if ($hasil['file_name']==''){
			 $datadb = array('aktif'=>$this->input->post('e'));
		}else{
				$datadb = array('gambar'=>$hasil['file_name'],'aktif'=>$this->input->post('e'));
		}
        $this->db->where('id_iklan',$this->input->post('id'));
        $this->db->update('iklan',$datadb);
    }
	public function pengunjung(){
		$ip = $_SERVER['REMOTE_ADDR'];
		$tanggal = date("Ymd");
        $waktu   = time();
		
		$text = "SELECT * FROM statistik WHERE ip='$ip' AND tanggal='$tanggal'";
		$stat = $this->model_app->manualQuery($text);
		$row = $stat->num_rows();
		if($row>0){
			$this->model_app->manualQuery("UPDATE statistik SET hits=hits+1, online='$waktu' WHERE ip='$ip' AND tanggal='$tanggal'");
		}else{
			$this->model_app->manualQuery("INSERT INTO statistik(ip, tanggal, hits, online) VALUES('$ip','$tanggal','1','$waktu')");
		}
		//$hasil = $this->app_model->manualQuery("SELECT sum(hits) as total FROM statistik"); 
		$hasil = $this->db->query("SELECT COUNT(hits) as total FROM statistik");
		foreach($hasil->result() as $dt){
			$totalpengunjung=$dt->total;
		}
		
		$hasil = $totalpengunjung;
		/*
		$d["ip"] = $ip;
		$d["browser"] = $this->agent->browser();
		$d["os"] = $this->agent->platform();
		*/
		return $hasil;
	}
	public function pengunjung_hari_ini(){
		$tanggal = date("Ymd");
		$data       = $this->db->query("SELECT * FROM statistik WHERE tanggal='$tanggal' GROUP BY ip");
		$hasil = $data->num_rows();
		return $hasil;
	}
	public function online(){
		$bataswaktu       = time() - 300;
        $data = $this->db->query("SELECT * FROM statistik WHERE online > '$bataswaktu'");
		$hasil = $data->num_rows();
		return $hasil;
	}
	
	public function ip_address(){
		$ip = $_SERVER['REMOTE_ADDR'];
		return $ip;
	}
	function manualQuery($q)
	{
		return $this->db->query($q);
	}
	function sambutan_update(){
       
		$datadb = array('isi'=>$this->input->post('b'));
           
            $this->db->where('id',1);
            $this->db->update('tb_sambutan',$datadb);
    }
}