Completed
Push — master ( 29d5ff...6f0ca5 )
by Nicolaas
01:35
created

code/GoogleCustomSearchPage.php (3 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @package googlesitesearch
5
 */
6
class GoogleCustomSearchPage extends Page
7
{
8
    private static $icon = "googlecustomsearch/images/treeicons/GoogleCustomSearchPage";
9
10
    private static $allowed_children = "none";
11
12
    private static $can_be_root = true;
13
14
    private static $description = "Page to search via Google and display search results.";
15
16
    /**
17
     * Standard SS variable.
18
     */
19
    private static $singular_name = "Google Search Results Page";
20
    public function i18n_singular_name()
21
    {
22
        return _t("GoogleCustomSearchPage.SINGULAR_NAME", "Google Search Results Page");
23
    }
24
25
    /**
26
     * Standard SS variable.
27
     */
28
    private static $plural_name = "Google Search Results Pages";
29
    public function i18n_plural_name()
30
    {
31
        return _t("GoogleCustomSearchPage.PLURAL_NAME", "Google Search Results Pages");
32
    }
33
34
    public function getCMSFields()
35
    {
36
        $fields = parent::getCMSFields();
37
        $fields->addFieldToTab("Root.Searches",
38
            new GoogleCustomSearchPage_RecordField("stats", "Search History Last 100 Days")
39
        );
40
        return $fields;
41
    }
42
43
    public function requireDefaultRecords()
44
    {
45
        if ($this->canCreate()) {
46
            DB::alteration_message("Creating a GoogleCustomSearchPage", "created");
47
            $page = new GoogleCustomSearchPage();
48
            $page->writeToStage('Stage');
49
            $page->publish('Stage', 'Live');
50
        }
51
    }
52
53
    public function populateDefaults()
54
    {
55
        parent::populateDefaults();
56
        $this->Title = "Search";
57
        $this->MenuTitle = "Search";
58
        $this->ShowInMenus = 0;
59
        $this->ShowInSearch = 0;
60
        $this->URLSegment = "search";
61
    }
62
63
    public function canCreate($member = null)
64
    {
65
        return GoogleCustomSearchPage::get()->count() ?  false : true;
66
    }
67
}
68
69
class GoogleCustomSearchPage_Controller extends Page_Controller
70
{
71
    private static $allowed_actions = array(
72
        "recordsearch"
73
    );
74
75
    public function init()
0 ignored issues
show
init uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
    {
77
        parent::init();
78
        //register any search
79
        if (isset($_GET["search"])) {
80
            $searchString = Convert::raw2sql($_GET["search"]);
81
            $forwardto = "";
82
            if (isset($_GET["forwardto"])) {
83
                $forwardto = Convert::raw2sql($_GET["forwardto"]);
84
            }
85
            GoogleCustomSearchPage_Record::add_entry($searchString, $forwardto);
86
        }
87
        if ($this->request->param("Action") != "recordsearch") {
88
            Requirements::themedCSS('GoogleCustomSearchPage');
89
            Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
90
            Requirements::javascript('googlecustomsearch/javascript/GoogleCustomSearchPage.js');
91
            $cxKey = Config::inst()->get("GoogleCustomSearchExt", "cx_key");
92
            Requirements::customScript("
93
					GoogleCustomSearchPage.cxKey = '".$cxKey."';
94
				",
95
                "GoogleCustomSearchPage"
96
            );
97
        } else {
98
            echo "registered ...";
99
        }
100
    }
101
102
    /**
103
     * template function,
104
     *
105
     * @ return String
106
     */
107
    public function SearchPhrase()
0 ignored issues
show
SearchPhrase uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
108
    {
109
        $string = "";
110
        if (isset($_GET['search'])) {
111
            $string = $_GET['search'];
112
        }
113
        return DBField::create_field('HTMLText', $string);
114
    }
115
116
    /**
117
     *
118
     * @return String
119
     */
120
    public function getTitle()
121
    {
122
        if ($searchPhrase = $this->SearchPhrase()->forTemplate()) {
123
            return $this->dataRecord->Title._t("GoogleCustomSearchPage.FOR", " for ").$searchPhrase;
124
        } else {
125
            return $this->dataRecord->Title;
126
        }
127
    }
128
129
    public function recordsearch($request)
0 ignored issues
show
recordsearch uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
130
    {
131
        if (isset($_GET["forwardto"]) && $_GET["forwardto"]) {
132
            return $this->redirect($_GET["forwardto"]);
133
        }
134
    }
135
}
136