<?
/* index.php: $pubmodule, $pubtask, $pubitemid */

// filter forms..
$filteruserpkey=$_POST['userpkey'];
$filterstartdate=$_POST['startdate'];
$filterstopdate=$_POST['stopdate'];
$filterstarttime=$_POST['starttime'];
$filterstoptime=$_POST['stoptime'];

$pubpageno = $_REQUEST['pageno'];
if ($pubpageno=="") $pubpageno=1;

$pubshowallnews = false;
if ($_GET['show']=="all") $pubshowallnews = true;
?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function news_button_showall() {
  document.location='index.php?module=news&task=list&show=all';
}

function news_button_add() {
  document.location='index.php?module=news&task=add';
}

function news_button_edit(newspkey) {
  document.location='index.php?module=news&task=edit&itemid='+newspkey;
}

function news_button_view(newspkey) {
  document.location='index.php?module=news&task=view&itemid='+newspkey;
}

function news_button_delete(newspkey) {
  document.location='index.php?module=news&task=delete&itemid='+newspkey;
}

function news_button_save() {
  if (validateForm()==true) {
    document.newsform.job.value='news_add'; document.newsform.submit();
  }
}

function news_button_update() {
  if (validateForm()==true) {
    document.newsform.job.value='news_edit'; document.newsform.submit();
  }
}

function news_button_purge() {
  document.newsform.job.value='news_remove'; document.newsform.submit();
}

function news_button_back() {
  history.go(-1);
}

</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/newsModel.php');
$mynewsModel=new newsModel;

?>

<?

// ................................................................................
// data retrieval functions..
// ................................................................................


// ................................................................................
// data modifications functions..
// ................................................................................


// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="Bulletin";
if ($pubtask=="submenu") {
  $subtitle="News Menu";
} else if (($pubtask=="main")||($pubtask=="list")) {
  $subtitle="News Bulletin";
} else if ($pubtask=="read") {
  $subtitle="Read Bulletin";
} else if ($pubtask=="add") {
  $subtitle="Add Bulletin";
} else if ($pubtask=="edit") {
  $subtitle="Edit Bulletin";
} else if ($pubtask=="view") {
  $subtitle="View BUlletin";
} else if ($pubtask=="delete") {
  $subtitle="Delete Bulletin";
} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." ··» ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="list")) {

  $news_list = $mynewsModel->getlist_news();
  include_once('views/news-list.tpl');

} else if ($pubtask=="read") {

  $news_data = $mynewsModel->getdata_news($pubitemid);
  include_once('views/news-read.tpl');

} else if ((($pubtask=="add")||($pubtask=="edit"))&&(($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2))) {

  $news_data = $mynewsModel->getdata_news($pubitemid);
  if ($pubtask=="newsadd") $news_data['news_date']=gettimestamp();
  include_once('views/news-edit.tpl');

} else if ($pubtask=="view") {

  $news_data = $mynewsModel->getdata_news($pubitemid);
  include_once('views/news-view.tpl');

} else if (($pubtask=="delete")&&(($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2))) {

  $news_data = $mynewsModel->getdata_news($pubitemid);
  include_once('views/news-view.tpl');

} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="list")) {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2)) {
    echo "<button name=\"add\" id=\"add\" onClick=\"news_button_add();\">&nbsp;<img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Add Bulletin</button>&nbsp;";
  }
  echo "<button name=\"all\" id=\"all\" onClick=\"news_button_showall();\">&nbsp;<img src='gfx/options/file.png' class='actionbutton'>&nbsp;&nbsp;Show All</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"news_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="read") {

  echo "<br><hr size=1>";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"news_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if (($pubtask=="add")&&(($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"news_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Bulletin</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"news_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if (($pubtask=="edit")&&(($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"news_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Bulletin</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"news_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2)) {
    echo "<button name=\"edit\" id=\"edit\" onClick=\"javascript:news_button_edit('".$news_data['news_pkey']."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
    echo "<button name=\"delete\" id=\"delete\" onClick=\"javascript:news_button_delete('".$news_data['news_pkey']."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  }
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"javascript:news_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if (($pubtask=="delete")&&(($pubuseraccess['access_news_managenews']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS BULLETIN?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"news_button_purge();\">&nbsp;Yes, Delete Bulletin&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"news_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>