<?php

class line_menu extends module {

    public $templ     = "menu.htm";
    public $_loc_vars = [ 'templ', 'root', 'noexit', 'autoLevel'  ];
    public $obj;

    public function _on_() {
        global $R, $core_db, $cat;
        $tpl    = $R->getTpl($this->templ, 'body');
        $tpli   = $R->getTpl($this->templ, 'item');
        $tplis  = $R->getTpl($this->templ, 'item_sel');
        $tplsep = $R->getTpl($this->templ, 'sep');
        if (!$this->root) {
            if (isset($this->autoLevel) && $this->autoLevel) {
                $this->autoLevel = intval($this->autoLevel); // Приводим к числу

                $path = $cat->getPath();
                if (count($path) == 1) return '';
                $path = array_reverse($path);

                if ($this->autoLevel > count($path)) {
                    $this->autoLevel = count($path) - 1;
                }

                $this->root = $path[$this->autoLevel];
            } else {
                $this->root = $cat->cat;
            }
        }
        $this->obj = new cobject(__REAL_CAT_OBJ_);

        $sql_where = '';

        $tpl = $R->set('root', 'root'.crc32($this->root), $tpl);

        $db2   = $core_db->q($this->obj->getSelect(' AND `parent_cat` = "' . $this->root .  '" AND `_sys_unvisible` = "0" '.$sql_where.' ORDER BY `_sys_order`'));
        $first = true;
        while ($db2->r()) {
            $this->obj->lRow($db2);
            if (!in_array($db2->f('cat_id'), $cat->path)) {
                $tt = $this->obj->assign($tpli);
            } else {
                $tt = $this->obj->assign($tplis);
            }
            if ($first) {
                $first = false;
            } else {
                $tpl = $R->parse('menu_item', $tplsep, $tpl);
            }

            $tpl = $R->parse('menu_item', $tt, $tpl);
        }

        return $R->clear($tpl);
    }


}

?>