<?php
include("../includes/site_config.php");
include('../session_handler.inc');
include('../error.php');
include('../includes/functions.php');

/*
if ($Sess_UserType != "A") {exit;}*/
 $newsid = form_get('id');
$act = form_get('act');
if($_POST['act'] == 'save') {
    if (isset($_REQUEST['id']))
        $sql_query = "UPDATE news SET title = '".form_get('title')."', body = '".form_get('body')."' WHERE news_id = '$newsid'";
    else
        $sql_query = "INSERT INTO news(title,body,news_date) values('".form_get('title')."',  '".form_get('body')."', now())";
    mysql_query($sql_query, $link);
    if (empty($_REQUEST['id']))
        $id = mysql_insert_id();
    else
        $id = $_POST['id'];
}
elseif($_GET['act'] == 'remove')
{
    $sde_eventid = $_GET['id'];
	$filename="news_$sde_eventid.jpg";
	$sql_query = "DELETE FROM news WHERE news_id = $_GET[id]";
    mysql_query($sql_query);
	if($filename!="")
	{
		$filepath = "../news/".$filename;
		unlink ($filepath);
	}
   
	
}
if (isset($_FILES['imgFile'])) {
if ($_FILES['imgFile']['type'] == "image/pjpeg" || $_FILES['imgFile']['type'] == "image/jpeg") {
            if ( $_FILES['imgFile']['type'] == "image/pjpeg"  || $_FILES['imgFile']['type'] == "image/jpeg") { $extension=".jpg"; }
    # check whether immediate authorisation
    if ($_FILES['imgFile']['name'] != "") {
       
            $tempusername="news_$id";
			$filename="$tempusername"."$extension";
          	$targetfile="news/"."$filename";
           	createThumbs(115, 115, $_FILES['imgFile']['tmp_name'], 'jpg', "../$targetfile");
			//copy($_FILES['imgFile']['tmp_name'],"../$targetfile");
			
			
        
           
        	
        } else {
            error_page(PRGPICADMIN_ERROR3,GENERAL_USER_ERROR);
        }
    }
}




/*
if($_GET['act'])
    header("Location: $CONST_LINK_ROOT/admin/admin_news.php");
*/

/*if($_GET['act'])
{
echo "<script>location.href=\"admin_news.php\"</script>";
}
*/
# retrieve the template
$area = 'member';

$sql_result = mysql_query("SELECT * FROM news order by news_id DESC", $link);

?>
<?php include("include/header.php"); ?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;News Admin</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="650px" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
       
    <tr>
      <td class="admin-head">&nbsp;</td>
    </tr>
    <tr>
      <td>

	<table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
   <!-- <tr>
      <td colspan="3" align="center" class="tdhead">
        <input type="normal" class='normal' onclick="document.location.href = '<?=$CONST_LINK_ROOT?>/admin/admin_news.php'" value="<?=SD_ADM_NEWS_ADD?>">      </td>
    </tr>-->
   <!-- <tr class="tdtoprow" align="center">
      <td>
        <?=SD_ADM_NEWS_TITLE?>      </td>
      <td ><?=SD_ADM_STORIES_IMAGE?></td>
      <td >
        <?=GENERAL_DELETE?>      </td>
     </tr>-->
        <?php
          while($news = mysql_fetch_object($sql_result)) {$change_to_timestamp = strtotime(htmlspecialchars($news->news_date));
											$new_date = date("d-m-Y",$change_to_timestamp);?>
    

        <tr align=center class="normal" >
          <td width="120"><? if(file_exists("../news/news_$news->news_id.jpg")){?><img src="<?=$CONST_LINK_ROOT?>/news/news_<?=$news->news_id?>.jpg?<?=time()?>" width="100" height="100" />
		  <? }else{?>
	<img src="<?=$CONST_LINK_ROOT?>/templates/default/images/site_news.jpg" width="100" height="100">
		<? }?>
		  <br />
            <a href="<?=$CONST_LINK_ROOT?>/admin/admin_news.php?act=remove&id=<?=$news->news_id?>" onclick="if (confirm('<?=SD_ADM_NEWS_TEXT1?>')) {return true;} else {return false;}" >
              <?=GENERAL_DELETE?>
               </a>&nbsp;|&nbsp;<a href="<?=$CONST_LINK_ROOT?>/admin/admin_news.php?act=edit&id=<?=$news->news_id?>"><?=GENERAL_EDIT?></a></td>
          <td align="left" valign="top"><a href="<?=$CONST_LINK_ROOT?>/admin/admin_news.php?id=<?=$news->news_id?>">
            <strong><?=htmlspecialchars($news->title)?></a><br /><?=$new_date?></strong>
          <br /><br /><?=htmlspecialchars($news->body)?></td>
          </tr>
	<tr><td colspan="3"><hr class="line"/></td></tr>
    <?php } ?>

  </table>

	  </td>
    </tr>
</table>




<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
        <tr>
      <td align="right">
      <?php 	  //require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");	  require_once("../user_status.inc.php");	  ?>
      </td>
    </tr>
    <tr>

    <td class="admin-head">
      <?= $newsid ? SD_ADM_NEWS_EDIT_SECTION_NAME : SD_ADM_NEWS_ADD_SECTION_NAME ?>
    </td>
    </tr>
    <tr>
      <td><table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>"><form method="post" action="<?=$CONST_LINK_ROOT?>/admin/admin_news.php<?php print("?PHPSESSID=".session_id()); ?>" enctype="multipart/form-data">
        <?php
		 if ($act == 'edit'){
        $sql_result = mysql_query("SELECT * FROM news WHERE news_id='".$newsid."'", $link);
        $news = mysql_fetch_object($sql_result); ?>
		 
		 <input type="hidden" name="id" value="<?=$newsid?>">
		 
       <? }
		?>
        
          <input type="hidden" name="act" value="save">
         
          <tr>
            <td align="center">

	   <table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">

                <tr>
                  <td colspan="2" class="normal">&nbsp;</td>
                </tr>
                <tr class="normal">
                  <td>
                    <?=SD_ADM_NEWS_TITLE?>                  </td>
                  <td> <input name="title" type="text" class="input" value="<?=htmlspecialchars($news->title)?>"></td>
                </tr>
                <tr class="normal">
                  <td>
                    <?=SD_ADM_NEWS_BODY?>                  </td>
                  <td> <textarea name="body" cols="50" rows="10" wrap="soft" class="inputl"><?=htmlspecialchars($news->body)?></textarea></td>
                </tr>
                <tr class="normal">
                  <td><?=SD_ADM_STORY_IMAGE?>
                  </td>
                  <td><input name="imgFile" type="file" >
                      <br></td>
                </tr>
                <tr>
                  <td colspan="2" align="center" class="normal"> <input name="submit" type="submit" class="normal" value="<?=GENERAL_SAVE?>">
				  <?  if ($act == 'edit'){?>
                    <input name="normal" type="submit" class="normal" onclick="window.location = '<?=$CONST_LINK_ROOT?>/admin/admin_news.php'" value="<?=GENERAL_CANCEL?>">   <? }?>               </td>

                </tr>
              </table></td>
          </tr>
        </form>
      </table></td>
    </tr>
</table></div><div class="admin-page-bottom"></div></div>
<?php mysql_close( $link ); ?>
<?php include("include/footer.php"); ?>