<?php

class admin_menu_model extends CI_Model {

    private $selected_page_name = '';
    private $current_page = '';
    private $no_icon = '/assets/components/ciext/img/dev-tool/sections.png';

    public function __construct() {
        parent::__construct();
        $this->current_page = str_replace($this->uri->current_language . '/', '', $this->uri->uri_string());        
        $this->lang->load('ciext/admin');
        
    }        
    
    public function get_current_page_name() {
        return $this->selected_page_name;
    }

    /**
     * Get menu items from DB
     * @return array
     */
    public function get_menu_items() {
        $data = $this->db->
                select('am.id_admin_menu, am.name as translate, am.page, am.image, am.parent_id, am.view_order, acl_resource, acl_action')->
                where('am.active', TRUE)->
                order_by('am.parent_id, view_order, translate', 'ASC')->
                get('admin_menu am');
//        return $this->prepare_items($data->result_array(), 0);
        return $this->_prepare_items($data->result_array());
    }
    
    /**
    * Creates a nesting of elements
    * @param array $data
    * @return array
    */
    private function _prepare_items($menu_data, $panel_size = 46) {
        // get parents && childs ids
        $main_block_id = 0;
        $parent_ids = array();
        $child_ids = array();
        foreach($menu_data as $menu_item){
            $id = $menu_item['id_admin_menu'];
            $pid = $menu_item['parent_id'];
            if(!in_array($pid, $parent_ids)){
                $parent_ids[] = $pid;
            }
            $child_ids[$id] = $pid;
        }
        
        // get parents and child ids 
        $parent_blocks_id = array();
        $sort_menu_data = array();
        foreach($child_ids as $id=>$pid){
            if(($pid == $main_block_id || $child_ids[$pid] == $main_block_id) && !isset($sort_menu_data[$pid])){
                $sort_menu_data[$pid] = array(
                    'block_title' => '',
                    'block_items' => array()
                );
            }
            //prepare pid
            while($pid != $main_block_id && $child_ids[$pid] != $main_block_id){
                $pid = $child_ids[$pid];
            }
            $parent_blocks_id[$id] = $pid;
        }        
        
        // prepare data to parse        
        reset($menu_data);
        foreach($menu_data as $index => $menu_item){
            
            // set block names
            if(isset($sort_menu_data[$menu_item['id_admin_menu']])){
                $sort_menu_data[$menu_item['id_admin_menu']]['block_title'] = $menu_item['translate'];
            }
            // continue if menu item container
            if(in_array($menu_item['id_admin_menu'], $parent_ids)){
                continue;
            }
            
            // Access check
            if (empty($menu_item['acl_resource']) || $this->rt_user->isAllowed($menu_item['acl_resource'], $menu_item['acl_action'])) {
                $menu_item['page'] = (!empty($menu_item['page'])) ? base_url() . $this->config->item('admin_page') . '/' . $menu_item['page'] : 'javascript:void(0)';
                
                // prepare image
                $menu_item['image'] = (!empty($menu_item['image'])) ? $menu_item['image'] : $this->no_icon;
                
                // prepare block parent id
                $block_pid = $parent_blocks_id[$menu_item['id_admin_menu']];
                $sort_menu_data[$block_pid]['block_items'][] = $menu_item;
            }
            
            if ($this->current_page == $menu_item['page'])
                $this->selected_page_name = $menu_item['translate'];
        }
        
        // Delete empty parent block
        foreach ($sort_menu_data as $k => $v) {
            if (empty($v['block_items']) || empty($v['block_title'])) {
                unset($sort_menu_data[$k]);
            }
        }

        $parse_array = array(
            'search_img' => '/assets/components/ciext/img/search.png',
            'search_load_img' => '/assets/components/ciext/img/search-load.gif',
            'blocks' => $sort_menu_data
        );
        
        return $this->parser->parse('ciext/admin_menu/menu', $parse_array, TRUE);
    }
    
}