<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

class sidebar {

    public static function sidebarshow($smarty, $hosturl, $active) {

        //can use db here

        $menudisply = self::displaymenusidebar($smarty, $hosturl, 0, $active, 1);
        $smarty->assign('menudisply', $menudisply);

        $smarty->display('_main/sidebar.html');
    }

    //show menu
    private static function displaymenusidebar($smarty, $hosturl, $parent, $active, $level) {

        //get the menu
        $menuhtml = "";
        //get the all about this level in menu
        $result = R::getAll('SELECT a.*, Deriv1.count FROM `awmenu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `awmenu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent= ?', [ $parent]);


        foreach ($result as $srel) {

            //code for active input
            $actvename = '';
            if (count($active) >= $level) {
                $activeno = $level - 1;
                $actvename = $active[$activeno];
            }


            //code for load db
            $roleid = $_SESSION['role_id']; //here session of role
            $getrolemenu = R::findOne('awrolemenu', 'role_id = ? and menu_id = ?', [$roleid, $srel['id']]);
            $countrslt = count($getrolemenu);
            if ($countrslt > 0) {
                $srel['rolevalue'] = $getrolemenu['checkval'];
            } else {
                $srel['rolevalue'] = 0;
            }


            //code for draw menu
            if ($srel['rolevalue'] != 0) {


                if ($srel['count'] > 0) {
                    $menuhtml .='<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }
                    $menuhtml .='"><a href="javascript:;">'
                            . '<i class="' . $srel["icon"] . '"></i> '
                            . '<span class="title">' . $srel["label"] . '</span>';

                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= '<span class="selected"></span><span class="arrow open"></span>';
                    } else {
                        $menuhtml .= '<span class="arrow "></span>';
                    }

                    $menuhtml .='</a>'
                            . '<ul class="sub-menu">'
                            . self::displaymenusidebar($smarty, $hosturl, $srel["id"], $active, $level + 1)
                            . '</ul>'
                            . '</li>';
                } elseif ($srel['count'] == 0) {
                    $menuhtml .='<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }
                    if ('index' == $srel['active_name']) {
                        $menuhtml .= ' start';
                    }
                    $menuhtml .='"><a href="' . $hosturl . '/' . $srel["link"] . '">'
                            . '<i class="' . $srel["icon"] . '"></i> '
                            . '<span class="title">' . $srel["label"] . '</span>';

                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= '<span class="selected"></span>';
                    }

                    $menuhtml .='</a>'
                            . '</li>';
                }
            }
        }
        return $menuhtml;
    }

}
