<?php  
class ControllerNewsArticle extends Controller {
	private $error = array(); 
	
	public function index() { 
				
			if (file_exists('catalog/view/javascript/jquery/tabs.js')&&$this->config->get('config_template')!=='default') {
			$this->document->addScript('catalog/view/javascript/jquery/tabs.js');
			}
			/*
			if(VERSION=='1.5.1'||VERSION=='1.5.1.1'||VERSION=='1.5.1.2'||VERSION=='1.5.1.3'){
			}else{
			$this->document->addStyle('catalog/view/javascript/jquery/my_fancybox/fancyboxx.css');
			$this->document->addScript('catalog/view/javascript/jquery/my_fancybox/jquery.fancybox.js.php');
			}
			if(VERSION=='1.5.5'||VERSION=='1.5.5.1'){
			$this->document->addScript('catalog/view/javascript/jquery/tabs.js');
			}*/ 
			
	
		$this->language->load('news/article');
	
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),			
			'separator' => false
		);
		
		$this->load->model('news/category');	
		
		if (isset($this->request->get['cpath'])) {
			$path = '';
				
			foreach (explode('_', $this->request->get['cpath']) as $path_id) {
				if (!$path) {
					$path = $path_id;
				} else {
					$path .= '_' . $path_id;
				}
				
				$category_info = $this->model_news_category->getCategory($path_id);
				
				if ($category_info) {
					$this->data['breadcrumbs'][] = array(
						'text'      => $category_info['name'],
						'href'      => $this->url->link('news/category', 'cpath=' . $path),
						'separator' => $this->language->get('text_separator')
					);
				}
			}
		}
		
		$this->load->model('news/author');	
		
		if (isset($this->request->get['author_id'])) {
			$this->data['breadcrumbs'][] = array( 
				'text'      => $this->language->get('text_brand'),
				'href'      => $this->url->link('news/author'),
				'separator' => $this->language->get('text_separator')
			);	
				
			$author_info = $this->model_news_author->getAuthor($this->request->get['author_id']);

			if ($author_info) {	
				$this->data['breadcrumbs'][] = array(
					'text'	    => $author_info['author'],
					'href'	    => $this->url->link('news/author/info', 'author_id=' . $this->request->get['author_id']),					
					'separator' => $this->language->get('text_separator')
				);
			}
		}
		
		if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_tag'])) {
			$url = '';
			
			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . $this->request->get['filter_name'];
			}
						
			if (isset($this->request->get['filter_tag'])) {
				$url .= '&filter_tag=' . $this->request->get['filter_tag'];
			}
						
			if (isset($this->request->get['filter_description'])) {
				$url .= '&filter_description=' . $this->request->get['filter_description'];
			}
			
			if (isset($this->request->get['filter_article_category_id'])) {
				$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
			}	
						
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_search'),
				'href'      => $this->url->link('news/search', $url),
				'separator' => $this->language->get('text_separator')
			); 	
		}
		
		if (isset($this->request->get['article_id'])) {
			$article_id = (int)$this->request->get['article_id'];
		} else {
			$article_id = 0;
		}
		
		$this->load->model('news/article');
		
		$article_info = $this->model_news_article->getArticle($article_id);
		
		if ($article_info) {
			$url = '';
			if (isset($this->request->get['cpath'])) {
				$url .= '&path=' . $this->request->get['cpath'];
			}
			
			if (isset($this->request->get['author_id'])) {
				$url .= '&author_id=' . $this->request->get['author_id'];
			}			

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . $this->request->get['filter_name'];
			}
						
			if (isset($this->request->get['filter_tag'])) {
				$url .= '&filter_tag=' . $this->request->get['filter_tag'];
			}
			
			if (isset($this->request->get['filter_description'])) {
				$url .= '&filter_description=' . $this->request->get['filter_description'];
			}	
						
			if (isset($this->request->get['filter_article_category_id'])) {
				$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
			}
												
			$this->data['breadcrumbs'][] = array(
				'text'      => $article_info['name'],
				'href'      => $this->url->link('news/article', $url . '&article_id=' . $this->request->get['article_id']),
				'separator' => $this->language->get('text_separator')
			);			
			
			$this->document->setTitle($article_info['name']);
			$this->document->setDescription($article_info['meta_description']);
			$this->document->setKeywords($article_info['meta_keyword']);
			$this->document->addLink($this->url->link('news/article', 'article_id=' . $this->request->get['article_id']), 'canonical');
			$this->data['heading_title'] = $article_info['name'];
			$this->data['date_added'] = date($this->config->get('config_article_date_format'), strtotime($article_info['date_added']));
			
			
			/*poll*/ 
		$this->language->load('news/poll');
				$this->load->model('news/poll');
			$this->data['text_vote']=$this->language->get('text_vote');
			$this->data['text_total_votes']=$this->language->get('text_total_votes');
			$this->data['text_no_votes']=$this->language->get('text_no_votes');
			$this->data['text_no_poll']=$this->language->get('text_no_poll');
		$poll_id=$article_info['poll_id'];	
			$this->data['poll_id']=$poll_id;
		

		if(isset($this->request->cookie['poll_answered'.$poll_id])){
			if($this->request->cookie['poll_answered'.$poll_id]==$poll_id){ 
			$this->data['answered']=TRUE;
			}
		}
		$this->data['poll_id']=$poll_id;
			$this->data['poll_data']=$this->model_news_poll->getPollData($poll_id);
			$this->data['text_poll_results']=$this->language->get('text_poll_results');
			$this->data['poll_results']=$this->url->link('news/poll&poll_id='.$poll_id);
			$this->data['poll_action']=$this->url->link('news/poll');
			$reactions=$this->model_news_poll->getPollResults($poll_id);
			$total_votes=$this->model_news_poll->getTotalResults($poll_id);
				if($reactions){ 
		$this->data['reactions']=TRUE;
			$percent=array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
			$totals=array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		foreach($reactions as $reaction){ 
		$totals[$reaction['answer'] - 1]++;
			}

			for($i=0;
			$i < 15;
			$i++){ 
		$percent[$i]=round(100 * ($totals[$i]/$total_votes));
		$vote[$i]=$totals[$i];
			}
		$this->data['percent']=$percent;
		$this->data['vote']=$vote;
			$this->data['total_votes']=$total_votes;
			$this->data['text_percent']=$this->language->get('text_percent');
			$this->data['text_answer']=$this->language->get('text_answer');
		}
		/*end poll*/ 
			
			
			$this->data['text_date_added'] = $this->language->get('text_date_added');
			$this->data['width'] = $this->config->get('config_image_article_width');
			$this->data['text_select'] = $this->language->get('text_select');
			$this->data['text_author'] = $this->language->get('text_author');
			$this->data['text_more'] = $this->language->get('text_more');
			$this->data['text_reward'] = $this->language->get('text_reward');
			$this->data['text_points'] = $this->language->get('text_points');	
			$this->data['text_discount'] = $this->language->get('text_discount');
			$this->data['text_stock'] = $this->language->get('text_stock');
			$this->data['text_price'] = $this->language->get('text_price');
			$this->data['text_tax'] = $this->language->get('text_tax');
			$this->data['text_discount'] = $this->language->get('text_discount');
			$this->data['text_option'] = $this->language->get('text_option');
			$this->data['text_qty'] = $this->language->get('text_qty');
			$this->data['text_or'] = $this->language->get('text_or');
			$this->data['text_write'] = $this->language->get('text_write');
			$this->data['text_note'] = $this->language->get('text_note');
			$this->data['text_share'] = $this->language->get('text_share');
			$this->data['text_wait'] = $this->language->get('text_wait');
			$this->data['text_tags'] = $this->language->get('text_tags');
			$this->data['text_related_product'] = $this->language->get('text_related_product');
			$this->data['text_login_to_download'] = $this->language->get('text_login_to_download');
			
			$this->data['entry_name'] = $this->language->get('entry_name');
			$this->data['entry_comment'] = $this->language->get('entry_comment');
			$this->data['entry_rating'] = $this->language->get('entry_rating');
			$this->data['entry_good'] = $this->language->get('entry_good');
			$this->data['entry_bad'] = $this->language->get('entry_bad');
			$this->data['entry_captcha'] = $this->language->get('entry_captcha');
			
			$this->data['button_cart'] = $this->language->get('button_cart');
			$this->data['button_wishlist'] = $this->language->get('button_wishlist');
			$this->data['button_compare'] = $this->language->get('button_compare');			
			$this->data['button_upload'] = $this->language->get('button_upload');
			$this->data['button_continue'] = $this->language->get('button_continue');
			
			$this->load->model('news/comment');

			$this->data['tab_description'] = $this->language->get('tab_description');
			$this->data['tab_image'] = $this->language->get('tab_image');
			$this->data['tab_poll'] = $this->language->get('tab_poll');
			$this->data['tab_download'] = $this->language->get('tab_download');
			$this->data['tab_comment'] = sprintf($this->language->get('tab_comment'), $this->model_news_comment->getTotalCommentsByArticleId($this->request->get['article_id']));
			$this->data['tab_related'] = $this->language->get('tab_related');
			
			$this->data['article_id'] = $this->request->get['article_id'];
			$this->data['author'] = $article_info['author'];
			$this->data['authors'] = $this->url->link('news/author/info', 'author_id=' . $article_info['author_id']);
			$this->data['model'] = $article_info['model'];
						
			$this->load->model('tool/image');

			if ($article_info['image']) {
				$this->data['popup'] = $this->model_tool_image->resize($article_info['image'], $this->config->get('config_article_popup_width'), $this->config->get('config_article_popup_height'));
			} else {
				$this->data['popup'] = '';
			}
			
			if ($article_info['image']) {
				$this->data['thumb'] = $this->model_tool_image->resize($article_info['image'], $this->config->get('config_article_thumb_width'), $this->config->get('config_article_thumb_height'));
			} else {
				$this->data['thumb'] = '';
			}
			/*version*/ 
			$this->data['downloads'] = array();
			$downloads_results= $this->model_news_article->getDownloads($this->request->get['article_id']);
			
			foreach ($downloads_results as $download) {
			if (file_exists(DIR_IMAGE.'icon/'.substr(strrchr($download['mask'], '.'), 1).'.png')) {
			$ext=HTTP_SERVER.'image/icon/'.substr(strrchr($download['mask'], '.'), 1).'.png';
			}else{
			$ext=HTTP_SERVER.'image/icon/file.png';			
			}
				$this->data['downloads'][] = array(
				'name' => $download['name'],		
				'mask' => $download['mask'],		
				'thumb' => $ext,		
				'href' => $this->url->link('news/article/download','&auth_key=' . $download['auth_key']),			
				);
			}
			
			$this->data['images'] = array();
			
			$results = $this->model_news_article->getArticleImages($this->request->get['article_id']);
			
			foreach ($results as $result) {
				$this->data['images'][] = array(
					'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('config_article_popup_width'), $this->config->get('config_article_popup_height')),
					'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_article_additional_width'), $this->config->get('config_article_additional_height')),					
					'title' => strip_tags($result['image_description'])
					);
			}	
						
			
			
			$this->data['comment_status'] = $this->config->get('config_comment_status');
			$this->data['comments'] = sprintf($this->language->get('text_comments'), (int)$article_info['comments']);
			$this->data['rating'] = (int)$article_info['rating'];
			$this->data['description'] = html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8');
			
			$this->data['articles'] = array();
			
			$results = $this->model_news_article->getArticleRelated($this->request->get['article_id']);			
			$this->load->model('news/utf8');
			
			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_article_related_width'), $this->config->get('config_article_related_height'));
				} else {
					$image = false;
				}
								
				if ($this->config->get('config_comment_status')) {
					$rating = (int)$result['rating'];
				} else {
					$rating = false;
				}
							
				$this->data['articles'][] = array(
					'article_id' => $result['article_id'],
					'thumb'   	 => $image,
               		'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
					'name'    	 => $result['name'],
					'author'    	 => $result['author'],
					'author_href'    	 => $this->url->link('news/author/info', 'author_id=' . $result['author_id']),
					'description'    	 => $this->model_news_utf8->utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 255) . '..',
					'rating'     => $rating,
					'comments'    => sprintf($this->language->get('text_comments'), (int)$result['comments']),
					'viewed'    => sprintf($this->language->get('text_viewed'), (int)$result['viewed']),
					'href'    	 => $this->url->link('news/article', 'article_id=' . $result['article_id']),
				);
			}	
			$this->data['products'] = array();
			
			$results = $this->model_news_article->getProductRelated($this->request->get['article_id']);
			
			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
				} else {
					$image = false;
				}
				
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}
						
				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = false;
				}
				
				if ($this->config->get('config_comment_status')) {
					$rating = (int)$result['rating'];
				} else {
					$rating = false;
				}
							
				$this->data['products'][] = array(
					'product_id' => $result['product_id'],
					'thumb'   	 => $image,
					'name'    	 => $result['name'],
					'price'   	 => $price,
					'special' 	 => $special,
					'rating'     => $rating,
					'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
					'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
				);
			}	
			
			$this->data['tags'] = array();
					
			$tags = explode(',', $article_info['tag']);
			
			foreach ($tags as $tag) {
				$this->data['tags'][] = array(
					'tag'  => trim($tag),
					'href' => $this->url->link('news/search', 'filter_tag=' . trim($tag))
				);
			}
			
			$this->model_news_article->updateViewed($this->request->get['article_id']);
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/article.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/article.tpl';
			} else {
				$this->template = 'default/template/news/article.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
						
			$this->response->setOutput($this->render());
		} else {
			$url = '';
			
			if (isset($this->request->get['cpath'])) {
				$url .= '&path=' . $this->request->get['cpath'];
			}
			
			if (isset($this->request->get['author_id'])) {
				$url .= '&author_id=' . $this->request->get['author_id'];
			}			

			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . $this->request->get['filter_name'];
			}	
					
			if (isset($this->request->get['filter_tag'])) {
				$url .= '&filter_tag=' . $this->request->get['filter_tag'];
			}
							
			if (isset($this->request->get['filter_description'])) {
				$url .= '&filter_description=' . $this->request->get['filter_description'];
			}
					
			if (isset($this->request->get['filter_article_category_id'])) {
				$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
			}
								
      		$this->data['breadcrumbs'][] = array(
        		'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('news/article', $url . '&article_id=' . $article_id),
        		'separator' => $this->language->get('text_separator')
      		);			
		
      		$this->document->setTitle($this->language->get('text_error'));

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
						
			$this->response->setOutput($this->render());
    	}
  	}
	
	public function comment() {
    	$this->language->load('news/article');
		
		$this->load->model('news/comment');

		$this->data['text_on'] = $this->language->get('text_on');
		$this->data['text_no_comments'] = $this->language->get('text_no_comments');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}  
		
		$this->data['comments'] = array();
		
		$comment_total = $this->model_news_comment->getTotalCommentsByArticleId($this->request->get['article_id']);
			
		$results = $this->model_news_comment->getCommentsByArticleId($this->request->get['article_id'], ($page - 1) * 5, 5);
      		
		foreach ($results as $result) {
        	$this->data['comments'][] = array(
        		'author'     => $result['author'],
				'text'       => $result['text'],
				'rating'     => (int)$result['rating'],
        		'comments'    => sprintf($this->language->get('text_comments'), (int)$comment_total),
        		'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
        	);
      	}			
			
		$pagination = new Pagination();
		$pagination->total = $comment_total;
		$pagination->page = $page;
		$pagination->limit = 5; 
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('news/article/comment', 'article_id=' . $this->request->get['article_id'] . '&page={page}');
			
		$this->data['pagination'] = $pagination->render();
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/comment.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/news/comment.tpl';
		} else {
			$this->template = 'default/template/news/comment.tpl';
		}
		
		$this->response->setOutput($this->render());
	}
	
	public function write() {
		$this->language->load('news/article');
		
		$this->load->model('news/comment');
		$this->load->model('news/utf8');
		
		$json = array();
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if (($this->model_news_utf8->utf8_strlen($this->request->post['name']) < 3) || ($this->model_news_utf8->utf8_strlen($this->request->post['name']) > 25)) {
				$json['error'] = $this->language->get('error_name');
			}
			
			if (($this->model_news_utf8->utf8_strlen($this->request->post['text']) < 25) || ($this->model_news_utf8->utf8_strlen($this->request->post['text']) > 1000)) {
				$json['error'] = $this->language->get('error_text');
			}
	
			if (empty($this->request->post['rating'])) {
				$json['error'] = $this->language->get('error_rating');
			}
	
			if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
				$json['error'] = $this->language->get('error_captcha');
			}
				
			if (!isset($json['error'])) {
				$this->model_news_comment->addComment($this->request->get['article_id'], $this->request->post);
				
				$json['success'] = $this->language->get('text_success');
			}
		}
		
		$this->response->setOutput(json_encode($json));
	}
	
	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
	public function download(){	
		$this->load->model('news/article');
		if (isset($this->request->get['auth_key'])) {
			$auth_key = $this->request->get['auth_key'];
		} else {
			$auth_key = 0;
		}
	
		if ($this->config->get('config_login_to_download')) {
				if (!$this->customer->isLogged()) {
					$this->session->data['redirect'] = $this->url->link('news/article/download','&auth_key='.$auth_key);
			  
					$this->redirect($this->url->link('account/login', '', 'SSL'));
				} 
		}
		$download_info=$this->model_news_article->getDownloadFile($auth_key);
						
		if($download_info) {
			$file = DIR_DOWNLOAD.$download_info['filename'];
			$mask = basename($download_info['mask']);
			if (!headers_sent()) {
				if (file_exists($file)) {
					header('Content-Type: application/octet-stream');
					header('Content-Description: File Transfer');
					header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
					header('Content-Transfer-Encoding: binary');
					header('Expires: 0');
					header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
					header('Pragma: public');
					header('Content-Length: ' . filesize($file));					
					readfile($file, 'rb');					
					exit;
				} else {//error
			$this->load->language('news/article');							
						$this->data['breadcrumbs'][] = array(
							'text'      => $this->language->get('text_error'),
							'href'      => $this->url->link('news/article/download','auth_key='.$auth_key),
							'separator' => $this->language->get('text_separator')
						);					
						$this->document->setTitle($this->language->get('text_download_error'));
						$this->data['heading_title'] = $this->language->get('text_download_error');
						$this->data['text_error'] = $this->language->get('text_download_not_found');
						$this->data['button_continue'] = $this->language->get('button_continue');
						$this->data['continue'] = $this->url->link('common/home');
						if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
							$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
						} else {
							$this->template = 'default/template/error/not_found.tpl';
						}			
						$this->children = array(
							'common/column_left',
							'common/column_right',
							'common/content_top',
							'common/content_bottom',
							'common/footer',
							'common/header'
						);						
						$this->response->setOutput($this->render());
				}
			} else {
				exit('Error: Headers already sent out!');
			}	
		} else {//error
			$this->load->language('news/article');										
      		$this->data['breadcrumbs'][] = array(
        		'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('news/article/download','auth_key='.$auth_key),
        		'separator' => $this->language->get('text_separator')
      		);					
      		$this->document->setTitle($this->language->get('text_download_error'));
      		$this->data['heading_title'] = $this->language->get('text_download_error');
      		$this->data['text_error'] = $this->language->get('text_download_not_found');
      		$this->data['button_continue'] = $this->language->get('button_continue');
      		$this->data['continue'] = $this->url->link('common/home');
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);						
			$this->response->setOutput($this->render());
		}
	}
}
?>