remove.php 2.65 KB
<?php
/**
 * Created by PhpStorm.
 * User: aleksandarhristov
 * Date: 30.01.15
 * Time: 14:23
 */

//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/remove.tpl');
}

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

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

		else
		{
			$page_to_remove = $_GET['page_to_remove'];
			$id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_to_remove'");


			$query=$db->exec("DELETE FROM pages 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_remove == "section")
	{
		if (empty($_GET['section_to_remove']))
		{
			$smarty->assign("error", "Something went wrong. Please try to fill the form again.");
			$smarty->display('tpl/failure.tpl');
		}

		else
		{
			$section_to_remove = $_GET['section_to_remove'];
			$section_to_remove = explode(": ", $section_to_remove);
			$section_to_remove = $section_to_remove[1];

			$id = $db->querySingle("SELECT id FROM sections WHERE  title = '$section_to_remove'");
			$query=$db->exec("DELETE FROM sections 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');
			}
		}
	}

}