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

// load abstract library
include_once dirname(__DIR__).'/libraries/abstract_sitemap.php';

/**
 * Class of sitemap_admin
 *
 * @author r.ratsun <r.ratsun@gmail.com>
 */
class sitemap_admin extends MX_Controller
{

    /**
     * Construct
     */
    public function __construct()
    {
        parent::__construct();

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

    /**
     * Manage action
     *
     * @return string
     */
    public function manage()
    {

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

        $this->load->library('ciext/ciext');
        $this->ciext->load_bootstrap();
        $this->ciext->load_jquery();
        $this->js_css->parse_css('sitemap/css/sitemap.css');
        $this->js_css->parse_js('sitemap/js/sitemap.js');

        $content = array();

        $content['error_links'] = $this->sitemap_model->get_error_links();
        $sitemap_modules        = $this->sitemap_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_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_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)) {
                $basepage = $this->sitemap_model->get_base_page();
                $temp     = '<option value="0">---</option>';
                foreach ($basepage as $value) {
                    $temp .= '<option value="'.$value['id_resource_type_page'].'">'.$value['name'].'</option>';
                }
                $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'    => '',
                    'basepage'    => $temp
                );
                $temp                 = '';
            }
        }

        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" 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['tabs']     = '  <li class="active"><a href="">'.$this->lang->line('base_properties').'</a></li><li><a href="cache_life">'.$this->lang->line('cache_life').'</a></li>';
        $content['template'] = $this->parser->parse('sitemap/template', $content, TRUE);

        $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['label_base_page']  = $this->lang->line('label_base_page');
        $content['generate_success'] = $this->lang->line('generate_success');
        $content['generate_error']   = $this->lang->line('generate_error');

        return array(
            'content' => $this->parser->parse('sitemap/base_template', $content, TRUE),
            'title'   => $this->lang->line('page_title'),
        );
    }

    /**
     * Cache life action
     *
     * @return array
     */
    public function cache_life()
    {
        $this->load->library('ciext/configurator');

        $conf            = new configurator();
        $conf->set_resource('sitemap');
        $conf->add_input('cache_life');
        $content['tabs'] = '  <li><a href="manage">'.$this->lang->line('base_properties').'</a></li><li class="active"><a href="">'.$this->lang->line('cache_life').'</a></li>';
        $conf->display_as('cache_life', $this->lang->line('cache_life_input'));

        $content['template'] = $conf->render();

        return array(
            'content' => $this->parser->parse('sitemap/base_template', $content, TRUE),
            'title'   => $this->lang->line('page_title'),
        );
    }

    /**
     * Ajax actions
     */
    public function action()
    {
        // Acceess check
        if (!$this->rt_user->isAllowed('sitemap', 'edit_sitemap')) {
            $this->admin_lib->show_denied();
        }
        $result = array('status' => FALSE);
        $method = $this->input->post('method');

        if (!empty($method)) {
            if (method_exists($this->sitemap_model, $method)) {
                $result = $this->sitemap_model->{$method}();
            }

            die(json_encode($result));
        }
    }
}
