<?php
require_once ("../config.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/arrays.inc.php");
require_once ("../classes/settings.class.php");


$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$id			= isset($_REQUEST['id'])	?	trim($_REQUEST['id'])	:	"0";
$message	= "";

if (!$objDB) {
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
	$objS	= new settings($objDB);
	$siteSettingsComm=$objS->select();

	//creating page array
	$Rootpage=array();
	$dirpath="../";
	if($handledir=opendir($dirpath)) 
	{
		while (false!==($file=readdir($handledir))) 
		{
			if ($file!= "." && $file!= "..") 
			{
				$splitbydotarray=explode(".",$file);
				switch($splitbydotarray[1])
				{
					case "php":
						$Rootpage[$file]=$file;
					break;
				}//end switch
			}//end if
		}//end while
	}//end if
	closedir($handledir);
	//sort array
	ksort($Rootpage);
	
	//Meta tags for each page
	$result=$objS->select_rows(TABLEPREFIX.'MetaTags','vTitle','');
	if(mysql_num_rows($result)>0)
	{
		 while ($row = mysql_fetch_array($result)) {
			$PageTitleArray[$row['vTitle']]=$row['vTitle'];
		  }//end for loop
	}//end if
	
	//checking login
	if(isset($_POST['btnGo']) && $_POST['btnGo']=='Update')
	{
		
                $txtPName=$_POST['txtPName'];
                $txtDes=$_POST['txtDes'];
                $txtDes2=$_POST['txtDes2'];
                $ddlContent=$_POST['ddlContent'];
		
		
		$cond="where vPageName='".addslashes($ddlContent)."'";
		$txtCnt=$objS->select_rows(TABLEPREFIX.'MetaTags','*',$cond);
		if(mysql_num_rows($txtCnt)>0)
		{
			//update db
			$field="vKeywords='".addslashes($txtDes)."'";
			$field2="vDescription='".addslashes($txtDes2)."'";
			$field3="vTitle='".addslashes($txtPName)."'";
		
			$objS->update_rows(TABLEPREFIX.'MetaTags',$field,$cond);
			$objS->update_rows(TABLEPREFIX.'MetaTags',$field2,$cond);
			$objS->update_rows(TABLEPREFIX.'MetaTags',$field3,$cond);
			$nId=$objS->fetchSingleValue($txtCnt,'nId');
			$msg='<font color="green">Contents Updated Successfully</font>.';
		}//end id
		else
		{
			$CheckCondition="where vTitle='".addslashes($txtTitle)."'";
			$check=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vTitle',$CheckCondition),'vTitle');
		
			if($check=='')
			{
				$value="'".addslashes($txtPName)."','".addslashes($ddlContent)."','".addslashes($txtDes)."','".addslashes($txtDes2)."'";
				$objS->insert_fields(TABLEPREFIX.'MetaTags', 'vTitle,vPageName,vKeywords,vDescription',$value);
				$nId=mysql_insert_id();
				$msg='<font color="green">Contents added successfully</font>.';
			}//end if
			else
			{
				$msg='Title already exist, choose another name.';
			}//end else
		}//end else
		header('location:settings.php?msg='.$msg.'&nId='.$nId);
	}//end  if
	else
	{
		//fetch contents
		$condition="where vPageName='index.php'";
		$txtCnt=$objS->select_rows(TABLEPREFIX.'MetaTags','*',$condition);
		if(mysql_num_rows($txtCnt)>0)
		{
			$txtDes=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vKeywords',$condition),'vKeywords');
			$txtDes2=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vDescription',$condition),'vDescription');
			$nId=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','nId',$condition),'nId');
			$txtPName=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vTitle',$condition),'vTitle');	
			$ddlContent=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vPageName',$condition),'vPageName');	
		}//end if
		else
		{
			$txtDes=$siteSettingsComm['metatagKey'];
			$txtDes2=$siteSettingsComm['metatagDes'];
			$txtPName=$siteSettingsComm['sitename'];
		}//end else
	}//end else


	$message=($_GET['msg']!='')?$_GET['msg']:$message;
	
	if(isset($_GET['nId']) && $_GET['nId']!='')
	{
		//fetch contents
		$condition="where nId='".$_GET['nId']."'";
		$txtDes=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vKeywords',$condition),'vKeywords');
		$txtDes2=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vDescription',$condition),'vDescription');
		$txtPName=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vTitle',$condition),'vTitle');
		$ddlContent=$objS->fetchSingleValue($objS->select_rows(TABLEPREFIX.'MetaTags','vPageName',$condition),'vPageName');	
	}//end if       
}//end else

include_once('title.inc.php');

?>
<script language="javascript" type="text/javascript" src="../js/metacontents.js"></script>
<!--script type="text/javascript" src="../js/floatingLayer.js"></script-->
<script type="text/javascript" src="../js/dropDown.js"></script>
<style>
td.top {
  background-color: #000080;
  text-align: right;
}

td.bottom {
  background-color: #ffe38c;
  padding: 15px;
}
#dhtmlgoodies_contentBox {
  border:1px solid #317082;
  height:0px;
  visibility:hidden;
  position:absolute;
  background-color:#E2EBED;
  overflow:hidden;
  padding:2px;
  width:450px;
}

#dhtmlgoodies_content {
  position:relative;  
  font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
  width:100%;
  font-size:0.8em;
}

#dhtmlgoodies_slidedown {
  position:relative;
  width:450px;
}
</style>
<script language="javascript" type="text/javascript">
function Validate()
{
	var s=document.frmContentMgmt;
	if(trim(s.txtDes.value)=='')
	{
		alert("Keywords can't be blank");
		s.txtDes.focus();
		return false;
	}//end if
	if(trim(s.txtDes2.value)=='')
	{
		alert("Description can't be blank");
		s.txtDes2.focus();
		return false;
	}//end if
	return true;
}//end fucntion

//show title
function ShowTitle(sName)
{
	document.getElementById('showTitle').innerHTML=sName;
}//end funciton
</script>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="72%" valign="top"><?php include_once('tophtml.php');?>
			<table width="100%"  border="0" cellspacing="0" cellpadding="5">
          <tr>
            <td><table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
              <!--  <td width="73%">Meta Tags Management - <span id="showTitle"></span></td>-->
                <td width="27%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        </table>
		<form name="frmContentMgmt" method="post" action="">
                    <table width="100%"  border="0" cellspacing="0" cellpadding="5" class="tblestyle_admin2">
            <tr>
              <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="2" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
				<?php  	}//end if
				      if ($objDB) 
					  { 
				?>
  				<tr align="left">
                  <td width="3%" class="maintext2">&nbsp;</td>
                  <td width="27%" valign="top" class="maintext2">Page Name <span class="warning">*</span></td>
                  <td width="70%" valign="top" class="maintext2"><?php echo $objS->select_tag('ddlContent','textbox3 txt-bx','onChange="showHint(this.value);"',$Rootpage,$ddlContent);?></td>
  				</tr>
				<?php 			
						}//end first if
				?>
              </table>
				<span id="showContent">
					<table width="100%"  border="0" cellspacing="0" cellpadding="0">
				<?php if ($objDB) 
					  { 
				?>
  				<tr align="left">
                  <td width="3%" class="maintext2">&nbsp;</td>
                  <td width="27%" valign="top" class="maintext2">Page Title <span class="warning">*</span></td>
                  <td width="70%" valign="top" class="maintext2"><input type="text" name="txtPName" class="textbox3 txt-bx" value="<?php echo $txtPName;?>" size="75"></td>
  				</tr>
                 <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Keywords <span class="warning">*</span></td>
                  <td valign="top" class="maintext2"><textarea name="txtDes" cols="50" rows="10" class="textbox_textarea txt-bx"><?php echo $txtDes;?></textarea></td>
                 </tr>
                <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Description <span class="warning">*</span></td>
                  <td valign="top" class="maintext2"><textarea name="txtDes2" cols="50" rows="10" class="textbox_textarea txt-bx"><?php echo $txtDes2;?></textarea></td>
                </tr>
				<?php 			
						}//end first if
				?>
              </table>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                     
                    </table>
                      <?php if ($objDB) 
					  { 
					?>
                      <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                      <tr>
                          <td width="30%"></td>
                        <td align="left"><input type="submit" name="btnGo" value="Update" onSubmit="return Validate();" class="loginbutton">
                                   </td>
                      </tr>
                    </table>
					<?php 		
						}//end first if
				?></td>
                  </tr>
                </table></span></td>
            </tr>
          </table></form></td>
        </tr>
    </table>      
    </td>
  </tr>
</table>
<!--<div class="calendar_widget" id="calendar_widget" style="display:none "><iframe id='calendar_widget_iframe' name='calendar_widget_iframe' style="border: none;" width=100% height=100% src="../date/calendar_widget.html" /></div>-->
<?php // /include_once('bottomhtml.php');?>