move code up one directory
[atutor.git] / mods / _standard / google_search / gsearch.php
1 <?php\r
2 /****************************************************************/\r
3 /* ATutor                                                                                                               */\r
4 /****************************************************************/\r
5 /* Copyright (c) 2002-2008                                      */\r
6 /* Written by Harris Wong                                                                       */\r
7 /* Inclusive Design Institute                                   */\r
8 /* http://atutor.ca                                                                                             */\r
9 /*                                                              */\r
10 /* This program is free software. You can redistribute it and/or*/\r
11 /* modify it under the terms of the GNU General Public License  */\r
12 /* as published by the Free Software Foundation.                                */\r
13 /****************************************************************/\r
14 // $Id$\r
15 ?>\r
16 <script src="http://www.google.com/uds/api?file=uds.js<?php echo "&amp;"; ?>hl=<?php echo $_SESSION['lang']; ?><?php echo "&amp;"; ?>v=1.0<?php echo "&amp;"; ?>key=<?php echo $search_key; ?>" type="text/javascript"></script>\r
17 <script type="text/javascript">\r
18         /* This can only be run after importing the google uds.  \r
19          * Ref:         google ajax api.\r
20          * @url         http://code.google.com/apis/ajaxsearch/documentation/reference.html\r
21          */\r
22         //<![CDATA[\r
23         var gsearch;  \r
24         function OnLoad() {\r
25           gsearch = new RawSearchControl();             //instantiate google search object.\r
26           /* The following is for the side menu search.  */\r
27           var q = "<?php echo $side_menu_q; ?>";\r
28           if (q != ""){\r
29                   gsearch.instantSearch(q);\r
30           }\r
31         }\r
32 \r
33         /**\r
34          * The RawSearchControl demonstrates how to use Searcher Objects\r
35          * outside of the standard GSearchControl. This includes calling\r
36          * searcher .execute() methods, reacting to search completions,\r
37          * and if you had previously disabled html generation, how to generate\r
38          * an html representation of the result.\r
39          */     \r
40         function RawSearchControl() {\r
41           // latch on to key portions of the document\r
42           this.searcherform = document.getElementById("searcher");\r
43           this.results = document.getElementById("results");\r
44           this.searchform = document.getElementById("searchform");\r
45           this.selector = "";\r
46           this.query = "";\r
47           this.currentSearchType = "";\r
48 \r
49           // create map of searchers as well as note the active searcher\r
50           this.searchers = new Array();\r
51           // create and wire up an instance of GwebSearch and one of\r
52           // GlocalSearch. Note that we register to handle search completion notifications\r
53           // when searches complete, they are called in the context of this instance\r
54           // of RawSearchControl and they are passed the searcher that just completed\r
55           for (var i=0; i<7; i++){\r
56                         var searcher = new GwebSearch();\r
57                         switch(i){\r
58                                 case 0: \r
59                                         searcher = new GwebSearch();\r
60                                         break;\r
61                                 case 1:\r
62                                         searcher = new GlocalSearch();\r
63                                         searcher.setCenterPoint("98074");\r
64                                         break;\r
65                                 case 2:\r
66                                         searcher = new GimageSearch();\r
67                                         break;\r
68                                 case 3:\r
69                                         searcher = new GvideoSearch();\r
70                                         break;\r
71                                 case 4:\r
72                                         searcher = new GnewsSearch();\r
73                                         break;\r
74                                 case 5:\r
75                                         searcher = new GblogSearch();\r
76                                         break;\r
77                                 case 6:\r
78                                         searcher = new GbookSearch();\r
79                                         break;\r
80                         }\r
81                   searcher.setNoHtmlGeneration();\r
82                   searcher.setSearchCompleteCallback(this,\r
83                                                                                          RawSearchControl.prototype.searchComplete,\r
84                                                                                          [searcher]\r
85                                                                                          );\r
86                   searcher.setResultSetSize(GSearch.LARGE_RESULTSET);\r
87                   searcher.setLinkTarget("temp_new_search");\r
88                   this.searchers[i] = searcher;\r
89           }\r
90 \r
91           // now, create a search form and wire up a submit and clear handler\r
92           this.searchForm = new GSearchForm(false, this.searchform);\r
93           this.searchForm.setOnSubmitCallback(this,\r
94                                                                                   RawSearchControl.prototype.onSubmit);\r
95         }\r
96 \r
97         /**\r
98          * figure out which searcher is active by looking at the radio\r
99          * button array\r
100          */\r
101         RawSearchControl.prototype.computeActiveSearcher = function() {\r
102                 if (this.currentSearchType=="" ){\r
103                           this.activeSearcher = 0;\r
104                 } else {\r
105                           this.activeSearcher = this.currentSearchType;\r
106                 }\r
107         }\r
108 \r
109         /**\r
110          * Inject the query into the form, and triggers a instant search\r
111          * @param       The query\r
112          */\r
113          RawSearchControl.prototype.instantSearch = function(q){\r
114                  this.searchForm.input.value = q;\r
115                  this.onSubmit(this.searchForm);\r
116          }\r
117 \r
118         /**\r
119           * figure out which link is being selected, then change that style.\r
120           * reset all other styles.\r
121           * @param      searcherTypeIndex       is the integer that maps onto the searcherType string\r
122           */\r
123         RawSearchControl.prototype.activate = function (searcherTypeIndex){\r
124                 for (var i=0; i<this.searchers.length; i++){\r
125                         var gsearcher = this.searchers[i];                                              \r
126                         searcherType = this.getSearcherTypeString(i)[0];        \r
127                         this.selector = document.getElementById(searcherType + "Selector");\r
128                         if (i==searcherTypeIndex){\r
129                                 //Run this query, activate the menu bar.\r
130                                 this.currentSearchType = searcherTypeIndex;\r
131                                 cssSetClass(this.selector, "active");\r
132                                 this.computeActiveSearcher();\r
133                                 var q = this.searchForm.input.value;\r
134                                 if (q) {\r
135                                         gsearcher.execute(q);\r
136                                 }\r
137                         } else {\r
138                                 cssSetClass(this.selector, "");\r
139                         }\r
140                 }\r
141         }\r
142 \r
143         /**\r
144          * This functiion will retrieve the string of the searcher type by the given index\r
145          */\r
146          RawSearchControl.prototype.getSearcherTypeString = function(searcherTypeIndex){\r
147                         var s;\r
148                         switch(searcherTypeIndex){\r
149                                 case 1:\r
150                                         s = new Array("local", "http://www.google.ca/maps?");\r
151                                         break;\r
152                                 case 2:\r
153                                         s = new Array("image", "http://www.google.ca/images?");\r
154                                         break;\r
155                                 case 3:\r
156                                         s = new Array("video", "http://video.google.ca/videosearch?");\r
157                                         break;\r
158                                 case 4:\r
159                                         s = new Array("news", "http://www.google.ca/news?");\r
160                                         break;\r
161                                 case 5:\r
162                                         s = new Array("blog", "http://www.google.ca/blogsearch?");\r
163                                         break;\r
164                                 case 6:\r
165                                         s = new Array("book", "http://www.google.ca/books?");\r
166                                         break;\r
167                                 case 0:\r
168                                 default:\r
169                                         s = new Array("web", "http://www.google.ca/search?");\r
170                                         break;\r
171                         }\r
172                         return s;\r
173          }\r
174 \r
175         /**\r
176          * onSubmit - called when the search form is "submitted" meaning that\r
177          * someone pressed the search button or hit enter. The form is passed\r
178          * as an argument\r
179          */\r
180         RawSearchControl.prototype.onSubmit = function(form) {\r
181           this.computeActiveSearcher();\r
182           if (form.input.value) {\r
183                 // if there is an expression in the form, call the active searcher's\r
184                 // .execute method\r
185                 this.query = form.input.value;\r
186                 this.searchers[this.activeSearcher].execute(form.input.value);\r
187           }\r
188           // always indicate that we handled the submit event\r
189           return false;\r
190         }\r
191 \r
192         /**\r
193          * onClear - called when someone clicks on the clear button (the little x)\r
194          */\r
195         RawSearchControl.prototype.onClear = function(form) {\r
196           this.clearResults();\r
197         }\r
198 \r
199         /**\r
200          * searchComplete - called when a search completed. Note the searcher\r
201          * that is completing is passes as an arg because thats what we arranged\r
202          * when we called setSearchCompleteCallback\r
203          */\r
204         RawSearchControl.prototype.searchComplete = function(searcher) {\r
205           // always clear old from the page\r
206           this.clearResults();\r
207 \r
208           // if the searcher has results then process them\r
209           if (searcher.results && searcher.results.length > 0) {\r
210                 // now manually generate the html that we disabled\r
211                 // initially and display it\r
212                 var div = createDiv("", "");\r
213                 this.results.appendChild(div);\r
214                 for (var i=0; i<searcher.results.length; i++) {\r
215                   var result = searcher.results[i];\r
216                   searcher.createResultHtml(result);\r
217                   if (result.html) {\r
218                         div = result.html.cloneNode(true);\r
219                   } else {\r
220                         div = createDiv("** failure to create html **");\r
221                   }\r
222                   this.results.appendChild(div);\r
223                 }\r
224                 div = createDiv("");\r
225                 a_elem = createAnchor("More results >>", this.generateMoreResultsURL());\r
226                 div.appendChild(a_elem);\r
227                 this.results.appendChild(div);\r
228           } else {\r
229                 var div = createDiv("No results for: " + this.searchForm.input.value, "");\r
230                 this.results.appendChild(div);\r
231           }\r
232         }\r
233 \r
234         /**\r
235          * This function returns the appropriate link for the "More results" at the bottom of a search.\r
236          * The "More results" should correspond to the category the search was performed in.(ie. maps, video, images...)\r
237          * @return      the URL described above.\r
238          */\r
239         RawSearchControl.prototype.generateMoreResultsURL = function(){\r
240                 var s = "";\r
241                 var link = this.getSearcherTypeString(this.activeSearcher)[1];\r
242                 return (link + "hl=<?php echo $_SESSION['lang']?>&q=" + this.searchForm.input.value);\r
243         }\r
244 \r
245         /**\r
246          * clearResults - clear out any old search results\r
247          */\r
248         RawSearchControl.prototype.clearResults = function() {\r
249           removeChildren(this.results);\r
250         }\r
251 \r
252         /**\r
253          * Static DOM Helper Functions\r
254          */\r
255         function removeChildren(parent) {\r
256           while (parent.firstChild) {\r
257                 parent.removeChild(parent.firstChild);\r
258           }\r
259         }\r
260         function createDiv(opt_text, opt_className) {\r
261           var el = document.createElement("div");\r
262           if (opt_text) {\r
263                 el.innerHTML = opt_text;\r
264           }\r
265           if (opt_className) { el.className = opt_className; }\r
266           return el;\r
267         }\r
268         function createAnchor(a_text, a_href, a_className){\r
269           var el = document.createElement("a");\r
270           if(a_text){\r
271                   el.innerHTML = a_text;\r
272                   el.href = a_href;\r
273                   el.target = "temp_google_search";\r
274           }\r
275           if (a_className){ a.className = a_className;}\r
276           return el;\r
277         }\r
278 \r
279         /**\r
280           * CSS helper\r
281           */\r
282         function cssSetClass(el, className) {\r
283                 el.className = className;\r
284         }\r
285 \r
286         // register to be called at OnLoad when the page loads\r
287         GSearch.setOnLoadCallback(OnLoad);\r
288         //]]>\r
289 </script>\r
290 \r
291 <form id="searcher" method="get" action="">\r
292   <table border='0'>\r
293         <tr><td colspan="2">\r
294         <div class="selector">\r
295         <ul id="navlist">\r
296                 <li><a id="webSelector" class="active" href="javascript:gsearch.activate(0);"><?php echo _AT('google_search'); ?></a></li>\r
297                 <li><a id="localSelector" href="javascript:gsearch.activate(1);"><?php echo _AT('google_search_local'); ?></a></li>\r
298                 <li><a id="imageSelector" href="javascript:gsearch.activate(2);"><?php echo _AT('google_search_images'); ?></a></li>\r
299                 <li><a id="videoSelector" href="javascript:gsearch.activate(3);"><?php echo _AT('google_search_videos'); ?></a></li>\r
300                 <li><a id="newsSelector" href="javascript:gsearch.activate(4);"><?php echo _AT('google_search_news'); ?></a></li>\r
301                 <li><a id="blogSelector" href="javascript:gsearch.activate(5);"><?php echo _AT('blogs'); ?></a></li>\r
302                 <li><a id="bookSelector" href="javascript:gsearch.activate(6);"><?php echo _AT('google_search_books'); ?></a></li>\r
303         </ul></div></td>\r
304         </tr>\r
305         <tr>\r
306         <td class="search-form">\r
307           <div id="searchform">Loading</div>\r
308         </td>\r
309         <!-- For clearing the results\r
310         <td>\r
311           <input name="clearResult" value="clear" type="button" class="button" onclick="gsearch.clearResults();"/>\r
312         </td>\r
313         -->\r
314         </tr>\r
315   </table>\r
316 </form>\r
317 <div id="results"></div>\r