<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	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, 09/13/2010

	class renderer
	{
		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;

			//$sel_app = $_SESSION['sel_app'];
			echo "<div class='fa-main'>\n";
			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 class='topnav'>";
				echo "<ul>\n";
				//$sel_app = $this->get_sel_app($applications);
				$sel_app = $_SESSION['sel_app'];
				add_access_extensions();
				foreach($applications as $app)
				{
					if ($sel_app == $app->id)
						$cur_app = $app;
					if ($_SESSION["wa_current_user"]->check_application_access($app))
					{
						$acc = access_string($app->name);
						$link = "$path_to_root/index.php?application=$app->id '$acc[1]";
						echo "<li><a ".($sel_app == $app->id ? "class='active' " : " ") . "href='$link>" . $acc[0] . "</a></li>\n";
					}
				}
				echo "</ul>\n"; 
				echo "</div>\n"; // topnav
				//echo "<div class='clear'></div>\n";
				add_access_extensions();
				if ($_SESSION['language']->dir == 'rtl')
					$style = "style='float:right;'";
				else
					$style = "";					
				echo "<div class='fa-side' $style>\n";
				echo "<div class='fa-submenu'>\n";
				echo "<table cellpadding=0 cellspacing=0>\n";
				$app = $cur_app;
				$acc = access_string($app->name);
				$imgs = array('orders'=>'invoice.gif', 'AP'=>'receive.gif', 'stock'=>'basket.png',
				 	'manuf'=>'cog.png', 'proj'=>'time.png', 'GL'=>'gl.png', 'system'=>'controller.png');
				if (!isset($imgs[$app->id]))
					$imgs[$app->id] = "controller.png";
				$j = 0;
				if ($sel_app == "system")
					$imgs2 = array("page_edit.png", "page_edit.png", "page_edit.png", "page_edit.png", "folder.gif");
				else	
					$imgs2 = array("folder.gif", "report.png", "page_edit.png", "money.png", "folder.gif");
				foreach ($app->modules as $module)
				{
					echo "<tr><td width=20></td><td width=180><h4>$module->name</h4></td></tr>\n";
					$i = 0;
					$apps = array();
					foreach ($module->lappfunctions as $appfunction)
						$apps[] = $appfunction;
					$k = count($apps);
					foreach ($module->rappfunctions as $appfunction)
						$apps[] = $appfunction;
					$lnk2 = $_SERVER['REQUEST_URI'];
					//var_dump($lnk2);
					$application = array();	
					foreach ($apps as $application)	
					{
						if ($i == $k)
							$line = "class='line'";
						else
							$line = "";
						$i++;
						//var_dump($application->link);
						$lnk = access_string($application->label);
						if ($_SESSION["wa_current_user"]->can_access_page($application->access))
						{
							if ($application->label != "")
							{
								if (strpos($lnk2, $application->link))
									$active = "class='active'";
								else
									$active = "";		
								//var_dump($active);
								echo "<tr><td $line $active><img src='$local_path_to_root/themes/exclusive/images/".$imgs2[$j]."' style='vertical-align:middle;'></td>";
								echo "<td $line><a $active href='$path_to_root/$application->link'>$lnk[0]</a></td></tr>\n";  
							}
						}
						else	
						{
							echo "<tr><td $line><img src='$local_path_to_root/themes/exclusive/images/".$imgs2[$j]."' style='vertical-align:middle;'></td>";
							echo "<td $line><a style='color:gray;' href='#'>$lnk[0]</a></td></tr>\n";  
						}
					}
					$j++;	
				}
				echo "</table>\n";
				echo "</div>\n"; // submenu
				//echo "<div class='clear'></div>\n";
				echo "</div>\n"; // fa-side
				if ($_SESSION['language']->dir == 'rtl')
					$style = "style='margin-left: 0; margin-right:250px;';'";
				else
					$style = "";					
				echo "<div id='main' $style>\n";
			}
			echo "<div class='fa-body'>\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>";
/*
				echo "<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 $path_to_root, $SysPrefs, $version, $db_connections, $Ajax, $Pagehelp;
			include_once($path_to_root . "/includes/date_functions.inc");

			if (!$no_menu)
				echo "</div>\n"; // main
			echo "</div>\n"; // fa-content
			echo "</div>\n"; // fa-body
			if (!$no_menu)
			{
				echo "<div class='fa-footer'>\n";
				if (isset($_SESSION['wa_current_user']))
				{
					$phelp = implode('; ', $Pagehelp);
					$Ajax->addUpdate(true, 'hotkeyshelp', $phelp);
					echo "<span id='hotkeyshelp' style='float:right;'>".$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-main
		}

		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;
		}
	}
