<?php

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

/**
 * Description of site_menu_model
 *
 * @author Igor Ratsun
 */
class site_menu_model extends CI_Model {

    protected $get_parent = array(0 => '---');

    protected function _get_parents() {
        if ($this->get_parent == array(0 => '---')) {
            $sql = $this->db->
                            select('sm.id_site_menu, smt.name')->
                            where('sm.active', TRUE)->
                            where('sm.id_site_menu = smt.id_site_menu')->
                            where('lan.id_lan = smt.id_lan')->
                            where('lan.short_name', $this->uri->current_language)->
                            get('site_menu sm, site_menu_translate smt, lan')->result_array();
            foreach ($sql as $value) {
                $this->get_parent[$value['id_site_menu']] = $value['name'];
            }
        }

        return $this->get_parent;
    }

    public function get_parent_column($value, $row) {
        $parents = $this->_get_parents();
        return (isset($parents[$value])) ? $parents[$value] : '';
    }

    public function get_edit_parent_column($name = '', $value = '') {
        $parents = $this->_get_parents();
        $primary_key = intval($this->uri->get_next_segment('edit'));

        $tpl = '<select name="' . $name . '">';
        foreach ($parents as $k => $v) {
            if ($k == $primary_key && $k != 0) {
                continue;
            }
            $tpl .= '<option value="' . $k . '" ' . ($k == $value ? 'selected="selected"' : '') . '>' . $v . '</option>';
        }

        return $tpl . '</select>';
    }

    public function get_pages($name = '', $value = '') {
        $this->load->model('ciext/core_model');

        $data = $this->db->
                        select('name, alias')->
                        where('active', TRUE)->
                        get('resource_type_page')->result_array();
        $html = '<select name="' . $name . '">';
        foreach ($data as $v) {
            $href = str_replace(base_url(), '/', $this->core_model->create_page_url($v['alias']));
            $html .= '<option value="' . $href . '"' . (($href == $value) ? ' selected="selected"' : '') . '>' . $v['name'] . '</option>';
        }
        $html .= '</select>';

        $js = '<div id="inp" style="float:right;padding:5px;"><a href="#" id="switch">Manual</a></div>
            <script type="text/javascript">
            $(function(){
                $("#switch").click(function(){
                    var sel = $("select[name=' . $name . ']");
                    var val = sel.val();
                    if (val == "") {
                        val = "' . $value . '";
                    }
                    $("#inp").html("<input type=\"text\" name=\"' . $name . '\" value=\"" + val + "\" />");
                    sel.remove();
                    return false;
                });
            });
            </script>';

        return $html . $js;
    }

    public function get_items() {
        $data = $this->db->
                        select('sm.id_site_menu, smt.name, sm.href, sm.parent_id')->
                        where('sm.active', TRUE)->
                        where('sm.id_site_menu = smt.id_site_menu')->
                        where('lan.id_lan = smt.id_lan')->
                        where('lan.short_name', $this->uri->current_language)->
                        order_by('sm.view_order, sm.parent_id', 'ASC')->
                        get('site_menu sm, site_menu_translate smt, lan')->result_array();
        
        // Prepare data
        $current_page = '/' . $this->uri->current_page;
        foreach ($data as &$v) {
            if ($this->uri->current_language != $this->config->item('language_short')) {
                $v['href'] = ($v['href'] == '/') ? '/' . $this->config->item('home_page') : $v['href'];
                $v['href'] = '/' . $this->uri->current_language . $v['href'];
            }
            
            $v['active'] = ($v['href'] == $current_page) ? 'class="active"' : '';
        }
        unset($v);
        
        return $this->_get_child($data);
    }

    protected function _get_child(&$data, $parent_id = 0, $level = 0) {
        $level++;
        $return = array();
        foreach ($data as $item) {
            if ($item['parent_id'] == $parent_id) {
                $children = $this->_get_child($data, $item['id_site_menu'], $level);
                $return[] = array(
                    'level_' . $level => $children,
                    'remove_marker_start' => (empty($children)) ? '{remove_marker_start}' : '',
                    'remove_marker_end' => (empty($children)) ? '{remove_marker_end}' : '',
                    'id' => $item['id_site_menu'],
                    'name' => $item['name'],
                    'href' => $item['href'],
                    'active' => $item['active']
                );
            }
        }

        return $return;
    }

}

