Dec 04, 2008, 11:55 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Search via SMF or Google: modx forums all of modxcms.com web
  MODxCMS.com   Forums   Help Login Register  
Pages: [1] 2   Go Down
  Print  
Author Topic: [Plugin] Highlight search terms  (Read 11600 times)
0 Members and 1 Guest are viewing this topic.
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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
Code:
            $resultPageLinks .= '<a href="[~' . $modx->documentObject['id'] . '~]&searched='.$searchString.'&FSF_offset=' . $nrp . '&FSF_search=' . urlencode($searchString) . '">' . $resultPageLinkNumber . '</a>';

and again to line 361
Code:
        $SearchForm.='<a class="FSF_resultLink" href="[~'.$SearchFormsrc['id'].'~]&searched='.$searchString.'" title="' . $SearchFormsrc['pagetitle'] . '">' . $SearchFormsrc['pagetitle'] . "</a>".$newline;

and this plugin (tied to OnWebPagePrerender)

Code:
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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
rthrash
Foundation
*
Posts: 9,577



WWW
« Reply #1 on: Oct 30, 2005, 07:58 AM »

Susan, very super-cool! Seems like a candidate for a core distribution addition to me. Smiley
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



WWW
« 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.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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.

Code:
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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
rthrash
Foundation
*
Posts: 9,577



WWW
« 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:
Code:
&hlcolors=`#ff0, #f0f, #0ff, #ff5, #f5f, #5ff`
which would then alternated through for the words via a span stylng:
Code:
<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



WWW
« 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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
rthrash
Foundation
*
Posts: 9,577



WWW
« Reply #7 on: Oct 30, 2005, 09:36 AM »

Nice Susan. Cheesy

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



WWW
« 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.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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)

Code:
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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
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

xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
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

xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.
sottwell
Documentation Team
*
Posts: 8,170



WWW
« 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

sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
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

xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Copyright © 2005-2008 MODxCMS, All rights reserved. Contact Us
Styles by ziworks.com

Powered by SMF 1.1.4 | SMF © 2005, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!