From ad2b30f52b2daa785180f5c78ddd58cee8a4cdfd Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Thu, 6 Apr 2017 14:02:20 +0200 Subject: [PATCH] Changed the way search modules get integrated with the theme via a new region "search". Improved current module for google search 2.0. --- .../config/google_search_20.json | 5 -- .../config/google_search_20.json-dist | 2 +- .../templates/block_gcse_search_form.tpl | 6 +++ ...arch.tpl => block_gcse_search_results.tpl} | 0 examples/demo/site/themes/bootstrap/page.tpl | 9 ++-- .../demo/site/themes/bootstrap/search.tpl | 6 +++ .../demo/site/themes/bootstrap/theme.info | 1 + modules/google_search_20/Readme.md | 14 ++++-- .../site/config/google_search_20.json-dist | 5 ++ .../site/templates/block_gcse_search_form.tpl | 6 +++ ...arch.tpl => block_gcse_search_results.tpl} | 2 +- .../src/google_custom_search_module_20.e | 50 ++++++++++++------- 12 files changed, 72 insertions(+), 34 deletions(-) delete mode 100644 examples/demo/site/modules/google_search_20/config/google_search_20.json rename modules/google_search_20/site/config/google_search_20.json => examples/demo/site/modules/google_search_20/config/google_search_20.json-dist (51%) create mode 100644 examples/demo/site/modules/google_search_20/templates/block_gcse_search_form.tpl rename examples/demo/site/modules/google_search_20/templates/{block_search.tpl => block_gcse_search_results.tpl} (100%) create mode 100644 examples/demo/site/themes/bootstrap/search.tpl create mode 100644 modules/google_search_20/site/config/google_search_20.json-dist create mode 100644 modules/google_search_20/site/templates/block_gcse_search_form.tpl rename modules/google_search_20/site/templates/{block_search.tpl => block_gcse_search_results.tpl} (58%) diff --git a/examples/demo/site/modules/google_search_20/config/google_search_20.json b/examples/demo/site/modules/google_search_20/config/google_search_20.json deleted file mode 100644 index 9cf9e80..0000000 --- a/examples/demo/site/modules/google_search_20/config/google_search_20.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "gcse": { - "cx":"" - } -} diff --git a/modules/google_search_20/site/config/google_search_20.json b/examples/demo/site/modules/google_search_20/config/google_search_20.json-dist similarity index 51% rename from modules/google_search_20/site/config/google_search_20.json rename to examples/demo/site/modules/google_search_20/config/google_search_20.json-dist index 7403693..ea94f8a 100644 --- a/modules/google_search_20/site/config/google_search_20.json +++ b/examples/demo/site/modules/google_search_20/config/google_search_20.json-dist @@ -1,5 +1,5 @@ { "gcse": { - "cx":"" + "search_engine_id":"" } } diff --git a/examples/demo/site/modules/google_search_20/templates/block_gcse_search_form.tpl b/examples/demo/site/modules/google_search_20/templates/block_gcse_search_form.tpl new file mode 100644 index 0000000..4542f09 --- /dev/null +++ b/examples/demo/site/modules/google_search_20/templates/block_gcse_search_form.tpl @@ -0,0 +1,6 @@ +
+
+ + +
+
diff --git a/examples/demo/site/modules/google_search_20/templates/block_search.tpl b/examples/demo/site/modules/google_search_20/templates/block_gcse_search_results.tpl similarity index 100% rename from examples/demo/site/modules/google_search_20/templates/block_search.tpl rename to examples/demo/site/modules/google_search_20/templates/block_gcse_search_results.tpl diff --git a/examples/demo/site/themes/bootstrap/page.tpl b/examples/demo/site/themes/bootstrap/page.tpl index 785cf06..e31018d 100644 --- a/examples/demo/site/themes/bootstrap/page.tpl +++ b/examples/demo/site/themes/bootstrap/page.tpl @@ -37,17 +37,14 @@ {$page.primary_nav/} {/if} + {if isset="$page.regions.search"}
-
-
- - -
-
+ {$page.regions.search/}
+ {/if}
diff --git a/examples/demo/site/themes/bootstrap/search.tpl b/examples/demo/site/themes/bootstrap/search.tpl new file mode 100644 index 0000000..1cedbb4 --- /dev/null +++ b/examples/demo/site/themes/bootstrap/search.tpl @@ -0,0 +1,6 @@ +
+
+ + +
+
diff --git a/examples/demo/site/themes/bootstrap/theme.info b/examples/demo/site/themes/bootstrap/theme.info index 6c96570..9ba80a7 100644 --- a/examples/demo/site/themes/bootstrap/theme.info +++ b/examples/demo/site/themes/bootstrap/theme.info @@ -4,6 +4,7 @@ author=jvelilla version=0.1 regions[page_top] = Top regions[header] = Header +regions[search] = Search regions[content] = Content regions[highlighted] = Highlighted regions[help] = Help diff --git a/modules/google_search_20/Readme.md b/modules/google_search_20/Readme.md index 3aaf7a9..71df20c 100644 --- a/modules/google_search_20/Readme.md +++ b/modules/google_search_20/Readme.md @@ -1,5 +1,13 @@ Google Custom Search Module 2.0. -https://developers.google.com/custom-search/docs/element -The current solution is based on -Results only +# Based on Google Custom Search Engine (CSE) +- https://developers.google.com/custom-search/docs/element +- Search form provides a parameter `q` for the search query. +- Results are displayed in associated page /gcse2 (using ) + +# Settings +- edit the `site/modules/google_search_20/config/google_search_20.json` file, and set the value `gcse.search_engine_id`. + +# How to get a Custom Search engine ID? +- See https://support.google.com/customsearch/answer/2649143?hl=en +- https://cse.google.com/all diff --git a/modules/google_search_20/site/config/google_search_20.json-dist b/modules/google_search_20/site/config/google_search_20.json-dist new file mode 100644 index 0000000..ea94f8a --- /dev/null +++ b/modules/google_search_20/site/config/google_search_20.json-dist @@ -0,0 +1,5 @@ +{ + "gcse": { + "search_engine_id":"" + } +} diff --git a/modules/google_search_20/site/templates/block_gcse_search_form.tpl b/modules/google_search_20/site/templates/block_gcse_search_form.tpl new file mode 100644 index 0000000..4542f09 --- /dev/null +++ b/modules/google_search_20/site/templates/block_gcse_search_form.tpl @@ -0,0 +1,6 @@ +
+
+ + +
+
diff --git a/modules/google_search_20/site/templates/block_search.tpl b/modules/google_search_20/site/templates/block_gcse_search_results.tpl similarity index 58% rename from modules/google_search_20/site/templates/block_search.tpl rename to modules/google_search_20/site/templates/block_gcse_search_results.tpl index 8126280..056ad79 100644 --- a/modules/google_search_20/site/templates/block_search.tpl +++ b/modules/google_search_20/site/templates/block_gcse_search_results.tpl @@ -1,6 +1,6 @@
-

Results for {$result.current_page.search_terms/}

+

Results for {$cms_search_query/}

diff --git a/modules/google_search_20/src/google_custom_search_module_20.e b/modules/google_search_20/src/google_custom_search_module_20.e index f297886..2c85e12 100644 --- a/modules/google_search_20/src/google_custom_search_module_20.e +++ b/modules/google_search_20/src/google_custom_search_module_20.e @@ -9,7 +9,6 @@ class GOOGLE_CUSTOM_SEARCH_MODULE_20 inherit - CMS_MODULE redefine setup_hooks @@ -19,7 +18,7 @@ inherit REFACTORING_HELPER - CMS_HOOK_RESPONSE_ALTER + CMS_HOOK_BLOCK CMS_HOOK_BLOCK_HELPER @@ -64,13 +63,13 @@ feature -- Router feature -- GCSE Keys gcse_cx_key (api: CMS_API): detachable READABLE_STRING_8 - -- Get google custom search cx key. + -- Get google custom search engine id. local utf: UTF_CONVERTER do if attached api.module_configuration (Current, Void) as cfg then if - attached cfg.text_item ("gcse.cx") as l_gcse_cx_key and then + attached cfg.text_item ("gcse.search_engine_id") as l_gcse_cx_key and then not l_gcse_cx_key.is_empty then Result := utf.utf_32_string_to_utf_8_string_8 (l_gcse_cx_key) @@ -83,6 +82,8 @@ feature -- Handler handle_search (api: CMS_API; req: WSF_REQUEST; res: WSF_RESPONSE) local r: CMS_RESPONSE + l_script: STRING + b: CMS_CONTENT_BLOCK do write_debug_log (generator + ".handle_search") create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api) @@ -95,9 +96,16 @@ feature -- Handler then r.set_value (l_query.value, "cms_search_query") if - attached smarty_template_block (Current, "search", api) as l_tpl_block + attached smarty_template_block (Current, "gcse_search_results", api) as l_tpl_block then r.add_block (l_tpl_block, "content") + + if attached gcse_cx_key (api) as l_ctx then + create l_script.make_from_string (gcse_20_script) + l_script.replace_substring_all ("#CX_VALUE", l_ctx) + create b.make_raw ("gcse2_js", Void, "", Void) + r.add_block (b, "footer") + end end end else @@ -106,7 +114,6 @@ feature -- Handler r.execute end - feature -- Hooks configuration setup_hooks (a_hooks: CMS_HOOK_CORE_MANAGER) @@ -115,18 +122,25 @@ feature -- Hooks configuration auto_subscribe_to_hooks (a_hooks) end - response_alter (a_response: CMS_RESPONSE) - -- - local - l_script: STRING + block_list: ITERABLE [like {CMS_BLOCK}.name] do - if attached gcse_cx_key (a_response.api) as l_ctx then - create l_script.make_from_string (gcse_20_script) - l_script.replace_substring_all ("#CX_VALUE", l_ctx) - a_response.add_javascript_content (l_script) - end + Result := <<"gcse_search_form">> end + get_block_view (a_block_id: READABLE_STRING_8; a_response: CMS_RESPONSE) + do + if a_block_id.is_case_insensitive_equal_general ("gcse_search_form") then + if a_response.request.is_get_request_method then + if attached smarty_template_block (Current, a_block_id, a_response.api) as l_tpl_block then + a_response.add_block (l_tpl_block, "search") + else + debug ("cms") + a_response.add_warning_message ("Error with block [" + a_block_id + "]") + end + end + end + end + end feature {NONE} -- Implementation @@ -134,9 +148,9 @@ feature {NONE} -- Implementation (function() { var cx = '#CX_VALUE'; var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true; - gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + - '//www.google.com/cse/cse.js?cx=' + cx; - var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s); + gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx; + var s = document.getElementsByTagName('script')[0]; + s.parentNode.insertBefore(gcse, s); })(); ]" end