<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (isset($_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"] != "") {
	$adminname = $_SESSION["sess_adminname"];
} else {
	header("Location:login.php");
	exit;
}
if (isset($_GET["txtSearch"]) and $_GET["txtSearch"] != "") {
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
} else if (isset($_POST["txtSearch"]) and $_POST["txtSearch"] != "") {
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}

/* ---------Patch----------- */


if ($_GET['msg'] != "") {
	switch ($_GET['msg']) {
		case 'add':
			$message = MSG_NEW_LANGUAGE."!!";
			break;
		case 'update':
			$message = MSG_LANGUAGE_DETAILS_UPDATED."!!";
			break;
                case 'update_status':
			$message = MSG_LANGUAGE_STATUS_UPDATED."!!";
			break;    
		case 'language_disable':
			$message = MSG_ENABLE_ATLEAST_ONE_LANGUAGE."!!";
			break;
                case 'language_enable':
			$message = MSG_ENABLE_ONLY_ONE_LANGUAGE."!!";
			break;
		case 'language_delete':
			$message = MSG_CANNOT_DELETE_ALL_LANGUAGES."!!";
			break;
                case 'language_failed':
                        $message = MSG_CANNOT_COPY_FILES."!!";
                        break;
                case 'language_rename_failed':
                        $message = MSG_CANNOT_RENAME_FILES."!!";
                        break;
                case 'language_undo':
                        $message = MSG_CANNOT_UNDO."!!";
                        break;
                case 'undo_success':
                        $message = MSG_UNDO_SUCCESS."!!";
                        break;
		case 'delete':
			$message = MSG_LANGUAGE_DELETED."!!";
			break;
	}
}

/* -------------Checking the count of active currencies-------------- */

$select_active_language = "SELECT nlanguageId FROM " . $tableprefix . "language_master WHERE langActive = '1'";
$result_active_language = mysql_query($select_active_language) or die(mysql_error());

/* -------------Checking the count of active currencies-------------- */

/* -------------Checking the count of currencies-------------- */

$select_language_count = "SELECT nlanguageId FROM " . $tableprefix . "language_master WHERE 1";
$result_language_count = mysql_query($select_language_count) or die(mysql_error());

if (mysql_num_rows($result_language_count) == 1) {
	$update_language_status = "UPDATE " . $tableprefix . "language_master SET langActive = '1' WHERE 1";
	mysql_query($update_language_status) or die(mysql_error());
	$update_language_row = mysql_fetch_assoc($result_language_count);
	$_SESSION['SESS_language_type'] = $update_language_row['nlanguageId'];
}


/* -------------Checking the count of active currencies-------- */

/* ---------Patch----------- */

if ($_GET['delete_permanent'] == 'N') {

	switch ($_GET['delete_status']) {

		CASE '0':
                        if(mysql_num_rows($result_active_language) == 1){
                           
                            $prev_lang = strtolower(mysql_result($result_active_language,0, 'nlanguageId'));
                            $_SESSION['SESS_prev_lang'] = $prev_lang;
                            
                            $prev_filename = backup_tables($HOST,$USER,$PASSWORD,$DATABASENAME,$tableprefix.'email_templates,'.$tableprefix.'content_management,'.$tableprefix.'help,'.$tableprefix.'help_categories,'.$tableprefix.'metatags');

                            $update_language1 = "UPDATE " . $tableprefix . "language_master SET langActive = '0' WHERE nlanguageId = " . $prev_lang;
                            mysql_query($update_language1) or die(mysql_error());

                            $update_language2 = "UPDATE " . $tableprefix . "language_master SET langActive = '1' ,prevLanguage= '".$prev_lang."', backupFile ='".$prev_filename."' WHERE nlanguageId = " . $_GET['del_id'];
                            mysql_query($update_language2) or die(mysql_error());

                            $_SESSION['SESS_language_type'] = $_GET['del_id'];
                            
                            /* ---------Patch----------- */
                            $msg = "update_status";


                            header("Location:language.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=" . $msg);
                            //exit;
                        }
                        elseif(mysql_num_rows($result_active_language) == 0){
                        
                            $delete_language = "UPDATE " . $tableprefix . "language_master SET langActive = '1' WHERE nlanguageId = " . $_GET['del_id'];
                            mysql_query($delete_language) or die(mysql_error());

                            $_SESSION['SESS_language_type'] = $_GET['del_id'];
                            
                            /* ---------Patch----------- */
                            $msg = "update";
                            header("Location:language.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=" . $msg);
                            //exit;
                        }
                        else{
                            //
                        }

			/* ---------Patch----------- */
			break;
		CASE '1':
                        
			if (mysql_num_rows($result_active_language) > 1) {
				$delete_language = "UPDATE " . $tableprefix . "language_master SET langActive = '0' WHERE nlanguageId = " . $_GET['del_id'];
				mysql_query($delete_language) or die(mysql_error());

				/* ---------Patch----------- */
				$msg = "update";
				header("Location:language.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=" . $msg);

				/* ---------Patch----------- */
			} else if (mysql_num_rows($result_active_language) == 1) {
				header("Location:language.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=language_disable");
			}
			break;
	}
} else if ($_GET['delete_permanent'] == 'Y') {

	if (mysql_num_rows($result_language_count) > 1) {

            $select_del_language = "SELECT languageName FROM " . $tableprefix . "language_master WHERE nlanguageId = " . $_GET['del_id'];

            $result_sel_language = mysql_query($select_del_language) or die(mysql_error());
            $source_folder_name = strtolower(mysql_result($result_sel_language,0, languageName));
            
            $delete_language = "DELETE FROM " . $tableprefix . "language_master WHERE nlanguageId = " . $_GET['del_id'];
            mysql_query($delete_language) or die(mysql_error());

            unlink('../language/'.$source_folder_name);
            header('Location:language.php?msg=delete');
            exit;
	} else {
		header('Location:language.php?msg=language_delete');
		exit;
	}
}


if($_GET['action'] =='undo'){
    
    $select_language = "SELECT * FROM " . $tableprefix . "language_master WHERE nlanguageId = " . $_GET['lang_id'];
    $result_language = mysql_query($select_language) or die(mysql_error());

    $prev_lang = strtolower(mysql_result($result_language,0, 'prevLanguage'));
    $file_name = strtolower(mysql_result($result_language,0, 'backupFile'));

    $update_language1 = "UPDATE " . $tableprefix . "language_master SET langActive = '0', prevLanguage ='0', backupFile ='' WHERE nlanguageId = " . $_GET['lang_id'];
    mysql_query($update_language1) or die(mysql_error());

    $update_language2 = "UPDATE " . $tableprefix . "language_master SET langActive = '1' WHERE nlanguageId = " . $prev_lang;
    mysql_query($update_language2) or die(mysql_error());

    $file_path = '../dbbackups/';
    

    if($file_name !=''){
        $schemafile = $file_path.$file_name;

        $sqlquery = @fread(@fopen($schemafile, 'r'), @filesize($schemafile));
        $sqlquery = splitsqlfile($sqlquery, ";");
        
        for($i = 0; $i < sizeof($sqlquery); $i++) {
            mysql_query($sqlquery[$i]);
        }
     
        header('Location:language.php?msg=undo_success');
    }

    

}
if (isset($_POST['gotoPage'])) {

	header("Location:" . $_SERVER['PHP_SELF'] . "?page=" . $_POST['gotoPage']);
}



$qryopt = "";
if ($txtSearch != "") {

    $qryopt .= "  WHERE languageName  like '" . addslashes($txtSearch) . "%'";
}
$sql = "SELECT *,CASE WHEN langActive = '1' THEN 'Yes' ELSE 'No' END AS active_status
						FROM " . $tableprefix . "language_master " . $qryopt . " order by nlanguageId DESC ";

if (!(isset($pagenum))) {
    $pagenum = 1;
}

$limit = getSettingsValue('npagination_limit');

$page = isset($_GET['page']) ? trim($_GET['page']) : 1;


$result = mysql_query($sql) or die(mysql_error());

$totalrows = mysql_num_rows($result);



/* ----------Pagination patch--------------- */

$page = $_GET['page'];

if (empty($page)) {
    $page = 1;
}

/* if($txtSearch != "")
 {
$limitvalue = 0;
//$page = 1;
}
else
{ */
$limitvalue = ($page - 1) * $limit;
//}

/* ----------Pagination patch--------------- */

$query_string = " LIMIT $limitvalue, $limit";

$sql = $sql . $query_string;

$rs = mysql_query($sql) or die(mysql_error());

$numofpages = ceil($totalrows / $limit);

//echo $sql;

include "../includes/htmltop.php";
?>
<script language="JavaScript" type="text/JavaScript">
    function clickSearch()
    {

        if(document.frmLanguage.txtSearch.value=='')
        {
            return confirm("<?php echo MSG_ENTER_LANGUAGE_NAME;?>")
            document.frmLanguage.txtSearch.focus();
            return false;
        }

        else
        {
            document.frmLanguage.submit();
            //return false;

        }

    }



    function deleteLanguage()
    {


        return confirm("<?php echo MSG_LANGUAGE_DELETE_CONFIRM;?>!!")

    }


    function blockLanguage(val)
    {


        if(val == '1')
        {

            return confirm("<?php echo MSG_LANGUAGE_DISABLE_CONFIRM;?>!!")

        }
        else if(val == '0')
        {

        	return confirm("<?php echo MSG_LANGUAGE_ENABLE_CONFIRM;?>!!")

        }

    }

    function undoChange(val){
         return confirm("<?php echo MSG_LANGUAGE_UNDO_CONFIRM;?>!!")
    }

</script>

<body>

	<center>

		<div align="left" id="layout">




			<!--header area start -->
			<?php
			include_once("../includes/topheader_admin.php")
			?>

			<!--header area end -->




			<div align="left" class="main_body">
				<!--main body start -->



				<?php
				include_once("../includes/admintopmenu.php")
				?>


				<div align="left" class="content_area">

					<!--content area start -->

					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<td height="35"><?php include_once("../includes/languagemenu.php") ?>
							</td>
						</tr>
						<tr>
							<td>

								<div align="left" class="page_title">
									<?php echo LANGUAGE_SETTINGS;?>
								</div>
								<div align="center" class="text_information grey_tr">
									<?php echo $message ?>
								</div>
								<div align="left">
									<form action="" method="get" name="frmLanguage">
										
										<tr class="" valign="top">
											<td align="right" colspan="6" valign="top">
												<table width="100%" border="0">
													<tr>
														<td align="right"></td>
														<td width="100%" align="left" valign="middle"><?php echo SEARCH_BY_LANGUAGE_NAME;?>:&nbsp;
															&nbsp; <input type="text" name="txtSearch" size="20"
															maxlength="50"
															value="<?php echo(htmlentities($txtSearch)); ?>"
															class="textbox"> <a style="cursor: pointer; text-decoration: none; background: #000; padding: 4px; color: #fff;"
															onClick="return clickSearch();">Go</a>
														</td>
													</tr>
												</table>
											</td>
										</tr>

									</form>

								</div>

								<div align="left">

									<form action="" method="post" name="frmCurrency1">

										<tr>
											<td colspan="6">

												<table border="0" width="100%" cellpadding="0"
													cellspacing="0" class='manage_table'>
													<tr class="">
														<th width="20%" align="left">#</th>
														<th width="24%" align="center"><?php echo LANGUAGE_NAME;?>
														</th>
														
														<th width="36%" align="center"><?php echo ACTIVE;?></th>
														<th width="20%" align="center"><?php echo ACTION;?></th>
													</tr>
													<?php
													if (mysql_num_rows($result) > 0) {

                                                    $i = 1;

                                                    while ($row = mysql_fetch_array($rs)) {
                                                        ?>
													<tr>
													<td><?php echo (($page * $limit) + $i - $limit) ?></td>
													<td align="center"><?php echo $link . htmlentities($row["languageName"]) . "</a>"; ?>
														</td>
														<td align="center"><?php echo htmlentities($row["active_status"]); ?>&nbsp;</td>
														<td align="left" valign="middle">
														<a
															href="languageedit.php?edit_id=<?php echo $row["nlanguageId"] ?>&qstring=<?php echo $_SERVER['QUERY_STRING'] ?>">
																<img title="<?php echo TITLE_EDIT; ?>" src="../images/btn_edit.gif"
																border="0">
														</a>
                                                                                                                    <?php if($row['langDeleteStatus'] == '1' && $row['langActive']!= '1'){?>
														<a
															href="language.php?del_id=<?php echo $row["nlanguageId"] ?>&delete_status=<?php echo $row['langDeleteStatus'] ?>&delete_permanent=Y"
															style="cursor: pointer"
                                                                                                                        onClick="return deleteLanguage();"> <img title="<?php echo TITLE_DELETE; ?>"
																src="../images/btn_del.gif" border="0">
														</a>
                                                                                                                    
                                                                                                                    &nbsp;/&nbsp;

                                                                                                                        <?php }
														if ($row['langActive'] == "0") {
                                                                                                                        ?> <a href="language.php?del_id=<?php echo $row["nlanguageId"] ?>&delete_status=<?php echo $row['langActive'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
															style="cursor: pointer"
                                                                                                                        onClick="return blockLanguage('<?php echo $row['langActive'] ?>')" class="enable_button"><?php echo LINK_ENABLE; ?></a> <?php
                                                                                                                     } else {
                                                                                                                    ?>     <a href="language.php?del_id=<?php echo $row["nlanguageId"] ?>&delete_status=<?php echo $row['langActive'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
															style="cursor: pointer"
                                                                                                                        onClick="return blockLanguage('<?php echo $row['langActive'] ?>')" class="disable_button"><?php echo LINK_DISABLE; ?></a> <?php

                                                                                                                         if($_GET['msg']== 'update_status' || $row["backupFile"] !=''){ ?>
                                                                                                                         / <a href="language.php?lang_id=<?php echo $row["nlanguageId"] ?>&action=undo&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
                                                                                                                              style="cursor: pointer" class="enable_button" onClick="return undoChange('<?php echo $row['nlanguageId'] ?>')"  title="<?php echo TITLE_UNDO.$row['languageName'];?>."><?php echo LINK_UNDO; ?></a>
                                                                                                                    <?php  }
                                                                                                                     }
                                                                                                                    
                                                                                                                ?>
														</td>
													</tr>
                                                                                                      
													<?php
													$i++;
                                                    }?>
                                                                                                          <tr><td colspan="4"><?php echo NOTE_UNDO; ?></td></tr>
                                                                                                        <?php

                                                } else {
                                                    ?>
													<tr>
														<td align="center" valign="middle" colspan="6"
															class="text_information"><?php echo NO_RECORDS_FOUND;?>
															!!</td>
													</tr>
													<?php
                                                }

                                                if ($page > 1) {
                                                    $pageprev = $page - 1;

                                                    $previous_page = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pageprev&txtSearch=$txtSearch alt='Previous' border='0' class='small_gray_button'>prev</a>";
                                                }


                                                if ($page < $numofpages) {
                                                    $pagenext = ($page + 1);

                                                    $next = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pagenext&txtSearch=$txtSearch alt='Next' border='0' class='small_gray_button'>next</a>";
                                                }

                                                if ($page < $numofpages || $page > 1) {
                                                    ?>


													<tr>
														<td colspan="6" align="right" valign="middle">
														
														<table width="100%" border="0" cellspacing="10" cellpadding="0">
															<tr align="left" valign="middle">
																<td width="21%" align="right"><?php echo $previous_page ?></td>
																<td width="23%" align="right">
																	<?php echo PAGE;?>(<?php echo S;?>) <?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;
																	<?php echo GO_TO;?>			
																</td>
																<td width="8%" align="right">
																	<select name="gotoPage" style="padding:4px 5px; margin-bottom:1px; ">
																		<?php
																		for ($i = 1; $i <= $numofpages; $i++) {
																		if ($i == $_GET['page']) {
																		$page_selected = "selected";
																		} else {
																		$page_selected = "";
																		}
																		?>
																		<option value="<?php echo $i ?>"
																		<?php echo $page_selected ?>>
																		<?php echo $i ?>
																		</option>
																		
																		<?php
																		}
																		?>
																	</select>
																</td>
																<td width="7%">
																	<a onClick="Javascript:document.frmLanguage1.submit();" class="go_button_2">Go</a>
																</td>
																<td width="41%" align="center"><?php echo $next ?></td>
															</tr>
														</table>
														</td>
													</tr>
													<?php
                                                }
                                                ?>
												</table>

									</form>



								</div>

							</td>
						</tr>
					</table>
					<!--content area end-->



				</div>


				<div align="left" class="clear_float">
					<img src="<?php echo $imagefolder; ?>/clear.gif" alt="" width="1"
						height="1" />
				</div>
				<!--main body end -->
			</div>


			<?php
			include "../includes/footer.php";
			?>