-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcess404Search.module
More file actions
executable file
·115 lines (90 loc) · 3.96 KB
/
Process404Search.module
File metadata and controls
executable file
·115 lines (90 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* ProcessWire 404 Search Results Generator
* by Adrian Jones
*
* Redirect 404 pages to search page with terms from failed URL
*
* ProcessWire 2.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class Process404Search extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => '404 Search Results Generator',
'summary' => 'Load search results into 404 page using terms from the failed URL',
'author' => 'Adrian Jones',
'href' => 'http://modules.processwire.com/modules/process404-search/',
'version' => 19,
'autoload' => true,
'singular' => true,
'icon' => 'search'
);
}
/**
* Default configuration for module
*
*/
static public function getDefaultData() {
return array(
"searchPage" => ""
);
}
/**
* Populate the default config data
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
public function init() {
$currentUrl = explode('/', $_SERVER['REQUEST_URI']);
if('/'.$currentUrl[1].'/' == $this->wire('config')->urls->admin) return;
$this->addHookAfter('ProcessPageView::pageNotFound', $this, 'search', array('priority'=>1000));
}
public function search($event) {
if($this->searchPage) {
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
$result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
$term = str_replace(array('-','_','/','%20'),' ',$result);
// New way of setting the value of get->q directly.
// This way, this module can work out of the box with no need to modify the search.php template file
$this->input->get->q = $term;
// Setting the value for $options['q'] in the render is for backwards compatibility
// and as a way to identify that this module is rendering the search template so the
// dev can choose to output conditional content such as if(isset($options['q'])) echo $pages->get($config->http404PageID)->body;
$searchPage = $this->pages->get($this->searchPage);
$searchPage->title = $this->pages->get($this->config->http404PageID)->title;
$event->return = $searchPage->render(array('q' => $term));
}
}
public static function getModuleConfigInputfields(array $data) {
$data = array_merge(self::getDefaultData(), $data);
$form = new InputfieldWrapper();
$fieldset = wire('modules')->get('InputfieldFieldset');
$fieldset->label = '404 Search Results Generator';
$fieldset->description = 'To make this module work, the only requirement is that you select your site\'s Search page below.
Optionally, you might like to add the following to your search.php file.
if(isset($options[\'q\'])) $content .= $pages->get($config->http404PageID)->body;
This will add the content of your 404 page\'s body field to the top of the search results. You might like to populate that field with something like:
<h3>The page you were looking for is not found.</h3>
<p>We have tried to find the content you were looking for, but if we didn\'t get it right, please use our search engine or navigation above to find the page.</p>';
$f = wire('modules')->get("InputfieldPageListSelect");
$f->name = 'searchPage';
$f->label = 'Search Page';
$f->required = true;
$f->attr('value', $data["searchPage"]);
$f->description = "Select your site's search page.";
$fieldset->add($f);
$form->add($fieldset);
return $form;
}
}