remove.php
3.66 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?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');
}
}
}
}