<?php
class ControllerModuleArticleList extends Controller {

    protected function index($setting) {
		static $module = 0;
        $this->language->load('module/article_list');		
        $this->load->model('news/category');
        $this->load->model('news/article');	
		$this->data['width'] = $this->config->get('config_image_article_width');
		$this->data['text_related'] = $this->language->get('text_related');	
		$this->data['text_date_added'] = $this->language->get('text_date_added');	
        $this->data['text_refine'] = $this->language->get('text_refine');
        $this->data['text_empty'] = $this->language->get('text_empty');
        $this->data['text_author'] = $this->language->get('text_author');
        $this->data['text_tax'] = $this->language->get('text_tax');
        $this->data['button_cart'] = $this->language->get('button_cart');
			
		if($setting['description_limit']){
		$description_limit=(int)$setting['description_limit'];
		}elseif($this->config->get('article_description_limit')){
		$description_limit=$this->config->get('article_description_limit');
		}			else{
			$description_limit=255;
		}	
			
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
							
		if ($setting['limit']!=0) {
			$limit = $setting['limit'];
		} else {
			$limit = 3;
		}
							
		if ($setting['parent_id']!=0) {
			$article_category_id = $setting['parent_id'];
		} else {
			$article_category_id = 0;
		}
		if ($setting['children_limit']!=0) {
			$children_limit = $setting['children_limit'];
		} else {
			$children_limit = 0;
		}
		
        $category_info = $this->model_news_category->getCategory($article_category_id);
		
        $this->data['href'] = 'index.php?route=module/article_list/getlist&cpath=' . $article_category_id .'&limit=' .$limit;
        $this->data['heading_title'] = $category_info['name'];
		/*sub category*/ 		
		
		$this->data['categories'] = array();

		$categories = $this->getCategories($article_category_id,$children_limit);

		foreach ($categories as $category) {
			$total = $this->model_news_article->getTotalArticles(array('filter_article_category_id'  => $category['article_category_id']));
			$this->data['categories'][] = array(
				'article_category_id' => $category['article_category_id'],
				'name'        => $category['name'],
				'total'        => $total,
				'href'        => 'index.php?route=module/article_list/getlist&cpath='. $category['article_category_id'].'&limit=' .$limit
			);	
		}		
		/*children category*/ 			
        $this->data['article_ajax_load'] = $this->getArticle($article_category_id,$page,$limit,$description_limit);
			
        $page = 1;		
		$data = array(
				'filter_article_category_id' => $setting['parent_id'], 
				'sort'               => 'p.date_added',
				'order'              => 'DESC',
				'start'              => ($page - 1) * $setting['limit'],
				'limit'              => $setting['limit']
			);
			$article_total = $this->model_news_article->getTotalArticles($data); 		
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->page =$page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = 'index.php?route=module/article_list/getlist&cpath='. $article_category_id .'&page={page}'.'&limit=' .$limit;
			$this->data['pagination'] = $pagination->render();
		
		$this->data['module'] = $module++; 
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/article_list/article_ajax_load.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/article_list/article_ajax_load.tpl';
        } else {
            $this->template = 'default/template/module/article_list/article_ajax_load.tpl';
        }

        $this->render();
    }
	public function getCategories($parent_id = 0, $limit) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_category c 
		LEFT JOIN " . DB_PREFIX . "news_category_description cd ON (c.article_category_id = cd.article_category_id) 
		LEFT JOIN " . DB_PREFIX . "news_category_to_store c2s ON (c.article_category_id = c2s.article_category_id) 
		WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' 
		AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name) LIMIT " . (int)$limit . "");		
		return $query->rows;
	}
    protected function getArticle($article_category_id,$page,$limit,$description_limit) {
        $this->load->model('news/category');
		$this->data['text_more'] = $this->language->get('text_more');
		$this->data['height'] = $this->config->get('config_image_article_height');
        $this->load->model('news/article');
		$this->load->model('news/utf8');
        $this->load->model('tool/image');
        $this->data['articles'] = array();
        $data = array(
            'filter_article_category_id' => $article_category_id,
            'sort' => 'p.date_added',
            'order' => 'DESC',
            'start' => ($page - 1) * $limit,
            'limit' => $limit
        );
        $article_total = $this->model_news_article->getTotalArticles($data);
        $results = $this->model_news_article->getArticles($data);
        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'],
                'date_added' => date($this->config->get('config_article_date_format'), strtotime($result['date_added'])),
                'related' => $related_articles,
                'thumb' => $image,
                '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, $description_limit) . '..',
                'rating' => $result['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', 'cpath=' . $article_category_id . '&article_id=' . $result['article_id'])
            );
        }
    }
	public function getlist(){	
        $this->language->load('module/article_list');
		$this->load->model('news/category');		
		$this->load->model('news/article');
		$this->data['width'] = $this->config->get('config_image_article_width');
		$this->data['text_empty'] = $this->language->get('text_empty');		
		$this->data['text_related'] = $this->language->get('text_related');	
		$this->data['text_date_added'] = $this->language->get('text_date_added');	
		$this->data['text_author'] = $this->language->get('text_author');	
		
		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 = 3;
		}
							
		if (isset($this->request->get['cpath'])) {
			$article_category_id = $this->request->get['cpath'];
		} else {
			$article_category_id = 0;
		}			
		$category_info = $this->model_news_category->getCategory($article_category_id);				
			
			$data = array(
				'filter_article_category_id' => $article_category_id, 
				'sort'               => 'p.date_added',
				'order'              => 'DESC',
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit
			);
					
			$article_total = $this->model_news_article->getTotalArticles($data); 			
		if($this->config->get('article_description_limit')){
		$description_limit=$this->config->get('article_description_limit');
		}			else{
			$description_limit=128;
		}	
			
        $this->data['article_ajax_load'] = $this->getArticle($article_category_id,$page,$limit,$description_limit);
					
			$pagination = new Pagination();
			$pagination->total = $article_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = 'index.php?route=module/article_list/getlist&cpath='.$article_category_id. '&page={page}'.'&limit=' .$limit;
		
			$this->data['pagination'] = $pagination->render();
		
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/article_list/article_ajax_list.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/article_list/article_ajax_list.tpl';
        } else {
            $this->template = 'default/template/module/article_list/article_ajax_list.tpl';
        }
       	$this->response->setOutput($this->render());
	}

}

?>