sottwell
Documentation Team

Posts: 8,170
|
 |
« on: Oct 30, 2005, 07:28 AM » |
|
I have a very crude implementation of highlighting search terms on the page. It took a quick hack of the FlexSearchForm: added the &searched='.$searchString.' to line 343 $resultPageLinks .= '<a href="[~' . $modx->documentObject['id'] . '~]&searched='.$searchString.'&FSF_offset=' . $nrp . '&FSF_search=' . urlencode($searchString) . '">' . $resultPageLinkNumber . '</a>';
and again to line 361 $SearchForm.='<a class="FSF_resultLink" href="[~'.$SearchFormsrc['id'].'~]&searched='.$searchString.'" title="' . $SearchFormsrc['pagetitle'] . '">' . $SearchFormsrc['pagetitle'] . "</a>".$newline;
and this plugin (tied to OnWebPagePrerender) if(isset($_GET['searched'])) {
$searched = $_GET['searched']; $content = $modx->documentOutput;
$new = str_replace($searched, '<span class="searchterm">'.$searched.'</span>', $content);
$modx->documentOutput = $new; } Style the .searchterm span in the CSS. As I said, it's very crude and only does one term and one color. But it's a start!
|
|
|
|
|
Logged
|
|
|
|
rthrash
Foundation

Posts: 9,577
|
 |
« Reply #1 on: Oct 30, 2005, 07:58 AM » |
|
Susan, very super-cool! Seems like a candidate for a core distribution addition to me. 
|
|
|
|
|
Logged
|
MODx is a framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Community participation and questions are encouraged, especially when you help us help you, read the wiki, and review snippet parameters – even if you have to look at the source. Searching the forums helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #2 on: Oct 30, 2005, 08:27 AM » |
|
Needs work, but I'm not familiar enough yet with exactly how the FlexSearchForm works to deal with more than one search term. I need to drop in a few debugging echo statements and see what exactly it's doing. Perhaps I can "explode" it and give each word a different color, sort of like Google does. But for now, I thought it was a pretty good use of a plugin.
|
|
|
|
|
Logged
|
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #3 on: Oct 30, 2005, 08:31 AM » |
|
Yeah, definitely need to be able to "explode" the string and deal with the individual words. I hate the thought of parsing through the whole document for each word, but right now that's the only way I can think of to do it. Change the class name to add the number for each word (found1, found2, etc) and still be able to style for it in the CSS.
|
|
|
|
|
Logged
|
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #4 on: Oct 30, 2005, 08:42 AM » |
|
This does the trick! Just put as many styles as you ever expect to have search words into the CSS file. if(isset($_GET['searched'])) {
$searched = $_GET['searched']; $content = $modx->documentOutput;
$searchArray = explode(' ', $searched); $i = 0; foreach($searchArray as $term) { $i++; $content = str_replace($term, '<span class="searchterm'.$i.'">'.$term.'</span>', $content); } $modx->documentOutput = $content; }
|
|
|
|
|
Logged
|
|
|
|
rthrash
Foundation

Posts: 9,577
|
 |
« Reply #5 on: Oct 30, 2005, 09:04 AM » |
|
what about just creating a few styles then having it alternate through them... maybe 4 or 5 by default. Then, we could pass in an array of colors as a parameter: &hlcolors=`#ff0, #f0f, #0ff, #ff5, #f5f, #5ff` which would then alternated through for the words via a span stylng: <span style="background-color:#ff0">first</span> blah blah blah <span style="background-color:#f0f">second</span> and so on... It'd be kinda cool to keep it all in the snippet call it seems for some reason.
|
|
|
|
|
Logged
|
MODx is a framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Community participation and questions are encouraged, especially when you help us help you, read the wiki, and review snippet parameters – even if you have to look at the source. Searching the forums helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #6 on: Oct 30, 2005, 09:33 AM » |
|
And then, put a <div> with a message at the very top of the page, like Google's top frame...with a button to clear it that's just a "bare" link back to the same page only without the search string in the URL. That would really be cool!
|
|
|
|
|
Logged
|
|
|
|
rthrash
Foundation

Posts: 9,577
|
 |
« Reply #7 on: Oct 30, 2005, 09:36 AM » |
|
Nice Susan.  Better than before (that is when your magic code appears on these here pages... lol)!
|
|
|
|
|
Logged
|
MODx is a framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Community participation and questions are encouraged, especially when you help us help you, read the wiki, and review snippet parameters – even if you have to look at the source. Searching the forums helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #8 on: Oct 30, 2005, 09:39 AM » |
|
Well, this has a serious problem. No matter where in the parsed document the searchterm appears, the <span...etc gets put in...even if it's actually part of a URL or in the head! Bad. I shall have to think on this. Even if I manage to just get the content for the document, it will stil have the problem of urls and image tags and such in the document content. Hm. I think it's actually going to take a very complex set of regular expressons to do this.
|
|
|
|
« Last Edit: Oct 30, 2005, 01:44 PM by sottwell »
|
Logged
|
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #9 on: Oct 30, 2005, 05:09 PM » |
|
Well. Got that figured out. It was rather frustrating, I spent hours combing Google and reading forum posts and trying various things...and it turns out that the PHP manual has an example of using eregi_replace to hightlight search terms. Oh, well...I must say, I've learned a lot more about regular expressions. So it basically works (I still need to filter out between A tags and Hx tags but that's really just to make it look prettier) if(isset($_GET['searched'])) {
$searched = $_GET['searched']; $output = $modx->documentOutput; // get the parsed document
$body= explode("<body>", $output); // break out the head
$searchArray = explode(' ', $searched); // break apart the search terms
$i = 0; // for individual class names
foreach($searchArray as $term) { $i++; $pattern = '(>[^<]*)('. quotemeta($term) .')'; $replacement = '\\1<span class="searchterm'.$i.'">\\2</span>'; $body[1] = eregi_replace($pattern, $replacement, $body[1]); }
$output = implode("<body>", $body);
$modx->documentOutput = $output;
}
|
|
|
|
|
Logged
|
|
|
|
xwisdom
Foundation

Posts: 1,732
|
 |
« Reply #10 on: Oct 30, 2005, 11:59 PM » |
|
Hi Susan,
Very cool additions indeed.
PS. Is it a plugin or a snippet?
|
|
|
|
|
Logged
|
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #11 on: Oct 31, 2005, 12:29 AM » |
|
Plugin. Hooked to the OnWebPagePrerender event. Sorry, thought I said that in the first place!
|
|
|
|
|
Logged
|
|
|
|
xwisdom
Foundation

Posts: 1,732
|
 |
« Reply #12 on: Oct 31, 2005, 12:34 AM » |
|
Plugin. Hooked to the OnWebPagePrerender event. Sorry, thought I said that in the first place!
Cool. Didn't see the Plugin part only saw the the changes made to "FlexSearchForm"
|
|
|
|
|
Logged
|
|
|
|
sottwell
Documentation Team

Posts: 8,170
|
 |
« Reply #13 on: Oct 31, 2005, 12:35 AM » |
|
I do have a related question for you core gurus. Is there a way to tie a plugin to a TV so that it will only run if the TV is present in the template?
One thing, it would be nice to not have it run at all if you don't want it, so you just don't put the TV in the template if you don't want it.
The other thing, that would make it much easier to allow the user to modify the colors and styles used to mark the search term on a document-by-document basis. For example, if I code it to have a search term with red text, and you have a template that makes all links red, it would be a Bad Thing. On the other hand, leaving it not styled and the user has to style it in his CSS is a bit awkward.
|
|
|
|
|
Logged
|
|
|
|
xwisdom
Foundation

Posts: 1,732
|
 |
« Reply #14 on: Oct 31, 2005, 12:48 AM » |
|
Hi Susan,
It's not with 0.9.0 to bind a Plugin to a TV but you can create a plugin that will look inside the template/document for a special TV. If found it can be made to execute special code.
|
|
|
|
|
Logged
|
|
|
|
|