antville/code/Site/search.hac
2002-05-15 23:24:11 +00:00

88 lines
2.8 KiB
Text

var deny = this.isNotPublic(user);
if (deny) {
res.message = deny;
user.cache.referrer = this.href();
res.redirect(this.members.href("login"));
}
res.skin = "weblog.page";
res.data.action = this.href(req.action);
res.title = "Search " + this.title;
res.body = this.renderSkinAsString("searchform");
if (req.data.q) {
var query = req.data.q;
// reuse search result container if it already exists
var folder = user.cache["search-"+query];
if (!folder) {
folder = new weblog();
user.cache["search-"+query] = folder;
}
// break up search string
var qarr = query.split(" ");
// construct query
var where = "where WEBLOG_ID = "+this._id+" and ISONLINE > 0 and ";
for (var i in qarr) {
where += "(TITLE like '%"+qarr[i]+"%' or TEXT like '%"+qarr[i]+"%') "
if (i < qarr.length-1)
where += "and ";
}
where += "order by CREATETIME desc";
folder.allstories.subnodeRelation = where;
var found = folder.allstories.size();
if (found == 0)
res.body += "<i>Nothing found for '"+query+"'.</i>";
else {
var start = 0;
var end = found;
if (found == 1)
res.body += "<i>Found 1 item for '"+query+"'.</i>";
else if (found <= 10)
res.body += "<i>Found "+found+" items for '"+query+"'.<i>";
else {
if (req.data.start)
start = Math.min (found-1, parseInt (req.data.start));
if (isNaN (start))
start = 0;
end = Math.min (found, start+10);
res.body += "<i>Found "+found+" items for '"+query+"'. ";
res.body += "Displaying items "+(start+1)+" to "+end+".</i>";
}
// note: I'm doing this without a "searchbody" skin, since
// I think there's not much need to customize the body of
// search results, esp. since its parts are fully customizable.
// of course, I may be wrong about that.
// render prev links, if necessary
if (start > 0) {
var sp = new Object();
sp.url = this.href() + "search?q="+escape(query)+"&start="+Math.max(0,start-10);
sp.text = "previous results";
res.body += "<br><br>"+renderSkinAsString("prevpagelink",sp);
}
// render result
res.body += '<table border="0">';
for (var i=start; i<end; i++)
res.body += folder.allstories.get (i).renderSkinAsString("searchview");
res.body += '</table>';
// render next links, if necessary
if (end < found) {
var sp = new Object();
sp.url = this.href() + "search?q="+escape(query)+"&start="+Math.min(found-1,start+10);
sp.text = "next results";
res.body += renderSkinAsString("nextpagelink",sp);
}
}
}