<?php

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

class sitemap_admin extends MX_Controller {

    public function __construct() {

        $this->load->driver('cache', $this->config->item('cache_init_config'));
        $this->lang->load('sitemap/admin');
        $this->load->model('sitemap/sitemap_admin_model');
    }

    public function manage() {

        // Acceess check
        if (!$this->rt_user->isAllowed('sitemap', 'view_sitemap')) {
            $this->admin_lib->show_denied();
        }

        $this->ciext->load_bootstrap();

        $content = array();

        $sitemap_modules = $this->sitemap_admin_model->get_modules();

        foreach ($sitemap_modules as $k => $v) {
            $content['modules'][$k]['module_name'] = $v;
        }

        $post = $this->input->post();
        $content['display'] = '';

        if (!empty($post)) {

            $save_array = array();

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

                $temp = explode("_", $k);

                if (in_array($temp[1], $sitemap_modules)) {

                    $save_array[$temp[1]][$temp[0]] = $v;
                }
            }

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

            $error = FALSE;

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

                $v['priority'] = round(floatval($v['priority']), 2);

                $save_array[$k]['priority'] = $v['priority'];

                if (!in_array($v['changefreq'], $options) || ($v['priority'] > 1 || $v['priority'] <= 0)) {
                    $error = TRUE;
                    break;
                }
            }
            if (!$error) {

                $this->sitemap_admin_model->set_modules($save_array);
                $content['display'] = '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button>' . $this->lang->line('display_success') . '</div>';
            } else {
                $content['display'] = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>' . $this->lang->line('display_error') . '</div>';
            }
        }

        $content['modules'] = $this->sitemap_admin_model->get_modules_db();

        $db_module_array = array();

        foreach ($content['modules'] as $key => $value) {
            $db_module_array[] = $value['module_name'];
        }


        foreach ($sitemap_modules as $k => $v) {
            if (!in_array($v, $db_module_array)) {
                $content['modules'][] = array('module_name' => $v, 'changefreq' => '<option value="always">always</option><option value="hourly" selected="selected">hourly</option><option value="daily">daily</option><option value="weekly">weekly</option><option value="monthly">monthly</option><option value="yearly">yearly</option><option value="never">never</option>', 'priority' => '');
            }
        }

        foreach ($db_module_array as $k => $v) {
            if (!in_array($v, $sitemap_modules)) {

                foreach ($content['modules'] as $key => $value) {

                    if ($value['module_name'] == $v) {

                        unset($content['modules'][$key]);
                    }
                }
            }
        }

        $content['button_save'] = '';
        $content['button_generate'] = '';

        // Edit acceess check
        if ($this->rt_user->isAllowed('sitemap', 'edit_sitemap')) {
            $content['button_save'] = '<a href="" class="btn btn-large" id="form_send">' . $this->lang->line('button_save') . '</a>';
            $content['button_generate'] = '<a href="javascript:generate();void(0);" class="btn btn-large">' . $this->lang->line('button_generate') . '</a>';
        }

        $content['label_module'] = $this->lang->line('label_module');
        $content['label_changefreq'] = $this->lang->line('label_changefreq');
        $content['label_priority'] = $this->lang->line('label_priority');
        $content['generate_success'] = $this->lang->line('generate_success');
        $content['generate_error'] = $this->lang->line('generate_error');

        return $this->parser->parse('sitemap/template', $content, TRUE);
    }

    public function action() {

        // Acceess check
        if (!$this->rt_user->isAllowed('sitemap', 'edit_sitemap')) {
            $this->admin_lib->show_denied();
        }

        if ($this->input->post('action') == 1) {
            $data['status'] = 'error';
            if ($this->sitemap_admin_model->generate()) {
                $data['status'] = 'success';
            }

            die(json_encode($data));
        }
    }
}
