<?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
 */
require_once ('_patientdata.php');

class sidebar {

    public static function sidebarshow($smarty, $hosturl, $active) {
        $programSetting = R::findOne('programesetting');
        $form = $programSetting->simpleview;     ## 1 for simple form  , 0 for complete form 
        switch ($form) {
            case 0:
                $form = ' and a.form  in (0 , 2) ';    ##   0 for complete form  , 2 simple and comlete
                break;
            case 1:
                $form = ' and a.form  in (1 , 2) ';   ##v     1 for simple form  , 2 simple and comlete
                break;
            default:
                break;
        }

        //can use db here

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

        $userMenuDisply = self::userMenuSidebar($smarty, $hosturl, 0, $active, 1, $form);
        $smarty->assign('userMenuDisply', $userMenuDisply);

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

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



        //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= ? ' . $form . ' order by sort', [$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;
            }


            //just for Shalan
            if ($_SESSION['user_id'] == 50) {
                //programing setting
                if ($srel['id'] == 53) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                // sub menu in programing setting
                if ($srel['parent'] == 53) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //sperm freezing report
                if ($srel['id'] == 85) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //emplogy report
                if ($srel['id'] == 87) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }


                //emplogy freezing
                if ($srel['id'] == 96) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //emplogy lab
                if ($srel['id'] == 97) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }
            }


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

                if ($srel['count'] > 0) {
                    $menuhtml .= '<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }

                    if ($srel["display"] == "icon") {
                        $menuhtml .= '"><a href="javascript:;">'
                                . '<i class="' . $srel["icon"] . '"></i> '
                                . '<span class="title">' . $srel["label"] . '</span>';
                    } elseif ($srel["display"] == "image") {
                        $menuhtml .= '"><a href="javascript:;">'
                                . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                . '<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, $form)
                            . '</ul>'
                            . '</li>';
                } elseif ($srel['count'] == 0) {
                    $menuhtml .= '<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }
                    if ('index' == $srel['active_name']) {
                        $menuhtml .= ' start';
                    }
                    if (strpos($srel["link"], 'ac') == false) {
                        $plus = '?';
                    } else {
                        $plus = '&';
                    }

                    if ($srel["withpatient"] == 1) {
                        if ($srel["display"] == "icon") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '&patientid=' . patientdata::patientid() . '">'
                                    . '<i class="' . $srel["icon"] . '"></i> '
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        } elseif ($srel["display"] == "image") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '&patientid=' . patientdata::patientid() . '">'
                                    . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        }
                    } else {
                        if ($srel["display"] == "icon") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '">'
                                    . '<i class="' . $srel["icon"] . '"></i> '
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        } elseif ($srel["display"] == "image") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '">'
                                    . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        }
                    }





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

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

    private static function userMenuSidebar($smarty, $hosturl, $parent, $active, $level, $form) {



        //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= ? ' . $form . ' order by sort', [$parent]);


        foreach ($result as $srel) {

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


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


            //just for Shalan
            if ($_SESSION['user_id'] == 50) {
                //programing setting
                if ($srel['id'] == 53) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                // sub menu in programing setting
                if ($srel['parent'] == 53) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //sperm freezing report
                if ($srel['id'] == 85) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //emplogy report
                if ($srel['id'] == 87) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }


                //emplogy freezing
                if ($srel['id'] == 96) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }

                //emplogy lab
                if ($srel['id'] == 97) {
                    if ($srel['rolevalue'] == 0) {
                        $srel['rolevalue'] = 1;
                    }
                }
            }


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

                if ($srel['count'] > 0) {
                    $menuhtml .= '<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }

                    if ($srel["display"] == "icon") {
                        $menuhtml .= '"><a href="javascript:;">'
                                . '<i class="' . $srel["icon"] . '"></i> '
                                . '<span class="title">' . $srel["label"] . '</span>';
                    } elseif ($srel["display"] == "image") {
                        $menuhtml .= '"><a href="javascript:;">'
                                . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                . '<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::userMenuSidebar($smarty, $hosturl, $srel["id"], $active, $level + 1, $form)
                            . '</ul>'
                            . '</li>';
                } elseif ($srel['count'] == 0) {
                    $menuhtml .= '<li class="';
                    if ($actvename == $srel['active_name']) {
                        $menuhtml .= 'active open';
                    }
                    if ('index' == $srel['active_name']) {
                        $menuhtml .= ' start';
                    }
                    if (strpos($srel["link"], 'ac') == false) {
                        $plus = '?';
                    } else {
                        $plus = '&';
                    }

                    if ($srel["withpatient"] == 1) {
                        if ($srel["display"] == "icon") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '&patientid=' . patientdata::patientid() . '">'
                                    . '<i class="' . $srel["icon"] . '"></i> '
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        } elseif ($srel["display"] == "image") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '&patientid=' . patientdata::patientid() . '">'
                                    . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        }
                    } else {
                        if ($srel["display"] == "icon") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '">'
                                    . '<i class="' . $srel["icon"] . '"></i> '
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        } elseif ($srel["display"] == "image") {
                            $menuhtml .= '"><a href="' . $hosturl . '/' . $srel["link"] . '">'
                                    . '<img src="../../upload/menu/' . $srel["image"] . '"  style="width:50px;height:46px;">'
                                    . '<span class="title">' . $srel["label"] . '</span>';
                        }
                    }





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

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

}
