<?php 
class Drainase extends Controller {

    function Drainase()
    {
    	parent::Controller();	
    	$this->load->helper('url');
    	$this->load->library('session');
	}
    
    function index()
    {
    	echo base_url();
    	$url = parse_url($_SERVER['REQUEST_URI']);
		parse_str($url['query'], $params);
    	echo $params['a'];
    }

	function upload_saluran_ckeditor(){
        $upload_gambar = $this->upload_image_file('upload');
        if(isset($upload_gambar['data']['upload_data']['file_name']) && trim($upload_gambar['data']['upload_data']['file_name']) != '') {
            $funcNum = $this->uri->segment('4');
            $CKEditor = $this->uri->segment('3');
            $langCode =$this->uri->segment('5');
 
        // Check the $_FILES array and save the file. Assign the correct path to some variable ($url).
        	$this->load->model('Admin_model');
    		$adres = $this->Admin_model->get_user_pagename($this->session->userdata('uzytkownik_id'));
            $url = prep_url($adres.'/foto/'.$upload_gambar['data']['upload_data']['file_name']);
            //echo '<script type="text/javascript">alert(123'.base_url().')</script>';
            //echo '<script type="text/javascript">alert("'.$url.'")</script>';
        // Usually you will assign here something only if file could not be uploaded.
        $message = '';
         
        //echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(2, '$url');</script>";
        $url_get = parse_url($_SERVER['REQUEST_URI']);
		parse_str($url_get['query'], $params);
		echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(".$params['CKEditorFuncNum'].", '$url');</script>";
        //echo $params['CKEditorFuncNum'];
        } else {
        	
            echo $upload_gambar['message']['error'];
        }

    }
    
    function upload_image_file($fieldnames){
    	
    	ini_set("memory_limit","50M");
    	$this->load->model('Admin_model');
    	$folder = $this->Admin_model->get_user_folder($this->session->userdata('uzytkownik_id'));
    	
    	$image_path = "./pages/".$folder."/foto/";
        $config['upload_path'] = $image_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1000';
        $config['max_width']  = '4096';
        $config['max_height']  = '3072';
        
        $this->load->library('upload', $config);
        $result['sukses'] = 0;
        $result['message'] = 'gagal' ;
        $result['data'] = array();
        if ( ! $this->upload->do_upload($fieldnames))
        {
            $error = array('error' => $this->upload->display_errors());
            $result['message'] = $error ;
        }    
        else
        {
        	$plik = $this->upload->data();
        	
        	$config['maintain_ratio'] = TRUE;
			//$config['create_thumb'] = FALSE;
			//$config['thumb_marker'] = FALSE;
			$config['source_image'] = './pages/'.$folder.'/foto/'.$plik['file_name'];
			//$config['new_image'] = './pages/'.$folder.'/foto/'.$plik['file_name'];//'/path/to/new_image.jpg';
			$config['image_library'] = 'gd2';
			$config['width'] = 800;
			$config['height'] = 600;
			$this->load->library('image_lib', $config); 
			$this->image_lib->resize();
        	
            $data = array('upload_data' => $this->upload->data());
            $result['sukses'] = 1;
            $result['message'] = 'Upload Sukses' ;
            $result['data'] = $data;
        }
        return $result;
    }
}  