<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager 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
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

// Secure and connect
require("../inc/dbconnect.php");
require("inc/secure_page.php");
require("../inc/tracker.php");

// Includes
require("inc/calendar.php");
require("inc/whosonline.php");
require("../inc/config.php");
require("inc/lang.php");

// Optimize all tables in db
mysql_select_db($DBName);
$Query = "SHOW TABLES FROM $DBName";
$result = mysql_query($Query);
while ($Row = mysql_fetch_row($result)) {
    $nTableName = $Row["0"];
	$Query = "OPTIMIZE TABLE $nTableName";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}

// Get visitor stats
nCleanupTracker(5);
$nAmountActive = nGetAmountActive();
$nAmountLoggedIn = nGetAmountLoggedIn();
?>

<?php include("inc/nav/header.php"); ?>
		
<div id="titlediv">
    <div class="clearfix container" id="pattern">
        <div class="row">
            <div class="col_12">
                <ul class="breadcrumbs hor-list">
                    <li><a href="home.php"><?php echo _("Dashboard"); ?></a></li>
                </ul>
                <h1><?php echo _("Dashboard"); ?></h1>
            </div>
        </div>
    </div>
</div>

<div class="container" id="actualbody">

<div class="row clearfix">
    <div class="col_9">
           <div class="widget clearfix">
               <h2><?php echo _("Welcome"); ?></h2>
               <div class="widget_inside report" style="padding-top:0;">
					<p>
						<?php echo _("You are currently logged in to the website manager for"); ?> <strong><?php echo $nCoName;?></strong>.<BR>
						<BR>
						<?php echo _("Your user role is"); ?> <?php if($nAdminAccess){ echo "<strong>"._("Administrator").".</strong>"; } else { echo "<strong>Editor.</strong>"; } ?> 
						<?php if($nAdminAccess){ ?>
							<?php echo _("You can edit all folders, pages, files and users on the site."); ?>
						<?php } else { ?>
							<?php echo _("You have access to edit the following pages:"); ?><BR><BR>
							<?php
							$nUserID = nGetUserIDFromName($ValidUser);
							if($nUserID){ $nPageList = nGetEditablePages($nUserID); }
							if(!$nPageList){ echo "<em class=\"caption\">"._("No access to any pages.")."</em>"; } else { echo "$nPageList"; }
							?>
							<BR><BR><BR>
							<p align="center"><em style="display:block; padding:0 50px;" class="caption"><?php echo _("For more detailed information about any of these pages, or to make changes to any pages for which you do not have access, please <strong>contact an Administrator</strong> of the system."); ?></em></p>
						<?php } ?>
					</p>
               </div>
           </div>
		   
		   <BR><BR><BR><BR>
		   
		   <div class="widget clearfix">
               <h2><?php echo _("Statistics"); ?></h2>
               <div class="widget_inside report" style="padding-top:0;">
					<p>
						<?php
						// Show visitor graph
						$nDaysInPast = 30;
						$nChartWidth = 667; $nChartHeight = 125;
						$nTodayDate = date("M.j");
						$nPastDate = nGetDay(date("Y-m-d"),-$nDaysInPast);
						$nPastDateARRAY = explode("-",$nPastDate);
						$nPastDate = date("M.j", mktime(0,0,0,$nPastDateARRAY[1],$nPastDateARRAY[2],$nPastDateARRAY[0]) );
						$nChartTitle = _("Visitors to site in the last 30 days")." ($nPastDate - $nTodayDate)";
						?>
						<img src="<?php echo nGetGoogleChartVisitorsURL($nDaysInPast,$nChartWidth,$nChartHeight,$nChartTitle);?>" width="<?php echo $nChartWidth;?>" height="<?php echo $nChartHeight;?>" alt="<?php echo $nChartTitle;?>" />
					</p>
               </div>
           </div>
    </div>
    <div class="col_3 last">
            <div class="widget clearfix">
                <h2><?php echo _("Website Visitors"); ?></h2>
                <div class="widget_inside report">
					<div class="button orange">
					    <span class="value"><?=$nAmountActive;?></span>
					    <span class="attr"><?php echo _("Visitors Online Now"); ?></span>
					</div>
					<div class="button green">
					    <span class="value"><?=$nAmountLoggedIn;?></span>
					    <span class="attr"><?php echo _("Managers Logged In"); ?></span>
					</div>
                </div>
            </div>
			
			<BR><BR><BR>
			
			<?php if($nAdminAccess){ ?>
			<div class="widget clearfix">
                <h2><?php echo _("Quick Links"); ?></h2>
                <div class="widget_inside">
					<p>
						<a href="<?php echo $nFullSitePath;?>" target="_blank"><?php echo _("View Website"); ?><img src="img/external.png" alt="" width="16" height="16" border="0" style="margin:0 0 -3px 4px;"></a><BR>
						<a href="site_settings.php"><?php echo _("Site Settings"); ?></a><BR>
						<a href="system_settings.php"><?php echo _("System Settings"); ?></a><BR>
						<a href="library_theme.php"><?php echo _("Change your website theme"); ?></a><BR>
					</p>
                </div>
            </div>
			<?php } ?>
    </div>
</div>

<?php include("inc/nav/footer.php") ?>

<?php
function nGetGoogleChartVisitorsURL($nDaysInPast,$nWidth=400,$nHeight=100,$nTitle=""){
	$nVisitorsARRAY = nGetVisitorsARRAY($nDaysInPast);
	$nDateARRAY = array(); $nPaymentTotalARRAY = array();
	foreach($nVisitorsARRAY AS $nDate => $nPaymentTotal){
		// considers date is formatted "YYYY-MM-DD"
		$nIndivDateARRAY = explode("-",$nDate);
		$nDateDay = $nIndivDateARRAY[2];
		$nDateDay = preg_replace('/^[0]/', '', $nDateDay); // trim off leading 0s
		if($nDateDay==1){ $nDateDay = nGetMonthName($nDate,1).".".$nDateDay; }
		$nDateARRAY[] = $nDateDay;
		// add payment total to it's own array
		$nPaymentTotalARRAY[] = $nPaymentTotal;
	}
	$nDateSet = implode("|",$nDateARRAY);
	$nPaymentSet = implode(",",$nPaymentTotalARRAY);
	$nMaxPayment = max($nPaymentTotalARRAY);
	$nMaxARRAY = array(1000000,500000,200000,100000,50000,20000,10000,5000,2000,1000,500,200,100,50,10,5);
	foreach($nMaxARRAY AS $nMaxThreshold){
		if($nMaxThreshold>$nMaxPayment){ $nMax=$nMaxThreshold; }
	}
	// special formatting for title before sending to Google
	$nTitle = str_replace(" ","+",$nTitle);
	$nTitle = str_replace("$","%24",$nTitle);
	$nURL = "http://chart.apis.google.com/chart?chxl=1:|".$nDateSet."&chxr=0,0,".$nMax."&chxs=0,888888,10.5,0,l,676767|1,676767,10.5,0,l,676767&chxt=y,x&chbh=a&chs=".$nWidth."x".$nHeight."&cht=bvg&chco=498ca9&chds=0,".$nMax."&chd=t:".$nPaymentSet."&chg=0,25,5,4&chtt=".$nTitle;
	return $nURL;
}
function nGetVisitorsARRAY($nTimeInPast=1){
	global $DBName, $Link;
	$nTotalOrders = 0;
	// Decide start/end dates for this data based on passed-in vars
	$nStartDate = date("m/d/Y", mktime(0,0,0,date("m"),date("d")-$nTimeInPast,date("Y")) );
	$nEndDate = date("Y-m-d");
	// Create an array to hold the sales totals for period
	$nSalesARRAY = array();
	for($nStep=$nTimeInPast; $nStep--; $nStep>=0){
		// $nStep = 30, 29, 28,..., 1
		$nCurrDate = nGetDay($nEndDate,-$nStep);
		$nSalesARRAY["$nCurrDate"] = nGetVisitorsOnDate($nCurrDate);
		
	}
	return $nSalesARRAY;
}
function nGetVisitorsOnDate($nDate){
	// Accepts date in format "YYYY-MM-DD", converts it to "MM/DD/YYYY"
	global $DBName,$Link;
	if(!$nDate){ return; }
	$nNextDayDate = nGetDay($nDate,1);
	//$nDate = nDateToDisplay($nDate);
	//$nNextDayDate = nDateToDisplay($nNextDayDate);
	$nTotalVisitors = 0;
	$nSQL = "SELECT nIP FROM nVisitLog WHERE (nAccessDate>='$nDate' AND nAccessDate<'$nNextDayDate')";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nTotalVisitors = mysql_num_rows($Result);
	return $nTotalVisitors;
}
function nGetEditablePages($nEditorID){
	global $DBName,$Link;
	$nPageARRAY = array();
	$nSQL = "SELECT nPageID,nNavTitle FROM xEditorPage LEFT JOIN nPage ON nPageID=nPage.nID WHERE nEditorID=$nEditorID ORDER BY nType,nNavTitle";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		$nPageName = $Row["nNavTitle"];
		$nPageARRAY[]="<a href=\"page_edit.php?id=$nPageID\">$nPageName</a>";
	}
	$nCode = implode(", ",$nPageARRAY);
	return $nCode;
}
function nGetUserIDFromName($nUserName){
	global $DBName,$Link;
	if(!$nUserName){ return; }
	$Query = "SELECT nID FROM nEditor WHERE nUser='$nUserName' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if($Row = mysql_fetch_array($Result)) {
		$nUserID = $Row["nID"];
	}
	return $nUserID;
}
?>
<?php require("../inc/db_close.php"); ?>

