<?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
*/

require("../inc/stop_cache.php");
$nAdminOnlyPg = 0;
// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../../inc/config.php");
require("../inc/lang.php");
include("../inc/paginator.php");
include("../inc/pageutil.php");
include("../inc/calendar.php");

$nShowPerPg = 20;

// Get passed-in vars
if(isset($_GET["pg"])){ $nPage = $_GET["pg"]; } else { $nPage = ""; }
if(!$nPage && isset($_POST["pg"])){ $nPage = $_POST["pg"]; }
if(isset($_GET["srt"])){ $nSort = $_GET["srt"]; } else { $nSort = ""; }
if(isset($_GET["ord"])){ $nSortOrder = $_GET["ord"]; } else { $nSortOrder = ""; }
if(isset($_GET["spp"])){ $nShowPerPgOverride = $_GET["spp"]; } else { $nShowPerPgOverride = ""; }
if($nShowPerPgOverride){ $nShowPerPg = $nShowPerPgOverride; }

// Setup SQL query for displayed listing
if(!$nSort){ $nSort="nName"; }  // default sort
if(!$nSortOrder){ $nSortOrder="ASC"; }  // default sort order
$nWhereARRAY[] = "nParentID=0";

$nWhereSQL = "";
if(isset($nWhereARRAY)){ $nWhereSQL = "WHERE ".implode(" AND ",$nWhereARRAY); } else { $nWhereSQL = ""; }
$nOrderBySQL = "ORDER BY $nSort $nSortOrder";
$nListingSQL = "SELECT * From nSection $nWhereSQL $nOrderBySQL";
?>
<div id="listContent" style="padding-top:0px;">
<?php
$nPassThru = "";
$nDivToUpdate = "ajaxSections";
$nPageInDiv = "list/sections.php";
$nCurrQueryStr = $_SERVER['QUERY_STRING'];
if($nCurrQueryStr){ $nFullPageInDiv = $nPageInDiv."?".$nCurrQueryStr; } else { $nFullPageInDiv = $nPageInDiv; }
$nPagingText = nGetPaging($nListingSQL,$nPage,$nShowPerPg,$nPassThru,$nDivToUpdate,$nPageInDiv);
if($nPassThru){ $nPassThruQry="?".$nPassThru; $nPassThru="&".$nPassThru; }
// for extrenal refresh requests
print "<script>refreshSections = function(){ loadContent('".$nDivToUpdate."','".$nFullPageInDiv."'); }</script>";
?>

<!--[if !IE]>start section inner<![endif]-->
<div class="section_inner">
	<!--[if !IE]>start table_wrapper<![endif]-->
	<div class="table_wrapper">
		<div class="table_wrapper_inner">
		<table cellpadding="0" cellspacing="0" width="100%">
			<tbody><tr>
				<th style="width:25px;"><?php echo _("No."); ?></th>
				<?$nSortFld="nName"; if($nSortFld==$nSort){ if($nSortOrder=="ASC"){ $nLink="?srt=$nSortFld&ord=DESC$nPassThru";$nSortClass=" class=\"asc\""; } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=" class=\"desc\""; } } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=""; }?>
				<th><a href="javascript:loadContent('<?php echo $nDivToUpdate?>','<?php echo $nPageInDiv.$nLink?>');"<?php echo $nSortClass;?>><?php echo _("Name"); ?></a></th>
				<th width="50" style="text-align:center;"><?php echo _("Pages"); ?></th>
				<th style="width:96px;"><?php echo _("Actions"); ?></th>
			</tr>
			<?php echo nGetSectionList();?>
		</tbody></table>
		</div>
	</div>
	<!--[if !IE]>end table_wrapper<![endif]-->
</div>
<!--[if !IE]>end section inner<![endif]-->

<?php if($nTotalPg>1){ ?>
	<!--[if !IE]>start pagination<![endif]-->
	<div class="pagination">
		<?php echo $nPagingText;?>
	</div>
	<!--[if !IE]>end pagination<![endif]-->
<?php } else {?>
	<!--[if !IE]>start pagination<![endif]-->
	<div class="pagination">
		<span class="page_no">&nbsp;</span>
	</div>
	<!--[if !IE]>end pagination<![endif]-->
<?php } ?>


<?php
function nGetSectionList(){
	global $DBName,$Link,$nListingSQL,$nAdminAccess;
	global $nShowPerPg,$nTotalRec,$nStartRec,$nEndRec,$nTotalPg;
	if(!$nListingSQL){ return ""; }
	$nTable = "";
	$nRowClass = "";
	$nLocked = "";
	$nCurrRec = 0;
	mysql_select_db($DBName); $Result = mysql_query($nListingSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		if($nCurrRec>=$nStartRec && $nCurrRec<=$nEndRec && $nCurrRec<=$nTotalRec){
			$nID = $Row["nID"];
			$nName = $Row["nName"];
			$nParentID = $Row["nParentID"];
			$nLocked = $Row["nLocked"];
			$nPageCount = nGetPageCount($nID);
			if($nPageCount===0){ $nPageCount="<span class=\"caption\">$nPageCount</span>"; }
			if($nRowClass=="second"){ $nRowClass="first"; } else { $nRowClass="second"; }
			$nChildrenCODE = nGetChildrenRows($nID,1,$nRowClass);
			if($nLocked){
				$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>$nCurrRec.</td>
				<td><em><strong>$nName</strong></em></td>
				<td align="center" style="text-align: center;">$nPageCount</td>
				<td>&nbsp;</td>
			</tr>
			$nChildrenCODE
CODE;
			} else {
				if($nAdminAccess){
					$nEditText = _("Edit");
					$nDeleteText = _("Delete");
					$nEditItemText = _("Edit Section");
					$nDeleteItemText = _("Delete Section");
					$nEditCODE = "<li><a href=\"dialog/section_add.php?id=$nID\" title=\"$nEditItemText\" class=\"modal edit\">$nEditText</a></li>";
					$nDeleteCODE = "<li><a href=\"dialog/section_delete.php?id=$nID\" title=\"$nDeleteItemText\" class=\"modal delete\">$nDeleteText</a></li>";
				} else {
					$nEditCODE = ""; $nDeleteCODE = "";
				}
				$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>$nCurrRec.</td>
				<td><strong>$nName</strong></td>
				<td align="center" style="text-align: center;">$nPageCount</td>
				<td>
					<div class="actions_menu">
						<ul>
							$nEditCODE
							$nDeleteCODE
						</ul>
					</div>
				</td>
			</tr>
			$nChildrenCODE
CODE;
			}
		}
	}
	// Add Unclassified section if needed
	$nCurrRec++;
	$nPageCount = nGetPageCount(0);
	if($nPageCount){
		$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>$nCurrRec.</td>
				<td><em><strong class="caption">Unclassified</strong></em></td>
				<td align="center" style="text-align: center;">$nPageCount</td>
				<td>&nbsp;</td>
			</tr>
CODE;
	}
	if(!$nTable){ $nTable = "<tr id=\"$nRowClass\"><td height=\"200\" align=\"center\" class=\"caption\" colspan=\"5\"><em>"._("No data available to display.")."</em></td></tr>"; }
	return $nTable;
}

function nGetChildrenRows($nParentID,$nLevel=1,$nRowClass="first"){
	global $DBName,$Link,$nOrderBySQL,$nAdminAccess;
	if(!$nParentID){ return ""; }
	$nTable = "";
	$nRowClass = "";
	$nLocked = "";
	$nCurrRec = 0+($nLevel*100); // set count higher for row hilite javascript
	$nCurrRec = "$nParentID$nCurrRec";
	$nSubMargin = ($nLevel*15)."px";
	$nSQL = "SELECT * FROM nSection WHERE nParentID=$nParentID $nOrderBySQL";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		$nID = $Row["nID"];
		$nName = $Row["nName"];
		$nParentID = $Row["nParentID"];
		$nLocked = $Row["nLocked"];
		$nPageCount = nGetPageCount($nID);
		if($nPageCount===0){ $nPageCount="<span class=\"caption\">$nPageCount</span>"; }
		$nChildrenCODE = nGetChildrenRows($nID,($nLevel+1));
		if($nRowClass=="second"){ $nRowClass="first"; } else { $nRowClass="second"; }
		if($nAdminAccess){
			$nEditText = _("Edit");
			$nDeleteText = _("Delete");
			$nEditItemText = _("Edit Section");
			$nDeleteItemText = _("Delete Section");
			$nEditCODE = "<li><a href=\"dialog/section_add.php?id=$nID\" title=\"$nEditItemText\" class=\"modal edit\">$nEditText</a></li>";
			$nDeleteCODE = "<li><a href=\"dialog/section_delete.php?id=$nID\" title=\"$nDeleteItemText\" class=\"modal delete\">$nDeleteText</a></li>";
		} else {
			$nEditCODE = ""; $nDeleteCODE = "";
		}
		if($nChildrenCODE){ $nDeleteCODE=""; }
		if($nLocked){
			$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>&nbsp;</td>
				<td><img src="img/sub.gif" alt="" width="9" height="9" border="0" style="margin:-3px 0 3px $nSubMargin"> <em>$nName</em></td>
				<td align="center" style="text-align: center;">$nPageCount</td>
				<td>&nbsp;</td>
			</tr>
			$nChildrenCODE
CODE;
			} else {
				$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>&nbsp;</td>
				<td><img src="img/sub.gif" alt="" width="9" height="9" border="0" style="margin:-3px 0 3px $nSubMargin"> $nName</td>
				<td align="center" style="text-align: center;">$nPageCount</td>
				<td>
					<div class="actions_menu">
						<ul>
							$nEditCODE
							$nDeleteCODE
						</ul>
					</div>
				</td>
			</tr>
			$nChildrenCODE
CODE;
			}
	}
	return $nTable;
}

function nGetPageCount($nSectionID){
	global $DBName,$Link;
	if($nSectionID===""){ return 0; }
	$nNumRecords = 0;
	$nSQL = "SELECT nID FROM nPage WHERE nSectionID=$nSectionID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nNumRecords = mysql_num_rows($Result);
	return $nNumRecords;
}
?>
<?php require("../../inc/db_close.php"); ?>
