<?php
class ControllerFeedGoogleSitemap extends Controller
{
	public function index()
	{
			if ($this->config->get('google_sitemap_status')) {

				ini_set('memory_limit', '256m');
				ini_set('max_execution_time', '900');

				$output  = '<?xml version="1.0" encoding="UTF-8"?>';
				$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

					$this->load->model('catalog/product');
					$this->load->type('tipofuturaserver');
					$this->load->model('catalog/category');
					$this->load->model('catalog/manufacturer');
					$this->load->model('catalog/information');

					$products = $this->model_catalog_product->getProducts( array('filter_list_in_feeds' => Type_DsimDnaoServer::Tp_Sim) );

					foreach ($products as $product) {
					 $output .= '<url>';
					 $output .= '<loc>' . str_replace('&','&amp;', $this->url->link('product/product', 'product_id=' . $product['product_id']) ) . '</loc>';
					 $output .= '<changefreq>weekly</changefreq>';
					 $output .= '<priority>1.0</priority>';
					 $output .= '</url>';
					}

					$output .= $this->getCategories(0);
					$manufacturers = $this->model_catalog_manufacturer->getManufacturers();
					$informations = $this->model_catalog_information->getInformations();

					foreach ($manufacturers as $manufacturer) {
						$output .= '<url>';
						$output .= '<loc>' . str_replace('&', '&amp;', $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $manufacturer['manufacturer_id'])) . '</loc>';
						$output .= '<changefreq>weekly</changefreq>';
						$output .= '<priority>0.7</priority>';
						$output .= '</url>';

						$products = $this->model_catalog_product->getProducts(array(
								'filter_manufacturer_id' => $manufacturer['manufacturer_id'],
								'filter_content'         => (ftr_geral::isIsset($this->config->get('config_setting_search_name'))) ? $this->config->get('config_setting_search_name') : Type_FilterContent::Tp_Default,
								'filter_list_in_feeds'   => Type_DsimDnaoServer::Tp_Sim
						));

						foreach ($products as $product) {
								$output .= '<url>';
								$output .= '<loc>' . str_replace('&', '&amp;', $this->url->link('product/product', 'manufacturer_id=' . $manufacturer['manufacturer_id'] . '&product_id=' . $product['product_id'])) . '</loc>';
								$output .= '<changefreq>weekly</changefreq>';
								$output .= '<priority>1.0</priority>';
								$output .= '</url>';
						}
					}

					foreach ($informations as $information) {
							$output .= '<url>';
							$output .= '<loc>' . str_replace('&', '&amp;', $this->url->link('information/information', 'information_id=' . $information['information_id'])) . '</loc>';
							$output .= '<changefreq>weekly</changefreq>';
							$output .= '<priority>0.5</priority>';
							$output .= '</url>';
					}

					$output .= '<url>';
					$output .= '<loc>' . str_replace('&', '&', $this->url->link('common/home')) . '</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);
			}
	}
	
	private function getCategories($parent_id, $path = '')
	{
			$output = '';
			$results = $this->model_catalog_category->getCategories($parent_id);
	
			foreach ($results as $result) {
					$output .= '<url>';
					$output .= '<loc>' . str_replace('&', '&amp;', $this->url->link('product/category', 'path=' . $path . $result['category_id'])) . '</loc>';
					$output .= '<changefreq>weekly</changefreq>';
					$output .= '<priority>0.7</priority>';
					$output .= '</url>';
	
					$output .= $this->getCategories($result['category_id'], $path . $result['category_id'] . '_');
			}
	
			return $output;
	}
}
