<?php namespace App\Controllers;

use CodeIgniter\Controller;
#use \App\Andy\PortfolioModel;
use \App\Andy\Andymodel;
use \App\Andy\ImgPreCheck;
use CodeIgniter\I18n\Time;

use \App\Models\PortfolioModel;

use \App\Models\PortfolioModelEnt;

class Portfolio  extends BaseController
{
		
		protected $id;
		protected $portfolioId ;

	
	
	public function testEntity()
	
	
	
	{
		$handle= new PortfolioModelEnt();
		$result= $handle->find(31);
		echo $result->category;
		
		echo "placeholder";
		//var_dump($result);
	}	
	
	
	
	
	
	
	
	
	public function prodInfo($Id)
	
	{
	
	 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	
	
	
	
	$handle = new PortfolioModel;
	$singleEntry= $handle->getOneToDel($Id);
	
	
	
	$data = [
                'title'  => 'home page ',
                'date'=>'20:15',
                'prodInfo'=>$singleEntry
		];
		
		
		
        echo view('productInfo',$data);
	
	
	
	
	}	
	
	
	
	
	
	
	public function index()
	{
	 //not used in routes but keep for logic of code 
	 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
	 
	 
		
		
			try 
			
			
			
			{
			$handle= new PortfolioModel();
			$mycount= $handle->isEmptyArt();
			}
			catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong ',
				   'date'=>$date
				   
				   
				   
					 ];

				echo view('header', $data);
				echo view('info', $data);
				echo view('footer',$data);	
				  
		   
			 die();
			 //above try catch works 
		   }
	
	     
	
	
	
			if($mycount == 0) 
			
			{
				
			$data = [
			'title'  => 'portfolio',
		   'info'=>'currently portfolio is empty',
				'date'=>$date		 
				];

				echo view('header', $data);
				echo view('info', $data);
				echo view('footer',$data);	
					
			}
	
			elseif ($mycount >= 1 ) 
			
			{
			
			$handle = new PortfolioModel();	
					 $data = [
					 'title'=>'paginate',
					'portfolio' => $handle->paginate(5),
					'pager' => $handle->pager,
					'category'=>'Art',
					'date'=>$date
				];
				
				
				 echo view('header',$data);
				echo view('displayPortfolio',$data);
				echo view('footer',$data);	
						
			
			
			
			}
	
	
	
}


		public function portfolioForm()
		{
		 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
		helper('form');
		helper('html');
		$data = [
						'title'  => 'landing page ',
						'info'=>'',
						'date'=>$date
						
						
						
						
						 ];

				
				echo view('portfolioForm' ,$data);	



			
		}


		public function portfolioAdd()
    {
				
		 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
				
				
				
				
				helper('uri');
				helper('html');
				helper('form');
				helper('security');  
        
        
       
					   try 
							{
							
							$handle= new PortfolioModel();
							$mycount= $handle->count();
							
						   }
					
			
					   catch ( \Exception  $e)
					
							   {
								 $data = [
											'title'  => 'info',
										  
									   'info'=>'somwething went wrong ',
									   'date'=>$date
									   
									   
									   
									   
									   
									   
									];

									echo view('header', $data);
									echo view('info', $data);
									echo view('footer',$data);	
								 
								 
								 
								 
								 
							   
								 die();
								 //above try catch works 
							   }
			
			
       
       if($mycount >= 50)
       
       
       {
		   
		 $data = [
											'title'  => 'info',
										  
									   'info'=>'you have reached portfolio maximum capacity  '
									   
									   
									   
									   
									   
									];

									echo view('header', $data);
									echo view('info', $data);
									echo view('footer',$data);	
								 
								 
								 
								 
								 
							   
								 die();
		   
		   
		
	  }	   
       
       
      elseif($mycount <= 50) 
      { 
       
       $title = $this->request->getVar('title'); 
	   $file = $this->request->getFile('userfile');
	   $name= $file->getName(); 
	   $tempfile = $file->getTempName();
	   $slug= url_title($title);
	    $category= $this->request->getVar('category');
	    $info= $this->request->getVar('info');
	    	 
	 //nowsome pre checks 
	  $handle = new ImgPreCheck();
	  //ImgPreCheck is one of my classes
	   
	  
	  
	  
	  $logic = $handle->allReadyExistsPortfolio($name);
	
	 
	 
	  if($logic == "true")
	 
	 {
	 	$data =[
		'title'=>'andything',
		'info'=>'image with that name already exists',
		'date'=>$date
		];
				
  	        echo view('header',$data);
		echo view('portfolioForm',$data);
		echo view('footer',$data);	
	        exit();
	 }
	 
	
	$logic2 = $handle->extCheck($name);
		
	if($logic2 == 0)
	
	{
	
		$data =[		'title'=>'andything',
		'info'=>'that is not an image file',
		'date'=>$date
		];
				
  	        echo view('header',$data);
		echo view('portfolioForm',$data);
		echo view('footer',$data);	
	 exit();
	
	}
	
	
	

     $size= $handle->getFileSize($tempfile);
	
	if($size > 380000)
	
	{
	$data =[
		'title'=>'andything',
		'info'=>'thats too large an image file',
		'date'=>$date
		];
				
  	        echo view('header',$data);
		echo view('portfolioForm',$data);
		echo view('footer');	
	 exit();
	
	}
	
	//  @chmod(ROOTPATH.'public/portfolio', 0777 );
	  
	  
	 $file->move(ROOTPATH.'public/portfolio', $name);
	 $handle = new PortfolioModel();
	$handle->insertPortfolio($name,$title,$slug,$category,$info);
	 
	  $data =[
		'title'=>'andything',
		'info'=>'looks like portfolio entry was successful',
		'date'=>$date
		];
				
  	        echo view('header',$data);
		echo view('info',$data);
		echo view('footer',$data);	
	
	 
	    	    
	   
	    
	    
	 
	}   //end elseif 
 }//end portfolio add
   
	    
	    
	  public function delPortfolio()
	  {
		 
		  $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
		 
		 
		 
		 helper('form');
	     helper('html');
		 
		 $handle= new PortfolioModel(); 
		$result= $handle->getAll();  
		  
		  
		 $data = [
                'title'  => 'del ',
                'portfolioList'=>$result,
                'info'=>' ',
                'date'=>$date
		];
		
		 echo view('header', $data);
        echo view('delPortfolioForm',$data);
        echo view('footer',$data);	 
		  
	  }
	
   
   
   
    public function delPortfolioDo()
    {
	
	 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
	
	
	
	helper('uri');
		helper('html');
		helper('form');
	helper('security');  
	
	
	$this->portfolioId  = $this->request->getVar('portfolioId'); 
	
	 
	 $handle = new PortfolioModel();
	$result=  $handle->getOneToDel($this->portfolioId);
	   if($result==null)
	   {
		 helper('form');
	     helper('html');
		 
		 $handle= new PortfolioModel(); 
		$result= $handle->getAll();  
		  
		  
		 $data = [
                'title'  => 'del ',
                'portfolioList'=>$result,
                'info'=>'no such item  ',
                'date'=>$date
		];
		
		 echo view('header', $data);
        echo view('delPortfolioForm',$data);
        echo view('footer',$data);	    
		 exit();  
		   
	   }
	   
	   
	   
	   
	    $image=  $result['image'];
	
	  //first get rid of image 
	  @chmod(ROOTPATH.'public/portfolio/'.$image , 0777 );
	  
	    unlink(realpath(ROOTPATH.'public/portfolio/'.$image));
	    //now need to remove entry from databse
	 $logic=  $handle->deleteOne($this->portfolioId);
	if ($logic == true)
	{
	
	
	
	$data = [
                'title'  => 'deletion',
               //'result2'=>$resultr_dum
	       //above works
	       'info'=>'looks like item in portfolio was deleted',
	       'date'=>$date
	       
	       
	       
	       
        ];

        echo view('header', $data);
        echo view('info', $data);
        echo view('footer',$data);	
	
	
		
	
	
	
	
	}
	
	}
			 
			  
			  
			  
			   
			   
    
			//-----------------------  
			    public function baby()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyBaby();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'Baby ',
						   'info'=>'currently Baby Section  is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Baby')->paginate(5);
							$pager=$handle->where('category','Baby')->pager;
							$category='Baby';		
							$data = [
										'title'  => 'Baby ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('shop',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function  
    
			   
			  
    
    //------------------------
			  
			   public function ladies()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyLadies();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'baby ',
						   'info'=>'currently Lady section is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Ladies')->paginate(5);
							$pager=$handle->where('category','Ladies')->pager;
							$category='Ladies';		
							$data = [
										'title'  => 'ladies ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								
								
							echo view('shop',$data);
							
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function 
			   
			 //------------------------------------------------ 
    
			  
			  
			  
			  
			   public function mens()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyMens();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'mens ',
						   'info'=>'currently mens section is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Mens')->paginate(5);
							$pager=$handle->where('category','Mens')->pager;
							$category='Mens';		
							$data = [
										'title'  => 'mens ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('shop',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function 
			   
			  
			  
			  
			  //---------------------------
			  
			   public function boys()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyBoys();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'mens ',
						   'info'=>'currently boys section is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Boys')->paginate(5);
							$pager=$handle->where('category','Boys')->pager;
							$category='Boys';		
							$data = [
										'title'  => 'boys ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('shop',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function 
			   
			  //----------------------
			  
			   public function girls()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyGirls();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'mens ',
						   'info'=>'currently girls section is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Girls')->paginate(5);
							$pager=$handle->where('category','Girls')->pager;
							$category='Girls';		
							$data = [
										'title'  => 'girls ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('shop',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function 
			  
			  
			  //-------------------
			  
			  public function accessory()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyAccessory();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'mens ',
						   'info'=>'currently accessory section is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Accessory')->paginate(5);
							$pager=$handle->where('category','Accessory')->pager;
							$category='Accessory';		
							$data = [
										'title'  => 'accessory ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('shop',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function 
			  
			  
			  
			  //----------------------------------------
			   public function art()
			  {
					helper('uri');
					helper('html');
					helper('form');
					helper('security');  
					$myTime = new Time('now');
					$date=	$myTime->toLocalizedString('MMM d, yyyy');   
								
				try 
			   	{
						$handle= new PortfolioModel();
						$mycount= $handle->isEmptyArt();
						$count= $mycount["COUNT(*)"];
						if($count ==0) 
			
			              {
				
							$data = [
							'title'  => 'art portfolio',
						   'info'=>'currently Art portfolio is empty',
								'date'=>$date		 
								];

								echo view('header', $data);
								echo view('info', $data);
								echo view('footer',$data);	
					
			              }
	
			   elseif ($mycount >= 1 ) 
			
						{
						
						$result=	$handle->where('category','Art')->paginate(5);
							$pager=$handle->where('category','Art')->pager;
							$category='Art';		
							$data = [
										'title'  => 'art portfolio ',
									  
								  
								  
								   'result'=>$result,
								   'pager'=>$pager,
									'category'=>$category,  
								   'date'=>$date
								   
								];

								echo view('header', $data);
								
							echo view('displayPortfolio',$data);
							echo view('footer',$data);	
									
								 
							
						
						
						
						}//end elseif 
									
									
						
						}
						catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong ',
							   'date'=>$date
							   
							   
							   
								 ];

							echo view('header', $data);
							echo view('info', $data);
							echo view('footer',$data);	
							  
					   
						 die();
						 //above try catch works 
					   }
				
	     
		
						
				  
			  }//end public function  
    
			   
			  
			  
			  
			public function test()
			
			{
				
				
				$handle= new PortfolioModel(); 
				$result= $handle->isEmptyAccessory();
			
			echo $result["COUNT(*)"];
			} 
			  
			  
			  
			  
			  
			  
    
}//class end
