mirror of
https://github.com/silverstripe/silverstripe-spamprotection.git
synced 2024-10-22 14:05:59 +02:00
API Refactor field mapping from FormSpamProtectionExtension into SpamProtector
API EditableSpamProtectionField may now be configured for spam on an individual field basis Translations added
This commit is contained in:
parent
b3af63e387
commit
c050dcc05a
@ -14,14 +14,92 @@ if(class_exists('EditableFormField')) {
|
|||||||
|
|
||||||
static $plural_name = 'Spam Protection Fields';
|
static $plural_name = 'Spam Protection Fields';
|
||||||
|
|
||||||
public function getFormField() {
|
/**
|
||||||
if($protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector')) {
|
* Fields to include spam detection for
|
||||||
$protector = Injector::inst()->create($protector);
|
*
|
||||||
|
* @var array
|
||||||
|
* @config
|
||||||
|
*/
|
||||||
|
private static $check_fields = array(
|
||||||
|
'EditableEmailField',
|
||||||
|
'EditableTextField',
|
||||||
|
'EditableNumericField'
|
||||||
|
);
|
||||||
|
|
||||||
return $protector->getFormField($this->Name, $this->Title, null);
|
public function getFormField() {
|
||||||
|
|
||||||
|
// Get protector
|
||||||
|
$protector = FormSpamProtectionExtension::get_protector();
|
||||||
|
if(empty($protector)) return false;
|
||||||
|
|
||||||
|
// Extract saved field mappings and update this field.
|
||||||
|
$fieldMapping = array();
|
||||||
|
foreach($this->getCandidateFields() as $otherField) {
|
||||||
|
$mapSetting = "Map-{$otherField->Name}";
|
||||||
|
$spamField = $this->getSetting($mapSetting);
|
||||||
|
$fieldMapping[$otherField->Name] = $spamField;
|
||||||
|
}
|
||||||
|
$protector->setFieldMapping($fieldMapping);
|
||||||
|
|
||||||
|
// Generate field
|
||||||
|
return $protector->getFormField($this->Name, $this->Title, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of all candidate spam detectable fields on this field's form
|
||||||
|
*
|
||||||
|
* @return DataList
|
||||||
|
*/
|
||||||
|
protected function getCandidateFields() {
|
||||||
|
|
||||||
|
// Get list of all configured classes available for spam detection
|
||||||
|
$types = self::config()->check_fields;
|
||||||
|
$typesInherit = array();
|
||||||
|
foreach ($types as $type) {
|
||||||
|
$subTypes = ClassInfo::subclassesFor($type);
|
||||||
|
$typesInherit = array_merge($typesInherit, $subTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
// Get all candidates of the above types
|
||||||
|
return $this
|
||||||
|
->Parent()
|
||||||
|
->Fields()
|
||||||
|
->filter('ClassName', $typesInherit)
|
||||||
|
->exclude('Title', ''); // Ignore this field and those without titles
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFieldConfiguration() {
|
||||||
|
$fields = parent::getFieldConfiguration();
|
||||||
|
|
||||||
|
// Get protector
|
||||||
|
$protector = FormSpamProtectionExtension::get_protector();
|
||||||
|
if (empty($protector)) return fields;
|
||||||
|
|
||||||
|
// Each other text field in this group can be assigned a field mapping
|
||||||
|
$mapGroup = FieldGroup::create(_t(
|
||||||
|
'EditableSpamProtectionField.SPAMFIELDMAPPING',
|
||||||
|
'Spam Field Mapping'
|
||||||
|
))->setDescription(_t(
|
||||||
|
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
|
||||||
|
'Select the form fields that correspond to any relevant spam protection identifiers'
|
||||||
|
));
|
||||||
|
|
||||||
|
// Generate field specific settings
|
||||||
|
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
|
||||||
|
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
|
||||||
|
foreach ($this->getCandidateFields() as $otherField) {
|
||||||
|
$mapSetting = "Map-{$otherField->Name}";
|
||||||
|
$fieldOption = DropdownField::create(
|
||||||
|
$this->getSettingName($mapSetting),
|
||||||
|
$otherField->Title,
|
||||||
|
$mappableFieldsMerged,
|
||||||
|
$this->getSetting($mapSetting)
|
||||||
|
)->setEmptyString('');
|
||||||
|
$mapGroup->push($fieldOption);
|
||||||
|
}
|
||||||
|
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
|
||||||
|
|
||||||
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFieldValidationOptions() {
|
public function getFieldValidationOptions() {
|
||||||
@ -32,7 +110,7 @@ if(class_exists('EditableFormField')) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Icon() {
|
public function getIcon() {
|
||||||
return 'spamprotection/images/' . strtolower($this->class) . '.png';
|
return 'spamprotection/images/' . strtolower($this->class) . '.png';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,11 +9,6 @@
|
|||||||
|
|
||||||
class FormSpamProtectionExtension extends Extension {
|
class FormSpamProtectionExtension extends Extension {
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $fieldMapping = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @config
|
* @config
|
||||||
*
|
*
|
||||||
@ -47,11 +42,12 @@ class FormSpamProtectionExtension extends Extension {
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activates the spam protection module.
|
* Instantiate a SpamProtector instance
|
||||||
*
|
*
|
||||||
* @param array $options
|
* @param array $options Configuration options
|
||||||
|
* @return SpamProtector
|
||||||
*/
|
*/
|
||||||
public function enableSpamProtection($options = array()) {
|
public static function get_protector($options = null) {
|
||||||
// generate the spam protector
|
// generate the spam protector
|
||||||
if(isset($options['protector'])) {
|
if(isset($options['protector'])) {
|
||||||
$protector = $options['protector'];
|
$protector = $options['protector'];
|
||||||
@ -63,6 +59,15 @@ class FormSpamProtectionExtension extends Extension {
|
|||||||
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
|
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
|
||||||
$protector = Injector::inst()->create($protector);
|
$protector = Injector::inst()->create($protector);
|
||||||
}
|
}
|
||||||
|
return $protector;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Activates the spam protection module.
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
*/
|
||||||
|
public function enableSpamProtection($options = array()) {
|
||||||
|
|
||||||
// captcha form field name (must be unique)
|
// captcha form field name (must be unique)
|
||||||
if(isset($options['name'])) {
|
if(isset($options['name'])) {
|
||||||
@ -79,8 +84,9 @@ class FormSpamProtectionExtension extends Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set custom mapping on this form
|
// set custom mapping on this form
|
||||||
|
$protector = self::get_protector($options);
|
||||||
if(isset($options['mapping'])) {
|
if(isset($options['mapping'])) {
|
||||||
$this->fieldMapping = $options['mapping'];
|
$protector->setFieldMapping($options['mapping']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the form field
|
// add the form field
|
||||||
@ -92,29 +98,4 @@ class FormSpamProtectionExtension extends Extension {
|
|||||||
|
|
||||||
return $this->owner;
|
return $this->owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasSpamProtectionMapping() {
|
|
||||||
return ($this->fieldMapping);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getSpamMappedData() {
|
|
||||||
if($this->fieldMapping) {
|
|
||||||
$result = array();
|
|
||||||
$data = $this->owner->getData();
|
|
||||||
|
|
||||||
foreach($this->fieldMapping as $fieldName => $mappedName) {
|
|
||||||
$result[$mappedName] = (isset($data[$fieldName])) ? $data[$fieldName] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -25,6 +25,16 @@ interface SpamProtector {
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
|
* @return FormField The resulting field
|
||||||
*/
|
*/
|
||||||
public function getFormField($name = null, $title = null, $value = null);
|
public function getFormField($name = null, $title = null, $value = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the fields to map spam protection too
|
||||||
|
*
|
||||||
|
* @param array $fieldMapping array of Field Names, where the indexes of the array are
|
||||||
|
* the field names of the form and the values are the standard spamprotection
|
||||||
|
* fields used by the protector
|
||||||
|
*/
|
||||||
|
public function setFieldMapping($fieldMapping);
|
||||||
}
|
}
|
0
lang/_manifest_exclude
Normal file
0
lang/_manifest_exclude
Normal file
6
lang/en.yml
Normal file
6
lang/en.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
en:
|
||||||
|
EditableSpamProtectionField:
|
||||||
|
SINGULARNAME: 'Spam Protection Field'
|
||||||
|
PLURALNAME: 'Spam Protection Fields'
|
||||||
|
SPAMFIELDMAPPING: 'Spam Field Mapping'
|
||||||
|
SPAMFIELDMAPPINGDESCRIPTION: 'Select the form fields that correspond to any relevant spam protection identifiers'
|
@ -64,6 +64,9 @@ class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, Tes
|
|||||||
public function getFormField($name = null, $title = null, $value = null) {
|
public function getFormField($name = null, $title = null, $value = null) {
|
||||||
return new TextField($name, $title, $value);
|
return new TextField($name, $title, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,6 +79,9 @@ class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, Tes
|
|||||||
public function getFormField($name = null, $title = null, $value = null) {
|
public function getFormField($name = null, $title = null, $value = null) {
|
||||||
return new TextField($name, $this->title, $value);
|
return new TextField($name, $this->title, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,4 +92,7 @@ class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, Tes
|
|||||||
public function getFormField($name = null, $title = null, $value = null) {
|
public function getFormField($name = null, $title = null, $value = null) {
|
||||||
return new TextField($name, 'Foo', $value);
|
return new TextField($name, 'Foo', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFieldMapping($fieldMapping) {}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user