|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ElasticSearchPage_Validator extends RequiredFields { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
protected $customRequired = array('Name'); |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Constructor |
|
9
|
|
|
*/ |
|
10
|
|
|
public function __construct() { |
|
11
|
|
|
$required = array('ResultsPerPage', 'Identifier'); |
|
12
|
|
|
|
|
13
|
|
|
parent::__construct($required); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
function php($data) { |
|
|
|
|
|
|
17
|
|
|
parent::php($data); |
|
18
|
|
|
$valid = true; |
|
|
|
|
|
|
19
|
|
|
//Debug::message("Validating data: " . print_r($data, true)); |
|
|
|
|
|
|
20
|
|
|
$valid = parent::php($data); |
|
21
|
|
|
//Debug::message("Returning false, just to check"); |
|
|
|
|
|
|
22
|
|
|
//return false; |
|
23
|
|
|
// |
|
24
|
|
|
|
|
25
|
|
|
if ($data['ClassesToSearch'] == array()) { |
|
26
|
|
|
$data['ClassesToSearch'] = ''; |
|
27
|
|
|
} |
|
28
|
|
|
$debug = $data['SiteTreeOnly']; |
|
|
|
|
|
|
29
|
|
|
// Check if any classes to search if site tree only is not ticked |
|
30
|
|
|
if (!$data['SiteTreeOnly']) { |
|
31
|
|
|
if (!$data['ClassesToSearch']) { |
|
32
|
|
|
$valid = false; |
|
33
|
|
|
$this->validationError("ClassesToSearch", |
|
34
|
|
|
"Please provide at least one class to search, or select 'Site Tree Only'", |
|
35
|
|
|
'error' |
|
36
|
|
|
); |
|
37
|
|
|
} else { |
|
38
|
|
|
$toSearch = $data['ClassesToSearch']; |
|
39
|
|
|
foreach ($toSearch as $clazz) { |
|
40
|
|
|
try { |
|
41
|
|
|
$instance = Injector::inst()->create($clazz); |
|
42
|
|
|
if (!$instance->hasExtension('SilverStripe\Elastica\Searchable')) { |
|
43
|
|
|
$this->validationError('ClassesToSearch', 'The class '.$clazz.' must have the Searchable extension'); |
|
44
|
|
|
} |
|
45
|
|
|
} catch (ReflectionException $e) { |
|
46
|
|
|
$this->validationError("ClassesToSearch", |
|
47
|
|
|
'The class '.$clazz.' does not exist', |
|
48
|
|
|
'error' |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// Check the identifier is unique |
|
57
|
|
|
$mode = Versioned::get_reading_mode(); |
|
58
|
|
|
$suffix = ''; |
|
59
|
|
|
if ($mode == 'Stage.Live') { |
|
60
|
|
|
$suffix = '_Live'; |
|
61
|
|
|
} |
|
62
|
|
|
$where = 'ElasticSearchPage'.$suffix.'.ID != '.$data['ID']." AND `Identifier` = '".$data['Identifier']."'"; |
|
63
|
|
|
$existing = ElasticSearchPage::get()->where($where)->count(); |
|
64
|
|
|
if ($existing > 0) { |
|
65
|
|
|
$valid = false; |
|
66
|
|
|
$this->validationError('Identifier', |
|
67
|
|
|
'The identifier '.$data['Identifier'].' already exists', |
|
68
|
|
|
'error' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
// Check number of results per page >= 1 |
|
74
|
|
|
if ($data['ResultsPerPage'] <= 0) { |
|
75
|
|
|
$valid = false; |
|
76
|
|
|
$this->validationError('ResultsPerPage', |
|
77
|
|
|
'Results per page must be >=1' |
|
78
|
|
|
,'error' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
return $valid; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.