Skip to content

Commit

Permalink
FIX Compatibility with userforms 3/4 getCMSFields, remove deprecated …
Browse files Browse the repository at this point in the history
…getSettings use etc
  • Loading branch information
robbieaverill committed Jul 7, 2017
1 parent 5cc9312 commit 3e7f021
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
85 changes: 73 additions & 12 deletions code/EditableSpamProtectionField.php
Expand Up @@ -24,6 +24,10 @@ class EditableSpamProtectionField extends EditableFormField
'EditableNumericField'
);

private static $db = array(
'SpamFieldSettings' => 'Text'
);

/**
* @var FormField
*/
Expand All @@ -45,7 +49,7 @@ public function getFormField()
$fieldMapping = array();
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$spamField = $this->getSetting($mapSetting);
$spamField = $this->spamMapValue($mapSetting);
$fieldMapping[$otherField->Name] = $spamField;
}
$protector->setFieldMapping($fieldMapping);
Expand Down Expand Up @@ -89,9 +93,47 @@ protected function getCandidateFields()
->exclude('Title', ''); // Ignore this field and those without titles
}

/**
* This method is in place for userforms 2.x
*
* @deprecated 3.0 Please use {@link getCMSFields()} instead
*/
public function getFieldConfiguration()
{
$fields = parent::getFieldConfiguration();
return $this->getCMSFields();
}

/**
* Write the spam field mapping values to a serialised DB field
*
* {@inheritDoc}
*/
public function onBeforeWrite()
{
$fieldMap = Convert::json2array($this->SpamFieldSettings);
if (empty($fieldMap)) {
$fieldMap = array();
}

foreach ($this->record as $key => $value) {
if (substr($key, 0, 8) === 'spammap-') {
$fieldMap[substr($key, 8)] = $value;
}
}
$this->setField('SpamFieldSettings', Convert::raw2json($fieldMap));

return parent::onBeforeWrite();
}

/**
* Used in userforms 3.x and above
*
* {@inheritDoc}
*/
public function getCMSFields()
{
/** @var FieldList $fields */
$fields = parent::getCMSFields();

// Get protector
$protector = FormSpamProtectionExtension::get_protector();
Expand All @@ -104,32 +146,51 @@ public function getFieldConfiguration()
}

// 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'
));
$mapGroup = FieldGroup::create()
->setTitle(_t('EditableSpamProtectionField.SPAMFIELDMAPPING', 'Spam Field Mapping'))
->setName('SpamFieldMapping')
->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),
'spammap-' . $mapSetting,
$otherField->Title,
$mappableFieldsMerged,
$this->getSetting($mapSetting)
$this->spamMapValue($mapSetting)
)->setEmptyString('');
$mapGroup->push($fieldOption);
}
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
$fields->addFieldToTab('Root.Main', $mapGroup);

return $fields;
}

/**
* Try to retrieve a value for the given spam field map name from the serialised data
*
* @param string $mapSetting
* @return string
*/
public function spamMapValue($mapSetting)
{
$map = Convert::json2array($this->SpamFieldSettings);
if (empty($map)) {
$map = array();
}

if (array_key_exists($mapSetting, $map)) {
return $map[$mapSetting];
}
return '';
}

/**
* Using custom validateField method
* as Spam Protection Field implementations may have their own error messages
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -24,4 +24,4 @@
},
"extra": [],
"license": "BSD-3-Clause"
}
}
19 changes: 19 additions & 0 deletions tests/EditableSpamProtectionFieldTest.php
Expand Up @@ -81,6 +81,25 @@ public function testValidateFieldAddsDefaultError()
$formFieldMock->validateField(array('MyField' => null), $formMock);
}

public function testSpamConfigurationShowsInCms()
{
$field = $this->getEditableFormFieldMock();
$fields = $field->getCMSFields();

$this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping'));
}

public function testSpamMapSettingsAreSerialised()
{
$field = $this->getEditableFormFieldMock();
$field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz'));
$field->write();

$this->assertJson($field->SpamFieldSettings);
$this->assertSame('bar', $field->spamMapValue('foo'));
$this->assertSame('baz', $field->spamMapValue('bar'));
}

protected function getFormMock()
{
$formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
Expand Down

0 comments on commit 3e7f021

Please sign in to comment.