<?php

class Blog extends CI_Controller {

        var $module = 'blog';
    public function __construct()
    {
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it

        $this->load->database();
        $this->load->helper('language'); 
        $this->load->library('session'); 
        $this->load->model('Security','security_model');
        $this->load->model('Blog_Model','blog_model'); 
        $this->load->model('Language','language_model');  
         //require_once(APPPATH.'controllers/admin/Security.php'); //include controller
            //$security = new Security();  //create object 
            //$security->permission(); //call function    

        //check user have login or not
        $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //end check user have login or not
    }
    function index()
    {
        
        $session_id = $this->security_model->getcode();
        //$sess = $this->security_model->admin($session_id);
        //$property = $this->Property->get();
        

        //check user have login or not
        // $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //end check user have login or not
        
        //$data['sess'] = $sess;  
        //$data['property'] = $property;
        $data['module'] = $this->module;
        $data['blog_content'] = $this->blog_model->get();
        $data['selectedmenu']="question_submit";



        $this->load->view('admin/blog_view.php', $data); 
    
    }
    
    function edit()
    {

        // echo "1";
        //$session_id = $this->security_model->getcode();
        //$this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //$property = $this->Property->get();
        
        //echo $this->input->post('save');

        if($this->input->post('save')){
            //echo "post save";
            $id = $this->input->post('id');

            $passtest=true;
            if(is_uploaded_file($_FILES['blog_image']['tmp_name'])){
                $result=$this->uploadfile('blog_image');
            //     $result['message']="";
            // $result['url']="";
            // $result['status']="";
                // $arr['partcimage'] =$url;
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['blog_image'] =$result['url'];
                    if(!empty($this->input->post('blog_imageolddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('blog_imageolddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['blog_image'] =$this->input->post('blog_imageolddata');
            }


            
            $arr['blog_video'] = $this->input->post('blog_video');
            // $arr['blog_image'] = $this->input->post('blog_image');
            $arr['blog_title'] = $this->input->post('blog_title');
            $arr['blog_short_text'] = $this->input->post('blog_short_text');
            $arr['blog_long_text'] = $this->input->post('blog_long_text');
            $arr['blog_date'] = $this->input->post('blog_date');
            $arr['facebooklink'] = $this->input->post('facebooklink');

            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');
            
            if($id == false){
                $arr['entered'] = date('Y-m-d H:i:s');
                $id = $this->blog_model->insertForm($arr);
                header('location:'.base_url().'admin/blog/edit?save=1&id='.$id);
                exit;
            }else{
                $this->blog_model->update($id, $arr);
                header('location:'.base_url().'admin/blog/edit?save=1&id='.$id);
                exit;
            }
        }
        
        $id = $this->input->get('id');
        
        $data['languages'] = $this->language_model->get_all();
        //$data['sess'] = $sess;  
        //$data['sess'] = $session_id;  
        //$data['property'] = $property;
        //$data['module'] = $this->module;

        $data['blog_content'] = $this->blog_model->get_id($id);
        



        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="blog";    
        $this->load->view('admin/blog_edit_view.php', $data);    
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->blog_model->delete($id);
       echo 'Deleted successfully.';
    }
    function uploadfile($filename){
        define ("MAX_SIZE","3000");
        $result=array();
        $result['message']="";
        $result['url']="";
        $result['status']="";

        //upload code
         //handle upload file
            $status="success";


            $image =$_FILES[$filename]["name"];
            $uploadedfile = $_FILES[$filename]['tmp_name'];

            if ($image){

                $filename = stripslashes($_FILES[$filename]['name']);

                $extension = $this->getExtension($filename);
                $extension = strtolower($extension);


                 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
                    {
                        // echo "Unknown Extension..!";
                        $status="fail";
                        $message="Unknown Extension..!";
                    }
                    else{
                        $size=filesize($uploadedfile);
                        if ($size > MAX_SIZE*1024)
                        {
                            // echo "File Size Excedeed..!!";
                            $status="fail";
                            $message="File Size Excedeed..!!";
                        }

                        if($extension=="jpg" || $extension=="jpeg" )
                        {
                        //$uploadedfile = $_FILES[$filename]['tmp_name'];
                        $src = imagecreatefromjpeg($uploadedfile);

                        }
                        else if($extension=="png")
                        {
                        //$uploadedfile = $_FILES[$filename]['tmp_name'];
                        $src = imagecreatefrompng($uploadedfile);

                        }
                        else 
                        {
                        $src = imagecreatefromgif($uploadedfile);
                        // echo $scr;
                        }

                        list($width,$height)=getimagesize($uploadedfile);

                        if($width>1000){ //if width >1000 then resize it
                            $newwidth=1000;
                            $newheight=($height/$width)*$newwidth;
                        }
                        else{
                            $newwidth=$width;
                            $newheight=$height;
                        }
                        
                        $tmp=imagecreatetruecolor($newwidth,$newheight);


                        // $newwidth1=1000;
                        // $newheight1=($height/$width)*$newwidth1;
                        // $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

                        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

                        // $filename = "../images/product-image/Cars/". $_FILES['image-1']['name'];
                        $newName = date('YmdHis').rand(1000, 9999); 
                        $filename = $_SERVER['DOCUMENT_ROOT']."/assets/images/uploads/". $newName.".".$extension;
                        imagejpeg($tmp,$filename,100);

                        imagedestroy($src);
                        imagedestroy($tmp);
                        $url='assets/images/uploads/'. $newName.".".$extension;
                        $message ="successful upload";
                    }
            }
                        // imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

            $result['message']=$message;
            $result['url']=$url;
            $result['status']=$status;

            return $result;
    }

    private function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
    }
    
}

?>