<?php
/*--------------------------------------------------\
| Kvcodes    	|               | default.css       |
|---------------------------------------------------|
| For use with:                                    	|
| FrontAccounting 									|
| http://www.kvcodes.com/  	            			|
| By Kvvaradha                            			|
|---------------------------------------------------|
| Note:                                         	|
| Changes can be made to this CSS that will be  	|
| reflected throughout FrontAccounting.             |
|                                                   |
\--------------------------------------------------*/
include_once("kvcodes.inc");
global $vanigam_options;

create_tbl_option();
$vanigam_options = VanigamGetAll();
function addhttp($url) {
	if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
		$url = "http://" . $url;
	}
	return $url;
}

class renderer{
	function get_icon($category){
		global  $path_to_root, $show_menu_category_icons;

		if ($show_menu_category_icons)
			$img = $category == '' ? 'right.gif' : $category.'.png';
		else	
			$img = 'right.gif';
		return "<img src='$path_to_root/themes/Vanigam/images/$img' style='vertical-align:middle;' border='0'>&nbsp;&nbsp;";
	}

	function wa_header(){
		page(_($help_context = "Dashboard"), false, true);
	}

	function wa_footer(){
		end_page(false, true);
	}

	function menu_header($title, $no_menu, $is_index){
		global $path_to_root, $SysPrefs, $db_connections, $version, $vanigam_options, $fonts_list;
			require_once("ExtraSettings.php"); ?>
			<script> 
			(function() {
			    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
			    link.type = 'image/x-icon';
			    link.rel = 'shortcut icon';
			    <?php if(isset($vanigam_options['favicon']) && file_exists(dirname(__FILE__).'/images/'.$vanigam_options['favicon']) && !is_dir(dirname(__FILE__).'/images/'.$vanigam_options['favicon'])){
			    	echo " link.href = '$path_to_root/themes/".user_theme()."/images/".$vanigam_options['favicon']."?".rand(2,5)."'; ";
			    }else {
			    	echo "link.href = '$path_to_root/themes/".user_theme()."/images/favicon.ico?".rand(2,5)."';";
			    } ?>
			    
			    document.getElementsByTagName('head')[0].appendChild(link);
			}());
			</script> <?php 

			$img = "<img src='$path_to_root/themes/".user_theme()."/images/login.gif' width='14' height='14' border='0' alt='"._('Logout')."'>&nbsp;&nbsp;";
			$himg = "<img src='$path_to_root/themes/".user_theme()."/images/help.gif' width='14' height='14' border='0' alt='"._('Help')."'>&nbsp;&nbsp;"; ?>
				
			<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
		<link href='<?php echo $path_to_root."/themes/".user_theme()."/css/iconFont/iconFont.css"; ?>' rel='stylesheet' type='text/css'> 
		<link href='<?php echo $path_to_root."/themes/".user_theme()."/css/bootstrap.min.css"; ?>' rel='stylesheet' type='text/css'> 
		
		<link href='<?php echo $path_to_root."/themes/".user_theme()."/css/kv-style.css"; ?>' rel='stylesheet' type='text/css'> 
		<link href='<?php echo $path_to_root."/themes/".user_theme()."/css/component.css"; ?>' rel='stylesheet' type='text/css'> 
  
		<script src="<?php echo $path_to_root."/themes/".user_theme()."/js/jquery.js"; ?>"></script>

		<script src='<?php echo $path_to_root."/themes/".user_theme()."/js/bootstrap.min.js"; ?>'></script>
		<script src='<?php echo $path_to_root."/themes/".user_theme()."/js/modernizr.custom.js"; ?>'></script>
		
	
		<!--  <link rel="stylesheet" href="<?php echo $path_to_root."/themes/".user_theme(); ?>/css/AdminLTE.css"> -->

<?php
if(isset($vanigam_options['font']) && isset($fonts_list[$vanigam_options['font']]))
    	echo   '<link href="https://fonts.googleapis.com/css2?family='.$fonts_list[$vanigam_options['font']]['link'].'&display=swap" rel="stylesheet"><style> 
 html, body { font-family : '.$fonts_list[$vanigam_options['font']]['font-family'].';}
 </style>
 <script src="'.$path_to_root.'/themes/'.user_theme().'/js/jquery.js"></script>'; 
			
 $local_path_to_root = $path_to_root;
	
 if (!$no_menu){	echo '	<div id="head-nav" class="navbar navbar-default">'; } 
 		echo '<div class="container-fluid">
				<div class="navbar-header">';

				if(isset($vanigam_options['logo']) && file_exists(dirname(__FILE__) .'/images/'.$vanigam_options['logo']) && !is_dir(dirname(__FILE__) .'/images/'.$vanigam_options['logo'])){
					$logo_img = $vanigam_options['logo'].'?'.rand(2,5);
				}else
					$logo_img = 'vanigam.png?'.rand(2,5);
				$role_sql = "SELECT role FROM ".TB_PREF."security_roles WHERE id=".$_SESSION['wa_current_user']->access." LIMIT 1";
				$role_res = db_query($role_sql, "Can't get current_user Role");
				if(db_num_rows($role_res) > 0 ) {
					if($row = db_fetch($role_res))
						$role_name = $row[0];
				} else
					$role_name  ='';
				echo ' <header class="main-header"> <!-- Logo -->';
		if (!$no_menu){	
			echo '<a class="logo" href="'.$path_to_root.'" style="float:left;" > <img src="'. $path_to_root.'/themes/Vanigam/images/'.$logo_img.'" style="max-width: 250px; padding-top:5px;  padding-bottom: 5px; '.(isset($vanigam_options['logo_height']) ? 'max-height:'.$vanigam_options['logo_height'].'px' : '' ).'" ><!--'.$db_connections[user_company()]["name"].' --> </a>		
				
					<!--<a href="#" class="navbar-brand navbar-right user-nav" > <span class="menuLine"> </span> <span class="menuLine"> </span> <span class="menuLine"> </span> </a>  -->';
					echo ' <ul class="nav navbar-nav navbar-right user-nav" id="ActionLinks">';
						
					echo '<div id="toggle">
						<div class="one"></div>
						<div class="two"></div>
						<div class="three"></div>
						</div>';
			
					include_once("$path_to_root/themes/".user_theme()."/notification.php");
					$noti = new show_notification();
					$noti->get_overdue_invoices();
					$noti->get_inventory_reorder();
					$noti->get_supplier_payments();		
					echo '<li class="dropdown profile_menu">
					<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="icon-user-3" > </i> ' . $_SESSION["wa_current_user"]->name .' <b class="caret"></b></a>
					<ul class="dropdown-menu" id="drop">';
					include($path_to_root.'/config_db.php');
			
					$pref = $db_connections[$_SESSION["wa_current_user"]->company]["tbpref"]; 
					if($pref == $def_coy.'_'){
					echo "  <li><a class='shortcut' href='$path_to_root/themes/Vanigam/theme-options.php'>" . _("Theme Options") . "</a> </li>";
					}
					echo ($_SESSION['wa_current_user']->company == 0 ? '<li><a href="'.$path_to_root.'/themes/Vanigam/master-login.php"> Master Login </a></li>' : '').' ';
			
					echo '	<li><a href="'.$path_to_root.'/admin/display_prefs.php?">Preference</a></li>';
					if ($SysPrefs->help_base_url != null && isset($vanigam_options['hide_help_link']) && $vanigam_options['hide_help_link'] == 0 ) {
					echo '<li><a class="right" onclick="javascript:openWindow(this.href,this.target); return false;" href="'. help_url().'"> <i class="icon-glyph" ></i>' . _("Help") .'</a></li>';
					}
					echo ' <li><a href="'.$path_to_root.'/admin/change_current_user_password.php?selected_id='. $_SESSION["wa_current_user"]->username .'">Change Password</a></li> 
					<li class="divider"></li>
					<li><a href="'.$local_path_to_root.'/themes/Vanigam/logout.php?">Sign Out</a></li>
					</ul>
					</li>
					</ul></div>';

					
		add_access_extensions();
		$applications = $_SESSION['App']->applications;
					
		$local_path_to_root = $path_to_root;
		$sel_app = $_SESSION['sel_app'];				

		if(isset($vanigam_options['enable_master_login']) && $vanigam_options['enable_master_login'] == 1) {
       		include_once($path_to_root."/themes/Vanigam/includes/users.php");
			$row = get_master_login($_SESSION['wa_current_user']->loginname);
			$companies = unserialize(base64_decode($row['companies']));
			$local_path_to_root = $path_to_root; 
			if(!empty($companies)){
				if(!in_array($_SESSION["wa_current_user"]->company, $companies)){
					$_SESSION['wa_current_user']->company = $companies[0];
					header("location:index.php");
				}
			}
        }        

       
      	if(isset($vanigam_options['enable_master_login']) && $vanigam_options['enable_master_login'] == 1) {
			echo '<div class="user-panel user-panel-dropdown">';
       		echo '<select name="CompanyName" class="ChangeCompany" data-live-search="true">';
							echo '<option value="'.$_SESSION['wa_current_user']->company.'" selected> '.$db_connections[$_SESSION['wa_current_user']->company]['name'].'</option>';
							foreach($db_connections as $cid => $single){
								if(isset($companies) && array($cid, $companies) && $cid != $_SESSION['wa_current_user']->company)
								echo '<option value="'.$cid.'">' . $db_connections[$cid]["name"] .'</option>';
							}

       		echo ' </select>';
			   echo '</div>';
    	}
        
        
		echo '<div id="menu" >';
		//$this->display_applications($sel_app);
		$indicator = "$path_to_root/themes/".user_theme(). "/images/ajax-loader.gif";
		echo '	<nav id="cbp-hrmenu" class="cbp-hrmenu">';
		echo '<ul>';

		echo'  <li><a href="'.$path_to_root.'" id="dashboard"><i class="icon-dashboard" ></i> '._('Dashboard').' </a></li>';					
		foreach($applications as $app){
			if ($_SESSION["wa_current_user"]->check_application_access($app)) {
				if(trim($app->id) == 'orders')
					$icon_root = 'dollar-2';
				elseif(trim($app->id) == 'AP')
					$icon_root = 'cart-2';
				elseif(trim($app->id) == 'stock')
					$icon_root = 'open';
				elseif(trim($app->id) == 'manuf')
					$icon_root = 'equalizer';
				elseif(trim($app->id) == 'assets')
					$icon_root = 'database';
				elseif(trim($app->id) == 'proj')
					$icon_root = 'fire';
				elseif(trim($app->id) == 'GL')
					$icon_root = 'stats-up';
				elseif(trim($app->id) == 'system')
					$icon_root = 'cog';
				else
					$icon_root ='drawer-2';

				$acc = access_string($app->name); 
				echo ' <li class='.($sel_app == $app->id ? : '').'>
					<a href="#"'.$acc[1].' > <i class="icon-'.$icon_root.'" ></i> &nbsp;'.$acc[0] .' <b class="aret"></b></a>
						 <div class="cbp-hrsub">
					<div class="cbp-hrsub-inner">';

				echo '<ul >';

				$kv_module_icon = array('clipboard-2', 'stats-up', 'steering-wheel', 'tag', 'cog' ); 
				$kv_small = 0 ; 
				
			foreach ($app->modules as $module)   {
					if (isset($module->name)) {// If parent
						echo "<li>
						<h5><i class='icon-".$kv_module_icon[$kv_small]."'></i>  " . $module->name . "</h5>";
						echo "	<ul>";
					
					$lapps = $rapps = array();
					foreach ($module->lappfunctions as $lappfunction)
						$lapps[] = $lappfunction;
					foreach ($module->rappfunctions as $rappfunction)
						$rapps[] = $rappfunction;
					$lapplication = $rapplication = array();
					echo ' <li class="col-sm-6 no-padding"> <ul>' ; 
					foreach ($lapps as $lapplication)    {
						$lnk = access_string($lapplication->label);
						if ($_SESSION["wa_current_user"]->can_access_page($lapplication->access)) {
							if ($lapplication->label != "") {
								echo  "<li><a href='".$path_to_root."/".$lapplication->link."' $lnk[1]><i class='icon-mic' style='color: green'></i> ".$lnk[0]."</a></li> \n";
							}
						}
						elseif (!$_SESSION["wa_current_user"]->hide_inaccessible_menu_items()){}
							//echo "<a href='#' class='disabled'>".$lnk[0]."</a>";																				
					}
					echo '</ul></li>';
					echo'  <li class=" col-sm-6 no-padding"> <ul>' ; 
					foreach ($rapps as $rapplication)    {
						$lnk = access_string($rapplication->label);
						if ($_SESSION["wa_current_user"]->can_access_page($rapplication->access)) {
							if ($rapplication->label != "") {
								echo  "<li><a href='".$path_to_root."/".$rapplication->link."'$lnk[1]><i class='icon-mic' style='color: green'></i> ".$lnk[0]."</a></li> \n";
							}
						}
						elseif (!$_SESSION["wa_current_user"]->hide_inaccessible_menu_items()){}										
					}
					echo '</ul></li> '; 
					}
					
					if (isset($module->name)) { // If parent
						echo "</ul></li>";
					}	
					echo '<div style="clear:both"> </div> ' ;
					$kv_small++;
				} 
				
				echo "</div> </div>  </li>";  
			}
		}					
		echo "</ul> </nav> </div>"; 
		?>					

		<script type="text/javascript">

		//<![CDATA[
		$(window).load(function(){
			$(function () {

			    $(".navbar-nav li").on('click', function (e) {
			    	//alert("Agwagwa");
			        if ($('ul.dropdown-menu', this).length) {
			            var elm = $('ul.dropdown-menu', this);			            
			            var off = elm.offset();
			            var l = off.left;
			            var w = elm.width();
			            var docH = $("#head-nav").height();
			            var docW = $("#head-nav").width();

			            var isEntirelyVisible = (l + w <= docW);
			           // alert(isEntirelyVisible );
			            if (!isEntirelyVisible) {
			                $('ul.dropdown-menu', this).addClass('edge');
			            } else {
			                $('ul.dropdown-menu', this).removeClass('edge');
			            }
			        }
			    });
			});
		});//]]> 

		</script><?php 		
		


		$color_scheme = (isset($lte_options['color_scheme']) ? $lte_options['color_scheme'] : 'default'); 		
		if($color_scheme == 'custom'){ 
		require_once('custom_color.php');
		}elseif($color_scheme != 'default') 	
			echo '<link rel="stylesheet" href="'.$path_to_root.'/themes/Vanigam/css/colorschemes/'.$color_scheme.'.css">';
		// if(file_exists($path_to_root.'/themes/Vanigam/css/colorschemes/'.$vanigam_options['color_scheme'].'.css'))	{
		// 		echo '<link rel="stylesheet" href="'.$path_to_root.'/themes/Vanigam/css/colorschemes/'.$vanigam_options['color_scheme'].'.css">';
		// 	}
		require_once("ExtraSettings.php");  

		if(isset($vanigam_options['hide_dashboard']) && $vanigam_options['hide_dashboard'] == 0){
			$root_url = $path_to_root.'?application=dashboard';
		} else
			$root_url = $path_to_root.'?application='.$_SESSION['wa_current_user']->prefs->startup_tab;

		echo "<img id='ajaxmark' src='$indicator' align='center' style='visibility:hidden;' alt='ajaxmark'>";
		echo '  </div> <!-- /.nav-collapse -->';
	}
				echo '
			</div>
		</div>' ; 
		if ($no_menu)
			echo "";
		elseif ($title && !$is_index){
				echo "<div class='kvcontainer'> <center><table id='title'><tr><td width='100%' class='titletext'>$title</td>"
				."<td align=right>"
				.(user_hints() ? "<span id='hints'></span>" : '')
				."</td>"
				."</tr></table></center>";
		}
	}

		function menu_footer($no_menu, $is_index)	{
			global $version, $allow_demo_mode, $app_title, $power_url,$power_by, $path_to_root, $Pagehelp, $Ajax, $vanigam_options;
			include_once($path_to_root . "/includes/date_functions.inc");

			include_once($path_to_root.'/config.php');

			$app_title = (isset($vanigam_options['powered_name']) ? $vanigam_options['powered_name'] : 'Vanigam' );
			
			
			$powered_url = (isset($vanigam_options['powered_url']) ? $vanigam_options['powered_url'] : 'http://frontaccounting.com');			
			$powered_url = addhttp($powered_url);
			echo '</div>';

			// if(kv_get_option('powered_name') != 'false'){
			// 	$app_title = kv_get_option('powered_name');
			// }else 
			// 	$app_title = 'Vanigam';

			// if(kv_get_option('powered_url') != 'false'){
			// 	$powered_url = addhttp(kv_get_option('powered_url'));
			// }else 
			// 	$powered_url = 'http://frontaccounting.com';
			

			echo "</div> <div  class='kvfooter'>"; // 'main_page'
			if ($no_menu == false) // bottom status line
			{
				if ($is_index)
					echo "<table class='bottomBar'>\n";
				else
					echo "<table class='bottomBar2'>\n";
				echo "<tr>";
				if (isset($_SESSION['wa_current_user'])) {
					$phelp = implode('; ', $Pagehelp);
					echo "<td class='bottomBarCell'  style='padding-left:20px;'>" . Today() . " | " . Now() . "</td>\n";
					echo "<td id='hotkeyshelp'  style='padding-right:20px;'>".$phelp."</td>";
					$Ajax->addUpdate(true, 'hotkeyshelp', $phelp);
					echo "<td align='center'><a target='_blank' href='$powered_url' tabindex='-1'>$app_title ";
					if(isset($vanigam_options['hide_version']) && $vanigam_options['hide_version']== 0 ){		echo $version;		}
					echo show_users_online()."</a></td>\n";
					
				}
				echo "</tr></table>\n";
			}
			echo '</div> ' ; 	?>
				<script src='<?php echo $path_to_root."/themes/".user_theme()."/js/cbpHorizontalMenu.js"; ?>'></script>
		<script type="text/javascript">		
			
			$(function() {
				cbpHorizontalMenu.init();
			});

		function myFunction() {
			  var x = document.getElementById("CompanyList");
			  if (x.className.indexOf("w3-show") == -1) {
			    x.className += " w3-show";
			  } else { 
			    x.className = x.className.replace(" w3-show", "");
			  }
		}
		$(".ChangeCompany").on('change', function (e) {  			   		
			var Cid = $(this).val();			   		
			$.ajax({
				type: "POST",
				url: "<?php echo $path_to_root; ?>/themes/Vanigam/includes/ajax.php?ChangeCompany="+Cid,
				data: 0,
				success: function(data){        
					if(data.trim()){
						window.location.reload();				           
					}else{
						alert("Failed to Delete". data);
					}               
				}
			});
		});

		$("#toggle").click(function() {
			$(this).toggleClass("on");
			$("#menu").slideToggle();
		});

		</script>

		<?php if(get_company_pref('no_item_list') == 1 || get_company_pref('no_customer_list') ==1 || get_company_pref('no_supplier_list') == 1) { ?>
			<link rel="stylesheet" href="<?php echo $path_to_root.'/themes/Saaisaran/css/select2.min.css'; ?>">
			<script src="<?php echo $path_to_root.'/themes/Saaisaran/js/select2.full.min.js' ; ?>"></script>
			<script type="text/javascript">
				$(function() {
						<?php if(get_company_pref('no_item_list') == 1) {?>
						call_kvcodes_select2('stock_id');
					<?php } if(get_company_pref('no_customer_list')) {?>
						call_kvcodes_select2('customer_id');
					<?php } if(get_company_pref('no_supplier_list')) {?>
						call_kvcodes_select2('supplier_id');
					<?php } ?>
					
				});
			</script>
		<?php } ?>
		<script type="text/javascript">
			
		function kvcodes_theme_refresh_select2(){
			<?php if(get_company_pref('no_item_list') == 1) {?>
						call_kvcodes_select2('stock_id');
			<?php } if(get_company_pref('no_customer_list')) {?>
						call_kvcodes_select2('customer_id');
			<?php } if(get_company_pref('no_supplier_list')) {?>
						call_kvcodes_select2('supplier_id');
			<?php } ?>
			//call_kvcodes_select2('branch_id');
		}

		function call_kvcodes_select2(elementId){
			if($("#"+elementId).length > 0 ){
				if(elementId == 'stock_id'){
					var type_string = $('#'+elementId).parent().next().next('img').attr('onclick');
					var index_pos = type_string.indexOf('type');
					type_string = type_string.substr(index_pos);
					index_pos = type_string.indexOf('&')-5;
					type_string = type_string.substr(5, index_pos);
				} else {
					var type_string = '';
				}
				//alert(type_string);
				$("#"+elementId).select2({
					allowClear :  true, 
					ajax : {
						url : "<?php echo $path_to_root.'/themes/Vanigam/includes/ajax.php?search_dropdown='; ?>"+elementId+"&type="+type_string,
						dataType: 'json',
						delay : 250,
						processResults : function(data){
							return { results : data };
						},
						cache : true
					}
				});
			}
		}
		</script>
	}	<?php 			
		}

		function display_applications(&$waapp){
			global $path_to_root, $vanigam_options;

			$selected_app = $waapp->get_selected_application();
			if (!$_SESSION["wa_current_user"]->check_application_access($selected_app))
				return;

			if (method_exists($selected_app, 'render_index')){
				$selected_app->render_index();
				return;
			}

			if( basename($_SERVER['PHP_SELF']) == 'index.php'){	
				require("dashboard.php");
			}else{
				echo "<table width='100%' cellpadding='0' cellspacing='0'>";
				foreach ($selected_app->modules as $module)	{
	        		if (!$_SESSION["wa_current_user"]->check_module_access($module))
	        			continue;
					// image
					echo "<tr>";
					// values
					echo "<td valign='top' class='menu_group'>";
					echo "<table border=0 width='100%'>";
					echo "<tr><td class='menu_group'>";
					echo $module->name;
					echo "</td></tr><tr>";
					echo "<td class='menu_group_items'>";
					foreach ($module->lappfunctions as $appfunction){
						$img = $this->get_icon($appfunction->category);
						if ($appfunction->label == "")
							echo "&nbsp;<br>";
						elseif ($_SESSION["wa_current_user"]->can_access_page($appfunction->access)) {
								echo $img.menu_link($appfunction->link, $appfunction->label)."<br>\n";
						}
						elseif (!$_SESSION["wa_current_user"]->hide_inaccessible_menu_items())	{
								echo $img.'<span class="inactive">'
									.access_string($appfunction->label, true)
									."</span><br>\n";
						}
					}
					echo "</td>";
					if (sizeof($module->rappfunctions) > 0)	{
						echo "<td width='50%' class='menu_group_items'>";
						foreach ($module->rappfunctions as $appfunction){
							$img = $this->get_icon($appfunction->category);
							if ($appfunction->label == "")
								echo "&nbsp;<br>";
							elseif ($_SESSION["wa_current_user"]->can_access_page($appfunction->access)) {
									echo $img.menu_link($appfunction->link, $appfunction->label)."<br>\n";
							}
							elseif (!$_SESSION["wa_current_user"]->hide_inaccessible_menu_items()){
									echo $img.'<span class="inactive">'
										.access_string($appfunction->label, true)
										."</span><br>\n";
							}
						}
						echo "</td>";
					}
					echo "</tr></table></td></tr>";
				}
				echo "</table>";
			}
		}
	}
?>

<!-- <a class="navbar-brand" href="'.$path_to_root.'">' . $db_connections[$_SESSION["wa_current_user"]->company]["name"] .'</a> -->
<style>
span.label {  position: absolute;   top: 9px;  right: 7px;  text-align: center;  font-size: 9px;  padding: 2px 3px;  line-height: .9;}
	/*.navbar-default .navbar-nav > li > .dropdown-menu > ul >#drop{
		float:right;
	}*/


@media (max-width: 767px){
.navbar-default .navbar-nav>li>a {  margin-top: 0px;}
.nav>li {   margin-top: -50px;}
}
/*	@media (max-width: 767px){
.navbar-default .navbar-nav .open .dropdown-menu>li>a {
    color: #fff;
}
}*/
/*@media (max-width: 767px){
.main-header .logo{
margin-left: 130px;}
}*/
</style>