<?php

	class Menu extends Modules{

		private $pages;
		private $db;
		private $system;
		private $menu;

		function __construct(){

			global $system;
			$this->system = $system;

			Modules::__construct();
			$this->makeMenu();

		}

		public function isAdmin(){

			global $session;

			return ( $session->get('nivel') == 1 ) ? true : false;

		}

		public function getLogoEmpresa(){

			$confs = $this->system;

			return $confs->getOption("logoEmpresa");


		}

		protected function makeMenu(){
			$modules = Modules::getModules();
			$menu = null;

			$modulesSorted = array();
			foreach($modules as $module){
				if(array_key_exists($module['position'], $modulesSorted)){
					$modulesSorted[ ($module['position'] + 1) ] = $module;
				}else{
					$modulesSorted[ $module['position'] ] = $module;
				}
			}

			ksort($modulesSorted);

			foreach($modulesSorted as $module){

				$subs = null;
				if( isset($module['submenus']) && $module['submenus'] != "" ){
					foreach ($module['submenus'] as $sub) {
						if( isset($sub['is_admin']) && $sub['is_admin'] == true ){
							if( $this->isAdmin() ){
								$subs .= '<li>' . '<a href="/sistema' . $sub['link'] . '">' . $sub['name'] . '</a>' . '</li>';
							}
						}else{
							$subs .= '<li><a href="/sistema' . $sub['link'] . '">' . $sub['name'] . '</a></li>';
						}
					}
				}

				if($subs){
					$a = '<a href="#" class="has-sub">';
					$ul = array('<ul class="sub">', '</ul>');
				}else{
					$ul = array(null, null);
					$a = '<a href="/sistema' . $module['link'] . '">';
				}

				$menu .= '<li>' . $a . '<i class="fa fa-arrow-circle-right"></i><span>' . $module['name'] . '</span></a>' . $ul[0] . $subs . $ul[1] . '</li>';
			}
			$this->menu = $menu;
		}

		public function getMenuConfigs(){
			return Modules::getConfigurators();
		}

		public function getMenu(){
			return $this->menu;
		}

	}

	loadBaseView('menu', new Menu);