| 1 | <cfsetting enablecfoutputonly=true> |
|---|
| 2 | <cfprocessingdirective pageencoding="utf-8"> |
|---|
| 3 | <!--- |
|---|
| 4 | Name : search.cfm |
|---|
| 5 | Author : Raymond Camden |
|---|
| 6 | Created : February 9, 2007 |
|---|
| 7 | Last Updated : November 17, 2007 |
|---|
| 8 | History : Small changes to search display (rkc 4/19/07) |
|---|
| 9 | : Fix by Dan S to block unreleased entries (rkc 11/17/07) |
|---|
| 10 | Purpose : Search Logic |
|---|
| 11 | ---> |
|---|
| 12 | |
|---|
| 13 | <!--- allow for /xxx shortcut ---> |
|---|
| 14 | <cfif cgi.path_info is not "/search.cfm"> |
|---|
| 15 | <cfset searchAlias = listLast(cgi.path_info, "/")> |
|---|
| 16 | <cfelse> |
|---|
| 17 | <cfset searchAlias = ""> |
|---|
| 18 | </cfif> |
|---|
| 19 | <cfparam name="form.search" default="#searchAlias#"> |
|---|
| 20 | <cfparam name="form.category" default=""> |
|---|
| 21 | |
|---|
| 22 | <cfif not len(trim(form.search))> |
|---|
| 23 | <cflocation url="#application.rooturl#/index.cfm" addToken="false"> |
|---|
| 24 | </cfif> |
|---|
| 25 | |
|---|
| 26 | <cfset form.search = left(htmlEditFormat(trim(form.search)),255)> |
|---|
| 27 | |
|---|
| 28 | <cfset cats = application.blog.getCategories()> |
|---|
| 29 | |
|---|
| 30 | <cfset params = structNew()> |
|---|
| 31 | <cfset params.searchTerms = form.search> |
|---|
| 32 | <cfif form.category is not ""> |
|---|
| 33 | <cfset params.byCat = form.category> |
|---|
| 34 | </cfif> |
|---|
| 35 | <cfset params.maxEntries = 100> |
|---|
| 36 | <!---// dgs: only get released items //---> |
|---|
| 37 | <cfset params.releasedonly = true /> |
|---|
| 38 | <cfset results = application.blog.getEntries(params)> |
|---|
| 39 | |
|---|
| 40 | <cfset title = rb("search")> |
|---|
| 41 | |
|---|
| 42 | <cfmodule template="tags/layout.cfm" title="#title#"> |
|---|
| 43 | |
|---|
| 44 | <cfoutput> |
|---|
| 45 | <div class="date"><b>#title#</b></div> |
|---|
| 46 | <div class="body"> |
|---|
| 47 | <form action="#application.rooturl#/search.cfm" method="post"> |
|---|
| 48 | <p> |
|---|
| 49 | You searched for <input type="text" name="search" value="#form.search#"> in |
|---|
| 50 | <select name="category"> |
|---|
| 51 | <option value="" <cfif form.category is "">selected</cfif>>all categories</option> |
|---|
| 52 | <cfloop query="cats"> |
|---|
| 53 | <option value="#categoryid#" <cfif form.category is categoryid>selected</cfif>>#categoryname#</option> |
|---|
| 54 | </cfloop> |
|---|
| 55 | </select> |
|---|
| 56 | There |
|---|
| 57 | <cfif results.totalEntries is 1>was one result<cfelse>were #results.totalEntries# results</a></cfif>. |
|---|
| 58 | <input type="submit" value="Search Again"> |
|---|
| 59 | </p> |
|---|
| 60 | </form> |
|---|
| 61 | <style> |
|---|
| 62 | .highlight { background-color: yellow; } |
|---|
| 63 | </style> |
|---|
| 64 | <cfif results.entries.recordCount> |
|---|
| 65 | <cfloop query="results.entries"> |
|---|
| 66 | <!--- remove html from result. ---> |
|---|
| 67 | <cfset newbody = rereplace(body, "<.*?>", "", "all")> |
|---|
| 68 | <!--- highlight search terms ---> |
|---|
| 69 | <!--- before we "highlight" our matches in the body, we need to find the first match. |
|---|
| 70 | We will create an except that begins 250 before and ends 250 after. This will give us slightly |
|---|
| 71 | different sized excerpts, but between you, me, and the door, I think thats ok. It is also possible |
|---|
| 72 | the match isn't in the entry but just the title. ---> |
|---|
| 73 | <cfset match = findNoCase(form.search, newbody)> |
|---|
| 74 | <cfif match lte 250> |
|---|
| 75 | <cfset match = 1> |
|---|
| 76 | </cfif> |
|---|
| 77 | <cfset end = match + len(form.search) + 500> |
|---|
| 78 | |
|---|
| 79 | <cfif len(newbody) gt 500> |
|---|
| 80 | <cfif match gt 1> |
|---|
| 81 | <cfset excerpt = "..." & mid(newbody, match-250, end-match)> |
|---|
| 82 | <cfelse> |
|---|
| 83 | <cfset excerpt = left(newbody,end)> |
|---|
| 84 | </cfif> |
|---|
| 85 | <cfif len(newbody) gt end> |
|---|
| 86 | <cfset excerpt = excerpt & "..."> |
|---|
| 87 | </cfif> |
|---|
| 88 | <cfelse> |
|---|
| 89 | <cfset excerpt = newbody> |
|---|
| 90 | </cfif> |
|---|
| 91 | |
|---|
| 92 | <!--- |
|---|
| 93 | We switched to regular expressions to highlight our search terms. However, it is possible for someone to search |
|---|
| 94 | for a string that isn't a valid regex. So if we fail, we just don't bother highlighting. |
|---|
| 95 | ---> |
|---|
| 96 | <cftry> |
|---|
| 97 | <cfset excerpt = reReplaceNoCase(excerpt, "(#form.search#)", "<span class='highlight'>\1</span>","all")> |
|---|
| 98 | <cfset newtitle = reReplaceNoCase(title, "(#form.search#)", "<span class='highlight'>\1</span>","all")> |
|---|
| 99 | <cfcatch> |
|---|
| 100 | <!--- only need to set newtitle, excerpt already exists. ---> |
|---|
| 101 | <cfset newtitle = title> |
|---|
| 102 | </cfcatch> |
|---|
| 103 | </cftry> |
|---|
| 104 | <p> |
|---|
| 105 | <b><a href="#application.blog.makeLink(id)#">#newtitle#</a></b> (#application.localeUtils.dateLocaleFormat(posted)# #application.localeUtils.timeLocaleFormat(posted)#)<br /> |
|---|
| 106 | <br /> |
|---|
| 107 | #excerpt# |
|---|
| 108 | </p> |
|---|
| 109 | </cfloop> |
|---|
| 110 | </cfif> |
|---|
| 111 | |
|---|
| 112 | </div> |
|---|
| 113 | </cfoutput> |
|---|
| 114 | |
|---|
| 115 | </cfmodule> |
|---|