<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Sitemap — dynamic XML sitemap for search engines.
 */
#[\AllowDynamicProperties]
class Sitemap extends Public_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(array(
			'service_model', 'portfolio_model', 'talent_model',
			'event_model', 'blog_model',
		));
	}

	public function index()
	{
		$urls = array();

		// Static pages
		foreach (array('', 'about', 'services', 'portfolio', 'talent', 'events', 'partners', 'blog', 'contact') as $seg)
		{
			$urls[] = array('loc' => base_url($seg), 'priority' => $seg === '' ? '1.0' : '0.8');
		}

		// Services
		foreach ($this->service_model->active() as $s)
		{
			$urls[] = array('loc' => base_url('services/view/'.$s['slug']), 'priority' => '0.7');
		}
		// Portfolio
		foreach ($this->portfolio_model->active() as $p)
		{
			$urls[] = array('loc' => base_url('portfolio/view/'.$p['slug']), 'priority' => '0.7');
		}
		// Talent
		foreach ($this->talent_model->active() as $t)
		{
			$urls[] = array('loc' => base_url('talent/view/'.$t['slug']), 'priority' => '0.6');
		}
		// Events
		foreach ($this->event_model->get_all(array('is_active' => 1)) as $e)
		{
			$urls[] = array('loc' => base_url('events/view/'.$e['slug']), 'priority' => '0.6');
		}
		// Blog
		foreach ($this->blog_model->published(200) as $b)
		{
			$urls[] = array('loc' => base_url('blog/view/'.$b['slug']), 'priority' => '0.6');
		}

		$this->output->set_content_type('application/xml');
		$xml  = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
		foreach ($urls as $u)
		{
			$xml .= "  <url>\n";
			$xml .= '    <loc>'.html_escape($u['loc'])."</loc>\n";
			$xml .= '    <changefreq>weekly</changefreq>'."\n";
			$xml .= '    <priority>'.$u['priority']."</priority>\n";
			$xml .= "  </url>\n";
		}
		$xml .= '</urlset>';
		$this->output->set_output($xml);
	}
}
