<?php

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

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

    public function __construct() {
        parent::__construct();

        $this->load->model('resmng/resmng_model');
        
        // Load lang
        $this->lang->load('resmng/tabs');
        $this->lang->load('resmng/install');
    }

    public function manager() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'resmng_view')) {
            $this->load->library('ciext/admin_lib');
            $this->admin_lib->show_denied();
        }

        // Disable user left menu
        $this->admin_lib->content['user_left_menu'] = '';
        
        // Load js
        $this->ciext->load_bootstrap();
        $this->ciext->load_jquery_ui();
        $this->js_css->add_js_string('var msgCloseTab = \'' . $this->lang->line('Date not saved. Close current tab?') . '\';');
        $this->js_css->parse_js('resmng/js/main.js');
        $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/plugins/ckeditor/ckeditor.js');
        $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/plugins/ckeditor/adapters/jquery.js');
        // Load css
        $this->js_css->parse_css('resmng/css/main.css');
        $this->js_css->parse_css('resmng/css/treemenu.css');

        $content = array_merge(array(
            'tree_menu' => $this->parser->parse('resmng/tree_menu', array('item' => $this->resmng_model->get_tree_menu()), TRUE) . $this->resmng_model->get_tree_menu_context_menu(),
            'tree_menu_cart' => $this->parser->parse('resmng/tree_menu', array('item' => $this->resmng_model->get_tree_menu(1)), TRUE) . $this->resmng_model->get_tree_menu_context_menu(1),
            'content' => ''
                ), $this->lang->load('resmng/tree_menu', '', TRUE));

        return array('content' => $this->parser->parse('resmng/template', $content, TRUE), 'title' => $this->lang->line('module_name'));
    }

    public function refrash_tree_menu() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'resmng_view')) {
            die(json_encode(array('res' => '', 'cart' => '')));
        }

        $lang = $this->lang->load('resmng/tree_menu');

        $rs = array(
            'res' => $this->parser->parse_string(
                    $this->parser->parse('resmng/tree_menu', array('item' => $this->resmng_model->get_tree_menu()), TRUE) .
                    $this->resmng_model->get_tree_menu_context_menu(), $lang, TRUE),
            'cart' => $this->parser->parse_string(
                    $this->parser->parse('resmng/tree_menu', array('item' => $this->resmng_model->get_tree_menu(1)), TRUE) .
                    $this->resmng_model->get_tree_menu_context_menu(1), $lang, TRUE),
        );

        die(json_encode($rs));
    }

    public function create() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'create_element')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $name = $this->input->post('name');
            $id_resource_type = $this->input->post('type');
            $parent_id = $this->input->post('id');

            $res_id = $this->resmng_model->create($name, $id_resource_type, $parent_id);
            $rs = array(
                'success' => intval($res_id > 0),
                'msg' => '',
                'resId' => $res_id
            );
        }

        die(json_encode($rs));
    }

    public function delete() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'delete_element')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $id = $this->input->post('id');

            $rs = array(
                'success' => intval($this->resmng_model->delete(array($id))),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }

    public function recovery() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'recove_from_trash')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $id = $this->input->post('id');

            $rs = array(
                'success' => intval($this->resmng_model->recovery(array($id))),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }

    public function clear_trash() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'delete_from_trash')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $id = $this->input->post('id');

            $rs = array(
                'success' => intval($this->resmng_model->clear_trash(array($id))),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }

    public function set_private() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'edite_private')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            // Prepare data
            $id = $this->input->post('id');
            $value = $this->input->post('value');

            $rs = array(
                'success' => intval($this->resmng_model->set_private($id, $value)),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }

    public function move() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'remove_element')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            // Prepare data
            $parent_id = $this->input->post('parent');
            $items = $this->input->post('item');

            if (empty($items)) {
                $rs = array(
                    'success' => 0,
                    'msg' => 'Empty items'
                );
            } else {
                $moved = 0;
                $position = 0;
                foreach ($items as $item_id) {
                    if ($this->resmng_model->move($item_id, $parent_id, $position++)) {
                        $moved++;
                    }
                }
                $rs = array(
                    'success' => intval(($moved > 0)),
                    'msg' => ''
                );
            }
        }

        die(json_encode($rs));
    }

    public function rename() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'rename_element')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $rs = array(
                'success' => intval($this->resmng_model->set_name($this->input->post('id'), $this->input->post('name'))),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }
    
    public function cloneitem() {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'clone_element')) {
            $rs = array(
                'success' => 0,
                'msg' => ''
            );
        } else {
            $rs = array(
                'success' => intval($this->resmng_model->clone_item($this->input->post('id'))),
                'msg' => ''
            );
        }

        die(json_encode($rs));
    }

    public function get_content() {

        die($this->resmng_model->get_content($this->input->post('id')));
    }

    public function set_content() {

        die($this->resmng_model->set_content());
    }
    
    public function convolute() {
        
        $rs = array('success' => 0);
        
        $id_resource = intval($this->input->post('id'));
        
        if (empty($id_resource)) {
            
            die(json_encode($rs));
        }
        
        $user_key = 'user_' . $this->rt_user->id_user;
        
        // Get convolute items
        $this->load->library('ciext/configurator');
        $this->configurator->set_resource('resmng');
        $data = $this->configurator->get_config();
        
        if (empty($data['convolute'][$user_key])) {
            $data['convolute'][$user_key] = array();
        }
        
        // Unset item
        $convolute = array_filter($data['convolute'][$user_key], function($v) use ($id_resource){
            
            return ($v != $id_resource);
        });
        
        if ($convolute == $data['convolute'][$user_key]) {
            $convolute[] = $id_resource;
        }
        
        $data['convolute'][$user_key] = $convolute;
        
        // Save data
        $this->configurator->set_config($data);
        
        $rs = array('success' => 1);
        
        die(json_encode($rs));
    }

}

