<?

function news_add($newsdate, $newstitle, $newssummary, $newstext, $newspicture, $newsalignment, $newsrelatedurl, $redirect)
{
  // convert params..
  $newspkey=md5(microtime());
  if ($newsdate=="") { $realdate=gettimestamp(); } else { $realdate=converttounix($newsdate); }
  if ($newsalignment=="left") {
    $realalignment = 1;
  } else {
    $realalignment = 2;
  }
  // upload file (currently no caption or desc)..
  $imagepkey = image_uploadimage($_FILES['newspicture']['name'],$_FILES['newspicture']['tmp_name'],"","");
  // update news..
  $sa = add_data("Bulletin [Add]", "news_details", "newspkey, newsdate, newstitle, newssummary, newstext, newspicture, newspicturealignment, newsrelatedurl",
                 "'".$newspkey."','".$realdate."','".$newstitle.
                 "','".$newssummary."','".$newstext."','".$imagepkey."',".$realalignment.
                 ",'".$newsrelatedurl."'");
  // return result..
  if ($sa==1) {
    if ($redirect=="newslist") {
      $m=msg_n_goto("index.php?module=news&task=list","Bulletin added successfully!");
    } else {
      $m=msg_n_goto("index.php?module=news&task=main","Bulletin added successfully!");
    }
    echo $m;
  } else {
    $e=error_n_goto("index.php?module=news&task=main","Error: Failed to add Bulletin!");
    echo $e;
  }
}

function news_edit($newspkey, $newsdate, $newstitle, $newssummary, $newstext, $newspicture, $newsalignment, $newsrelatedurl, $redirect)
{
  // convert params..
  if ($newsdate=="") { $realdate=gettimestamp(); } else { $realdate=converttounix($newsdate); }
  if ($newsalignment=="left") {
    $realalignment = 1;
  } else {
    $realalignment = 2;
  }
  // upload file (currently no caption or desc)..
  $imagepkey = image_uploadimage($_FILES['newspicture']['name'],$_FILES['newspicture']['tmp_name'],"","");
  // update news..
  if ($imagepkey!="") {
    $sa = update_data("Bulletin [Edit]", "news_details", 
                      "newsdate = '".$realdate.
                      "', newstitle = '".$newstitle.
                      "', newssummary = '".$newssummary.
                      "', newstext = '".$newstext.
                      "', newspicture = '".$imagepkey.
                      "', newspicturealignment = ".$realalignment.
                      ", newsrelatedurl = '".$newsrelatedurl."'",
                      "where newspkey = '".$newspkey."'");
  } else {
    $sa = update_data("Bulletin [Edit]", "news_details", 
                      "newsdate = '".$realdate.
                      "', newstitle = '".$newstitle.
                      "', newssummary = '".$newssummary.
                      "', newstext = '".$newstext.
                      "', newspicturealignment = ".$realalignment.
                      ", newsrelatedurl = '".$newsrelatedurl."'",
                      "where newspkey = '".$newspkey."'");
  }
  // return result..
  if ($sa==1) {
    if ($redirect=="newslist") {
      $m=msg_n_goto("index.php?module=news&task=list","Buletin updated successfully!");
    } else {
      $m=msg_n_goto("index.php?module=news&task=main","Buletin updated successfully!");
    }
    echo $m;
  } else {
    $e=error_n_goto("index.php?module=news&task=main","Error: Failed to update Bulletin!");
    echo $e;
  }
}

function news_delete($newspkey)
{
  // TODO: delete corresponding picture and picture record!!
  // delete news..
  $sa = delete_data("Bulletin [Delete]", "news_details", "where newspkey = '".$newspkey."'");
  // return result..
  if ($sa==1) {
    if ($redirect=="newslist") {
      $m=msg_n_goto("index.php?module=news&task=list","Buletin deleted successfully!");
    } else {
      $m=msg_n_goto("index.php?module=news&task=main","Buletin deleted successfully!");
    }
    echo $m;
  } else {
    $e=error_n_goto("index.php?module=news&task=main","Error: Failed to delete Bulletin!");
    echo $e;
  }
}

function news_getnewsflash() {
  global $db;
  $myquery="Select * from news_flash"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {    
    while($myrow=$myresult->fetchRow()) {
      return $myrow['newstext'];
    }
  } else {
    return "";
  }
}

function news_editnewsflash($newstext)
{
  $updatedate=gettimestamp();
  // update newsflash..
  $sa = update_data("Buletin [Pinda Info Ringkas]", "news_flash", "newstext = '".$newstext.
                    "', newsupdatetime = '".$updatedate.
                    "', newsupdatedby = '".$_SESSION['Xuserpkey']."'",
                    "");
  // return result..
  if ($sa==1) {
    $m=msg_n_goto("index.php","Info Ringkas Berjaya Dipinda!");
    echo $m;
  } else {
    $e=error_n_goto("index.php","Ralat: Info Ringkas Gagal Dipinda!");
    echo $e;
  }
}

?>