<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_images extends CI_Controller {
	/**
	 OWNERS
	 */
	function my_images()
	{
		parent::__construct();
		$this->load->model('image_model');
		
	}

	public function index()
	{
		echo 'Going Nowhere slowly!';
		
	}
	
	
    //ADD IMAGE FROM REDACTOR
	function redactor_add_image()
	{
		 
		//$file = $_POST['redactor_file'];
		$img = $this->input->post('file', TRUE);
			//upload file

			$config['upload_path'] = './assets/images/';
			$config['allowed_types'] = 'gif|jpg|jpeg|png';

			$config['remove_spaces']  = TRUE;
			$config['encrypt_name']  = TRUE;
			
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload())
			{
					
					//$data['pro_id'] = $pro_id;
					$data['error'] =  $this->upload->display_errors();
					echo 
					'{ "filelink": "" }'; 
					//$this->output->set_header("HTTP/1.0 403 ERROR");
					
			}	
			else
			{	
			
			
			//$file = array('upload_data'
			$data = array('upload_data' => $this->upload->data());
			$file =  $this->upload->file_name;
			$width = $this->upload->image_width;
		    $height = $this->upload->image_height;	
		   	//delete old photo
			//$this->delete_old_child_photo($child_id,$pro_id);
			//$this->update_cover_book_image($child_id,$pro_id,$file);
					 
				if (($width > 1200) || ($height > 1400)){
 
					    $this->image_model->downsize_image($file);
								
				}
 
				 //populate array with values
				$insertdata = array(
					
					'type' => 'img',
					'img_file' => $file 

        		);
				//insert into database
				
				$this->db->insert('images',$insertdata);
				
			   	$data['filename'] = $file;
				
				$image = base_url('/') . 'assets/images/'.$file;
			   //redirect 
			    echo '{"filelink": "'.$image.'"}';
		}	
		
	}

/**
++++++++++++++++++++++++++++++++++++++++++++
//
//Functions
++++++++++++++++++++++++++++++++++++++++++++	
GALLERY 	
++++++++++++++++++++++++++++++++++++++++++++
//
//Functions
++++++++++++++++++++++++++++++++++++++++++++	
*/
function add_gallery(){
	
	 $this->load->library('upload');  // NOTE: always load the library outside the loop
	 $child_id = $this->input->post('child_id', TRUE);
	 $story_id = $this->input->post('story_id1', TRUE);
	 $pro_id = $this->input->post('pro_id', TRUE);
	 $this->total_count_of_files = count($_FILES['files']['name']);
	 /*Because here we are adding the "$_FILES['userfile']['name']" which increases the count, and for next loop it raises an exception, And also If we have different types of fileuploads */
	
	 for($i=0; $i<$this->total_count_of_files; $i++)
	 {
	
	   $_FILES['userfile']['name']    = $_FILES['files']['name'][$i];
	   $_FILES['userfile']['type']    = $_FILES['files']['type'][$i];
	   $_FILES['userfile']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
	   $_FILES['userfile']['error']       = $_FILES['files']['error'][$i];
	   $_FILES['userfile']['size']    = $_FILES['files']['size'][$i];
	
	   
	   $config['upload_path'] = './assets/' . $pro_id . '/children/';
	   $config['allowed_types'] = 'jpg|jpeg|gif|png';
	   $config['overwrite']     = FALSE;
	   $config['max_size']	= '0';
	   $config['max_width']  = '8324';
	   $config['max_height']  = '8550';
	   $config['min_width']  = '200';
	   $config['min_height']  = '200';
	   $config['remove_spaces']  = TRUE;
	   $config['encrypt_name']  = TRUE;
	   
	
	  $this->upload->initialize($config);
	
		  if($this->upload->do_upload())
		  {
			//$file = array('upload_data'
			  $data = array('upload_data' => $this->upload->data());
			  $file =  $this->upload->file_name;
			  $width = $this->upload->image_width;
			  $height = $this->upload->image_height;	
			  //delete old photo
			  //$this->delete_old_child_photo($child_id,$pro_id);
			  //$this->update_pro_book_image($child_id,$pro_id,$file);
					   
				  if (($width > 850) || ($height > 700)){
						  
						  $this->load->model('gallery_model');
						  $str = 'children';	 
						  $this->gallery_model->downsize_image($file,$pro_id,$str);
								  
				  }
  					
  				  
				  //populate array with values
				  $data = array(
					  'child_id' => $child_id,
					  'story_id' => $story_id,  
					  'img_file'=> $file
				   
				  );
				  //IF NEW LS
				  if($story_id == '0'){
				 
					  //insert into database
					  
					  $this->db->insert('gallery_images',$data);
				  }else{
					  
					  $this->db->insert('gallery_images',$data);
				  }
				  //load respective view 
				 $data['pro_id'] = $pro_id;
				 
				 //crop 
				  $data['filename'] = $file;
				  $data['width'] = $this->upload->image_width;
				  $data['height'] = $this->upload->image_height;
				  $data['info'] = $this->owner_model->get_child($child_id);
				  $data['parents'] = $this->owner_model->get_parents($pro_id);
				  $image = base_url('/') . 'assets/' . $pro_id.'/children/'.$file;
				 //redirect 
				  $data['basicmsg'] = 'Photo added successfully!';
				  echo '<div class="alert alert-success">
					  <button type="button" class="close" data-dismiss="alert">×</button>
					  '. $data['basicmsg'].'
					   </div>
					   <script type="text/javascript">
					  
					   show_gallery('.$story_id.');
					   </script>
					   ';
				  //$this->output->set_header("HTTP/1.0 200 OK");
			
			
		  }else{
			//ERROR
				$data['pro_id'] = $pro_id;
				$data['error'] =  $this->upload->display_errors();
				echo 
				'<div class="alert alert-error">
				<button type="button" class="close" data-dismiss="alert">×</button>'
				 . $data['error'].'
				 </div><script type="text/javascript">
					console.log("error");
					$("#gal-cover").hide();
				</script>'; 
			
		  }
	 }
	

}



	
//SHOW GALLERY on Edit pages			
function show_upload_images_json($child_id, $pro_id)
	{
			$query = $this->db->select('img_file, gal_img_id');
			$query = $this->db->from('gallery_images');
			$query = $this->db->where('child_id',$child_id);
			
			$query = $this->db->get();
			
        	if($query->num_rows() > 0){
				$x =0;
				echo '[';
				foreach($query->result() as $row){

					$img_file = $row->img_file;
					$gal_img_id = $row->gal_img_id;
					if(($query->num_rows() - $x) == 1){
						
						$close_tag = '';
					}else{
						
						$close_tag = ',';
					}
					
				 
					echo '
					{ "thumb": "'.base_url('/').'img/timbthumb.php?src='.base_url('/').'assets/'.$pro_id.'/children/'.$img_file.'&q=100&w=180&h=130", "image": "'.base_url('/').'img/timbthumb.php?src='.base_url('/').'assets/'.$pro_id.'/children/'.$img_file.'&q=100&w=180&h=130", "title": "imagetitle"}'.$close_tag;
					
					$x++;
				
				}
				echo ']';
			
				
			}else{
			
				return '0';
			}			
			
			
	}	
			
function add_gallery_do()
	{
		$img = $this->input->post('userfile', TRUE);
			$child_id = $this->input->post('child_id', TRUE);
			$story_id = $this->input->post('story_id', TRUE);
			$pro_id = $this->input->post('pro_id', TRUE);
			//upload file

			$config['upload_path'] = './assets/' . $pro_id . '/children/';
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['file_name']	= $pro_id . '-' . $child_id . '-' .  date('m-y-d') . 'pro-pic' . rand(0,100). '.jpg';
			$config['max_size']	= '8024';
			$config['max_width']  = '8324';
			$config['max_height']  = '8550';
			$config['min_width']  = '200';
			$config['min_height']  = '200';
			$config['remove_spaces']  = TRUE;
			
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload())
			{
					
					$data['pro_id'] = $pro_id;
					$data['error'] =  $this->upload->display_errors();
					echo 
					'<div class="alert alert-error">
         			<button type="button" class="close" data-dismiss="alert">×</button>'
           			 . $data['error'].'
       				 </div><script type="text/javascript">
					 	$("#gal-cover").hide();
				    </script>'; 
					//$this->output->set_header("HTTP/1.0 403 ERROR");
					
			}	
			else
			{	
			
			
			//$file = array('upload_data'
			$data = array('upload_data' => $this->upload->data());
			$file =  $this->upload->file_name;
			$width = $this->upload->image_width;
		    $height = $this->upload->image_height;	
		   	//delete old photo
			//$this->delete_old_child_photo($child_id,$pro_id);
			//$this->update_pro_book_image($child_id,$pro_id,$file);
					 
				if (($width > 850) || ($height > 700)){
						
						$this->load->model('gallery_model');
						$str = 'children';	 
						$this->gallery_model->downsize_image($file,$pro_id,$str);
								
				}


			   //populate array with values
				$data = array(
					'child_id' => $child_id,
					'story_id' => $story_id,  
			     	'img_file'=> $file
				 
        		);
				//insert into database
				
				$this->db->insert('gallery_images',$data);
				
				//load respective view 
			   $data['pro_id'] = $pro_id;
			   
			   //crop 
			   	$data['filename'] = $file;
				$data['width'] = $this->upload->image_width;
				$data['height'] = $this->upload->image_height;
				$data['info'] = $this->owner_model->get_child($child_id);
				$data['parents'] = $this->owner_model->get_parents($pro_id);
				$image = base_url('/') . 'assets/' . $pro_id.'/children/'.$file;
			   //redirect 
			    $data['basicmsg'] = 'Photo added successfully!';
				echo '<div class="alert alert-success">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		'. $data['basicmsg'].'
       				 </div>
					 <script type="text/javascript">show_gallery("'.$story_id.'");</script>
					 ';
				$this->output->set_header("HTTP/1.0 200 OK");
		}
	}	
		
		
	}
	
	
	
	

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */