edit.php 3 KB
<?php

//include the smarty class and the global functions
require "libs/Smarty.class.php";
require "global_functions.php";

//initialize smarty
$smarty = new Smarty;

//connect to the database
$db = new SQLite3('wiki.db');

//get current page to determine the action about to be performed; needed to set the active id to the <li> in the sidebar menu
$current_page=$_SERVER['PHP_SELF'];

//assign a value to the current page variable in the admin sidebar wrapper template using the current_page() function from global_functions.php
$smarty->assign("current_page",current_page());

//assign values to the array needed to display all the pages found in the database; needed when creating a section
$smarty->assign("db_page_title", get_pages());

//assign values to the array needed to display all the pages found in the database; needed when creating a section
$smarty->assign("page_section", get_sections());


if (!isset($_GET['btn_submit']))
{
	$smarty->display('tpl/edit.tpl');
}

else
{
	$to_edit = $_GET['select_option'];

	if ($to_edit == "page")
	{
		if (empty($_GET['page_to_edit']) || empty($_GET['new_page_title']))
		{
			$smarty->assign("error", "Something went wrong. Please try to fill the form again.");
			$smarty->display('tpl/failure.tpl');
		}

		else
		{
			$page_to_edit = $_GET['page_to_edit'];
			$id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_to_edit'");
			$title = $_GET['new_page_title'];
			$text = $db->escapeString($_GET['new_page_text']);

			$query=$db->exec("UPDATE pages SET title = '$title', text = '$text' WHERE id = '$id'");

			//if something went wrong show the failure template with the error message
			if(!$query)
			{
				$smarty->assign("error", $db->lastErrorMsg());
				$smarty->display('tpl/failure.tpl');
			}

			//else if everything went fine show the success template
			else
			{
				$smarty->display('tpl/success.tpl');
			}
		}

	}

	else if ($to_edit == "section")
	{
		if (empty($_GET['section_to_edit']) || empty($_GET['new_section_title']))
		{
			$smarty->assign("error", "Something went wrong. Please try to fill the form again.");
			$smarty->display('tpl/failure.tpl');
		}

		else
		{
			$section_to_edit = $_GET['section_to_edit'];

			$section_to_edit = explode(": ", $section_to_edit);
			$page_to_edit = $section_to_edit[0];
			$section_to_edit = $section_to_edit[1];

			$page_id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_to_edit'");
			$id = $db->querySingle("SELECT id FROM sections WHERE page_id = '$page_id' AND title = '$section_to_edit'");
			$title = $_GET['new_section_title'];
			$text = $db->escapeString($_GET['new_section_text']);

			$query=$db->exec("UPDATE sections SET title = '$title', text = '$text' WHERE id = '$id'");

			//if something went wrong show the failure template with the error message
			if(!$query)
			{
				$smarty->assign("error", $db->lastErrorMsg());
				$smarty->display('tpl/failure.tpl');
			}

			//else if everything went fine show the success template
			else
			{
				$smarty->display('tpl/success.tpl');
			}
		}
	}

}




?>