<?php
/**********************************************************************
	Copyright (C) FrontAccounting Team.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
	See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
// Author: Joe Hunt, 01/06/2019. Release 2.4.

	class renderer
	{
		function get_sel_app($applications)
		{
			$lnk = $_SERVER['REQUEST_URI'];
			$url = explode('?', $lnk);
			$asset = false;
			$reps = false;
			if (isset($url[1]))
			{
				$asset = strstr($url[1], "FixedAsset");
				$reps = strstr($url[1], "Class");

			}
			$lnk = $url[0];					
			$pos = strrpos($lnk, "/");
			$lnk = substr($lnk, $pos + 1);
			foreach($applications as $app)
			{
				foreach ($app->modules as $module)
				{
					$apps = array();
					foreach ($module->lappfunctions as $appfunction)
						$apps[] = $appfunction;
					foreach ($module->rappfunctions as $appfunction)
						$apps[] = $appfunction;
					$application = array();	
					foreach ($apps as $application)	
					{
						$url = explode('?', $application->link);
						$app_lnk = $url[0];					
						$pos = strrpos($app_lnk, "/");
						if ($pos > 0)
						{
							$app_lnk = substr($app_lnk, $pos + 1);
							if ($app_lnk == $lnk)  
								return ($asset != false ? "assets" : $app->id);
							elseif ($reps)
							{
								$pos = strpos($reps, '=');
								$reps = substr($reps, $pos + 1);
								
								switch ($reps)
								{
									case '0': return "orders"; 
									case '1': return "AP";
									case '2': return "stock";
									case '3': return "manuf";
									case '7': return "assets";
									case '4': return "proj";
									case '5':
									case '6': return "GL";
								}
							}
						}	
					}
				}
			}
			return $_SESSION['sel_app'];
		}

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

		function wa_footer()
		{
			end_page(false, true);
		}
		function shortcut($url, $label) 
		{
			echo "<li>";
			echo menu_link($url, $label);
			echo "</li>";
		}
		function menu_header($title, $no_menu, $is_index)
		{
			global $path_to_root, $SysPrefs, $version, $db_connections, $installed_extensions;
			if ($_SESSION['language']->dir == 'rtl')
				$this->rtl_css_helper();
			echo "<div class='fa-body'>";
			if (!$no_menu)
			{
				$applications = $_SESSION['App']->applications;
				$local_path_to_root = $path_to_root;
				$pimg = "<img src='$local_path_to_root/themes/".user_theme()."/images/preferences.png' style='width:14px;height:14px;border:0;vertical-align:middle;padding-bottom:3px;' alt='"._('Preferences')."'>&nbsp;&nbsp;";
				$limg = "<img src='$local_path_to_root/themes/".user_theme()."/images/password.png' style='width:14px;height:14px;border:0;vertical-align:middle;padding-bottom:3px;' alt='"._('Change Password')."'>&nbsp;&nbsp;";
				$img = "<img src='$local_path_to_root/themes/".user_theme()."/images/logoff.png' style='width:14px;height:14px;border:0;vertical-align:middle;padding-bottom:3px;' alt='"._('Logout')."'>&nbsp;&nbsp;";
				$himg = "<img src='$local_path_to_root/themes/".user_theme()."/images/help.png' style='width:14px;height:14px;border:0;vertical-align:middle;padding-bottom:3px;' alt='"._('Help')."'>&nbsp;&nbsp;";
				echo "<div id='header'>\n";
				echo "<ul>\n";
				echo "	<li><a href='$local_path_to_root/admin/display_prefs.php?'>$pimg" . _("Preferences") . "</a></li>\n";
				echo "	<li><a href='$local_path_to_root/admin/change_current_user_password.php?selected_id=" . $_SESSION["wa_current_user"]->username . "'>$limg" . _("Change password") . "</a></li>\n";
				if ($SysPrefs->help_base_url != null)
					echo "	<li><a target = '_blank' onclick=" .'"'."javascript:openWindow(this.href,this.target); return false;".'" '. "href='". 
						help_url()."'>$himg" . _("Help") . "</a></li>";
				echo "	<li><a href='$path_to_root/access/logout.php?'>$img" . _("Logout") . "</a></li>";
				echo "</ul>\n"; 
				$cpath = company_path();
				$logo = get_company_pref('coy_logo');
				$logo_img = "$cpath/images/$logo";
				$indicator = "$path_to_root/themes/".user_theme(). "/images/ajax-loader.gif";
				echo "<span><img src='$logo_img' style='height:30px;border:0;'></span><span style='padding-left:350px;'><img id='ajaxmark' src='$indicator' class='busy'></span>\n";
				echo "<div style='clear:both;'></div>\n";
				echo "</div>\n"; // header
				echo "<div id='nav'>";
				add_access_extensions();
				$bull = "<img src='$local_path_to_root/themes/".user_theme()."/images/menu_right.png' style='width:12px;height:12px;border:0;vertical-align:middle;'>&nbsp;";
				//$bull = "&bull;&nbsp;";
				$i = 0;
				echo "<ul>\n";
				$sel_app = $this->get_sel_app($applications);
				foreach($applications as $app)
				{
					$i++;
					if (!$_SESSION["wa_current_user"]->check_application_access($app))
						continue;
					$acc = access_string($app->name);
					$link = "$path_to_root/index.php?application=$app->id '$acc[1]";
					
					echo "<li ".($sel_app == $app->id ? "class='active'" : "") ."><a href='$link>" . $acc[0] . "</a>\n";

					$j = -1;
					$cl = ($i > 5 ? "class='right'" : "");
					echo "<ul $cl>\n";
					echo "<table class='menu_group'>\n";
					foreach ($app->modules as $module)
					{
						if (!$_SESSION["wa_current_user"]->check_module_access($module))
							continue;
						$j++;
						$apps = array();
						$l = $k = 0;
						foreach ($module->lappfunctions as $appfunction)
							$apps[] = $appfunction;
						$l = count($apps);
						foreach ($module->rappfunctions as $appfunction)
							$apps[] = $appfunction;
						$count = 0;	
						foreach ($apps as $value)
						{
							if (!empty($value->label))
								$count++;
						}

						$application = array();	
						// Remove the number circle set $circle = ""
						echo "<tr><td colspan=2 class='line'>$module->name</td></tr><tr><td class='menu_group'>\n";
						echo "<table width='250px'>\n";
						foreach ($apps as $application)	
						{
							echo "<tr><td class='menu_group_items'>\n";
							if ($l == $k)
								echo "</td><tr></table></td><td class='menu_group'><table width='250px'>
									<tr><td class='menu_group_items'>\n";
							$k++;
							$lnk = access_string($application->label);
							if ($_SESSION["wa_current_user"]->can_access_page($application->access))
							{
								if ($application->label != "")
								{
									echo "<a href='$path_to_root/$application->link'>$bull{$lnk[0]}</a>\n";
								}
							}
							elseif (!$_SESSION["wa_current_user"]->hide_inaccessible_menu_items())
								echo "<span style='color:#bbb'>$bull{$lnk[0]}</span>\n";
						}
						echo "</td></tr></table>\n";
					}
					echo "</td></tr></table>\n";
					echo "</ul>\n"; 
					echo "</li>\n";
				}
				echo "</ul>\n";
				echo "</div>\n"; // nav
				echo "<div id='main'>\n";
			}
			echo "<div class='fa-content'>\n";
			if ($no_menu)
				echo "<br>";
			elseif ($title && !$no_menu && !$is_index)
			{
				echo "<center><table id='title'><tr><td width='50%' class='titletext'>$title</td>" .
				"<td align=right  class='titletext'><span>" . $db_connections[$_SESSION["wa_current_user"]->company]["name"] . "</span>" .
				"<span>&nbsp;&nbsp;" . _("Fiscal Year") . "&nbsp;&nbsp;" . begin_fiscalyear() . " - " . end_fiscalyear() . "</span>" .
				"</td><td align=right>"	. (user_hints() ? "<span id='hints'></span>" : '') . "</td>" .
				"</tr></table></center>";
			}
		}

		function menu_footer($no_menu, $is_index)
		{
			global $path_to_root, $SysPrefs, $version, $db_connections, $Ajax, $Pagehelp;
			include_once($path_to_root . "/includes/date_functions.inc");

			echo "</div>\n"; // fa-content
			if (!$no_menu)
			{
				echo "</div>\n"; // main
				echo "<div class='fa-footer'>\n";
				if (isset($_SESSION['wa_current_user']))
				{
					$phelp = implode(' | ', $Pagehelp);
					$Ajax->addUpdate(true, 'hotkeyshelp', $phelp);
					echo "<span id='hotkeyshelp'>".$phelp."</span>";
					echo "<span class='power'><a target='_blank' href='$SysPrefs->power_url'>&nbsp;&nbsp;$SysPrefs->power_by $version</a></span>\n";
					echo "<span class='date'>".Today() . "&nbsp;" . Now()."</span>\n";
					echo "<span class='date'>" . $db_connections[$_SESSION["wa_current_user"]->company]["name"] . "</span>\n";
					echo "<span class='date'>" . $_SERVER['SERVER_NAME'] . "</span>\n";
					echo "<span class='date'>" . $_SESSION["wa_current_user"]->name . "</span>\n";
					echo "<span class='date'>" . _("Theme:") . " " . user_theme() . "</span>\n";
					echo "<span class='date'>".show_users_online()."</span>\n";
				}
				echo "</div>\n"; // footer
			}
			echo "</div>\n"; // fa-body
		}

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

			$sel = $waapp->get_selected_application();
			meta_forward("$path_to_root/admin/dashboard.php", "sel_app=$sel->id");	
			end_page();
			exit;
		}
		function rtl_css_helper()
		{
			echo "<style>#nav ul ul ul { position: absolute; left: auto; right: 100%; top: 0; }
  #nav ul .right { left: 0; right: auto; }
  #nav ul .right ul { left: 100%; right: auto; } </style>\n";
		}
	}
	