<?php

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

/**
 * Description of item_resource_model
 *
 * @author Igor Ratsun
 */
class item_resource_model extends CI_Model implements items {

    protected $resource = array();

    public function init($ids = array()) {
        if (!empty($ids)) {
            if (count($ids) == 1) {
                $this->db->where('id_resource_type_resource', intval(reset($ids)));
            } else {
                $this->db->where_in('id_resource_type_resource', array_map('intval', $ids));
            }
        }
        $rs = $this->db->
                        select('id_resource_type_resource, name, alias, editor')->
                        get('resource_type_resource')->result_array();

        // Prepare folders
        foreach ($rs as $key => $value) {
            $this->resource[$value['id_resource_type_resource']] = $rs[$key];
        }
    }

    public function get_name($id = 0) {

        return (!empty($this->resource[$id]['name'])) ? $this->resource[$id]['name'] : '';
    }

    public function get_alias($id = 0) {

        return (!empty($this->resource[$id]['alias'])) ? 'id_' . $this->resource[$id]['alias'] : '';
    }

    public function create($name) {
        // Get languages
        $lan = $this->db->
                        select('id_lan')->
                        where('active', TRUE)->
                        get('lan')->result_array();

        // Create main
        $this->db->
                insert('resource_type_resource', array('name' => $name));

        $main_id = $this->db->insert_id();

        if (empty($main_id)) {

            return FALSE;
        }

        // Create translate
        $data = array();
        foreach ($lan as $v) {
            $data[] = array(
                'id_resource_type_resource' => $main_id,
                'id_lan' => $v['id_lan']
            );
        }

        if (!empty($data)) {
            $this->db->insert_batch('resource_type_resource_translate', $data);
        }

        return $main_id;
    }

    public function delete($ids = array()) {
        if (empty($ids)) {

            return FALSE;
        }

        // Clear translate
        $this->db->
                where_in('id_resource_type_resource', $ids)->
                delete('resource_type_resource_translate');

        // Clear main
        $this->db->
                where_in('id_resource_type_resource', $ids)->
                delete('resource_type_resource');

        return ($this->db->affected_rows() > 0);
    }

    protected function _get_data($id) {
        $sql = $this->db->
                        select('rr.id_resource_type_resource, lan.id_lan, lan.name, rrt.content, rr.alias, rr.editor')->
                        join('resource_type_resource_translate rrt', 'rrt.id_lan = lan.id_lan AND rrt.id_resource_type_resource = rr.id_resource_type_resource', 'left')->
                        where('active', TRUE)->
                        where('rr.id_resource_type_resource', $id)->
                        get('lan, resource_type_resource rr')->result_array();
        
        // prepare data
        foreach ($sql as &$v) {
            $v['content'] = htmlspecialchars($v['content']);
        }
        unset($v);

        return $sql;
    }

    public function get_content($id = 0) {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'edit_resource_content')) {
            return FALSE;
        }

        $sql = $this->_get_data($id);

        if (empty($sql)) {
            return FALSE;
        }

        $rs = $this->parser->parse('resmng/item_resource_content', array_merge(
                        array(
            'item' => $sql,
            'alias' => $sql[0]['alias'],
            'editor' => ($sql[0]['editor']) ? 'checked="checked"' : ''
                        ), $this->lang->load('resmng/item_resource')
                ), TRUE);

        return $rs;
    }

    public function set_name($id = 0, $name = '') {
        // Update name
        $this->db->
                where('id_resource_type_resource', intval($id))->
                update('resource_type_resource', array('name' => $name));

        return ($this->db->affected_rows() > 0);
    }

    public function set_content($id = 0) {
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'edit_resource_content')) {
            return FALSE;
        }

        // Update main data
        $data = array(
            'editor' => ($this->input->post('editor')) ? 1 : 0
        );
        if ($this->rt_user->isAllowed('resmng', 'private')) { 
            $data['alias'] = $this->input->post('alias');
        }
        $this->db->
                where('id_resource_type_resource', $id)->
                update('resource_type_resource', $data);

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

        // Prepare data
        $insert = array();
        foreach ($content as $id_lan => $data) {
            $insert[] = array(
                'id_resource_type_resource' => $id,
                'id_lan' => intval($id_lan),
                'content' => $data,
            );
        }
        if (empty($insert)) {
            return FALSE;
        }

        // Clear old data
        $this->db->where('id_resource_type_resource', $id)->delete('resource_type_resource_translate');

        $this->db->insert_batch('resource_type_resource_translate', $insert);

        return $this->db->insert_id();
    }

    public function clone_item($id, $parent_id = NULL) {
        $name = $this->get_name($id);
        if (is_null($parent_id)) {
            $this->load->helper('string');
            $name = increment_string($name, '_');
        }
        $new_id = $this->create($name);
        $data = $this->_get_data($id);

        if (!empty($new_id) && !empty($data)) {
            foreach ($data as $row) {
                $_POST['alias'] = $row['alias'];
                $_POST['editor'] = $row['editor'];

                $_POST['content'][$row['id_lan']] = htmlspecialchars_decode($row['content']);
            }
            $this->set_content($new_id);

            return $new_id;
        }

        return FALSE;
    }

}

