<?php 
class ControllerFeedNewsGoogleBase extends Controller {
	public function index() {
		if ($this->config->get('news_google_base_status')) { 
			$output  = '<?xml version="1.0" encoding="UTF-8" ?>';
			$output .= '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">';
            $output .= '<channel>';
			$output .= '<title>' . $this->config->get('config_name') . '</title>'; 
			$output .= '<description>' . $this->config->get('config_meta_description') . '</description>';
			$output .= '<link>' . HTTP_SERVER . '</link>';
			
			$this->load->model('news/category');
			
			$this->load->model('news/article');
			
			$this->load->model('tool/image');
			
			$articles = $this->model_news_article->getArticles();
			
			foreach ($articles as $article) {
				if ($article['description']) {
					$output .= '<item>';
					$output .= '<title>' . $article['name'] . '</title>';
					$output .= '<link>' . $this->url->link('news/article', 'article_id=' . $article['article_id']) . '</link>';
					$output .= '<description>' . $article['description'] . '</description>';
					$output .= '<g:author>' . html_entity_decode($article['author'], ENT_QUOTES, 'UTF-8') . '</g:author>';
					$output .= '<g:condition>new</g:condition>';
					$output .= '<g:id>' . $article['article_id'] . '</g:id>';
					
					if ($article['image']) {
						$output .= '<g:image_link>' . $this->model_tool_image->resize($article['image'], 500, 500) . '</g:image_link>';
					} else {
						$output .= '<g:image_link>' . $this->model_tool_image->resize('no_image.jpg', 500, 500) . '</g:image_link>';
					}
					
			   
					$categories = $this->model_news_article->getCategories($article['article_id']);
					
					foreach ($categories as $category) {
						$cpath = $this->getPath($category['article_category_id']);
						
						if ($cpath) {
							$string = '';
							
							foreach (explode('_', $cpath) as $cpath_id) {
								$category_info = $this->model_news_category->getCategory($cpath_id);
								
								if ($category_info) {
									if (!$string) {
										$string = $category_info['name'];
									} else {
										$string .= ' &gt; ' . $category_info['name'];
									}
								}
							}
						 
							$output .= '<g:article_type>' . $string . '</g:article_type>';
						}
					}					
					$output .= '</item>';
				}
			}
			
			$output .= '</channel>'; 
			$output .= '</rss>';	
			
			$this->response->addHeader('Content-Type: application/rss+xml');
			$this->response->setOutput($output);
		}
	}
	
	protected function getPath($parent_id, $current_cpath = '') {
		$category_info = $this->model_news_category->getCategory($parent_id);
	
		if ($category_info) {
			if (!$current_cpath) {
				$new_cpath = $category_info['article_category_id'];
			} else {
				$new_cpath = $category_info['article_category_id'] . '_' . $current_cpath;
			}	
		
			$cpath = $this->getPath($category_info['parent_id'], $new_cpath);
					
			if ($cpath) {
				return $cpath;
			} else {
				return $new_cpath;
			}
		}
	}		
}
?>