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

$headin = 'Add Email';
$buton = 'Add Email';
$cpage = basename($_SERVER['PHP_SELF']).'?'.$_SERVER['QUERY_STRING'];
$sql1 = "SELECT * FROM ".TBL_EMAIL;
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 
$cupage = 'Email';

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

if(isset($_POST['submit'])){
        $valid = validmess($_POST['subject'], "Subject"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	$valid = validmess($_POST['message'], "Message"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	if($error == ''){
		if($_POST['submit'] == 'Add Email'){
			$realname = strtolower($_FILES['attech']['name']);
			$path = ATTCH_DIR;
			$ctrlname = "attech";
			$file = handle_upload($realname,$path,$ctrlname);

				$sqll = "INSERT INTO ".TBL_EMAIL." (subject,added_date,message,img) VALUES ('".trim($_POST['subject'])."','".trim($_POST['added_date'])."','".trim(htmlentities(addslashes($_POST['message'])))."','".trim($file)."') ";
				$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: send-email.php?msg=add');
				}
		}
	
		if($_POST['submit'] == 'Edit Email'){
			$sqll = "UPDATE ".TBL_EMAIL." SET subject = '".trim($_POST['subject'])."',added_date = '".trim($_POST['added_date'])."', message = '".trim(htmlentities(addslashes($_POST['message'])))."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: send-email.php?msg=edit');
			}
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
include_once(ABS_ROOT_PATH."fckeditor/fckeditor.php") ;
?>
<link href="<?=EDITOR_DIR;?>sample.css" rel="stylesheet" type="text/css" />
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="send-email.php">Manage Emails</a></td>
    <td align="center" class="submenu">Add Email</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" enctype="multipart/form-data" action="<?php echo $cpage;?>">
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">Email</td>
                </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="left" width="40%">Email Subject: </td>
                    <td align="left" width="60%"><input type="text" name="subject" id="subject" value="<?php echo ($_POST['subject'] == '') ? $row['subject'] : $_POST['subject']; ?>" /></td>
                    </tr>
					 <tr>
                    <td>Email Date: </td>
                    <td><input type="text" name="added_date" id="added_date" value="<?php echo ($_POST['added_date'] == '') ? $row['added_date'] : $_POST['added_date']; ?>" /></td>
                    </tr>
					<tr>
                    <td >Attechment Img: </td>
                    <td><input type="file" name="attech" id="attech" value="<?php echo ($_POST['attech'] == '') ? $row['attech'] : $_POST['attech']; ?>" /></td>
                    </tr>
                        </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">Message</td>
              </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td colspan="2" align="center">
				<?php
				$oFCKeditor = new FCKeditor('message') ;
				$oFCKeditor->BasePath =  SITE_URL.'fckeditor/' ;
				$oFCKeditor->Width = 600;
				$oFCKeditor->Height = 400;
				$oFCKeditor->Value = ($_POST['message'] == '') ? html_entity_decode(stripslashes($row['message'])) : $_POST['message'];
				$oFCKeditor->Create() ;
				?>				
			</td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td align="right" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td align="left"><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $buton;?>" /></td>
					 <td align="right"><?php if(isset($_GET['id'])) { ?><a href="sendmail.php?id=<?=$_GET['id'];?>">Send Email</a><?php } ?></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); ?>