editor.php 2.22 KB
<?php
/**
 * Created by PhpStorm.
 * User: aleksandarhristov
 * Date: 12.02.15
 * Time: 11:27
 */

//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 the selected option for what the user wants to edit (page or section)
$to_edit = $_POST['select_option'];
$smarty->assign("to_edit", $to_edit);

//if the user wants to edit a page
if($to_edit == "page")
{
	//if theres no title value display the failure template with the error message
	if(empty($_POST['page_to_edit']))
	{
		$smarty->assign("error", "Something went wrong. Please try to fill the form again.");
		$smarty->display("tpl/failure.tpl");
	}

	//else if there is a value for the title of the page the user wants to edit
	else
	{
		//assign the old text, page_id, old title etc. and display the editor template
		$page_to_edit = $_POST['page_to_edit'];
		$smarty->assign("old_title", $page_to_edit);
		$old_text = $db->querySingle("SELECT text FROM pages WHERE title='$page_to_edit'");
		$smarty->assign("old_text", $old_text);
		$smarty->assign("page_id", "");
		$smarty->display("tpl/editor.tpl");
	}
}

//else if the user wants to edit a section
else if($to_edit == "section")
{
	//if theres no title value display the failure template with the error message
	if(empty($_POST['section_to_edit']))
	{
		$smarty->assign("error", "Something went wrong. Please try to fill the form again.");
		$smarty->display("tpl/failure.tpl");
	}
	//else if there is a value for the title of the section the user wants to edit
	else
	{
		//assign the old text, page_id, old title etc. and display the editor template
		$old_title = $_POST['section_to_edit'];
		$old_title = explode(": ", $old_title);
		$section_to_edit = $old_title[1];
		$page_to_edit = $old_title[0];
		$page_id = $db->querySingle("SELECT id FROM pages WHERE title = '$page_to_edit'");
		$smarty->assign("old_title", urlencode($section_to_edit));
		$smarty->assign("page_id", $page_id);
		$old_text = $db->querySingle("SELECT text FROM sections WHERE title='$section_to_edit' AND page_id='$page_id'");
		$smarty->assign("old_text", $old_text);
		$smarty->display("tpl/editor.tpl");
	}
}
?>