remove.php
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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');
			}
		}
	}
} 
