<?php
class ControllerFeedNewsGoogleSitemap extends Controller {
   public function index() {
	  if ($this->config->get('news_google_sitemap_status')) {
		 $output  = '<?xml-stylesheet type="text/xsl" href="catalog/controller/feed/gss.xsl"?>';
		 $output .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';
		 
		 $this->load->model('news/article');
		 
		 $articles = $this->model_news_article->getArticles();
		 
		 foreach ($articles as $article) {
			$output .= '<url>';
			$output .= '<loc>' . $this->url->link('news/article', 'article_id=' . $article['article_id']) . '</loc>';
		 	$output .= '<lastmod>' . date('Y-m-d',  strtotime($article['date_modified'])) . '</lastmod>';
			$output .= '<changefreq>weekly</changefreq>';
			$output .= '<priority>1.0</priority>';
			$output .= '</url>';   
		 }
		 
			 $this->load->model('news/category');		 
			 $output .= $this->getCategories(0);
			 
		 $this->load->model('news/author');
		 
		 $authors = $this->model_news_author->getAuthors();
		 foreach ($authors as $author) {
			$output .= '<url>';
			$output .= '<loc>' . $this->url->link('news/author/info', 'author_id=' . $author['author_id']) . '</loc>';
			//$output .= '<lastmod></lastmod>';
			$output .= '<changefreq>weekly</changefreq>';
			$output .= '<priority>0.7</priority>';
			$output .= '</url>';         
		 }
		 
		 
			$output .= '<url>';
			$output .= '<loc>' . $this->url->link('news/author') . '</loc>';
			$output .= '<changefreq>weekly</changefreq>';
			$output .= '<priority>1.0</priority>';
			$output .= '</url>';  
		 
		 $output .= '</urlset>';
		 
		 $this->response->addHeader('Content-Type: application/xml');
		 $this->response->setOutput($output);
	  }
   }
   
   protected function getCategories($parent_id, $current_cpath = '') {
	  $output = '';
	  
	  $results = $this->model_news_category->getCategories($parent_id);
	  
	  foreach ($results as $result) {
		 if (!$current_cpath) {
			$new_cpath = $result['article_category_id'];
		 } else {
			$new_cpath = $current_cpath . '_' . $result['article_category_id'];
		 }

		 $output .= '<url>';
		 $output .= '<loc>' . $this->url->link('news/category', 'cpath=' . $new_cpath) . '</loc>';
		 $output .= '<lastmod>' . date('Y-m-d',  strtotime($result['date_modified'])) . '</lastmod>';
		 $output .= '<changefreq>weekly</changefreq>';
		 $output .= '<priority>0.7</priority>';
		 $output .= '</url>';         
		 
		   $output .= $this->getCategories($result['article_category_id'], $new_cpath);
	  }

	  return $output;
   }      
}
?>