<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$obj7 = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add New Email';
$buton = 'Add Email';

$sql1 = "SELECT * FROM news_subscribr";
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 

if(isset($_GET['action']) && $_GET['action'] == 'edit'){
	$headin = 'Edit Email';
	$buton = 'Edit Email';
	$obj2 = new DBClass();
	$sql = "SELECT * FROM news_subscribr WHERE id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}

if(isset($_POST['submit'])){
	
	$sql7 = "SELECT * FROM news_subscribr WHERE email = '".$_POST['email']."'";

	$rs7 = $obj7->executeQry($sql7,$link);

	$row7 = $obj7->fetchrow();	 

	if($row7['email'] == $_POST['email'] && ($_POST['email']!= '')){ $error[] = "This email is already registered.";}
	
	if($error == ''){
		if($_POST['submit'] == 'Add Email'){
			$sql7 = "SELECT * FROM news_subscribr WHERE email = '".$_POST['email']."'";

	$rs7 = $obj7->executeQry($sql7,$link);

	$row7 = $obj7->fetchrow();	 

	if($row7['email'] == $_POST['email'] && ($_POST['email']!= '')){ $error[] = "This email is already registered.";}	
			$sqll = "INSERT INTO news_subscribr (email,register_date) VALUES ('".trim($_POST['email'])."','".date("Y-m-d")."')";
			$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: newsSubscribrs.php?msg=add');
					exit;
				}
		}
	
		if($_POST['submit'] == 'Edit Email'){
			$sqll = "UPDATE news_subscribr SET email = '".trim($_POST['email'])."', register_date = '".date("Y-m-d")."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: newsSubscribrs.php?msg=edit');
				exit;
			}
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="newsSubscribrs.php">Manage Subscribers</a></td>
    <td align="center" class="submenu">Add New CRN</td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35"><?php echo $headin;?></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF" class="innermain"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
	<form name="pageform" id="pageform" method="post" action="">
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">Subscribers Details</td>
                </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">Enter Subscribers Email id: </td>
                    <td align="left" width="70%"><input type="text" name="email" id="email" value="<?php echo ($_POST['email'] == '') ? $row['email'] : $_POST['email']; ?>" /></td>
                  </tr>
               </table>
			  </td>
              </tr>
              
                  <tr>
                    <td align="center"><input name="submit" type="submit" class="bg_bt" id="button" value="Add Email" /></td>
                  </tr>
                  </table></td>
              </tr>
</form>
              </table></td>
          </tr>
          </table></td>
      </tr>
      </table></td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>