<?php

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

/**
 * Description of seo_admin
 *
 * @author Igor Ratsun
 */
class seo_admin extends MX_Controller {

    public function __construct() {
        // Acceess check
        if (!$this->rt_user->isAllowed('seo', 'edit_seo_manage')) {
            $this->admin_lib->show_denied();
        }
    }

    public function manage() {
        // Unset left menu
        //$this->admin_lib->content['user_left_menu'] = '';
        // Set top menu active
        $this->admin_lib->content['active_menu'] = 4;

        $this->ciext->load_bootstrap();
        $this->load->model('seo/seo_model');
        $this->load->helper('form');
        $this->lang->load('seo/seo');
        $folder = base_url() . $this->config->item('assets_folder') . '/' . $this->config->item('components_folder');
        $this->js_css->parse_js('seo/js/seo.js');
        $this->js_css->parse_css('seo/css/seo_module.css');

        $content = array_merge(array(
            'form_open' => form_open(base_url() . $this->config->item('admin_page') . '/mconfig/index/seo/action', 'id="change_form"'),
            'form_close' => form_close()
                ), $this->lang->language);
        
        $page_data = $this->seo_model->get_page_data();
        $content = ($page_data) ? $this->parser->parse('seo/seo', array_merge($content, $page_data), TRUE) : '<div class="alert alert-error">' . $this->lang->line('not_found') . '</div>';
        
        return array(
            'content' => $content,
            'title' => $this->lang->line('title'),
        );
    }

    public function action() {
        $this->load->model('seo/seo_model');

        $action = $this->input->post('action');

        $rs = array();

        if ($action == 'getpage') {
            $data = $this->seo_model->get_page_data($this->input->post('id_page'));
            foreach (array('seo_title', 'seo_keywords', 'seo_description') as $value) {
                if (isset($data[$value])) {
                    foreach ($data[$value] as $v) {
                        $rs[] = array(
                            'name' => $v['name'],
                            'value' => $v['value']
                        );
                    }
                }
            }

            die(json_encode($rs));
        }

        if (!is_null($this->input->post('id_page'))) {
            die($this->seo_model->save());
        }

        die('0');
    }

}

