<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Sitemap extends CI_Controller {

    function __construct() {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Area_model');
        $this->load->model('InfoStructures_model');
        $this->load->model('Information_model');
        $this->load->model('Coupon_model');
    }

    public function index() {
        $this->permission->is_admin();
        $this->create($this->config->item('site_url'));
    }

    public function save($site_code) {
        $site_array = $this->config->item('site');
        $site = $site_array[$site_code];
        $site_url = $site['site_url'];
        $sitemap_name = $site['site_sitemap'];
        if (!empty($sitemap_name)) {
            $this->create($site_url, $sitemap_name);
        } else {
            echo 'no set sitemap name';
        }
    }

    public function create($site_url, $site_sitemap = false) {
        @set_time_limit(0);
		ini_set('max_execution_time', '0');
		ini_set ('memory_limit', '512M');
        $data = array();
        $sitemap_string = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ';
        $data['lastEditList'] = $this->Information_model->get_last_edit_list(false);
        foreach ($data['lastEditList'] as $item) {
            if ($item->ic_status == 1 && $item->ic_url != '' && strlen($item->ic_content)>50 ) {
                $sitemap_string.='<url><loc>' . $site_url . $item->ic_url . '</loc><lastmod>' . date('c', strtotime($item->ic_datetime)) . '</lastmod></url>';
            }
        }
        $sitemap_string.='</urlset>               ';

        if ($site_sitemap) {
            $this->load->helper('file');
            if (!write_file($site_sitemap, $sitemap_string)) {
                echo 'Unable to write the file';
            } else {
                echo 'File written!';
            }
        } else {
            $this->load->helper('download');
            force_download('sitemap.xml', $sitemap_string);
        }
        //echo $sitemap_string;die();
        // $this->load->view('bootstrap/header', $data);
        //$this->load->view('bootstrap/sitemap');
        // $this->load->view('bootstrap/footer');
    }

}
