<?php
App::uses('AppController', 'Controller');
class CatalogsController extends AppController {

////////////////////////////////////////////////////////////

    public $components = [
        'RequestHandler',
    ];

////////////////////////////////////////////////////////////

    public function beforeFilter() {
        parent::beforeFilter();
    }

////////////////////////////////////////////////////////////

    public function wholesalers() {
		
		 $directory_name = "wholesalers/";
		    
		 $this->set(compact('directory_name'));
		 
		 $manufacturer_catalog="wholesalers";
		 
         $catalog = $this->Catalog->find('first', [
		 'fields' => array('Catalog.name'),
            'conditions' => [
                'Catalog.id' =>8,
            ],            
        ]);	
        $this->set(compact('catalog')); 
		
		$meta_title = "Wholesalers Category";
		$meta_keyword = "Wholesalers Category";
		$meta_desc = "Wholesalers Category";
 
 	$this->set(compact('meta_title','meta_keyword','meta_desc','manufacturer_catalog'));
	  
		$this->render('index');
     }
     
     public function retailer() {
		
		 $directory_name = "retailer/";
		    
		 $this->set(compact('directory_name'));
		 
		 $manufacturer_catalog="retailer";
		 
         $catalog = $this->Catalog->find('first', [
		 'fields' => array('Catalog.name'),
            'conditions' => [
                'Catalog.id' =>9,
            ],            
        ]);	
        $this->set(compact('catalog')); 
		
		$meta_title = "Retailer Category";
		$meta_keyword = "Retailer Category";
		$meta_desc = "Retailer Category";
 
 	$this->set(compact('meta_title','meta_keyword','meta_desc','manufacturer_catalog'));
	  
		$this->render('index');
     }

   public function manufacturers() {
		
		 $directory_name = "manufacturers/";
		    
		 $this->set(compact('directory_name'));
		 
		 $manufacturer_catalog="manufacturers";
		 
         $catalog = $this->Catalog->find('first', [
		 'fields' => array('Catalog.name'),
            'conditions' => [
                'Catalog.id' =>3,
            ],            
        ]);	
        $this->set(compact('catalog'));	
   
		$meta_title = "Automotive and Industrial Lubricants Manufacturers In India";
		$meta_desc = "We are one of the prominent manufacturers of Automotive and Industrial Lubricants in Delhi India, offering Engine Oil, Car Polish, Bike Polish, and much more.";
		$meta_keyword = "Automotive and Industrial Lubricants Manufacturers, Engine Oil In India, Bike Polish In Delhi, Car Polish, Bike Engine Oil, Tyre Polish";
 
 	$this->set(compact('meta_title','meta_keyword','meta_desc','manufacturer_catalog'));
	  
		$this->render('index');
     }
	 
	   public function suppliers() {
		
		$directory_name = "suppliers/";
		
		    
		$this->set(compact('directory_name'));
		 
          $catalog = $this->Catalog->find('first', [
		  'fields' => array('Catalog.name'),
            'conditions' => [
                'Catalog.id' => 4,
            ],
            
        ]);
        $this->set(compact('catalog')); 
				
	  $this->loadModel('Sitepage');	
   
	$meta_title = "Suppliers Of Automotive Engine Oil | Automotive Lubricant";
	$meta_desc = "Automotive Lubricant - We are one of the esteemed Automotive Engine Oil Suppliers based in India offering Car Oil, Tractor Engine Oil, Coolant, and much more.";
	$meta_keyword = "Automotive Lubricant, Automotive Engine Oil Suppliers, Tractor Engine Oil, Coolant, Car Oil, Car Polish, Gear Oil";
 
 	$this->set(compact('meta_title','meta_keyword','meta_desc'));

		$this->render('index');
     }
	 
	   public function exporters() {
		
		
		 $directory_name = "exporters/";
		    
		 $this->set(compact('directory_name'));
		 
        $catalog = $this->Catalog->find('first', [
		'fields' => array('Catalog.name'),
            'conditions' => [
                'Catalog.id' => 5,
            ],            
        ]);
        $this->set(compact('catalog')); 
		
		 $meta_title = "Lithium Grease Exporters | Engine Oil From India";
		 $meta_desc = "ADOLF7 Automotive Industries Pvt Ltd is a leading Lithium Grease Exporter based in India, offering a wide range of Engine Oils, Lubricants, and much more.";
		 $meta_keyword = "Lithium Grease Exporters, Engine Oil From India, Lubricants, Automotive Engine Oil, Bike Engine Oil, Car Engine Oil, Coolant";
 
		 $this->set(compact('meta_title','meta_keyword','meta_desc'));
		
		$this->render('index');
     }

	 
	public function productdetails($slug = null) {
		 		  
				   
	  $directory_name = str_replace($this->webroot,'',$this->here);
	  $directory_name = str_replace($slug,'',$directory_name);
	  $directory_name = str_replace('.html','',$directory_name);
	  $catalog_slug = str_replace('/','',$directory_name);
 
		$directory_name = $catalog_slug."/";
		$this->set(compact('directory_name'));
		 
		 if($catalog_slug == 'wholesalers') {
			 	$catalog_id = 8;
		 }
		 
		 if($catalog_slug == 'retailer') {
			 	$catalog_id = 9;
		 }
		 
		 if($catalog_slug == 'suppliers') {
			 	$catalog_id = 4;
		 }
		 
		  if($catalog_slug == 'manufacturers') {
			   $manufacturer_catalog="manufacturers";
		
			 	$catalog_id = 3;
		 }
		 
		  if($catalog_slug == 'exporters') {
			 	$catalog_id =5;
		 }
		   $this->set(compact('manufacturer_catalog'));
		
			$country = $catalog_slug;  
 		
		$this->loadModel('Catalog');
		 		 
		$catalog = $this->Catalog->find('first', [
			'fields' => array('Catalog.name','Catalog.description','Catalog.meta_title','Catalog.meta_keyword','Catalog.meta_desc'),
            'recursive' => 0,				 		
            'conditions' =>  array(
			'Catalog.id' => $catalog_id
			)
        ]);

    	$this->loadModel('Product');	
       
        $product = $this->Product->find('first', [
            'recursive' => -1,
			'fields' => array('Product.name','Product.image'),
            'conditions' => [              
                'Product.active' => 1,
                'Product.slug' => $slug
            ]
        ]);
		  
		if (empty($product)) {
			throw new NotFoundException(__('Page Not Found.'));
        }
		  
	$product_name = $product['Product']['name'];
	
	if(!empty($catalog['Catalog']['meta_title'])) {	
		$meta_title   = $catalog['Catalog']['meta_title'];
		
		$meta_title = str_replace('catname', $product_name, $meta_title );
	}
	if(!empty($catalog['Catalog']['meta_keyword'])) {	
		$meta_keyword = $catalog['Catalog']['meta_keyword'];
		
		$meta_keyword = str_replace('catname', $product_name, $meta_keyword );
	}
	if(!empty($catalog['Catalog']['meta_desc'])) {	
		$meta_desc   = $catalog['Catalog']['meta_desc'];
		
		$meta_desc = str_replace('catname', $product_name, $meta_desc );
	}
			
	$this->set(compact('meta_title','meta_keyword','meta_desc'));		 
  				
    $this->set(compact('country','location','product','catalog'));

	}
	
////////////////////////////////////////////////////////////

    public function view($id = null) {
 
		 
        $product = $this->Catalog->find('first', [
            'recursive' => -1,
            'contain' => [
                'Category',

            ],
            'conditions' => [
                'Catalog.active' => 1,
                'Catalog.slug' => $id
            ]
        ]);
		 
		 
		  $term = $product['Catalog']['name'];
		  $product_id = $product['Catalog']['id'];
		  
                  $conditions[] = [
                //    'Catalog. LIKE' => '%' . $term . '%'// . '  and id != '. $product_id
                ];
							
        if (empty($product)) {
            return $this->redirect(['action' => 'index'], 301);
        }

        $this->Catalog->updateViews($product);
		 
        $this->set(compact('product'));
 
    }
    

	public function admin_index() { 
		
		$products = $this->Catalog->find('all', array(
			'recursive' => 0,
			'order' => array(
				'Catalog.id' => 'DESC'
			),
			'conditions' => array(
			),
		));
		
		
		$this->set(compact('products'));
		
	}
 
    public function admin_add() {
 
        if ($this->request->is('post')) {
			
			$this->request->data['Catalog']['slug'] = $this->generateSlug($this->request->data['Catalog']['name']);
 			
			$this->request->data['Catalog']['categories'] = @implode(',', $this->request->data['Catalog']['categories']);	

            $this->Catalog->create();
            if ($this->Catalog->save($this->request->data)) {
                $this->Flash->flash('The Catalog has been saved');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->flash('The Catalog could not be saved. Please, try again.');
            }
        }       
     	
    }

////////////////////////////////////////////////////////////

    public function admin_edit($id = null) {
 

        if (!$this->Catalog->exists($id)) {
            throw new NotFoundException('Invalid product');
        }
		
		 $product = $this->Catalog->find('first', [
                'conditions' => [
                    'Catalog.id' => $id
                ]
            ]);
 			
        if ($this->request->is('post') || $this->request->is('put')) {
  
		$this->request->data['Catalog']['slug'] = $this->generateSlug($this->request->data['Catalog']['name']);
				
			$this->request->data['Catalog']['categories'] = @implode(',', $this->request->data['Catalog']['categories']);	
			
            if ($this->Catalog->save($this->request->data)) {
                $this->Flash->flash('The Catalog has been saved');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->flash('The Catalog could not be saved. Please, try again.');
            }
        } else {
            $product = $this->Catalog->find('first', [
                'conditions' => [
                    'Catalog.id' => $id
                ]
            ]);
            $this->request->data = $product;
        }

        $this->set(compact('id'));

        $this->set(compact('product'));

    }
 
 public function admin_change_catalog_status()
    {
        if(!$this->request->is('AJAX'))
        {
            return $this->redirect(array('controller'=>'catalogs', 'action'=>'index'));
        }
        
        if(!$this->Catalog->save(array('id'=>$this->request->data['catalog_id'], 'active'=>$this->request->data['status'])))
        {
            echo json_encode(array('status'=>'failure', 'message'=>'Unable to update status at the moment.'));
            die;
        }
        
        echo json_encode(array('status'=>'success', 'message'=>'Catalog status updated.'));
        die;
    }
	
    public function admin_delete($id = null) {
        $this->Catalog->id = $id;
        if (!$this->Catalog->exists()) {
            throw new NotFoundException('Invalid Catalog');
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Catalog->delete()) {
            $this->Flash->flash('Catalog deleted');
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->flash('Catalog was not deleted');
        return $this->redirect(['action' => 'index']);
    }

 
}
