<?php

class Bloglist extends CI_Controller {

        var $module = 'bloglist';
    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_list_Model','blog_list_model'); 
        $this->load->model('Blog_tag_Model','blog_tag_model'); 
        $this->load->model('Blog_tag_hub_Model','blog_tag_hub_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_list_content'] = $this->blog_list_model->get();
        $data['selectedmenu']="question_submit";



        $this->load->view('admin/blog_list_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['cover_img_link']['tmp_name'])){
                $result=$this->uploadfile('cover_img_link');
            //     $result['message']="";
            // $result['url']="";
            // $result['status']="";
                // $arr['partcimage'] =$url;
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['cover_img_link'] =$result['url'];
                    if(!empty($this->input->post('blog_imageolddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('blog_imageolddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['cover_img_link'] =$this->input->post('blog_imageolddata');
            }


            
            $arr['display'] = $this->input->post('display');
            // $arr['blog_image'] = $this->input->post('blog_image');
            $arr['date'] = $this->input->post('date');
            $arr['title'] = $this->input->post('title');
            // $arr['select2-multiple'] = $this->input->post('select2-multiple');

            // echo "select2-multiple";
            // echo $this->input->post('select2-multiple');
            // $multiple=$this->input->post('select2-multiple');
            // var_dump($multiple);
            // foreach ($multiple as $value) {
            //     echo $value;
            // }
            $arr['cover_img_link_alttext']=$this->input->post('cover_img_link_alttext');
            // $arr['description'] = $this->input->post('description');
            $arr['content'] = $this->input->post('content');

            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');
            
            if($id == false){
                $arr['update_time'] = date('Y-m-d H:i:s');
                $id = $this->blog_list_model->insert($arr);
                //insert tag
                $this->blog_tag_hub_model->deleteByblogid($id);
                $select2array=$this->input->post('select2-multiple');
                if(!empty($select2array)){
                    foreach ($select2array as $select2row) {
                        $inputarr=array();
                        $inputarr['blog_id']=$id;
                        $inputarr['tag_id']=$select2row;
                        $inputarr['update_time'] = date('Y-m-d H:i:s');
                        $this->blog_tag_hub_model->insert($inputarr);
                    }
                }

                //end insert tag


                header('location:'.base_url().'admin/bloglist/edit?save=1&id='.$id);
                exit;
            }else{
                $this->blog_list_model->update($id, $arr);
               //insert tag
                $this->blog_tag_hub_model->deleteByblogid($id);
                $select2array=$this->input->post('select2-multiple');
                if(!empty($select2array)){
                    foreach ($select2array as $select2row) {
                        $inputarr=array();
                        $inputarr['blog_id']=$id;
                        $inputarr['tag_id']=$select2row;
                        $inputarr['update_time'] = date('Y-m-d H:i:s');
                        $this->blog_tag_hub_model->insert($inputarr);
                    }
                }

                //end insert tag
                header('location:'.base_url().'admin/bloglist/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_list_content'] = $this->blog_list_model->get_id($id);
        


        $data['blog_tag_content'] =$this->blog_tag_model->get();
        //get blog tag hub content
        
        $data['blogtaghub'] = $this->blog_tag_hub_model->get_blog_id($id);
        //end get blog tag hub content


        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="blog";    
        $this->load->view('admin/blog_list_edit_view.php', $data);    
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->blog_list_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;
    }
    
}

?>