<?php

include_once 'application/modules/sitemap/libraries/sitemap.php';

class sitemap_admin_model extends CI_Model {

    public function generate() {

        $sitemap_modules = $this->get_modules();

        $liba = array();

        foreach ($sitemap_modules as $v) {
            $module = $v . '_sitemap_api';
            $this->load->library($v . '/' . $module);
            $liba = array_merge($liba, $this->{$module}->get_mudule_map());
        }

        $this->createXML($liba);

        return TRUE;
    }

    public function createXML($data = array()) {
        
        $mapdata = '<?xml version="1.0" encoding="UTF-8"?>
                  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';


        foreach ($data as $key => $val) {
            $mapdata.= '<url><loc>' . $val->get_loc() . '</loc><lastmod>' . $val->get_lastmod() . '</lastmod><changefreq>' . $val->get_changefreq() . '</changefreq><priority>' . $val->get_priority() . '</priority></url>';
        }
        $mapdata.= '</urlset>';
        
        $this->cache->save('sitemap_xml', $mapdata, $this->config->item('cache_life_time'));

        return $mapdata;
    }

    public function get_modules() {
        $sitemap_modules = array();

        $query = $this->db->
                        select('acl_resource_name')->
                        order_by('id_acl_resource', 'DESC')->
                        get('acl_resource')->result_array();
        
        if (!empty($query)) {
            foreach ($query as $v) {
                foreach (array_keys($this->config->item('modules_locations')) as $loc) {
                    if (file_exists($loc . $v['acl_resource_name'] . '/libraries/' . $v['acl_resource_name'] . '_sitemap_api.php')) {
                        $sitemap_modules[] = $v['acl_resource_name'];
                    }
                }
            }
        }
        
        return $sitemap_modules;
    }

    public function set_modules($save_array = array()) {

        if (!empty($save_array)) {

            $this->db->empty_table('sitemap_config');

            foreach ($save_array as $k => $v) {

                $data = array(
                    'module_name' => $k,
                    'changefreq' => $v['changefreq'],
                    'priority' => $v['priority']
                );
                $this->db->insert('sitemap_config', $data);
            }
        }
    }

    public function get_modules_db() {

        $options = array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never');
        $temp = '';

        $sitemap_modules_db = $this->db->
                        select('module_name,changefreq,priority')->
                        get('sitemap_config')->result_array();

        foreach ($sitemap_modules_db as $k => $v) {

            foreach ($options as $key => $value) {
                if ($value == $v['changefreq']) {
                    $temp.= '<option selected="selected" value="' . $value . '">' . $value . '</option>';
                } else {
                    $temp.= '<option value="' . $value . '">' . $value . '</option>';
                }
            }

            $sitemap_modules_db[$k]['changefreq'] = $temp;
            $temp = '';
        }

        return $sitemap_modules_db;
    }

}
