mirror of
https://github.com/silverstripe/silverstripe-spamprotection.git
synced 2024-10-22 14:05:59 +02:00
reverting indentation back
This commit is contained in:
parent
ee9057d233
commit
935bcd0bf4
@ -8,158 +8,158 @@
|
|||||||
*/
|
*/
|
||||||
if(class_exists('EditableFormField')) {
|
if(class_exists('EditableFormField')) {
|
||||||
|
|
||||||
class EditableSpamProtectionField extends EditableFormField {
|
class EditableSpamProtectionField extends EditableFormField {
|
||||||
|
|
||||||
private static $singular_name = 'Spam Protection Field';
|
private static $singular_name = 'Spam Protection Field';
|
||||||
|
|
||||||
private static $plural_name = 'Spam Protection Fields';
|
private static $plural_name = 'Spam Protection Fields';
|
||||||
/**
|
/**
|
||||||
* Fields to include spam detection for
|
* Fields to include spam detection for
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
* @config
|
* @config
|
||||||
*/
|
*/
|
||||||
private static $check_fields = array(
|
private static $check_fields = array(
|
||||||
'EditableEmailField',
|
'EditableEmailField',
|
||||||
'EditableTextField',
|
'EditableTextField',
|
||||||
'EditableNumericField'
|
'EditableNumericField'
|
||||||
);
|
);
|
||||||
|
|
||||||
public function getFormField() {
|
public function getFormField() {
|
||||||
// Get protector
|
// Get protector
|
||||||
$protector = FormSpamProtectionExtension::get_protector();
|
$protector = FormSpamProtectionExtension::get_protector();
|
||||||
if(!$protector) return false;
|
if(!$protector) return false;
|
||||||
|
|
||||||
// Extract saved field mappings and update this field.
|
// Extract saved field mappings and update this field.
|
||||||
$fieldMapping = array();
|
$fieldMapping = array();
|
||||||
foreach($this->getCandidateFields() as $otherField) {
|
foreach($this->getCandidateFields() as $otherField) {
|
||||||
$mapSetting = "Map-{$otherField->Name}";
|
$mapSetting = "Map-{$otherField->Name}";
|
||||||
$spamField = $this->getSetting($mapSetting);
|
$spamField = $this->getSetting($mapSetting);
|
||||||
$fieldMapping[$otherField->Name] = $spamField;
|
$fieldMapping[$otherField->Name] = $spamField;
|
||||||
}
|
}
|
||||||
$protector->setFieldMapping($fieldMapping);
|
$protector->setFieldMapping($fieldMapping);
|
||||||
|
|
||||||
// Generate field
|
// Generate field
|
||||||
return $protector->getFormField($this->Name, $this->Title, null);
|
return $protector->getFormField($this->Name, $this->Title, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of all candidate spam detectable fields on this field's form
|
* Gets the list of all candidate spam detectable fields on this field's form
|
||||||
*
|
*
|
||||||
* @return DataList
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
protected function getCandidateFields() {
|
protected function getCandidateFields() {
|
||||||
|
|
||||||
// Get list of all configured classes available for spam detection
|
// Get list of all configured classes available for spam detection
|
||||||
$types = self::config()->check_fields;
|
$types = self::config()->check_fields;
|
||||||
$typesInherit = array();
|
$typesInherit = array();
|
||||||
foreach ($types as $type) {
|
foreach ($types as $type) {
|
||||||
$subTypes = ClassInfo::subclassesFor($type);
|
$subTypes = ClassInfo::subclassesFor($type);
|
||||||
$typesInherit = array_merge($typesInherit, $subTypes);
|
$typesInherit = array_merge($typesInherit, $subTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all candidates of the above types
|
// Get all candidates of the above types
|
||||||
return $this
|
return $this
|
||||||
->Parent()
|
->Parent()
|
||||||
->Fields()
|
->Fields()
|
||||||
->filter('ClassName', $typesInherit)
|
->filter('ClassName', $typesInherit)
|
||||||
->exclude('Title', ''); // Ignore this field and those without titles
|
->exclude('Title', ''); // Ignore this field and those without titles
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFieldConfiguration() {
|
public function getFieldConfiguration() {
|
||||||
$fields = parent::getFieldConfiguration();
|
$fields = parent::getFieldConfiguration();
|
||||||
|
|
||||||
// Get protector
|
// Get protector
|
||||||
$protector = FormSpamProtectionExtension::get_protector();
|
$protector = FormSpamProtectionExtension::get_protector();
|
||||||
if (!$protector) return $fields;
|
if (!$protector) return $fields;
|
||||||
|
|
||||||
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
|
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each other text field in this group can be assigned a field mapping
|
// Each other text field in this group can be assigned a field mapping
|
||||||
$mapGroup = FieldGroup::create(_t(
|
$mapGroup = FieldGroup::create(_t(
|
||||||
'EditableSpamProtectionField.SPAMFIELDMAPPING',
|
'EditableSpamProtectionField.SPAMFIELDMAPPING',
|
||||||
'Spam Field Mapping'
|
'Spam Field Mapping'
|
||||||
))->setDescription(_t(
|
))->setDescription(_t(
|
||||||
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
|
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
|
||||||
'Select the form fields that correspond to any relevant spam protection identifiers'
|
'Select the form fields that correspond to any relevant spam protection identifiers'
|
||||||
));
|
));
|
||||||
|
|
||||||
// Generate field specific settings
|
// Generate field specific settings
|
||||||
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
|
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
|
||||||
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
|
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
|
||||||
foreach ($this->getCandidateFields() as $otherField) {
|
foreach ($this->getCandidateFields() as $otherField) {
|
||||||
$mapSetting = "Map-{$otherField->Name}";
|
$mapSetting = "Map-{$otherField->Name}";
|
||||||
$fieldOption = DropdownField::create(
|
$fieldOption = DropdownField::create(
|
||||||
$this->getSettingName($mapSetting),
|
$this->getSettingName($mapSetting),
|
||||||
$otherField->Title,
|
$otherField->Title,
|
||||||
$mappableFieldsMerged,
|
$mappableFieldsMerged,
|
||||||
$this->getSetting($mapSetting)
|
$this->getSetting($mapSetting)
|
||||||
)->setEmptyString('');
|
)->setEmptyString('');
|
||||||
$mapGroup->push($fieldOption);
|
$mapGroup->push($fieldOption);
|
||||||
}
|
}
|
||||||
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
|
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Using custom validateField method
|
* Using custom validateField method
|
||||||
* as Spam Protection Field implementations may have their own error messages
|
* as Spam Protection Field implementations may have their own error messages
|
||||||
* and may not be based on the field being required, e.g. Honeypot Field
|
* and may not be based on the field being required, e.g. Honeypot Field
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param Form $form
|
* @param Form $form
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function validateField($data, $form) {
|
public function validateField($data, $form) {
|
||||||
$formField = $this->getFormField();
|
$formField = $this->getFormField();
|
||||||
|
|
||||||
if(isset($data[$this->Name])) {
|
if(isset($data[$this->Name])) {
|
||||||
$formField->setValue($data[$this->Name]);
|
$formField->setValue($data[$this->Name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$validator = $form->getValidator();
|
$validator = $form->getValidator();
|
||||||
if (!$formField->validate($validator)) {
|
if (!$formField->validate($validator)) {
|
||||||
$errors = $validator->getErrors();
|
$errors = $validator->getErrors();
|
||||||
$foundError = false;
|
$foundError = false;
|
||||||
|
|
||||||
// field validate implementation may not add error to validator
|
// field validate implementation may not add error to validator
|
||||||
if(count($errors) > 0) {
|
if(count($errors) > 0) {
|
||||||
// check if error already added from fields' validate method
|
// check if error already added from fields' validate method
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
if($error['fieldName'] == $this->Name) {
|
if($error['fieldName'] == $this->Name) {
|
||||||
$foundError = $error;
|
$foundError = $error;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($foundError !== false) {
|
if($foundError !== false) {
|
||||||
// use error messaging already set from validate method
|
// use error messaging already set from validate method
|
||||||
$form->addErrorMessage($this->Name, $foundError['message'], $foundError['messageType'], false);
|
$form->addErrorMessage($this->Name, $foundError['message'], $foundError['messageType'], false);
|
||||||
} else {
|
} else {
|
||||||
// fallback to custom message set in CMS or default message if none set
|
// fallback to custom message set in CMS or default message if none set
|
||||||
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
|
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFieldValidationOptions() {
|
public function getFieldValidationOptions() {
|
||||||
return new FieldList();
|
return new FieldList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRequired() {
|
public function getRequired() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getIcon() {
|
public function getIcon() {
|
||||||
return 'spamprotection/images/' . strtolower($this->class) . '.png';
|
return 'spamprotection/images/' . strtolower($this->class) . '.png';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function showInReports() {
|
public function showInReports() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user