<?php 
class ControllerVideos extends Controller { 
	function index() {
		$customer =& $this->locator->get('customer'); 
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$database =& $this->locator->get('database');
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$currency =& $this->locator->get('currency');
		$tax      =& $this->locator->get('tax');
	    $template->set('title', 'Feedback Videos');
    	$view = $this->locator->create('template');
    	
       if($request->get('feedback_id')){ 
       	   $feedback_info=$database->getRow("select f.video_url,f.product_id,f.date_added,c.firstname,c.lastname from feedback f left join customer c on c.customer_id=f.customer_id  where feedback_id='".$request->get('feedback_id')."'");
       	  $videos_data=$this->parse_youtube_url($feedback_info['video_url'],'embed');
       	   
       	   $product_info = $database->getRow("select p.product_id,p.price,i.image_id,p.quantity,p.sold,p.shipping,p.weight,p.date_added,p.tax_class_id,p.viewed,p.uploader,p.diggs,p.brand,p.color,p.material,p.model,p.sku,pd.name,i.filename from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.product_id = '" .$feedback_info['product_id']. "' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1'");
           $review_total=$database->getRow("select count(*) as total from review where product_id='" . $feedback_info['product_id'] . "'");
           $ave_rating=$database->getRow("select sum(rating) as sum from review where product_id='" .$feedback_info['product_id']. "'");
           $ave_rating=$ave_rating['sum'];    
  	       $ave_rating =number_format($ave_rating/$review_total['total'],1);
       	   $sale_price_data=explode(' ',$currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'])));
       	   $product_data=array(
       	        'name'  => $product_info['name'],
       	        'product_id'=>$feedback_info['product_id'],
       	        'price' => '<label>'.$sale_price_data[0].'</label>'.' '.$sale_price_data[1],
       	        'thumb' => $image->resize($product_info['filename'], 100, 100),
       	        'review_total' => $review_total['total'],
       	        'ave_rating'  =>$ave_rating['sum'],
       	        'author'  =>$feedback_info['firstname'].' '.$feedback_info['lastname'],
       	        'date_added'=>$feedback_info['date_added']
       	   );      
       	   $view->set('product_data',$product_data);
       	   $view->set('videos', $videos_data);       	   
       }			
	
		$template->set('content', $view->fetch('content/videos.tpl'));
		$template->set($module->fetchPublic());
		$response->set($template->fetch('layout.tpl'));	
	}
	
	function parse_youtube_url($url,$return='embed',$width='',$height='',$rel=0)  
	{   
	    $urls = parse_url($url);   
	       
	    //expect url is http://youtu.be/abcd, where abcd is video iD  
	    if($urls['host'] == 'youtu.be') $id = ltrim($urls['path'],'/');   
	    //expect  url is http://www.youtube.com/embed/abcd   
	    else if(strpos($urls['path'],'embed') == 1) $id = end(explode('/',$urls['path']));   
	     //expect url is abcd only   
	    else if(strpos($url,'/')===false) $id = $url;   
	    //expect url is http://www.youtube.com/watch?v=abcd   
	    else{   
	        parse_str($urls['query']);   
	        $id = $v;   
	    }   
	    //return embed iframe   
	    if($return == 'embed') return '<iframe width="'.($width?$width:560).'" height="'.($height?$height:349).'" src="http://www.youtube.com/embed/'.$id.'?rel='.$rel.'" frameborder="0" allowfullscreen></iframe>';   
	    //return normal thumb   
	    else if($return == 'thumb') return 'http://i1.ytimg.com/vi/'.$id.'/default.jpg';   
	    //return hqthumb   
	    else if($return == 'hqthumb') return 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg';  
	    // else return id   
	    else return $id;   
	} 	

}
?>  