<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
/**
	 * 
	 */
 
	class Add_slider_model extends CI_Model
	{
		 private $original_path;
    private $resized_path;
    private $thumbs_path;
		
		function __construct()
		{
			parent::__construct();
			$this->original_path = 'uploads/';
        $this->resized_path = 'uploads/resized/';
        $this->thumbs_path = 'uploads/thumbs/';
		}

		

 public function image_upload($file, $rsize_w, $rsize_h, $thumb_w, $thumb_h){
        //set preferences

        //allowed file types. * means all types
        $config['allowed_types'] = 'jpeg|jpg|png|gif';
        //allowed max file size. 0 means unlimited file size
        $config['max_size'] = '0';
        //max file name size
        $config['max_filename'] = '255';
        //whether file name should be encrypted or not
        $config['encrypt_name'] = TRUE;
        //store image info once uploaded
        $image_data = array();
        //check for errors
        $is_file_error = FALSE;
        //check if file was selected for upload
        if (!$_FILES) {
            $is_file_error = TRUE;
            $this->session->set_flashdata("error",'Select an image file.');
        }
        //if file was selected then proceed to upload
        if (!$is_file_error) {

            //file upload destination
            $upload_path = $this->original_path;
            $config['upload_path'] = $upload_path;

            //load the preferences
            $this->load->library('upload', $config);
            //check file successfully uploaded. 'image_name' is the name of the input
            if (!$this->upload->do_upload($file)) {
                //if file upload failed then catch the errors
                $this->session->set_flashdata("error",$this->upload->display_errors());
                $is_file_error = TRUE;

            } else {

                //store the file info
                $image_data = $this->upload->data();

                $config = array(
                    'image_library' 	=> 'gd2',
                    'source_image'      => $image_data['full_path'], //path to the uploaded image
                    'new_image'         => $this->resized_path, //path to
                    'maintain_ratio'    => true,
                    'width'             => $rsize_w,
                    'height'            => $rsize_h
                );

                //this is the magic line that enables you generate multiple thumbnails
                //you have to call the initialize() function each time you call the resize()
                //otherwise it will not work and only generate one thumbnail
                $this->image_lib->initialize($config);
                $this->image_lib->resize();

                $config = array(
                    'image_library' 	=> 'gd2',
                    'source_image'      => $image_data['full_path'], //path to the uploaded image
                    'new_image'         => $this->thumbs_path, //path to
                    'maintain_ratio'    => true,
                    'width'             => $thumb_w,
                    'height'            => $thumb_h
                );

                //this is the magic line that enables you generate multiple thumbnails
                //you have to call the initialize() function each time you call the resize()
                //otherwise it will not work and only generate one thumbnail
                $this->image_lib->initialize($config);
                $this->image_lib->resize();

            }


        }
        return  array('filedata' =>$image_data , 'error'=>$is_file_error);
    }


		function db_insert()
		{
			$alt = $this->input->post('alt');
			$slider_slug = $this->input->post('slider_slug');
			
					$this->load->library('image_lib');
        $img = $_FILES['img'];

        for($i=0;$i<count($img['name']);$i++){
            $_FILES['photo']['name'] 	 = $img['name'][$i];
            $_FILES['photo']['type'] 	 = $img['type'][$i];
            $_FILES['photo']['tmp_name'] = $img['tmp_name'][$i];
            $_FILES['photo']['error'] 	 = $img['error'][$i];
            $_FILES['photo']['size'] 	 = $img['size'][$i];

            $returnd =  $this->image_upload('photo', 453, 453, 226, 226);
        }

        	if (!$returnd['error']) {
					 $filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'alt' =>$alt,
						'slider_slug' =>$slider_slug,
						
						'img' =>$filename
					);

					$q = $this->db->insert('slider', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}

				}else{
					return FALSE;
				}
						}
			
					
				
			
		



// UPDATE PRODUCT FUNCTION 
		function update()
		{
			$alt = $_POST['alt'];
			$slider_slug = $_POST['slider_slug'];
			$id = $_POST['id'];
			
					$this->load->library('image_lib');
        $img = $_FILES['img'];

        for($i=0;$i<count($img['name']);$i++){
            $_FILES['photo']['name'] 	 = $img['name'][$i];
            $_FILES['photo']['type'] 	 = $img['type'][$i];
            $_FILES['photo']['tmp_name'] = $img['tmp_name'][$i];
            $_FILES['photo']['error'] 	 = $img['error'][$i];
            $_FILES['photo']['size'] 	 = $img['size'][$i];

            $returnd =  $this->image_upload('photo', 453, 453, 226, 226);
        }

        	if (!$returnd['error']) {
					 $filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'alt' =>$alt,
						'slider_slug' =>$slider_slug,
						
						'img' =>$filename
					);

					$q = $this->db->where('id', $id);
					$q = $this->db->update('slider', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}

				}else{
					
							
							$data =  array(
						'alt' =>$alt,
						'slider_slug' =>$slider_slug,
						
					);

							$q = $this->db->where('id', $id);
					$q = $this->db->update('slider', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}
				}
				}
			
					
				
			
		
// UPDATE PRODUCT FUNCTION 
// UPDATE PRODUCT FUNCTION 

		function get_all(){
			$q = $this->db->get('slider');
			if($q->num_rows() > 0){
				// return $q->num_rows();
				return $q->result();
			}else{
				return 0;
			}
		}

		

			function select($id)
			{
				$data = $this->db->where('id', $id);
				$data = $this->db->get('slider');
				return $data->result();
			}

			


				function delete($id)
				{
					$data = $this->db->where('id', $id);
					$data = $this->db->delete('slider');
					if($data){
						return 1;
					}else{
						return 0;
					}
				}


	}	

?>