Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 31, 2015
1 parent 75d6e1d commit eb34394
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 313 deletions.
242 changes: 127 additions & 115 deletions code/GoogleCustomSearchPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,123 +3,135 @@
/**
* @package googlesitesearch
*/
class GoogleCustomSearchPage extends Page {

private static $icon = "googlecustomsearch/images/treeicons/GoogleCustomSearchPage";

private static $allowed_children = "none";

private static $can_be_root = true;

private static $description = "Page to search via Google and display search results.";

/**
* Standard SS variable.
*/
private static $singular_name = "Google Search Results Page";
function i18n_singular_name() { return _t("GoogleCustomSearchPage.SINGULAR_NAME", "Google Search Results Page");}

/**
* Standard SS variable.
*/
private static $plural_name = "Google Search Results Pages";
function i18n_plural_name() { return _t("GoogleCustomSearchPage.PLURAL_NAME", "Google Search Results Pages");}

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Searches",
new GoogleCustomSearchPage_RecordField("stats", "Search History Last 100 Days")
);
return $fields;
}

public function requireDefaultRecords() {
if($this->canCreate()) {
DB::alteration_message("Creating a GoogleCustomSearchPage", "created");
$page = new GoogleCustomSearchPage();
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
}
}

public function populateDefaults() {
parent::populateDefaults();
$this->Title = "Search";
$this->MenuTitle = "Search";
$this->ShowInMenus = 0;
$this->ShowInSearch = 0;
$this->URLSegment = "search";
}

public function canCreate($member = null) {
return GoogleCustomSearchPage::get()->count() ? false : true;
}

class GoogleCustomSearchPage extends Page
{

private static $icon = "googlecustomsearch/images/treeicons/GoogleCustomSearchPage";

private static $allowed_children = "none";

private static $can_be_root = true;

private static $description = "Page to search via Google and display search results.";

/**
* Standard SS variable.
*/
private static $singular_name = "Google Search Results Page";
public function i18n_singular_name()
{
return _t("GoogleCustomSearchPage.SINGULAR_NAME", "Google Search Results Page");
}

/**
* Standard SS variable.
*/
private static $plural_name = "Google Search Results Pages";
public function i18n_plural_name()
{
return _t("GoogleCustomSearchPage.PLURAL_NAME", "Google Search Results Pages");
}

public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Searches",
new GoogleCustomSearchPage_RecordField("stats", "Search History Last 100 Days")
);
return $fields;
}

public function requireDefaultRecords()
{
if ($this->canCreate()) {
DB::alteration_message("Creating a GoogleCustomSearchPage", "created");
$page = new GoogleCustomSearchPage();
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
}
}

public function populateDefaults()
{
parent::populateDefaults();
$this->Title = "Search";
$this->MenuTitle = "Search";
$this->ShowInMenus = 0;
$this->ShowInSearch = 0;
$this->URLSegment = "search";
}

public function canCreate($member = null)
{
return GoogleCustomSearchPage::get()->count() ? false : true;
}
}

class GoogleCustomSearchPage_Controller extends Page_Controller {

private static $allowed_actions = array(
"recordsearch"
);

public function init() {
parent::init();
//register any search
if(isset($_GET["search"])) {
$searchString = Convert::raw2sql($_GET["search"]);
$forwardto = "";
if(isset($_GET["forwardto"])) {
$forwardto = Convert::raw2sql($_GET["forwardto"]);
}
GoogleCustomSearchPage_Record::add_entry($searchString, $forwardto);
}
if($this->request->param("Action") != "recordsearch") {
Requirements::themedCSS('GoogleCustomSearchPage');
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearchPage.js');
$cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
Requirements::customScript("
class GoogleCustomSearchPage_Controller extends Page_Controller
{

private static $allowed_actions = array(
"recordsearch"
);

public function init()
{
parent::init();
//register any search
if (isset($_GET["search"])) {
$searchString = Convert::raw2sql($_GET["search"]);
$forwardto = "";
if (isset($_GET["forwardto"])) {
$forwardto = Convert::raw2sql($_GET["forwardto"]);
}
GoogleCustomSearchPage_Record::add_entry($searchString, $forwardto);
}
if ($this->request->param("Action") != "recordsearch") {
Requirements::themedCSS('GoogleCustomSearchPage');
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearchPage.js');
$cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
Requirements::customScript("
GoogleCustomSearchPage.cxKey = '".$cxKey."';
",
"GoogleCustomSearchPage"
);
}
else {
echo "registered ...";
}
}

/**
* template function,
*
* @ return String
*/
public function SearchPhrase(){
$string = "";
if(isset($_GET['search'])) {
$string = $_GET['search'];
}
return DBField::create_field('HTMLText', $string);
}

/**
*
* @return String
*/
function getTitle(){
if($searchPhrase = $this->SearchPhrase()->forTemplate()) {
return $this->dataRecord->Title._t("GoogleCustomSearchPage.FOR", " for ").$searchPhrase;
}
else {
return $this->dataRecord->Title;
}
}

function recordsearch($request) {
if(isset($_GET["forwardto"]) && $_GET["forwardto"]) {
return $this->redirect($_GET["forwardto"]);
}
}

"GoogleCustomSearchPage"
);
} else {
echo "registered ...";
}
}

/**
* template function,
*
* @ return String
*/
public function SearchPhrase()
{
$string = "";
if (isset($_GET['search'])) {
$string = $_GET['search'];
}
return DBField::create_field('HTMLText', $string);
}

/**
*
* @return String
*/
public function getTitle()
{
if ($searchPhrase = $this->SearchPhrase()->forTemplate()) {
return $this->dataRecord->Title._t("GoogleCustomSearchPage.FOR", " for ").$searchPhrase;
} else {
return $this->dataRecord->Title;
}
}

public function recordsearch($request)
{
if (isset($_GET["forwardto"]) && $_GET["forwardto"]) {
return $this->redirect($_GET["forwardto"]);
}
}
}
124 changes: 62 additions & 62 deletions code/extensions/GoogleCustomSearchExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,77 @@
* @author nicolaas [at] sunnysideup.co.nz
* Simple extension to add Google Custom Search to SilverStripe
*/
class GoogleCustomSearchExt extends SiteTreeExtension {
class GoogleCustomSearchExt extends SiteTreeExtension
{

/**
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
* @var String
*/
private static $api_key = "";
/**
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
* @var String
*/
private static $api_key = "";

/**
*
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
* @var String
*/
private static $cx_key = "";
/**
*
* @see: https://developers.google.com/custom-search/json-api/v1/introduction#identify_your_application_to_google_with_api_key
* @var String
*/
private static $cx_key = "";


/**
* returns the form
* @return Form
*/
public function getGoogleSiteSearchForm($name = "GoogleSiteSearchForm") {
$formIDinHTML = "Form_".$name;
if($page = GoogleCustomSearchPage::get()->first()) {
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearch.js');
$apiKey = Config::inst()->get("GoogleCustomSearchExt", "api_key");
$cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
if($apiKey && $cxKey) {
Requirements::customScript("
/**
* returns the form
* @return Form
*/
public function getGoogleSiteSearchForm($name = "GoogleSiteSearchForm")
{
$formIDinHTML = "Form_".$name;
if ($page = GoogleCustomSearchPage::get()->first()) {
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearch.js');
$apiKey = Config::inst()->get("GoogleCustomSearchExt", "api_key");
$cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
if ($apiKey && $cxKey) {
Requirements::customScript("
GoogleCustomSearch.apiKey = '".$apiKey."';
GoogleCustomSearch.cxKey = '".$cxKey."';
GoogleCustomSearch.formSelector = '#".$formIDinHTML."';
GoogleCustomSearch.inputFieldSelector = '#".$formIDinHTML."_search';
GoogleCustomSearch.resultsSelector = '#".$formIDinHTML."_Results';
",
"GoogleCustomSearchExt"
);
$form = new Form(
$this->owner,
'GoogleSiteSearchForm',
new FieldList(
$searchField = new TextField('search'),
$resultField = new LiteralField($name."_Results", "<div id=\"".$formIDinHTML."_Results\"></div>")
),
new FieldList(new FormAction('doSearch', _t("GoogleCustomSearchExt.GO", "Full Results")))
);
$form->setFormMethod('GET');
if($page = GoogleCustomSearchPage::get()->first()) {
$form->setFormAction($page->Link());
}
$form->disableSecurityToken();
$form->loadDataFrom($_GET);
$searchField->setAttribute("autocomplete", "off");
$form->setAttribute("autocomplete", "off");
return $form;
}
else {
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);
}
}
else {
user_error("You must create a GoogleCustomSearchPage first.", E_USER_NOTICE);
}
}

/**
* returns the Google Search page...
* @return GoogleCustomSearchPage
*/
public function GoogleCustomSearchPage(){
return GoogleCustomSearchPage::get()->first();
}
"GoogleCustomSearchExt"
);
$form = new Form(
$this->owner,
'GoogleSiteSearchForm',
new FieldList(
$searchField = new TextField('search'),
$resultField = new LiteralField($name."_Results", "<div id=\"".$formIDinHTML."_Results\"></div>")
),
new FieldList(new FormAction('doSearch', _t("GoogleCustomSearchExt.GO", "Full Results")))
);
$form->setFormMethod('GET');
if ($page = GoogleCustomSearchPage::get()->first()) {
$form->setFormAction($page->Link());
}
$form->disableSecurityToken();
$form->loadDataFrom($_GET);
$searchField->setAttribute("autocomplete", "off");
$form->setAttribute("autocomplete", "off");
return $form;
} else {
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);
}
} else {
user_error("You must create a GoogleCustomSearchPage first.", E_USER_NOTICE);
}
}

/**
* returns the Google Search page...
* @return GoogleCustomSearchPage
*/
public function GoogleCustomSearchPage()
{
return GoogleCustomSearchPage::get()->first();
}
}
Loading

0 comments on commit eb34394

Please sign in to comment.