Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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