<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/list/themes.php - version 1.0
*/

$nAdminOnlyPg = 1;

require("../inc/stop_cache.php");

// 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");

$nShowPerPg = 999;

// Get passed-in vars
if(isset($_POST["nTheme"])){ $nThemeToActivate = $_POST["nTheme"]; } else { $nThemeToActivate = ""; }
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; }

if(isset($_GET["t"])){ $nType = $_GET["t"]; } else { $nType = ""; }

if($nType){ $nPassThru = "?nType='$nType'"; } else { $nPassThru = ""; }
$nDivToUpdate = "themesTable";
$nPageInDiv = "list/themes.php";
$nCurrQueryStr = $_SERVER['QUERY_STRING'];
if($nCurrQueryStr){ $nFullPageInDiv = $nPageInDiv."?".$nCurrQueryStr; } else { $nFullPageInDiv = $nPageInDiv; }
if($nPassThru){ $nPassThruQry="?".$nPassThru; $nPassThru="&".$nPassThru; }
// for extrenal refresh requests
print "<script>refreshThemes = function(){ loadContent('".$nDivToUpdate."','".$nFullPageInDiv."'); }</script>";

if($nThemeToActivate){
	// Activate new theme
	$Query = "UPDATE nSettings SET nValue='$nThemeToActivate' WHERE nSetting='activeTemplate' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}

$nCurrentTheme = nGetActiveThemePath();
?>


	<ul class="gallery large">
		<?php echo nGetThemeList();?>
	</ul>
	
	<table border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td width="680"><?//php echo nGetThemeList();?></td>
	  </tr>
	</table>


<?php
function nGetThemeList(){
	global $DBName,$Link;
	global $ValidUser,$nCurrentTheme,$nDivToUpdate,$nPageInDiv;
	$nTable = "";
	// Get all folders in Theme directory
	$nCount = 1;
	$path = "../../tmpl/";
	$pagepath = "../tmpl/";
	$dir_handle = @opendir($path) or die("Unable to open $path");    
	while ($file = readdir($dir_handle)) {
		if($file == "." || $file == ".." || $file == "index.php" )
			continue;
		if(is_dir("$path$file/")){
			$nFolderName="$file";
			$nFolderPath="$path$file/";
			// show theme only if it has valid config file
			if(file_exists("$path$file/config.php")){
				require("$path$file/config.php");
				// create HTML
				$nThemeInfo = "";
				if($file."/"==$nCurrentTheme){
					$nActivateForm="<form action=\"#\" style=\"margin:4px;\"><input type=\"button\" name=\"nActivate\" id=\"nActivate\" value=\""._("This theme is active")."\" disabled></form>";
					$nDelete = "";
				} else {
					$nActivateForm="<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" name=\"FormTmplActvate$nCount\" id=\"FormTmplActvate$nCount\" style=\"margin:4px;\" onsubmit=\"return submitForm('$nDivToUpdate','$nPageInDiv','FormTmplActvate$nCount');return false;\"><input type=\"hidden\" name=\"nTheme\" id=\"nTheme\" value=\"{$file}/\"><input type=\"submit\" class=\"button gray\" name=\"nActivate\" id=\"nActivate\" value=\""._("Make this theme active")."\"></form>";
					//$nDelete = "<a href=\"dialog/image_delete.php?id=$nID\" title=\"Delete Theme\" onClick=\"Modalbox.show(this.href,{title: this.title, width: 450, overlayClose:false}); return false;\" class=\"delete_product\"><img src=\"img/spacer.gif\" alt=\"Delete Image\" width=\"14\" height=\"13\" border=\"0\"></a>";
					$nDelete = "";
				}
				if($nThemeAuthor){ $nThemeInfo.=_("Author").": $nThemeAuthor<BR>"; }
				if($nThemeVersion || $nThemeReleaseDate){ $nThemeInfo.=_("Released").": $nThemeReleaseDate<BR>"; }
				if($nThemeNotes){ $nThemeInfo.="<em>$nThemeNotes</em><BR>"; }
				$nImgTag = "<img src=\"$pagepath$file/$nThumbnail\" width=\"250\" height=\"172\" alt=\"\" border=\"0\">";
				$nTable.= <<<CODE
					<!-- BEGIN Theme //-->
					<li>
						$nImgTag<BR>
						<div style="width:300px; height:130px; padding:0 10px;">
							<strong>$nThemeName</strong> v$nThemeVersion<BR>
							$nThemeInfo<BR>
							<div align="center">$nActivateForm</div>
						</div>
					</li>
CODE;
				$nCount++;
			}
		}
	}
	closedir($dir_handle);
	$nTable = str_replace("[nTotal]",($nCount-1),$nTable);
	
	return $nTable;
}

function nGetActiveThemePath(){
	global $DBName,$Link;
	$nSettingsARRAY = array();
	$nSQL = "SELECT * FROM nSettings WHERE nSetting='activeTemplate'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nActiveTmplPath = $Row["nValue"];
	}
	return $nActiveTmplPath;
}
?>
<?php require("../../inc/db_close.php"); ?>
