create.php 3.81 KB
<?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');
				}
			}
		}
	}


}

?>