create.php
3.81 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
121
122
123
124
125
126
127
128
129
<?php
/**
* Created by PhpStorm.
* User: aleksandarhristov
* Date: 27.01.15
* Time: 11:31
#################################################
* create.php
* this script creates new pages/sections by adding them to the database
*#################################################
**/
//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());
//if the form submit button isn't pressed - show the form template
if (!isset($_GET['btn_submit']))
{
$smarty->display('tpl/create.tpl');
}
//else if the form submit button was pressed
else
{
//get the values for what to add (page or section) and the title of what we add from the user input
$to_add = $_GET['select_option'];
$title = $_GET['title'];
//if there was no title entered
if (empty($title))
{
//display the failure template with the error message
$smarty->assign("error", "The title field has to have a value!");
$smarty->display('tpl/failure.tpl');
}
//else if the user entered a title
else
{
//if the user chose to create a new page
if($to_add == 'page')
{
$checkIfExists = return_page_id($title);
if($checkIfExists!=0)
{
$smarty->assign("error", "The page already exists!");
$smarty->display('tpl/failure.tpl');
}
else
{
//insert the new page in the database
$query = $db->exec("INSERT INTO pages (title) VALUES ('$title')");
//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->assign("page_title", $title);
$smarty->assign("action", "create");
$smarty->display('tpl/success.tpl');
}
}
}
//else if the user chose to create a new section
else if($to_add == 'section')
{
//if the user didn't choose a page to add the section to (can only happen if there's no page in the database), show the failure template with the error message
if(empty($_GET['page_to_add_section_to']))
{
$smarty->assign("error", "There has to be a page in the wiki first!");
$smarty->display('tpl/failure.tpl');
}
//else if there's a page in the database and therefore the user chose it
else
{
//get the value of this page to add the section to and perform a database query to find the id of this page, needed for the page_id field in the sections table
$page_to_add_section_to = $_GET['page_to_add_section_to'];
$page_id = return_page_id($page_to_add_section_to);
//insert the new section in the database
$query = $db->exec("INSERT INTO sections (page_id, title) VALUES ('$page_id', '$title')");
//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
{
$strPageSection = return_page_title($page_id).": ".$title;
$smarty->assign("section_title", $strPageSection);
$smarty->assign("action", "create");
$smarty->display('tpl/success.tpl');
}
}
}
}
}
?>