<?php 
class ControllerNewsCategory extends Controller {  
	public function index() { 
		$this->language->load('news/category');
		
		$this->load->model('news/category');
		
		$this->load->model('news/article');
		
		$this->load->model('tool/image'); 
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = FALSE;
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
							
		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = $this->config->get('config_catalog_limit');
		}
					
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
       		'separator' => false
   		);	
			
		if (isset($this->request->get['cpath'])) {
			$path = '';
		
			$parts = explode('_', (string)$this->request->get['cpath']);
		
			foreach ($parts as $path_id) {
				if (!$path) {
					$path = (int)$path_id;
				} else {
					$path .= '_' . (int)$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')
        			);
				}
			}		
		
			$article_category_id = (int)array_pop($parts);
		} else {
			$article_category_id = 0;
		}
		
		$category_info = $this->model_news_category->getCategory($article_category_id);
	
		if ($category_info) {
	  		$this->document->setTitle($category_info['name']);
			$this->document->setDescription($category_info['meta_description']);
			$this->document->setKeywords($category_info['meta_keyword']);
			
			$this->data['heading_title'] = $category_info['name'];
			
			$this->data['text_refine'] = $this->language->get('text_refine');
		$this->data['text_related'] = $this->language->get('text_related');	
			$this->data['text_empty'] = $this->language->get('text_empty');			
			$this->data['text_date_added'] = $this->language->get('text_date_added');
			$this->data['text_author'] = $this->language->get('text_author');
			$this->data['text_more'] = $this->language->get('text_more');
			$this->data['text_points'] = $this->language->get('text_points');
			$this->data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
			$this->data['text_display'] = $this->language->get('text_display');
			$this->data['text_list'] = $this->language->get('text_list');
			$this->data['text_grid'] = $this->language->get('text_grid');
			$this->data['text_sort'] = $this->language->get('text_sort');
			$this->data['text_limit'] = $this->language->get('text_limit');
					
			$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_continue'] = $this->language->get('button_continue');
			$this->data['width'] = $this->config->get('config_image_article_width');
			$this->data['category_width'] =$this->config->get('config_article_category_width');
					
			if ($category_info['image']) {
				$this->data['thumb'] = $this->model_tool_image->resize($category_info['image'], $this->config->get('config_article_category_width'), $this->config->get('config_article_category_height'));
			} else {
				$this->data['thumb'] = '';
			}
									
			$this->data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
			$this->data['compare'] = $this->url->link('news/compare');
			
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}	
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
								
			$this->data['categories'] = array();
			
			$results = $this->model_news_category->getCategories($article_category_id);
			
			foreach ($results as $result) {
				$data = array(
					'filter_article_category_id'  => $result['article_category_id'],
					'filter_sub_category' => true
				);
				
				if ($result['image']) {
						$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_article_category_width'), $this->config->get('config_article_category_height'));
				} else {
						$image = $this->model_tool_image->resize('no_image.jpg', $this->config->get('config_article_category_width'), $this->config->get('config_article_category_height'));
				}
				$article_total = $this->model_news_article->getTotalArticles($data);				
				
				$this->data['categories'][] = array(	
					'name'  => $result['name'] . ($this->config->get('config_article_count') ? ' (' . $article_total . ')' : ''),
					'image'=>$image,
					'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '_' . $result['article_category_id'] . $url)
				);
			}
			
			$this->data['articles'] = array();
			
			$data = array(
				'filter_article_category_id' => $article_category_id, 
				'sort'               => $sort,
				'order'              => $order,
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit
			);
					
			$article_total = $this->model_news_article->getTotalArticles($data); 
			
			$results = $this->model_news_article->getArticles($data);
			$this->load->model('news/utf8');
			if($this->config->get('article_description_limit')){
				$description_limit=$this->config->get('article_description_limit');
			}			else{
				$description_limit=255;
			}
			foreach ($results as $result) {	
					$related_articles = array();
						
					$related = $this->model_news_article->getArticleRelatedLimit($result['article_id'],3);	
					foreach ($related as $relate) {							
						$related_articles[] = array(
							'article_id' => $relate['article_id'],
							'name'    	 => $relate['name'],
							'href'    	 => $this->url->link('news/article', 'article_id=' . $relate['article_id']),
						);
					}	
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_article_width'), $this->config->get('config_image_article_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'],
					'related' => $related_articles,
					'thumb'       => $image,
					'name'        => $result['name'],
					'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
					'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, $description_limit) . '..',
					'rating'      => $result['rating'],
					'viewed'     => sprintf($this->language->get('text_viewed'), (int)$result['viewed']),
					'comments'     => sprintf($this->language->get('text_comments'), (int)$result['comments']),
					'href'        => $this->url->link('news/article', 'cpath=' . $article_category_id .'&article_id=' . $result['article_id'])
				);
			}
			
			$url = '';
	
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
							
			$this->data['sorts'] = array();
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_date_desc'),
				'value' => 'p.date_added-DESC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=p.date_added&order=DESC' . $url)
			);
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_asc'),
				'value' => 'pd.name-ASC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=pd.name&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_desc'),
				'value' => 'pd.name-DESC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=pd.name&order=DESC' . $url)
			);
						
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_sort_asc'),
				'value' => 'p.sort_order-ASC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=p.sort_order&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_sort_desc'),
				'value' => 'p.sort_order-DESC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=p.sort_order&order=DESC' . $url)
			);
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_viewed_asc'),
				'value' => 'p.viewed-ASC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=p.viewed&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_viewed_desc'),
				'value' => 'p.viewed-DESC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=p.viewed&order=DESC' . $url)
			);
			
			if ($this->config->get('config_comment_status')) {
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_desc'),
					'value' => 'rating-DESC',
					'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=rating&order=DESC' . $url)
				); 
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_asc'),
					'value' => 'rating-ASC',
					'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=rating&order=ASC' . $url)
				);
			}
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_author_asc'),
				'value' => 'na.author-ASC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=na.author&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_author_desc'),
				'value' => 'na.author-DESC',
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . '&sort=na.author&order=DESC' . $url)
			);
			
			$url = '';
	
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->data['limits'] = array();
			
			$this->data['limits'][] = array(
				'text'  => $this->config->get('config_catalog_limit'),
				'value' => $this->config->get('config_catalog_limit'),
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&limit=' . $this->config->get('config_catalog_limit'))
			);
						
			$this->data['limits'][] = array(
				'text'  => 25,
				'value' => 25,
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&limit=25')
			);
			
			$this->data['limits'][] = array(
				'text'  => 50,
				'value' => 50,
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&limit=50')
			);

			$this->data['limits'][] = array(
				'text'  => 75,
				'value' => 75,
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&limit=75')
			);
			
			$this->data['limits'][] = array(
				'text'  => 100,
				'value' => 100,
				'href'  => $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&limit=100')
			);
						
			$url = '';
	
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
	
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
					
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('news/category', 'cpath=' . $this->request->get['cpath'] . $url . '&page={page}');
		
			$this->data['pagination'] = $pagination->render();
		
			$this->data['config_sort'] = $this->config->get('config_article_sort');
			$this->data['sort'] = $sort;
			$this->data['order'] = $order;
			$this->data['limit'] = $limit;
		
			$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/category.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/category.tpl';
			} else {
				$this->template = 'default/template/news/category.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['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
						
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('news/category', $url),
				'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());
		}
  	}
}
?>