1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* **BasePage** is the foundation which can be used for constructing your own pages. |
4
|
|
|
* By default it is hidden from the CMS - we rely on developers creating their own |
5
|
|
|
* `Page` class in the `mysite/code` which will extend from the **BasePage**. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class BasePage extends SiteTree { |
9
|
|
|
|
10
|
|
|
private static $icon = 'cwp/images/icons/sitetree_images/page.png'; |
|
|
|
|
11
|
|
|
|
12
|
|
|
// Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide" |
13
|
|
|
private static $hide_ancestor = 'BasePage'; |
|
|
|
|
14
|
|
|
|
15
|
|
|
private static $pdf_export = false; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
*Domain to generate PDF's from, DOES not include protocol |
19
|
|
|
*i.e. google.com not http://google.com |
20
|
|
|
*/ |
21
|
|
|
private static $pdf_base_url = ""; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases |
25
|
|
|
* where multiple versions of the binary are available to choose from. This |
26
|
|
|
* should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf) |
27
|
|
|
* @see BasePage_Controller::generatePDF(); |
28
|
|
|
*/ |
29
|
|
|
private static $wkhtmltopdf_binary = null; |
|
|
|
|
30
|
|
|
|
31
|
|
|
private static $generated_pdf_path = 'assets/_generated_pdfs'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
private static $api_access = array( |
|
|
|
|
34
|
|
|
'view' => array('Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'), |
35
|
|
|
'edit' => array('Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID') |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
public static $related_pages_title = 'Related pages'; |
39
|
|
|
|
40
|
|
|
private static $many_many = array( |
|
|
|
|
41
|
|
|
'Terms' => 'TaxonomyTerm', |
42
|
|
|
'RelatedPages' => 'BasePage' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
private static $many_many_extraFields = array( |
|
|
|
|
46
|
|
|
'RelatedPages' => array( |
47
|
|
|
'SortOrder' => 'Int' |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
private static $plural_name = 'Base Pages'; |
|
|
|
|
52
|
|
|
|
53
|
|
|
public $pageIcon = 'images/icons/sitetree_images/page.png'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the footer holder. |
57
|
|
|
*/ |
58
|
|
|
public function getFooter() { |
59
|
|
|
return FooterHolder::get_one('FooterHolder'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the full filename of the pdf file, including path & extension |
64
|
|
|
*/ |
65
|
|
|
public function getPdfFilename() { |
66
|
|
|
$baseName = sprintf('%s-%s', $this->URLSegment, $this->ID); |
67
|
|
|
|
68
|
|
|
$folderPath = Config::inst()->get('BasePage', 'generated_pdf_path'); |
69
|
|
|
if($folderPath[0] != '/') $folderPath = BASE_PATH . '/' . $folderPath; |
70
|
|
|
|
71
|
|
|
return sprintf('%s/%s.pdf', $folderPath, $baseName); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Build pdf link for template. |
76
|
|
|
*/ |
77
|
|
|
public function PdfLink() { |
78
|
|
|
if(!Config::inst()->get('BasePage', 'pdf_export')) return false; |
79
|
|
|
|
80
|
|
|
$path = $this->getPdfFilename(); |
81
|
|
|
|
82
|
|
|
if((Versioned::current_stage() == 'Live') && file_exists($path)) { |
83
|
|
|
return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path)); |
84
|
|
|
} else { |
85
|
|
|
return $this->Link('downloadpdf'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function RelatedPages() { |
90
|
|
|
return $this->getManyManyComponents('RelatedPages')->sort('SortOrder'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function RelatedPagesTitle() { |
94
|
|
|
return $this->stat('related_pages_title'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Remove linked pdf when publishing the page, |
99
|
|
|
* as it would be out of date. |
100
|
|
|
*/ |
101
|
|
|
public function onAfterPublish(&$original) { |
|
|
|
|
102
|
|
|
$filepath = $this->getPdfFilename(); |
103
|
|
|
if(file_exists($filepath)) { |
104
|
|
|
unlink($filepath); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove linked pdf when unpublishing the page, |
110
|
|
|
* so it's no longer valid. |
111
|
|
|
* |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function doUnpublish() { |
115
|
|
|
if(!parent::doUnpublish()) return false; |
116
|
|
|
|
117
|
|
|
$filepath = $this->getPdfFilename(); |
118
|
|
|
if(file_exists($filepath)) { |
119
|
|
|
unlink($filepath); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @todo Remove once CWP moves to 3.3 core (which includes this in SiteTree) |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
public function doRestoreToStage() { |
130
|
|
|
$this->invokeWithExtensions('onBeforeRestoreToStage', $this); |
131
|
|
|
$result = parent::doRestoreToStage(); |
132
|
|
|
$this->invokeWithExtensions('onAfterRestoreToStage', $this); |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getCMSFields() { |
138
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
139
|
|
|
// Related Pages |
140
|
|
|
$components = GridFieldConfig_RelationEditor::create(); |
141
|
|
|
$components->removeComponentsByType('GridFieldAddNewButton'); |
142
|
|
|
$components->removeComponentsByType('GridFieldEditButton'); |
143
|
|
|
$components->removeComponentsByType('GridFieldFilterHeader'); |
144
|
|
|
$components->addComponent(new GridFieldSortableRows('SortOrder')); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$dataColumns = $components->getComponentByType('GridFieldDataColumns'); |
147
|
|
|
$dataColumns->setDisplayFields(array( |
|
|
|
|
148
|
|
|
'Title' => _t('BasePage.ColumnTitle', 'Title'), |
149
|
|
|
'ClassName' => _t('BasePage.ColumnPageType', 'Page Type') |
150
|
|
|
)); |
151
|
|
|
|
152
|
|
|
$fields->findOrMakeTab( |
153
|
|
|
'Root.RelatedPages', |
154
|
|
|
_t('BasePage.RelatedPages','Related pages') |
155
|
|
|
); |
156
|
|
|
$fields->addFieldToTab( |
157
|
|
|
'Root.RelatedPages', |
158
|
|
|
GridField::create( |
159
|
|
|
'RelatedPages', |
160
|
|
|
_t('BasePage.RelatedPages','Related pages'), |
161
|
|
|
$this->RelatedPages(), |
162
|
|
|
$components |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
// Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc) |
167
|
|
|
if(!$this->has_many('Tags') && !$this->many_many('Tags')) { |
|
|
|
|
168
|
|
|
$components = GridFieldConfig_RelationEditor::create(); |
169
|
|
|
$components->removeComponentsByType('GridFieldAddNewButton'); |
170
|
|
|
$components->removeComponentsByType('GridFieldEditButton'); |
171
|
|
|
|
172
|
|
|
$autoCompleter = $components->getComponentByType('GridFieldAddExistingAutocompleter'); |
173
|
|
|
$autoCompleter->setResultsFormat('$Name ($TaxonomyName)'); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$dataColumns = $components->getComponentByType('GridFieldDataColumns'); |
176
|
|
|
$dataColumns->setDisplayFields(array( |
177
|
|
|
'Name' => _t('BasePage.Term','Term'), |
178
|
|
|
'TaxonomyName' => _t('BasePage.Taxonomy','Taxonomy') |
179
|
|
|
)); |
180
|
|
|
|
181
|
|
|
$fields->findOrMakeTab('Root.Tags', _t('BasePage.TagsTabTitle', 'Tags')); |
182
|
|
|
$fields->addFieldToTab( |
183
|
|
|
'Root.Tags', |
184
|
|
|
TreeMultiselectField::create( |
185
|
|
|
'Terms', |
186
|
|
|
_t('BasePage.Terms','Terms'), |
187
|
|
|
'TaxonomyTerm' |
188
|
|
|
)->setDescription(_t('BasePage.TermsDescription', 'Click to search for additional terms')) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
}); |
192
|
|
|
return parent::getCMSFields(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Provides data for translation navigation. |
197
|
|
|
* Collects all site translations, marks the current one, and redirects |
198
|
|
|
* to the translated home page if a. there is a translated homepage and b. the |
199
|
|
|
* translation of the specific page is not available. |
200
|
|
|
*/ |
201
|
|
|
public function getAvailableTranslations() { |
202
|
|
|
|
203
|
|
|
if(!class_exists('Translatable')){ |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$translations = new ArrayList(); |
208
|
|
|
$globalTranslations = Translatable::get_existing_content_languages(); |
|
|
|
|
209
|
|
|
|
210
|
|
|
foreach ($globalTranslations as $loc=>$langName) { |
211
|
|
|
|
212
|
|
|
// Find out the language name in native language. |
213
|
|
|
$nativeLangName = i18n::get_language_name($loc, true); |
214
|
|
|
if (!$nativeLangName) { |
215
|
|
|
$nativeLangName = i18n::get_language_name(i18n::get_lang_from_locale($loc), true); |
216
|
|
|
} |
217
|
|
|
if (!$nativeLangName) { |
218
|
|
|
// Fall back to the locale name. |
219
|
|
|
$nativeLangName = $langName; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Eliminate the part in brackets (e.g. [mandarin]) |
223
|
|
|
$nativeLangName = preg_replace('/ *[\(\[].*$/', '', $nativeLangName); |
224
|
|
|
|
225
|
|
|
// Find out the link to the translated page. |
226
|
|
|
$link = null; |
227
|
|
|
$page = $this->getTranslation($loc); |
|
|
|
|
228
|
|
|
if ($page) { |
229
|
|
|
$link = $page->Link(); |
230
|
|
|
} |
231
|
|
|
if (!$link) { |
232
|
|
|
// Fall back to the home page |
233
|
|
|
$link = Translatable::get_homepage_link_by_locale($loc); |
234
|
|
|
} |
235
|
|
|
if (!$link) { |
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// Assemble the table for the switcher. |
240
|
|
|
$translations->push(new ArrayData(array( |
241
|
|
|
'Locale' => i18n::convert_rfc1766($loc), |
242
|
|
|
'LangName' => $nativeLangName, |
243
|
|
|
'Link' => $link, |
244
|
|
|
'Current' => (Translatable::get_current_locale()==$loc) |
245
|
|
|
))); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if ($translations->count()>1) return $translations; |
249
|
|
|
else return null; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Returns the native language name for the selected locale/language, empty string if Translatable is not available |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getSelectedLanguage() |
258
|
|
|
{ |
259
|
|
|
if (!class_exists('Translatable')) { |
260
|
|
|
return ''; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$language = explode('_', Translatable::get_current_locale()); |
264
|
|
|
$languageCode = array_shift($language); |
265
|
|
|
$nativeName = i18n::get_language_name($languageCode, true); |
266
|
|
|
|
267
|
|
|
return $nativeName; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
class BasePage_Controller extends ContentController { |
272
|
|
|
|
273
|
|
|
private static $allowed_actions = array( |
|
|
|
|
274
|
|
|
'downloadpdf', |
275
|
|
|
'SearchForm', |
276
|
|
|
'results' |
277
|
|
|
); |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* How many search results should be shown per-page? |
281
|
|
|
* @var int |
282
|
|
|
*/ |
283
|
|
|
public static $results_per_page = 10; |
284
|
|
|
|
285
|
|
|
public static $search_index_class = 'SolrSearchIndex'; |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* If spelling suggestions for searches are given, enable |
289
|
|
|
* suggested searches to be followed immediately |
290
|
|
|
* |
291
|
|
|
* @config |
292
|
|
|
* @var bool |
293
|
|
|
*/ |
294
|
|
|
private static $search_follow_suggestions = true; |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Which classes should be queried when searching? |
298
|
|
|
* @var array |
299
|
|
|
*/ |
300
|
|
|
public static $classes_to_search = array( |
301
|
|
|
array( |
302
|
|
|
'class' => 'Page', |
303
|
|
|
'includeSubclasses' => true |
304
|
|
|
) |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Serve the page rendered as PDF. |
309
|
|
|
*/ |
310
|
|
|
public function downloadpdf() { |
311
|
|
|
if(!Config::inst()->get('BasePage', 'pdf_export')) return false; |
312
|
|
|
|
313
|
|
|
// We only allow producing live pdf. There is no way to secure the draft files. |
314
|
|
|
Versioned::reading_stage('Live'); |
315
|
|
|
|
316
|
|
|
$path = $this->dataRecord->getPdfFilename(); |
317
|
|
|
if(!file_exists($path)) { |
318
|
|
|
$this->generatePDF(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return SS_HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/* |
325
|
|
|
* This will return either pdf_base_url from YML, CWP_SECURE_DOMAIN |
326
|
|
|
* from _ss_environment, or blank. In that order of importance. |
327
|
|
|
*/ |
328
|
|
|
public function getPDFBaseURL() { |
329
|
|
|
//if base url YML is defined in YML, use that |
330
|
|
|
if(Config::inst()->get('BasePage', 'pdf_base_url')){ |
331
|
|
|
$pdf_base_url = Config::inst()->get('BasePage', 'pdf_base_url').'/'; |
332
|
|
|
//otherwise, if we are CWP use the secure domain |
333
|
|
|
} elseif (defined('CWP_SECURE_DOMAIN')){ |
334
|
|
|
$pdf_base_url = CWP_SECURE_DOMAIN.'/'; |
|
|
|
|
335
|
|
|
//or if neither, leave blank |
336
|
|
|
} else { |
337
|
|
|
$pdf_base_url = ''; |
338
|
|
|
} |
339
|
|
|
return $pdf_base_url; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/* |
343
|
|
|
* Don't use the proxy if the pdf domain is the CWP secure domain |
344
|
|
|
* Or if we aren't on a CWP server |
345
|
|
|
*/ |
346
|
|
|
public function getPDFProxy($pdf_base_url) { |
347
|
|
|
if (!defined('CWP_SECURE_DOMAIN') || $pdf_base_url == CWP_SECURE_DOMAIN.'/') { |
|
|
|
|
348
|
|
|
$proxy = ''; |
349
|
|
|
} else { |
350
|
|
|
$proxy = ' --proxy ' . SS_OUTBOUND_PROXY . ':' . SS_OUTBOUND_PROXY_PORT; |
|
|
|
|
351
|
|
|
} |
352
|
|
|
return $proxy; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Render the page as PDF using wkhtmltopdf. |
357
|
|
|
*/ |
358
|
|
|
public function generatePDF() { |
359
|
|
|
if(!Config::inst()->get('BasePage', 'pdf_export')) return false; |
360
|
|
|
|
361
|
|
|
$binaryPath = Config::inst()->get('BasePage', 'wkhtmltopdf_binary'); |
362
|
|
|
if(!$binaryPath || !is_executable($binaryPath)) { |
363
|
|
|
if(defined('WKHTMLTOPDF_BINARY') && is_executable(WKHTMLTOPDF_BINARY)) { |
364
|
|
|
$binaryPath = WKHTMLTOPDF_BINARY; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
if(!$binaryPath) { |
369
|
|
|
user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if(Versioned::get_reading_mode() == 'Stage.Stage') { |
373
|
|
|
user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
set_time_limit(60); |
377
|
|
|
|
378
|
|
|
// prepare the paths |
379
|
|
|
$pdfFile = $this->dataRecord->getPdfFilename(); |
380
|
|
|
$bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
381
|
|
|
$footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
382
|
|
|
|
383
|
|
|
// make sure the work directory exists |
384
|
|
|
if(!file_exists(dirname($pdfFile))) Filesystem::makeFolder(dirname($pdfFile)); |
385
|
|
|
|
386
|
|
|
//decide the domain to use in generation |
387
|
|
|
$pdf_base_url = $this->getPDFBaseURL(); |
388
|
|
|
|
389
|
|
|
// Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
390
|
|
|
if (defined('CWP_ENVIRONMENT')) { |
391
|
|
|
Config::inst()->nest(); |
392
|
|
|
Config::inst()->update('Director', 'alternate_protocol', 'http'); |
393
|
|
|
//only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
394
|
|
|
if($pdf_base_url){ |
395
|
|
|
Config::inst()->update('Director', 'alternate_base_url', 'http://'.$pdf_base_url); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$bodyViewer = $this->getViewer('pdf'); |
400
|
|
|
|
401
|
|
|
// write the output of this page to HTML, ready for conversion to PDF |
402
|
|
|
file_put_contents($bodyFile, $bodyViewer->process($this)); |
403
|
|
|
|
404
|
|
|
// get the viewer for the current template with _pdffooter |
405
|
|
|
$footerViewer = $this->getViewer('pdffooter'); |
406
|
|
|
|
407
|
|
|
// write the output of the footer template to HTML, ready for conversion to PDF |
408
|
|
|
file_put_contents($footerFile, $footerViewer->process($this)); |
409
|
|
|
|
410
|
|
|
if (defined('CWP_ENVIRONMENT')) { |
411
|
|
|
Config::inst()->unnest(); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
//decide what the proxy should look like |
415
|
|
|
$proxy = $this->getPDFProxy($pdf_base_url); |
416
|
|
|
|
417
|
|
|
// finally, generate the PDF |
418
|
|
|
$command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 --orientation Portrait --disable-javascript --quiet --print-media-type '; |
419
|
|
|
$retVal = 0; |
|
|
|
|
420
|
|
|
$output = array(); |
421
|
|
|
exec($command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", $output, $return_val); |
422
|
|
|
|
423
|
|
|
// remove temporary file |
424
|
|
|
unlink($bodyFile); |
425
|
|
|
unlink($footerFile); |
426
|
|
|
|
427
|
|
|
// output any errors |
428
|
|
|
if($return_val != 0) { |
429
|
|
|
user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
// serve the generated file |
433
|
|
|
return SS_HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Site search form |
438
|
|
|
*/ |
439
|
|
|
public function SearchForm() |
440
|
|
|
{ |
441
|
|
|
$searchText = $this->getRequest()->getVar('Search'); |
442
|
|
|
|
443
|
|
|
$fields = new FieldList( |
444
|
|
|
TextField::create('Search', false, $searchText) |
445
|
|
|
); |
446
|
|
|
$actions = new FieldList( |
447
|
|
|
new FormAction('results', _t('SearchForm.GO', 'Go')) |
448
|
|
|
); |
449
|
|
|
|
450
|
|
|
$form = SearchForm::create($this, 'SearchForm', $fields, $actions); |
451
|
|
|
$form->setFormAction('search/SearchForm'); |
452
|
|
|
|
453
|
|
|
return $form; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Get search form with _header suffix |
458
|
|
|
* |
459
|
|
|
* @return SearchForm |
460
|
|
|
*/ |
461
|
|
|
public function HeaderSearchForm() |
462
|
|
|
{ |
463
|
|
|
return $this->SearchForm()->setTemplate('SearchForm_header'); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Process and render search results. |
468
|
|
|
* |
469
|
|
|
* @param array $data The raw request data submitted by user |
470
|
|
|
* @param SearchForm $form The form instance that was submitted |
471
|
|
|
* @param SS_HTTPRequest $request Request generated for this action |
472
|
|
|
* @return HTMLText |
473
|
|
|
*/ |
474
|
|
|
public function results($data, $form, $request) { |
475
|
|
|
// Check parameters for terms, pagination, and if we should follow suggestions |
476
|
|
|
$keywords = empty($data['Search']) ? '' : $data['Search']; |
477
|
|
|
$start = isset($data['start']) ? $data['start'] : 0; |
478
|
|
|
$suggestions = isset($data['suggestions']) |
479
|
|
|
? $data['suggestions'] |
480
|
|
|
: $this->config()->search_follow_suggestions; |
481
|
|
|
|
482
|
|
|
$results = CwpSearchEngine::create() |
483
|
|
|
->search( |
484
|
|
|
$keywords, |
485
|
|
|
$this->getClassesToSearch(), |
486
|
|
|
$this->getSearchIndex(), |
487
|
|
|
$this->getSearchPageSize(), |
488
|
|
|
$start, |
489
|
|
|
$suggestions |
490
|
|
|
); |
491
|
|
|
|
492
|
|
|
// Customise content with these results |
493
|
|
|
$properties = array( |
494
|
|
|
'MetaTitle' => _t('CWP_Search.MetaTitle', 'Search {keywords}', array('keywords' => $keywords)), |
495
|
|
|
'NoSearchResults' => _t('CWP_Search.NoResult', 'Sorry, your search query did not return any results.'), |
496
|
|
|
'EmptySearch' => _t('CWP_Search.EmptySearch', 'Search field empty, please enter your search query.'), |
497
|
|
|
'PdfLink' => '', |
498
|
|
|
'Title' => _t('SearchForm.SearchResults', 'Search Results'), |
499
|
|
|
); |
500
|
|
|
$this->extend('updateSearchResults', $results, $properties); |
501
|
|
|
|
502
|
|
|
// Customise page |
503
|
|
|
$response = $this->customise($properties); |
504
|
|
|
if($results) { |
505
|
|
|
$response = $response |
506
|
|
|
->customise($results) |
507
|
|
|
->customise(array( 'Results' => $results->getResults() )); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
// Render |
511
|
|
|
$templates = $this->getResultsTemplate($request); |
512
|
|
|
return $response->renderWith($templates); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Select the template to render search results with |
517
|
|
|
* |
518
|
|
|
* @param SS_HTTPRequest $request |
519
|
|
|
* @return array |
520
|
|
|
*/ |
521
|
|
|
protected function getResultsTemplate($request) { |
522
|
|
|
$templates = array('Page_results', 'Page'); |
523
|
|
|
if ($request->getVar('format') == 'rss') { |
524
|
|
|
array_unshift($templates, 'Page_results_rss'); |
525
|
|
|
} |
526
|
|
|
if ($request->getVar('format') == 'atom') { |
527
|
|
|
array_unshift($templates, 'Page_results_atom'); |
528
|
|
|
} |
529
|
|
|
return $templates; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Provide scripts as needed by the *default* theme. |
534
|
|
|
* Override this function if you are using a custom theme based on the *default*. |
535
|
|
|
* |
536
|
|
|
* @deprecated 1.6..2.0 Use "starter" theme instead |
537
|
|
|
*/ |
538
|
|
|
public function getBaseScripts() { |
539
|
|
|
$scripts = array(); |
540
|
|
|
$this->extend('updateBaseScripts', $scripts); |
541
|
|
|
return $scripts; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Provide stylesheets, as needed by the *default* theme assumed by this recipe. |
546
|
|
|
* Override this function if you are using a custom theme based on the *default*. |
547
|
|
|
* |
548
|
|
|
* @deprecated 1.6..2.0 Use "starter" theme instead |
549
|
|
|
*/ |
550
|
|
|
public function getBaseStyles() { |
551
|
|
|
$styles = array(); |
552
|
|
|
$this->extend('updateBaseStyles', $styles); |
553
|
|
|
return $styles; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Provide current year. |
558
|
|
|
*/ |
559
|
|
|
public function CurrentDatetime() { |
560
|
|
|
return SS_Datetime::now(); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
public function getRSSLink() { |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Get the search index registered for this application |
568
|
|
|
* |
569
|
|
|
* @return CwpSearchIndex |
570
|
|
|
*/ |
571
|
|
|
protected function getSearchIndex() |
572
|
|
|
{ |
573
|
|
|
// Will be a service name in 2.0 and returned via injector |
574
|
|
|
/** @var CwpSearchIndex $index */ |
575
|
|
|
$index = null; |
576
|
|
|
if (self::$search_index_class) { |
577
|
|
|
$index = Object::singleton(self::$search_index_class); |
578
|
|
|
} |
579
|
|
|
return $index; |
|
|
|
|
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Gets the list of configured classes to search |
584
|
|
|
* |
585
|
|
|
* @return array |
586
|
|
|
*/ |
587
|
|
|
protected function getClassesToSearch() |
588
|
|
|
{ |
589
|
|
|
// Will be private static config in 2.0 |
590
|
|
|
return self::$classes_to_search; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Get page size for search |
595
|
|
|
* |
596
|
|
|
* @return int |
597
|
|
|
*/ |
598
|
|
|
protected function getSearchPageSize() |
599
|
|
|
{ |
600
|
|
|
// Will be private static config in 2.0 |
601
|
|
|
return self::$results_per_page; |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
|