Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/application/classes/Controller/Product.php
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Product extends Controller_Frontend {

	public function action_index()
	{
		$this->redirect('Product/Category');
		exit;
	}
	
	public function action_category()
	{

		$page = !empty($_GET['page']) ? addslashes($_GET['page']) : 1;
		$page = ($page-1)*Kohana::$config->load('site.itemPerPageShop');
		$keyword = !empty($_GET['keyword']) ? addslashes($_GET['keyword']) : '';
		$filtr = !empty($_GET['filtr']) ? addslashes($_GET['filtr']) : '';
		$filtr_value = !empty($_GET['filtr_value']) ? addslashes($_GET['filtr_value']) : 1;
		$sort = !empty($_GET['sort']) ? addslashes($_GET['sort']) : 'order';
		$direct = !empty($_GET['direct']) ? addslashes($_GET['direct']) : 'desc';
		
		$id = !empty($_GET['id']) ? addslashes($_GET['id']) : '';
		$categories_id = !empty($_GET['categories_id']) ? addslashes($_GET['categories_id']) : '';
		
		$types_id = !empty($_GET['types_id']) ? addslashes($_GET['types_id']) : '';
		$tags = !empty($_GET['tags']) ? addslashes($_GET['tags']) : '';
		$size = !empty($_GET['size']) ? addslashes($_GET['size']) : '';
		$price = !empty($_GET['price']) ? addslashes($_GET['price']) : '';
		
			$products = ORM::Factory('item')->where('item.status','=', 1)->where(DB::expr('DATE_FORMAT(`lifetime_date`,"%Y-%m-%d %H-%i-%s")'),'>=', date('Y-m-d H:i:s'));
			
			$products = $products
					->join('item_type_links','LEFT')
					->on('item_type_links.id', '=', 'item.types_id')
					->join('item_types','LEFT')
					->on('item_types.id', '=', 'item_type_links.types_id');
					
			if( !empty( $filtr ) ) {
				$products = $products
							->where($filtr,'=', $filtr_value);
			}
			
			if( !empty( $price ) ) {
				$price = explode("_", $price);
				$products = $products
							->where('item.price','>=', $price[0])
							->where('item.price','<=', $price[1]);
			}
			if( !empty( $id ) ) {
				$products = $products
							->where('item.id','=', $id);
			}
			if( !empty( $categories_id ) ) {
		
				$products = $products
							->where('item.categories_id','=', $categories_id);
			}
			
			if( !empty( $types_id ) ) {
		
				$products = $products
							//->where('item.types_id','=', $types_id);
							->where('item_type_links.types_id','=', $types_id);
			}
			
			if( !empty( $size ) ) {
		
				$products = $products
							->where('item.size','=', $size);
			}
			
			if( !empty( $tags ) ) {
				$products = $products
						->where('item.tags','LIKE', '%'.$tags.'%');
			}
			
			if( !empty( $keyword ) ) {
				$products = $products
				->join('item_categories','LEFT')
					->on('item_categories.id', '=', 'item.categories_id')
				
					->and_where_open()
					->where('item_categories.title','LIKE', '%'.$keyword.'%')
				
					->or_where('item_types.title','LIKE', $keyword.'%')
					->or_where('item.tags','LIKE', '%'.$keyword.'%')
					->or_where('item.title','LIKE', '%'.$keyword.'%')
					->and_where_close();
					
				
						
			}
			
			$users_test = clone $products;
			$users_test = $users_test->find_all();
			
			$products = $products
								->limit(Kohana::$config->load('site.itemPerPageShop'))
								->order_by($sort, $direct)
								->offset($page)
								->group_by('item.id')
								->find_all();
								
								
			$this->pagination = new Pagination(array(
				'items_per_page' => Kohana::$config->load('site.itemPerPageShop'), 
				'style' => 'kogio',
				'total_items' => $users_test->count()
			));
					
		$this->template->title = __t('Kategorie produktów');
        $this->template->content = View::factory('product/category')
									->bind('keyword', $keyword)
									->bind('pagination', $this->pagination)
									->bind('page', $page)
									->bind('sort', $sort)
									->bind('filtr', $filtr)
									->bind('products', $products);
	}
	
	public function action_detail()
	{
		$seo = $this->request->param('seo');
		$id = $this->request->param('id');
		
		//$s = Session::instance();
		//$cart_session = $s->get('cart_session');
			
		/*
		$product = ORM::Factory('item', $id);
		
		if ($_POST) {
            $data = ac_form::clear_form_data($_POST);
            $validata = $this->valid_kontakt_form($data);
            if (sizeof($validata['errors'])) {
                $message = __t('Błędnie wypełniony formularz.');
                $errors = $validata['errors'];
            } else {
			
					if( !empty( $data['comment_text'] ) ) {
					
						$c = ORM::Factory('moderator_page_comment');
						$c->title = '';
						$c->description = $data['comment_text'];
						$c->pages_id = $id;
						$c->is_change = 1;
						$c->mark = $data['rate_star'];
						$c->user_id = Auth::instance()->get_user()->id;
						$c->save();
						
						Message::success(__t('You just added an opinion. Your opinion is now waiting for verification and will appear on the site after 24 hours!'));
						$this->redirect($_SERVER['HTTP_REFERER']);						
					} else {
						Message::success(__t('Please fill in all fields!'));
						$this->redirect($_SERVER['HTTP_REFERER']);
					}
			
			}
		}
		
		
		$product->viewed += 1;
		$product->save();
		*/
		
		$params = explode("!", $seo ); 
		$name = explode("_", $params[1]);
		$category = $params[1];

		$items = array();
		$max_page = 0;
		
		$path = 'uploads/items/etykiety_podglad/'.$params[0].'/'.$params[1].'/';
		if( is_dir( $path ) ) {
			$dir=opendir($path);
			
			
			while($file_name=readdir($dir))
			{
				if(($file_name!=".")&&($file_name!="..")) {
					if( $seo != $file_name ) {
					$items[$file_name] = $file_name;
					$max_page++;
					}
				}
			}
		}
		
		sort($items);
		
		$type = $params[0];
		$divid = 6;
		if( $type == "wyborowa" ) $divid = 6;
		
		$max_page = floor($max_page/$divid);
		
		$this->template->title = __t('Karta produktu');
		$this->template->content = View::factory('product/detail')
									->bind('items', $items)
									->bind('path', $path)
									->bind('max_page', $max_page)
									->bind('divid', $divid)
									->bind('type', $type)
									->bind('cart_session', $cart_session)
									->bind('category', $category)
									->bind('seo', $seo);
		
	}	
	
	public function action_add_to_favorite()
	{
		if( !Auth::instance()->logged_in() ) {
			Message::error( __t('Musisz być zalogowany aby móc dokonać tej operacji'));
			$this->redirect('/');
			exit;
		}
		
		$user_id = Auth::instance()->get_user()->id;
		$id = $this->request->param('id');
		
		$item_favorite = ORM::Factory('item_favorite');
		$item_favorite->item_id = $id;
		$item_favorite->user_id = $user_id;
		$item_favorite->save();
		
		Message::success( __t('Congrats! The product has been added to your favorites!'));
		$this->redirect('Account/Favorite');

	}	
	
	public function action_download()
	{
		$canBuy = Kohana::$config->load('site.canBuy');
		if( !$canBuy ) {
			Message::success('Strona jest obecnie wyłączona');
			$this->redirect('/');
		}
	
		$id = $this->request->param('id');
		$file = ORM::Factory('File', $id);
		
		if( !Auth::instance()->logged_in() ) $user_id = 0;
		else $user_id = Auth::instance()->get_user()->id;
	
		$user_discount = ORM::Factory('User_Discount')->where('user_id','=', $user_id)->and_where_open()->where('file_id','=', $id)->or_where('file_id','=', 0)->and_where_close()->find();
		
		$this->template->title = 'Download';
        $this->template->content = View::factory('product/download')
									->bind('user_discount', $user_discount)
									->bind('file', $file);
	}
	
	public function action_get()
	{
		$hash = $this->request->param('id');
		$file = ORM::Factory('File')->where('hash','=', $hash)->find();

		if( !Auth::instance()->logged_in() ) $this->redirect('login');
		
		header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename="'.basename("$file->filename").'"'); 
		header('Content-Transfer-Encoding: binary');
		header('Connection: Keep-Alive');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Content-Length: ' . filesize("uploads/$file->filename"));
		ob_clean();
		flush();
		readfile("uploads/$file->filename");
		exit;
	}
	
	private function valid_kontakt_form($data) {
        //var_dump($data);
        $prefix = 'kontakt';
        $errors = array();


        return array(
            'data' => $data,
            'errors' => $errors
        );
    }
	
}