remove.php 3.66 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 the user hasn't submitted the form yet
if (!isset($_GET['btn_submit']))
{
	$smarty->display('tpl/remove.tpl');
}

//else if they have submitted the form
else
{
	//get the user choice of what they want to remove/delete
	$to_remove = $_GET['select_option'];

	//if the user wants to remove a page
	if ($to_remove == "page")
	{
		//if the variable with the page value is empty print an error and show the failure template
		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 if the variable with the page value isn't empty
		else
		{
			//get the title of the page to remove and find its id
			$page_to_remove = $_GET['page_to_remove'];
			$id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_to_remove'");

			//delete the page
			$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 the user wants to remove a section
	else if ($to_remove == "section")
	{
		//if the variable with the section title value is empty print an error and display the failure tempalte
		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 if it's not empty
		else
		{
			//get the title of the section we want to remove in format page: section
			//explode the string to get the section title without the page and the page title so that we can find the page_id as well
			$section_to_remove = $_GET['section_to_remove'];
			$section_to_remove = explode(": ", $section_to_remove);
			$page_of_section_to_remove = $section_to_remove[0];
			$section_to_remove = $section_to_remove[1];
			$page_id = $db->querySingle("SELECT id FROM pages WHERE  title = '$page_of_section_to_remove'");

			//find the id of this section and delete it from the database
			$id = $db->querySingle("SELECT id FROM sections WHERE  title = '$section_to_remove'");

			$query=$db->exec("DELETE FROM sections WHERE id = '$id' AND page_id='$page_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');
			}
		}
	}

}