Permalink
Browse files

API Refactor field mapping from FormSpamProtectionExtension into Spam…

…Protector

API EditableSpamProtectionField may now be configured for spam on an individual field basis
Translations added
  • Loading branch information...
1 parent b3af63e commit c050dcc05a57b1f6ad25bdbc6c794ad814f61cf7 @tractorcow tractorcow committed Feb 18, 2014
View
94 code/EditableSpamProtectionField.php
@@ -13,17 +13,95 @@ class EditableSpamProtectionField extends EditableFormField {
static $singular_name = 'Spam Protection Field';
static $plural_name = 'Spam Protection Fields';
+
+ /**
+ * Fields to include spam detection for
+ *
+ * @var array
+ * @config
+ */
+ private static $check_fields = array(
+ 'EditableEmailField',
+ 'EditableTextField',
+ 'EditableNumericField'
+ );
public function getFormField() {
- if($protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector')) {
- $protector = Injector::inst()->create($protector);
-
- return $protector->getFormField($this->Name, $this->Title, null);
+
+ // 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);
+ }
+
+ // 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;
- return false;
+ // 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() {
return new FieldList();
}
@@ -32,12 +110,12 @@ public function getRequired() {
return false;
}
- public function Icon() {
+ public function getIcon() {
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function showInReports() {
return false;
}
}
-}
+}
View
55 code/extensions/FormSpamProtectionExtension.php
@@ -8,11 +8,6 @@
*/
class FormSpamProtectionExtension extends Extension {
-
- /**
- * @var array
- */
- private $fieldMapping = array();
/**
* @config
@@ -45,13 +40,14 @@ class FormSpamProtectionExtension extends Extension {
'authorIp',
'authorId'
);
-
+
/**
- * Activates the spam protection module.
- *
- * @param array $options
+ * Instantiate a SpamProtector instance
+ *
+ * @param array $options Configuration options
+ * @return SpamProtector
*/
- public function enableSpamProtection($options = array()) {
+ public static function get_protector($options = null) {
// generate the spam protector
if(isset($options['protector'])) {
$protector = $options['protector'];
@@ -63,6 +59,15 @@ public function enableSpamProtection($options = array()) {
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_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)
if(isset($options['name'])) {
@@ -79,8 +84,9 @@ public function enableSpamProtection($options = array()) {
}
// set custom mapping on this form
+ $protector = self::get_protector($options);
if(isset($options['mapping'])) {
- $this->fieldMapping = $options['mapping'];
+ $protector->setFieldMapping($options['mapping']);
}
// add the form field
@@ -92,29 +98,4 @@ public function enableSpamProtection($options = array()) {
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;
- }
-}
+}
View
12 code/interfaces/SpamProtector.php
@@ -25,6 +25,16 @@
* @param string $name
* @param string $title
* @param mixed $value
+ * @return FormField The resulting field
*/
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);
+}
View
0 lang/_manifest_exclude
No changes.
View
6 lang/en.yml
@@ -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'
View
9 tests/FormSpamProtectionExtensionTest.php
@@ -64,6 +64,9 @@ class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, Tes
public function getFormField($name = null, $title = null, $value = null) {
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) {
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) {
return new TextField($name, 'Foo', $value);
}
+
+ public function setFieldMapping($fieldMapping) {}
+
}

0 comments on commit c050dcc

Please sign in to comment.