Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search autocomplete on search.php page #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions ajax_bbspost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,30 @@

header("Content-type: application/json; charset=utf-8");

$sql = sprintf_esc("select * from bbs_posts where id = %d limit 1",$_POST["id"]);
$r = SQLLib::selectRow($sql);
$sql = new SQLSelect();
$sql->AddTable("bbs_posts");
$sql->AddField("bbs_topics.topic as topic");
$sql->AddField("bbs_topics.id as topicID");
$sql->AddField("bbs_posts.id as id");
if ($_POST["search"]) $sql->AddField("'".$_POST["search"]."' as searchQuery");
$sql->AddField("bbs_posts.post as post");
$sql->AddField("bbs_posts.added as postDate");
$sql->AddJoin("left","bbs_topics","bbs_posts.topic = bbs_topics.id");

$r = array();
if ($_POST["search"])
{
$sql->AddWhere(sprintf_esc("(bbs_posts.post LIKE '%%%s%%' or bbs_topics.topic LIKE '%%%s%%')",_like($_POST["search"]),_like($_POST["search"])));
$sql->AddOrder("bbs_posts.added DESC");
$sql->SetLimit(5);
$r = SQLLib::selectRows( $sql->GetQuery() );
}
else if ($_POST["id"])
{
$sql->AddWhere(sprintf_esc("id = %d",$_POST["id"]));
$sql->SetLimit(1);
$r = SQLLib::selectRows( $sql->GetQuery() );
}

echo json_encode($r);
?>
10 changes: 9 additions & 1 deletion autocompleter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Autocompleter = Class.create({
parent.insert(this.finalSelection);
}

this.searchBox = new Element("input",{"class":"autocompleteSearch","style":"position:relative","placeholder":this.options.placeholder});
var searchBoxOptions={"class":"autocompleteSearch","style":"position:relative","placeholder":this.options.placeholder};
if (this.options.searchBox) { searchBoxOptions["name"]="what"; searchBoxOptions["autocomplete"]="off"; }
this.searchBox = new Element("input",searchBoxOptions);
parent.insert( this.searchBox );

if (this.options.dropdown)
Expand Down Expand Up @@ -163,6 +165,12 @@ Autocompleter = Class.create({
{
var item = li.retrieve("item");

if (this.options.onClickUrl)
{
location.href=this.options.onClickUrl+item.id;
return;
}

this.select( item.id, item.name );
}
else
Expand Down
116 changes: 116 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,122 @@ function checkForNewsTickers()
});
}

// trims the post content of a bbs topic message, so it fits the autocomplete list
function trimPostContent(postContent,query,maxlen)
{
if (postContent.indexOf(query)>=0)
{
return postContent.substring(Math.max(0,postContent.indexOf(query)-Math.floor(maxlen/2)),postContent.indexOf(query)+Math.floor(maxlen/2));
}
else
{
return postContent.substring(0,maxlen);
}
}

String.prototype.replaceAll = function(strReplace, strWith)
{
var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var reg = new RegExp(esc, 'ig');
return this.replace(reg, strWith);
};

// switches between different search types on search page:
// "prod","group","party","user","bbs"
function changeSearchType(searchType,pouetContentUrl)
{
var urlArray={"prod":"./ajax_prods.php",
"group":"./ajax_groups.php",
"party":"./ajax_parties.php",
"user":"./ajax_users.php",
"bbs":"./ajax_bbspost.php"};

// empty the search field container
var e = document.getElementById("searchInputSpan");
var child = e.lastElementChild;
while (child)
{
e.removeChild(child);
child = e.lastElementChild;
}

// re-add the input field
e.innerHTML="<input autocomplete='off' id='pouetSearchInput' type='text' size='25' />";

if (searchType=="prod")
{
new Autocompleter(document.getElementById("pouetSearchInput"),
{
"dataUrl":urlArray[searchType],
"processRow": function(item) {
var s = item.name.escapeHTML();
if (item.groupName) s += " <small class='group'>" + item.groupName.escapeHTML() + "</small>";
return s;
},
"onClickUrl":"prod.php?which=",
"searchBox":true
}
);
}
else if (searchType=="group")
{
new Autocompleter(document.getElementById("pouetSearchInput"), {
"dataUrl":urlArray[searchType],
"processRow": function(item) {
return item.name.escapeHTML() + (item.disambiguation ? " <span class='group-disambig'>" + item.disambiguation.escapeHTML() + "</span>" : "");
},
"onClickUrl":"groups.php?which=",
"searchBox":true
});
}
else if (searchType=="party")
{
new Autocompleter(document.getElementById("pouetSearchInput"), {
"dataUrl":urlArray[searchType],
"onClickUrl":"party.php?which=",
"searchBox":true
});
}
else if (searchType=="user")
{
new Autocompleter(document.getElementById("pouetSearchInput"), {"dataUrl":urlArray[searchType],
"processRow": function(item) {
return "<img class='avatar' src='"+pouetContentUrl+"avatars/" + item.avatar.escapeHTML() + "'/> " + item.name.escapeHTML() + " <span class='glops'>" + item.glops + " glöps</span>";
},
"onClickUrl":"user.php?who=",
"searchBox":true
});
}
else if (searchType=="bbs")
{
new Autocompleter(document.getElementById("pouetSearchInput"), {"dataUrl":urlArray[searchType],
"width":500,
"processRow": function(item)
{
var topicName=item.topic;
var postContent=item.post;

if (topicName.toLowerCase().indexOf(item.searchQuery.toLowerCase())>=0)
{
topicName=topicName.replaceAll(item.searchQuery,"<span style='background-color:yellow;color:black'>"+item.searchQuery+"</span>")
}

// trim post content to a reasonable size
postContent=trimPostContent(postContent.toLowerCase(),item.searchQuery.toLowerCase(),60);

if (postContent.toLowerCase().indexOf(item.searchQuery.toLowerCase())>=0)
{
postContent = postContent.replaceAll(item.searchQuery,"<span style='background-color:yellow;color:black'>"+item.searchQuery+"</span>")
}

return "<b>"+topicName+"</b><br/>... "+postContent+" ..." ;
},
"onClickUrl":"topic.php?post=",
"searchBox":true
});
}
}

// on load scripts - keep this to minimum
document.observe("dom:loaded",function(){
Cookie.init({name: 'pouetSettings', expires: 365});
Expand Down
12 changes: 10 additions & 2 deletions search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function __construct() {
function RenderBody() {
echo "<div class='content r1'>\n";
echo "I'm looking for\n";
echo "<input type='text' name='what' size='25' value=\""._html($_GET["what"])."\"/>\n";
echo "<span id='searchInputSpan'><input id='pouetSearchInput' type='text' size='25' value=\""._html($_GET["what"])."\"/></span>\n";
echo "and this is a [\n";

$types = array("prod","group","party"/*,"board"*/,"user","bbs");
$a = array();
$selected = $_GET["type"] ? $_GET["type"] : "prod";
foreach($types as $t)
$a[] = "<input type='radio' name='type' value='".$t."' id='search".$t."' ".($t==$selected?" checked='checked'":"")." />&nbsp;<label for='search".$t."'>".$t."</label>\n";
$a[] = "<input onClick='changeSearchType(this.value,\"".POUET_CONTENT_URL."\")' type='radio' name='type' value='".$t."' id='search".$t."' ".($t==$selected?" checked='checked'":"")." />&nbsp;<label for='search".$t."'>".$t."</label>\n";

echo implode(" |\n",$a);

Expand Down Expand Up @@ -592,3 +592,11 @@ function GetForwardURL()
require("include_pouet/menu.inc.php");
require_once("include_pouet/footer.php");
?>

<script>
document.observe("dom:loaded",function()
{
changeSearchType("prod","<?php POUET_CONTENT_URL ?>");
}
);
</script>