<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	
    public $template = array();
    public $is_auth = 0;
    public $userinfo = array();
    public $response;
    public $configApp;
	
    public $orderProvinsi = array(
                            'Aceh', 
                            'Sumatera Utara', 
                            'Bengkulu',
                            'Jambi',
                            'Riau',
                            'Sumatera Barat',
                            'Sumatera Selatan',
                            'Sumatera Utara',
                            'Lampung',
                            'Bangka Belitung',
                            'Kepulauan Riau',
                            'Banten',
                            'Jakarta',
                            'Jawa Barat',
                            'Jawa Tengah',
                            'Jawa Timur',
                            'Yogyakarta',
                            'Bali',
                            'Kalimantan Barat',
                            'Kalimantan Selatan',
                            'Kalimantan Tengah',
                            'Kalimantan Timur',
                            'Kalimantan Utara',
                            'Gorontalo',
                            'Nusa Tenggara Barat',
                            'Nusa Tenggara Timur',
                            'Sulawesi Barat',
                            'Sulawesi Selatan',
                            'Sulawesi Tengah',
                            'Sulawesi Tenggara',
                            'Sulawesi Utara',
                            'Maluku',
                            'Papua Barat',
                            'Papua'
                          );
		public $bulanarr = array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
	function __construct() {
        parent::__construct();
		
        $this->configApp = $this->config->item("common");
        $this->template['document_root'] = $_SERVER['DOCUMENT_ROOT'];
      	$this->response = $this->session->flashdata('response');

      	if ($this->session->userdata('userinfo')) {
        	$this->userinfo = $this->session->userdata('userinfo');
      		log_message('debug', 'Found User Info on session: '.print_r($this->userinfo, TRUE));
          	$this->template['userinfo'] = $this->userinfo;
      	}
    	
		if ($this->session->flashdata('response')) {
			$this->response = $this->session->flashdata('response');
			$this->template['response'] = $this->response;
		}

        $this->template['title'] = 'WGaY | Admin';
        $this->template['active'] = 'home';
        $this->template['bulanradio'] = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        $this->template['bulan'] = array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
        $this->template['hari'] = array("","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu","Minggu");
        $this->template['status'] = array('Tidak Aktif', 'Aktif');
		$this->template['akses'] = array('1'=>'Admin','2'=>'Marketing','3'=>'Apoteker');
		$this->template['kategori'] = array('1'=>'Umum','2'=>'Mahasiswa');
		$this->template['topik'] = array('1'=>'Manfaat Kawasan Konservasi Perairan','2'=>'Strategi Pengelolaan Kawasan Konservasi Perairan','3'=>'Status dan Ancaman terhadap Ekosistem Pesisir');
		$this->template['topikpeserta'] = array('1'=>'Biologi, Populasi dan Ekologi','2'=>'Sosial, Ekonomi dan Kelembagaan','3'=>'Pengelolaan dan Konservasi');
		$this->template['presentasi_oral'] = array('1'=>'Presentasi Oral','2'=>'Presentasi Poster');
        $this->template['lunas'] = array('Belum Lunas', 'Lunas');
	}

    public function render_popup($view){
        $this->load->view('header_popup', $this->template);
        $this->load->view($view, $this->template);
        $this->load->view('footer_popup', $this->template);
	}
    public function render($view){
        $this->load->view('header', $this->template);
        $this->load->view($view, $this->template);
        $this->load->view('footer', $this->template);
    }
	public function money_format($nilai,$koma="0"){
		$form = number_format($nilai,$koma,"",".");
		return $form.",-";
	}
    function reArrayFiles(&$file_post) {
        $file_ary = array();
        $file_count = count($file_post['name']);
        $file_keys = array_keys($file_post);

        for ($i=0; $i<$file_count; $i++) {
          foreach ($file_keys as $key) {
              $file_ary[$i][$key] = $file_post[$key][$i];
          }
        }

        return $file_ary;
    }
    public function upload_foto($file,$folder,$thumb=FALSE,$thumb_width){
    	$file_image = "";

    	//$upload_dir = $this->config->item('upload_path');
    	$upload_dir = "uploads/";
		
		$FILE_MIMES = array('image/jpeg','image/jpg','image/png','image/x-icon');
		$FILE_EXTS  = array('.jpeg','.jpg','.png'.'.ico');
		
		if (isset($file) && $file['name'] != "") {
			$file_type 	= $file['type']; 
			$file_image = $file['name'];
			$file_size 	= $file['size'];
			$file_ext 	= strtolower(substr($file_image,strrpos($file_image,".")));

			$temp_name 	= $file['tmp_name'];
			$file_image = str_replace("\\","",$file_image);
			$file_image = str_replace("'","",$file_image);

            $file_edit = str_replace(" ","_",$file_image);
            $random_digit=rand(0000,9999);
            $file_image = $folder.'_'.$random_digit."_".$file_edit;

			$file_path 	= $upload_dir.$folder.'/'.$file_image;

			if (!in_array($file_type, $FILE_MIMES) && !in_array($file_ext, $FILE_EXTS) ) {
				die(json_encode(array('status'=>'error', 'message' => "Sorry, $file_image($file_type) is not allowed to be uploaded")));
			}
			$result = move_uploaded_file($temp_name, $file_path);

			if ($result) {
				if($thumb==TRUE){
					$thumb_path = $upload_dir.$folder.'/thumb/';
					$thumbnail = $thumb_path.$file_image;
					$upload_image = $file_path;
					if($thumb_width==""){
						$thumb_width = 300;
					}else{
						$thumb_width = $thumb_width;
					}
					$info = getimagesize($upload_image);
					$aspectRatio = $info[1] / $info[0];
					$thumb_height = (int)($aspectRatio * $thumb_width) . "px";
					$thumb_width .= "px";  

					list($width,$height) = getimagesize($upload_image);
					$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
					switch($file_ext){
						case '.jpg':
							$source = imagecreatefromjpeg($upload_image);
							break;
						case '.jpeg':
							$source = imagecreatefromjpeg($upload_image);
							break;

						case '.png':
							$source = imagecreatefrompng($upload_image);
							break;
						case '.gif':
							$source = imagecreatefromgif($upload_image);
							break;
						default:
							$source = imagecreatefromjpeg($upload_image);
					}
					//$source = imagecreatefrompng($upload_image);
					imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
					switch($file_ext){
						case '.jpg' || '.jpeg':
							imagejpeg($thumb_create,$thumbnail,100);
							break;
						case '.png':
							imagepng($thumb_create,$thumbnail,100);
							break;

						case '.gif':
							imagegif($thumb_create,$thumbnail,100);
							break;
						default:
							imagejpeg($thumb_create,$thumbnail,100);
						break;
					}				
				}
				return $file_image;
			}
			else{
				return "";
			}
		}
    }	
}
require_once(APPPATH.'core/AdminController.php');
