<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');

$obj = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add News';
$buton = 'Add News';
$cpage = basename($_SERVER['PHP_SELF']).'?'.$_SERVER['QUERY_STRING'];

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


if(isset($_GET['msg'])){
	if($_GET['msg'] == add){
		$info[] = 'News Added Successfully';
	}
}

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

if(isset($_POST['submit'])){
        $valid = validmess($_POST['title'], "Title"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	$valid = validmess($_POST['news'], "News"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	if($error == ''){
		if($_POST['submit'] == 'Add News'){
			$sql1 = "SELECT * FROM ".TBL_NEWS." WHERE title = '".$_POST['title']."'";
			$rs1 = $obj->executeQry($sql1,$link);
			$row1 = $obj->fetchrow();	
			if($row1['title'] == $_POST['title']){
				$error[] = 'News with same title already exist...';
			} else {
				$sqll = "INSERT INTO ".TBL_NEWS." (title,added_date,news) VALUES ('".trim($_POST['title'])."','".trim($_POST['added_date'])."','".trim(htmlentities(addslashes($_POST['news'])))."') ";
				$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					if($_POST['last'] == 'yes') {
					@header('Location: news.php?msg=add');
					}
					else {
					@header('Location: add_news.php?msg=add');
					}
				}
			}
		}
	
		if($_POST['submit'] == 'Edit News'){
			$sqll = "UPDATE ".TBL_NEWS." SET title = '".trim($_POST['title'])."',added_date = '".trim($_POST['added_date'])."', news = '".trim(htmlentities(addslashes($_POST['news'])))."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: news.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="news.php">Manage News</a></td>
    <td align="center" class="submenu">Add News</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">News Title</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="20%">News Title: </td>
                    <td align="left" width="80%"><input type="text" name="title" id="title" value="<?php echo ($_POST['title'] == '') ? $row['title'] : $_POST['title']; ?>" /></td>
                    </tr>
					 <tr>
                    <td align="left" width="20%">News Date: </td>
                    <td align="left" width="80%"><input type="text" name="added_date" id="added_date" value="<?php if(isset($row['added_date']) && ($row['added_date'] != '')) { echo $row['added_date']; } else {  echo date('Y-m-d'); } ?>" /></td>
                    </tr>
					 <tr>
                    <td align="left" width="20%">Last News: </td>
                    <td align="left" width="80%"><input type="checkbox" name="last" id="last" value="yes"/></td>
                    </tr>
                        </table></td>
              </tr>
			 
                        </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">News</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('news') ;
				$oFCKeditor->BasePath =  SITE_URL.'fckeditor/' ;
				$oFCKeditor->Width = 600;
				$oFCKeditor->Height = 400;
				$oFCKeditor->Value = ($_POST['news'] == '') ? html_entity_decode(stripslashes($row['news'])) : $_POST['news'];
				$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="center"><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $buton;?>" /></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); ?>