<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
/**
	 * 
	 */
 
	class Add_product_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'] = 'avi|mpg|mpeg|wmv|jpg';
        //allowed max file size. 0 means unlimited file size
          $config['max_size'] = '0';
            //max file name size
            $config['max_filename'] = '0';
        //whether file name should be encrypted or not
        $config['encrypt_name'] = FALSE;
        //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
                        'quality'          =>'100%',
						'new_image'         => $this->resized_path, //path to
						'maintain_ratio'    => false,
						'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
                        'quality'          =>'100%',
						'new_image'         => $this->thumbs_path, //path to
						'maintain_ratio'    => false,
						'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()
		{
			$name = $this->input->post('name');
			$price = $this->input->post('price');
			$sail_price = $this->input->post('sail_price');
			$m_title = $this->input->post('m_title');
			$short_description = $this->input->post('short_description');
			$m_des = $this->input->post('m_des');
			$imported_from = $this->input->post('imported_from');
			$size = $this->input->post('size');
			$des = $this->input->post('des');
			$cat_slug = $this->input->post('cat_slug');
			$dliveryfee = $this->input->post('dliveryfee');
			$gettag = $this->input->post('tags');
			$tags = implode('', $gettag);
			$stock = $this->input->post('stock');
			$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($name)));
				$sure = $this->db->where('slug', $slug);
				$sure = $this->db->get('product');
				if($sure->num_rows() > 0){
					return 3;
				}else{
					$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);

            $filename = $returnd['filedata']['file_name'];
            $dd = array(
                'prroduct_id'=>$slug,
                'img'=>$filename
            );
            $this->db->insert('product_imgs', $dd);
        }

        	if (!$returnd['error']) {
					 $filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'name' =>$name,
						'price' =>$price,
						'sail_price' =>$sail_price,
						'm_title' =>$m_title,
						'size' =>$size,
						'imported_from' =>$imported_from,
						'm_des' =>$m_des,
						'short_description' =>$short_description,
						'des' =>$des,
						'slug' =>$slug,
						'cat_slug' =>$cat_slug,
						'tags' =>$tags,
						'stock' =>$stock,
						'dliveryfee' =>$dliveryfee,
						'img' =>$filename
					);

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

				}else{
					return FALSE;
				}
						}
			
					
				
			
		}



// UPDATE PRODUCT FUNCTION 
		function update()
		{
			$name = $_POST['name'];
			$price = $_POST['price'];
			$sail_price = $_POST['sail_price'];
			$m_title = $_POST['m_title'];
			$size = $_POST['size'];
			$imported_from = $_POST['imported_from'];
			$m_des = $_POST['m_des'];
			$short_description = $_POST['short_description'];
			$des = $_POST['des'];
			$slug = $_POST['slug'];
			$cat_slug = $_POST['cat_slug'];
			$dliveryfee = $_POST['dliveryfee'];
			$gettag = $_POST['tags'];
            $tags = implode('', $gettag);
            $stock = $_POST['stock'];
			$id = $_POST['id'];
		
				$sure = $this->db->get('product');
				if($sure->num_rows() > 0){
					$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(count($img['name']) > 1 ) {
                $filename = $returnd['filedata']['file_name'];
                $dd = array(
                    'prroduct_id'=>$id,
                    'img'=>$filename
                );
                $this->db->insert('product_imgs', $dd);
            }
        }

        	if (!$returnd['error']) {
					 $filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'name' =>$name,
						'price' =>$price,
						'sail_price' =>$sail_price,
						'm_title' =>$m_title,
						'm_des' =>$m_des,
						'size' =>$size,
						'imported_from' =>$imported_from,
						'short_description' =>$short_description,
						'des' =>$des,
						'slug' =>$slug,
						'cat_slug' =>$cat_slug,
                        'tags' =>$tags,
                        'stock' =>$stock,
                        'dliveryfee' =>$dliveryfee,
						'img' =>$filename
					);

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

				}else{
					
							
							$data =  array(
						'name' =>$name,
						'price' =>$price,
						'sail_price' =>$sail_price,
						'm_title' =>$m_title,
						'm_des' =>$m_des,
						'short_description' =>$short_description,
						'slug' =>$slug,
						'size' =>$size,
						'imported_from' =>$imported_from,
						'des' =>$des,
                        'tags' =>$tags,
                        'stock' =>$stock,
                        'dliveryfee' =>$dliveryfee,
						'cat_slug' =>$cat_slug
					);

							$q = $this->db->where('id', $id);
					$q = $this->db->update('product', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}
				}
				}else{
					$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(count($img['name']) > 1 ) {
                $filename = $returnd['filedata']['file_name'];
                $dd = array(
                    'prroduct_id'=>$id,
                    'img'=>$filename
                );
                $this->db->insert('product_imgs', $dd);
            }
        }

        	if (!$returnd['error']) {
					 $filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'name' =>$name,
						'price' =>$price,
						'sail_price' =>$sail_price,
						'm_title' =>$m_title,
						'm_des' =>$m_des,
						'short_description' =>$short_description,
						'des' =>$des,
						'size' =>$size,
						'imported_from' =>$imported_from,
						'slug' =>$slug,
						'cat_slug' =>$cat_slug,
                        'tags' =>$tags,
                        'stock' =>$stock,
                        'dliveryfee' =>$dliveryfee,
						'img' =>$filename
					);
							$q = $this->db->where('id', $id);
					$q = $this->db->update('product', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}

				}else{
					$filename = $returnd['filedata']['file_name'];
							
							$data =  array(
						'name' =>$name,
						'price' =>$price,
						'sail_price' =>$sail_price,
						'm_title' =>$m_title,
						'm_des' =>$m_des,
						'short_description' =>$short_description,
						'des' =>$des,
						'size' =>$size,
						'imported_from' =>$imported_from,
                        'tags' =>$tags,
                        'stock' =>$stock,
                        'dliveryfee' =>$dliveryfee,
						'slug' =>$slug,

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



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

		function desc(){
			$this->db->order_by("id", "desc");
			
			$q = $this->db->get('product');
			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('product');
				return $data->result();
			}

			function slug($sl)
			{
				$data = $this->db->where('slug', $sl);
				$data = $this->db->get('product');
				return $data->result();
			}

			function catis($sl)
			{
				$data = $this->db->where('cat_slug', $sl);
				$data = $this->db->get('product');
				return $data->result();
			}
			function singlecat($sl)
			{
				$data = $this->db->where('slug', $sl);
				$data = $this->db->get('category');
				return $data->result();
			}
			function results()
			{
				$sl = $this->input->post('search');
				$data = $this->db->like('name', $sl);
				$data = $this->db->or_like('cat_slug', $sl);
				$data = $this->db->or_like('slug', $sl);
				$data = $this->db->or_like('des', $sl);
				$data = $this->db->or_like('m_des', $sl);
				$data = $this->db->or_like('m_title', $sl);
				$data = $this->db->get('product');
				return $data->result();
			}

			// function update()
			// {
			// 	$name = $_POST['name'];
			// 	$title = $_POST['title'];
			// 	$des = $_POST['des'];
			// 	$id = $_POST['id'];
			// 	$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($name)));
			// 	$sure = $this->db->where('slug', $slug);
			// 	$sure = $this->db->get('category');
			// 	if($sure->num_rows() > 0){
			// 		$data =  array(
			// 			'category_name' =>$name,
			// 			'cat_title' =>$title,
			// 			'cat_dis' =>$des

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

			// 	}else{
			// 		$data =  array(
			// 			'category_name' =>$name,
			// 			'slug'=> $slug,
			// 			'cat_title' =>$title,
			// 			'cat_dis' =>$des

			// 		);
			// 			$up = $this->db->where('id', $id);
			// 			$up = $this->db->update('category', $data);
			// 			if($up){
			// 				return 3;
			// 			}else{
			// 				return 0;
			// 			}
			// 	}
			// }


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


	}	

?>