1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author nicolaas [at] sunnysideup.co.nz |
4
|
|
|
* Simple extension to add Google Custom Search to SilverStripe |
5
|
|
|
*/ |
6
|
|
|
class GoogleCustomSearchExt extends SiteTreeExtension |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key |
11
|
|
|
* @var String |
12
|
|
|
*/ |
13
|
|
|
private static $api_key = ""; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key |
18
|
|
|
* @var String |
19
|
|
|
*/ |
20
|
|
|
private static $cx_key = ""; |
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* returns the form |
25
|
|
|
* @return Form |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function getGoogleSiteSearchForm($name = "GoogleSiteSearchForm") |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$formIDinHTML = "Form_".$name; |
30
|
|
|
if ($page = GoogleCustomSearchPage::get()->first()) { |
|
|
|
|
31
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
32
|
|
|
Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearch.js'); |
33
|
|
|
$apiKey = Config::inst()->get("GoogleCustomSearchExt", "api_key"); |
34
|
|
|
$cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key"); |
35
|
|
|
if ($apiKey && $cxKey) { |
36
|
|
|
Requirements::customScript(" |
37
|
|
|
GoogleCustomSearch.apiKey = '".$apiKey."'; |
38
|
|
|
GoogleCustomSearch.cxKey = '".$cxKey."'; |
39
|
|
|
GoogleCustomSearch.formSelector = '#".$formIDinHTML."'; |
40
|
|
|
GoogleCustomSearch.inputFieldSelector = '#".$formIDinHTML."_search'; |
41
|
|
|
GoogleCustomSearch.resultsSelector = '#".$formIDinHTML."_Results'; |
42
|
|
|
", |
43
|
|
|
"GoogleCustomSearchExt" |
44
|
|
|
); |
45
|
|
|
$form = new Form( |
46
|
|
|
$this->owner, |
47
|
|
|
'GoogleSiteSearchForm', |
48
|
|
|
new FieldList( |
49
|
|
|
$searchField = new TextField('search'), |
50
|
|
|
$resultField = new LiteralField($name."_Results", "<div id=\"".$formIDinHTML."_Results\"></div>") |
51
|
|
|
), |
52
|
|
|
new FieldList(new FormAction('doSearch', _t("GoogleCustomSearchExt.GO", "Full Results"))) |
53
|
|
|
); |
54
|
|
|
$form->setFormMethod('GET'); |
55
|
|
|
if ($page = GoogleCustomSearchPage::get()->first()) { |
56
|
|
|
$form->setFormAction($page->Link()); |
57
|
|
|
} |
58
|
|
|
$form->disableSecurityToken(); |
59
|
|
|
$form->loadDataFrom($_GET); |
60
|
|
|
$searchField->setAttribute("autocomplete", "off"); |
61
|
|
|
$form->setAttribute("autocomplete", "off"); |
62
|
|
|
return $form; |
63
|
|
|
} else { |
64
|
|
|
user_error("You must set an API Key and a CX key in your configs to use the Google Custom Search Form", E_USER_NOTICE); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
user_error("You must create a GoogleCustomSearchPage first.", E_USER_NOTICE); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* returns the Google Search page... |
73
|
|
|
* @return GoogleCustomSearchPage |
74
|
|
|
*/ |
75
|
|
|
public function GoogleCustomSearchPage() |
76
|
|
|
{ |
77
|
|
|
return GoogleCustomSearchPage::get()->first(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.