<?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 ($_GET["begin"] == "") {
	$begin = 0;
	$num = 1;
	$numBegin = 1;
}

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 (isset($_GET['msg'])) {
	$message = CUSTOMS_FIELDS_UPDTD." !!";
}
if ($_GET["msg1"]) {
	$message = $_GET["msg1"];
}

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

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

		CASE 'N':
			$delete_custom_field = "UPDATE " . $tableprefix . "custom_fields SET vcustom_delete_status = 'Y' WHERE ncustom_id = " . $_GET['del_id'];
			mysql_query($delete_custom_field);

			header("Location:custom_fields.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg1=Custom Field Is Disabled Successfully!!");

			break;
		CASE 'Y':
			$delete_custom_field = "UPDATE " . $tableprefix . "custom_fields SET vcustom_delete_status = 'N' WHERE ncustom_id = " . $_GET['del_id'];
			mysql_query($delete_custom_field);

			header("Location:custom_fields.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg1=Custom Field Is Enabled Successfully!!");

			break;
	}
} else if ($_GET['delete_permanent'] == 'Y') {

	$delete_custom_fields = "DELETE FROM " . $tableprefix . "custom_fields WHERE ncustom_id = " . $_GET['del_id'];
	mysql_query($delete_custom_fields) or die(mysql_error());

	header('Location:custom_fields.php?msg1='.CF_DELETE_SUCCESS);
}

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

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

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

    $qryopt .= "  WHERE vcustom_field_name  like '" . addslashes($txtSearch) . "%'";
}
$sql = " SELECT *,CASE WHEN vcustom_delete_status = 'Y' THEN 'No' ELSE 'Yes' END AS vcustom_deleted,
					CASE WHEN vcustom_field_type = 'text_box' THEN 'Text Box'
		 	 WHEN vcustom_field_type = 'text_area' THEN 'Text Area' END AS vcustom_type
					FROM " . $tableprefix . "custom_fields " . $qryopt . " ORDER BY ncustom_id DESC ";

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

$limit = getSettingsValue('npagination_limit');

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


$page = $_GET['page'];

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

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

$totalrows = mysql_num_rows($result);

$limitvalue = ($page - 1) * $limit;

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

$sql = $sql . $query_string;

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

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

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

        var form_name = new Array('txtSearch');		
        var display_name = new Array('<?php echo CUST_FIELD;?>');
        var count = 1;	

        if(check_fields('frmCustomField',form_name,display_name,count))	
        {
  
  	
            document.frmCustomField.submit();	
   
        }
        else
        {
  
            return false;
  
        }
     
    }
    function blockCustomField(val)
    {
        if(val == 'N')
        {

            return confirm("<?php echo MSG_CUSTMFLD_DISABLE_CONFIRM;?>!!")
	
        }
        else if(val == 'Y')
        {
	
            return confirm("<?php echo MSG_CUSTMFLD_ENABLE_CONFIRM;?>!!")
	
        }	

    }
    function deleteCustomField()
    {
        return confirm("<?php echo MSG_CUSTMFLD_DELETE_CONFIRM;?>!!")
    }
</script>
<body>
	<center>
		<div align="left" id="layout">
			<!--header area start -->
			<?php
			include_once("../includes/topheader_admin.php")
			?>
			<!--header area end -->
			<div align="right" class="main_body">
				<!--main body start -->
				<?php
				include_once("../includes/admintopmenu.php")
				?>
				<div align="right" class="content_area">
					<!--content area start -->
					<div align="left" class="page_title">
						<?php echo USER_REG_FIELDS;?>
					</div>
					<div align="center" class="text_information_success grey_tr">
						<?php echo $message ?>
					</div>
					<br>
					<div align="right">
						<form action="" method="get" name="frmCustomField">
							<table width="100%" border="0" cellpadding="0" cellspacing="0"
								class="manage_top">
								<tr><td width="75%" align="left" valign="middle" class=''>
								<?php echo SEARCH_BY_CUS_FIELDS;?> <input
										type="text" name="txtSearch" size="20" maxlength="50"
										value="<?php echo(($txtSearch)); ?>" class="textarea"> &nbsp;<a onClick="return clickSearch();" class="search_button"> <?php echo BTN_SEARCH; ?></a>
									</td>
									<td align="right"><a href="custom_fieldsadd.php?form_id=1"
                                                                                            style="cursor: pointer" class="add_new_button"><?php echo BTN_ADD_NEW; ?></a></td>
								</tr>
							</table>
						</form>
					</div>
					<div align="right">
						<form action="" method="post" name="frmCustomField1">
							<table width="100%" border="0" cellpadding="0" cellspacing="0"
								class="manage_table">
								<tr>
									<td colspan="5" align="right"><?php echo TEXT_FOR_USER;?>.</td>
								</tr>
								<tr class="form_header">
									<th width="5%" align="left">#</th>
									<th width="40%" align="left" class=""><?php echo FIELD_NAME;?>
									</th>
									<th width="25%" align="left"><?php echo FIELD_TYPE;?></th>
									<th width="10%" align="left"><?php echo ACTIVE;?></th>
									<th width="22%" align="center"><?php echo ACTION;?></th>
								</tr>
								<?php
								if (mysql_num_rows($result) > 0) {
    $i = 1;

    while ($row = mysql_fetch_array($rs)) {

   switch ($row["vcustom_type"]){
   	case 'Text Box':$st2=TEXTBOX;
   	break;
   	case 'Text Area' :$st2=TEXTAREA;
   	break;
}
?>
								<tr class="">
									<td><?php echo (($page * $limit) + $i - $limit) ?></td>
									<td align="left"><?php echo ($row["vcustom_field_name"]) . "</a>"; ?>
									</td>
									<td align="left"><?php echo $st2; ?></td>

									<?php
									$select_custom_data = "SELECT nvalue_id,ncustom_field_id
		FROM " . $tableprefix . "custom_fields_value
		WHERE vfield_name_value != ''
		AND ncustom_field_id = " . $row['ncustom_id'];
									$result_custom_data = mysql_query($select_custom_data) or die(mysql_error());
									switch ($row["vcustom_deleted"]){
										case 'Yes':$st1=YES;
										break;
										case 'No' :$st1=NO1;
										break;
        }
     //   if (mysql_num_rows($result_custom_data) == 0) {


            ?>
									<td align="left"><?php echo ($st1); ?></a>&nbsp;</td>
									<td align="left"><a
										href="customfields_edit.php?edit_id=<?php echo $row["ncustom_id"] ?>&qstring=<?php echo $_SERVER['QUERY_STRING'] ?>"><img
											src="../images/btn_edit.gif" border="0"> </a>&nbsp;/&nbsp;<a
										href="custom_fields.php?del_id=<?php echo $row["ncustom_id"] ?>&delete_status=<?php echo $row['vcustom_delete_status'] ?>&delete_permanent=Y"
										style="cursor: pointer"
										onClick="return deleteCustomField('<?php echo  $row['vcustom_delete_status'] ?>')"><img
											src="../images/btn_del.gif" border="0"> </a>&nbsp;/&nbsp; <?php
											if ($row['vcustom_delete_status'] == "Y") {
                                            ?> <a
										href="custom_fields.php?del_id=<?php echo $row["ncustom_id"] ?>&delete_status=<?php echo $row['vcustom_delete_status'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
										style="cursor: pointer"
                                                                                onClick="return blockCustomField('<?php echo $row['vcustom_delete_status'] ?>')"><?php echo LINK_ENABLE; ?></a> <?php
            } else {
                ?> <a
										href="custom_fields.php?del_id=<?php echo $row["ncustom_id"] ?>&delete_status=<?php echo $row['vcustom_delete_status'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
										style="cursor: pointer"
                                                                                onClick="return blockCustomField('<?php echo $row['vcustom_delete_status'] ?>')"><?php echo LINK_DISABLE; ?></a> <?php
                                            }
                                            ?>
									</td>
									<?php
                      //                  } 
                                        
                      /*                  else {
                                            $custom_data_row = mysql_fetch_assoc($result_custom_data);
                                            ?>

									<td width="2%" align="left"><?php echo ($st1); ?></td>
									<td width="9%" align="left"><span class="text_information"><?php echo NON_EMPTY_CUSFIELD;?>
											!! </span></td>

									<?php
                                            }  */
                                            ?>


									<td width="0%"></td>
								</tr>
								<?php
								$i++;
                                        }
                                    } 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 alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";
}


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

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


								<tr>
									<td colspan="7" align="center" class="page_nos" >&nbsp;</td>
									<td align="left" class="btn_gotbl" valign="middle">&nbsp;</td>
								</tr>
								<?php
								if ($page < $numofpages || $page > 1) {
    ?>
								<tr>
									<td class="page_nos" align="center" colspan="7">
									
									<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."(".S.")";?><?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;
											<?php echo GO_TO;?>
										</td>
										<td width="8%" align="right">
											<select class="textarea2" 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.frmCustomField1.submit();" class="go_button_2"><?php echo GO; ?></a>
										</td>
										<td width="41%" align="center"><?php echo $next ?></td>
										</tr>
									</table> 
									</td>
								</tr>
								<?php
                            }
                            ?>
							</table>

						</form>



					</div>


					<!--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";
			?>