<?php

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

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

    protected $folders = array();

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

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

    public function get_name($id = 0) {

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

    public function get_alias($id = 0) {

        return '';
    }

    public function create($name) {
        $this->db->
                insert('resource_type_folder', array('name' => $name));

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

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

            return FALSE;
        }

        // Clear deleted
        $this->db->
                where_in('id_resource_type_folder', $ids)->
                delete('resource_type_folder');

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

    public function get_content($id = 0) {

        return FALSE;
    }

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

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

    public function set_content($id = 0, $data = array()) {

        return FALSE;
    }
    
    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, '_');
        }
        
        return $this->create($name);
    }

}