<?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.google.com/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.9 http://www.google.com/schemas/sitemap/0.9/sitemap.xsd">';

			$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->getSiteMapProducts();

			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>';

				foreach ($products as $product) {
					if ($manufacturer['manufacturer_id'] == $product['manufacturer_id']) {
						$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;
	}
}