<?
//switch board section
$action = isset($_POST['action']) ? $_POST['action'] : '';
switch($action){
case "Edit Page":
editPage();
break;
case "New Page":
displayPage();
break;
case "Save Page":
if(savePage()){
displayAllPageNames();
}else{
displayPage($_POST['page_name'], $_POST['page_content'], implode("<br/>", $GLOBALS['msg']));
}
break;
case "Roll Back":
rollBackDisplay();
break;
case "Make Change":
commitRollBack();
displayAllPageNames();
break;
case "List Pages":
default:
displayAllPageNames();
}
////end of switchboard
function commitRollBack(){
if (!isset($_POST['enliven_id'])){
die ("error, no roll back item selected");
}else{
$id=mysql_real_escape_string(trim($_POST['enliven_id']));
}
mysql_query("
Update
html_content
set
date_updated=now()
where
id='$id'")
or die(mysql_error());
}
function editPage(){
if(isset($_POST['id'])){
$id = mysql_real_escape_string(trim($_POST['id']));
$result = mysql_query("select id, page_name, page_content from html_content where id='$id'");
if (mysql_num_rows($result) == 0){
die ("error in query. Id not found");
}
$row = mysql_fetch_assoc($result);
displayPage($row['page_name'], $row['page_content'], $row['id'],null);
} else {
displayAllPageNames();
}
}
function savePage(){
if (empty($_POST['page_name'])){
$msg[] = "You have not provided a page name";
}
if (empty($_POST['page_content'])){
$msg[] = "You have not provided any page content";
}
if (count($msg) > 0){
$_GLOBAL['msg'] = $msg;
return false;
}
//cleanse each item
//nb we don't need $id as we always insert not update
$array = array("page_content", "page_name");
foreach ($array as $f){
$q[] = "$f='".mysql_real_escape_string(trim($_POST[$f]))."'";
}
$qry = "
Insert
into html_content
set
id=null,
date_updated=now()," . implode(",", $q);
$r = @mysql_query($qry);
if ($r){
return true;
}else{
die(mysql_error());
}
}
function displayPage($page_name=null, $content=null, $msg=null){
echo <<<EOL
$msg<br/>
<form action="{$_SERVER['PHP_SELF']}" method="post" enctype="multipart/form-data">
<table border="1" width="100%">
<tr>
<td width="15%">Page Name:</td>
<td width="85%"><input type="text" name="page_name" value=$page_name/></td>
</tr>
<tr>
<td>Content:</td>
<td><textarea name="page_content">$content</textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="reset" value="reset" />
<input type="submit" name="action" value="Save Page" />
</td>
</tr>
</table>
</form>
EOL;
}
function displayAllPageNames(){
$result=mysql_query("select distinct id, page_name from html_content")
or die(mysql_error());
echo <<<EOL
<table border="1">
<tr>
<th>Page Name</th>
<th>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="submit" name="action" value="New Page" />
</form>
</th>
</tr>
EOL;
while($row = mysql_fetch_assoc($result)){
echo <<<EOL
<tr>
<td>{$row['page_name']}</td>
<td>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="hidden" name="id" value="{$row['id']}" />
<input type="hidden" name="id" value="{$row['page_name']}" />
<input type="submit" name="action" value="Edit Page" />
<input type="submit" name="action" value="Roll Back" />
</form>
</td>
</tr>
EOL;
} //end of while clause
echo "</table>";
}
function rollBackDisplay(){
$output = <<<EOL
<form method="post" action="{$_SERVER['PHP_SELF']}" >
<table border='1'>
<tr>
<th>name</th>
<th>content</th>
<th>time</th>
<th>make live</th>
</tr>
EOL;
$result = mysql_query ("
select
id,
page_name,
content,
date_updated
from
html_content
where
page_name='".mysql_real_escape_string(trim($_POST['page_name']))."'")
or die (mysql_error());
while ($row=mysql_fetch_assoc($result)){
$output .= <<<EOL
<tr>
<td>{$row['name']}</td>
<td>{$row['content']}</td>
<td>{$row['date_updated']}</td>
<td><input type="radio" name="enliven_id" value="{$row['id']}" /></td>
</tr>
EOL;
} //end of while clause
$output .= <<<EOL
<tr>
<td colspan="4">
<input type="reset" name="reset" value="reset"/>
<input type="submit" name="action" value="Make Change"/>
</td
</tr>
</table>
</form>
EOL;
echo $output;
}
?>