Commit 278c06e2 by Aleksandar Hristov

Non-final: added search function, fixed errors, added better output etc

1 parent 69fefce9
......@@ -60,12 +60,12 @@ else
//get the new title submitted by the user
$title = $_POST['new_title'];
$title = str_replace("+", " ", $title);
//get the new text submitted by the user
$text = $db->escapeString($_POST['new_text']);
$plain_text = strip_tags($text);
//update the page in the database with the new title and text
$query = $db->exec("UPDATE pages SET title = '$title', text = '$text' WHERE id = '$id'");
$query = $db->exec("UPDATE pages SET title = '$title', text = '$text', plain_text='$plain_text' WHERE id = '$id'");
//if something went wrong show the failure template with the error message
if(!$query)
......@@ -76,7 +76,8 @@ else
//else if everything went fine show the success template
else
{
$smarty->assign("page_id", $id);
$smarty->assign("action", "edit");
$smarty->display("tpl/success.tpl");
}
}
......@@ -106,7 +107,8 @@ else
//get the new values for the text and title and update the row for this section in the database
$title = urldecode($_POST['new_title']);
$text = $db->escapeString($_POST['new_text']);
$query = $db->exec("UPDATE sections SET title = '$title', text = '$text' WHERE id = '$id'");
$plain_text = strip_tags($text);
$query = $db->exec("UPDATE sections SET title = '$title', text = '$text', plain_text='$plain_text' WHERE id = '$id'");
//if something went wrong show the failure template with the error message
if(!$query)
......@@ -117,6 +119,9 @@ else
//else if everything went fine show the success template
else
{
$smarty->assign("page_id", $page_id);
$smarty->assign("section_id", $id);
$smarty->assign("action", "edit");
$smarty->display("tpl/success.tpl");
}
}
......
......@@ -124,6 +124,27 @@ function return_section_id($section_title)
}
function return_real_section_id($page_id, $section_id)
{
global $db;
$query = $db->query("SELECT id FROM sections WHERE page_id = '$page_id'");
$i=1;
while ($row = $query->fetchArray())
{
if ($row['id'] == $section_id)
{
return $i;
}
else
{
$i++;
}
}
}
function return_page_id($page_title)
{
global $db;
......@@ -134,7 +155,35 @@ function return_page_id($page_title)
}
function return_real_page_id($page_id)
{
global $db;
$query = $db->query("SELECT id FROM pages WHERE id = '$page_id'");
$i=1;
while ($row = $query->fetchArray())
{
if ($row['id'] == $page_id)
{
return $i;
}
else
{
$i++;
}
}
}
function return_page_title($page_id)
{
global $db;
$page_title = $db->querySingle("SELECT title FROM pages WHERE id = '$page_id'");
return $page_title;
}
function main_page($return_this, $current_page)
{
global $db;
......
......@@ -26,10 +26,10 @@ class MyDB extends SQLite3
$query = $db->exec('CREATE TABLE admin (username varchar(255), password VARCHAR (255))');
if(!$query) echo $db->lastErrorMsg();
$query = $db->exec('CREATE TABLE pages (id INTEGER PRIMARY KEY AUTOINCREMENT, title varchar(250) NULL, text TEXT NULL)');
$query = $db->exec('CREATE TABLE pages (id INTEGER PRIMARY KEY AUTOINCREMENT, title varchar(250) NULL, text TEXT NULL, plain_text TEXT NULL)');
if(!$query) echo $db->lastErrorMsg();
$query = $db->exec('CREATE TABLE sections (id INTEGER PRIMARY KEY AUTOINCREMENT, page_id INTEGER NULL, title VARCHAR (250) NULL, text TEXT NULL )');
$query = $db->exec('CREATE TABLE sections (id INTEGER PRIMARY KEY AUTOINCREMENT, page_id INTEGER NULL, title VARCHAR (250) NULL, text TEXT NULL, plain_text TEXT NULL )');
if(!$query) echo $db->lastErrorMsg();
?>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: aleksandarhristov
* Date: 14.09.15
* Time: 09:37
*/
//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');
if (isset($_GET['s']))
{
$intCtr=null;
$current_page=$db->querySingle("SELECT MIN (id) FROM pages");
if (!empty($_GET['s']))
{
$strSearch = " ".$_GET['s'];
$sql = "SELECT id, page_id, title, plain_text
FROM sections
WHERE upper(plain_text) LIKE upper('%$strSearch%')";
$result = $db->query($sql);
$i=0;
while ($row = $result->fetchArray())
{
$arrResult[$row[$i]] = array();
$arrResult[$row[$i]]['section_id'] = $row['id'];
$arrResult[$row[$i]]['page_id'] = $row['page_id'];
$arrResult[$row[$i]]['section_title'] = $row['title'];
$arrResult[$row[$i]]['section_text'] = "...".substr(stristr($row['plain_text'], $strSearch), 0, 300)."...";
$arrResult[$row[$i]]['section_text'] = preg_replace("/".preg_quote($strSearch)."\w*/i", "<mark><u><strong>$0</strong></u></mark>", $arrResult[$row[$i]]['section_text']);
}
if(empty($arrResult))
{
$error = "No result found!";
$arrResult = null;
}
else
{
$error = null;
}
$intCtr = count($arrResult);
}
//assign smarty variables and display the search.tpl
$smarty->assign("current_page", $current_page);
$smarty->assign("pages", get_pages());
$smarty->assign("pages_id", get_pages_id());
$smarty->assign("arrResult", $arrResult);
$smarty->assign("counter", $intCtr);
$smarty->assign("error", $error);
$smarty->display("tpl/search.tpl");
}
\ No newline at end of file
......@@ -13,8 +13,7 @@
<div id="container2">
<h1 id="container2-header">Error!</h1>
</div>
<div class="alert alert-danger" role="alert">{$error}</div>
<div class="alert alert-danger" role="alert">{$_SERVER['HTTP_REFERER']} {$error}</div>
</div>
</div>
{include file="tpl/footer.tpl"}
\ No newline at end of file
......@@ -47,8 +47,8 @@
<a href="#menu-toggle" id="menu-toggle" class="hidden-xs"><i class="fa fa-bars fa-2x icon pull-left"></i></a>
</div>
<div class="col-xs-6" id="search-container">
<form name="#" method="#" action="#" class="form-search">
<input type="text" class="pull-right" name="search_param" id="search-rounded" placeholder="Suche">
<form method="get" action="search.php" class="form-search">
<input type="text" class="pull-right" name="s" id="search-rounded" placeholder="Suche">
</form>
</div>
</div>
......
{include file="tpl/header.tpl"}
<div id="wrapper">
{include file="tpl/user_sidebar_wrapper.tpl"}
{include file="tpl/menu_tray.tpl"}
<div id="container2">
<h1 id="container2-header">Slimspots Wiki - Search</h1>
</div>
<div class="container-fluid">
{if isset($error)}
<div class="alert alert-danger" role="alert">{$error}</div>
{else}
<h4>{$counter} result(s) found:</h4><hr>
<ul style="list-style-type: none;">
{foreach from=$arrResult key=key item=item}
<li><h4><a href="index.php?{$item.page_id}#{return_real_section_id({$item.page_id}, {$item.section_id})}"><u>{return_page_title({$item.page_id})} - {$item.section_title}</u></a></h4></li>
<li><em>{$item.section_text}</em></li><br/><hr style="background-color: #428BCA;">
{/foreach}
</ul>
{/if}
</div>
</div>
{include file="tpl/footer.tpl"}
\ No newline at end of file
......@@ -14,7 +14,16 @@
<h1 id="container2-header">Success!</h1>
</div>
<div class="alert alert-success" role="alert">The action was performed successfully!</div>
<div class="alert alert-success" role="alert">The action was performed successfully!
{if (isset($action) AND $action=="edit")}
<br/><br/>
<a href="edit.php" class="btn dark-blue">New edit</a>
{if isset($section_id)}
<a href="index.php?{$page_id}#{$section_id}" class="btn dark-blue">Go to last edited</a>
{else}
<a href="index.php?{$page_id}" class="btn dark-blue">Go to last edited</a>
{/if}
{/if}
</div>
</div>
{include file="tpl/footer.tpl"}
\ No newline at end of file
......@@ -22,7 +22,7 @@
{assign var=var value=1}
{foreach page_sections($pages[pages]) as $section_title}
{assign var=current_page_id value=return_page_id($pages[pages])}
{assign var=link value="?$current_page_id#$var"}
{assign var=link value="index.php?$current_page_id#$var"}
<li><a href={$link} class="sub-text-padding" >{$section_title}</a></li>
{assign var=var value=$var+1}
{/foreach}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!