<?php
/**
 * Created by PhpStorm.
 * User: CHALONG LATEX
 * Date: 14-Sep-16
 * Time: 4:22 PM
 */
if(!defined('BASEPATH')) exit('No direct script access allowed');

class Slider extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('slider_model') ;
        $this->load->model('lang_model');

       checklogin();
    }

    public function index()
    {
        $data['lists'] = $this->slider_model->get_all();
        $this->template->write_view('content', 'slider',$data);
        $this->template->write_view('level_script', 'slider/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function lists()
    {
        $data['lists'] = $this->slider_model->get_all();
        $this->template->write_view('content', 'slider',$data);
        $this->template->write_view('level_script', 'slider/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function form($item_id='')
    {
        $data['item'] = $this->slider_model->get($item_id);
        $data['langs'] = $this->lang_model->where("is_active",'Y')->get_all();

        $this->template->write_view('content', 'sliderform',$data);
        $this->template->write_view('level_script', 'slider/formscript');
        $this->template->add_js('assets/plugins/editor.markdown/js/bootstrap-markdown.min.js');
        $this->template->add_js('assets/js/progress.js');
        $this->template->render();
    }
    public function add(){


        $data ="" ;
        foreach(@$_POST as $key =>$value){
            if($value !=""){
                $data[$key] =$value ;
            }
        }

        unset($data['is_ajax']) ;

        $slider_id = '';

        if(isset($data) && $data !="" && @$data['id'] =="")
        {
            $slider_id = $this->slider_model->insert($data) ;
        }
        else if(  @$data['id'])
            $slider_id = $this->slider_model->update($data,$data['id']) ;

        if($slider_id)
        {
            $slider = $this->slider_model->get($data['id']);
            $data_path['bg_image'] = $slider->bg_image;
            $data_path['bg_lazyimage'] = $slider->bg_lazyimage;

            if(isset($_FILES) && @$_FILES['bg_image']['name'] !=""){
                $path = './upload/images/sliders/'.$slider->id.'/';

                if (!is_dir($path)) {
                    mkdir($path, 0777, TRUE);
                }
                $file_name = time().'_bg_image_'.@$_FILES['bg_image']['name'];
                $data_path['bg_image'] ="sliders/".$slider->id.'/'.$file_name;
                $upload = $this->upload_image('bg_image',$path,$file_name);

                if(is_array($upload))
                {
                    unlink(realpath('./upload/images/'.$slider->bg_image));
                }
            }

            if(isset($_FILES) && @$_FILES['bg_lazyimage']['name'] !="") {
                $path = './upload/images/sliders/' . $slider->id . '/';
                if (!is_dir($path)) {
                    mkdir($path, 0777, TRUE);
                }
                $file_name = time() . '_bg_lazyimage_' . @$_FILES['bg_lazyimage']['name'];
                $data_path['bg_lazyimage'] = "sliders/" . $slider->slider_ref . '/' . $file_name;
                $upload = $this->upload_image('bg_lazyimage', $path, $file_name);

                if (is_array($upload)) {
                    unlink(realpath('./upload/images/' . $slider->bg_lazyimage));
                }
            }
            $this->slider_model->update($data_path,@$data['id']) ;
        }
    }
    function upload_image($image='',$path='', $new_name='') {

        // define parameters
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['file_name'] = $new_name;
        $this -> load -> library('upload');
        $this->upload->initialize($config);

        // upload the image
        if ($this -> upload -> do_upload($image)) {
            return $this -> upload -> data();
        } else {
            print_r($this -> upload -> display_errors());
            return FALSE;
        }
    }

}