Skip to content

Commit

Permalink
Merge pull request #31 from helpfulrobot/convert-to-psr-2
Browse files Browse the repository at this point in the history
Converted to PSR-2
  • Loading branch information
dhensby committed Nov 25, 2015
2 parents 3495ecf + 7ee2dce commit 965389d
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 398 deletions.
253 changes: 132 additions & 121 deletions code/EditableSpamProtectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,136 @@
*
* @package spamprotection
*/
if(class_exists('EditableFormField')) {

class EditableSpamProtectionField extends EditableFormField {

private static $singular_name = 'Spam Protection Field';

private 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() {
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if(!$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 (!$protector) return $fields;

if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
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 validateField($data, $form) {
$formField = $this->getFormField();
if (!$formField->validate($form->getValidator())) {
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
}
}

public function getFieldValidationOptions() {
return new FieldList();
}

public function getRequired() {
return false;
}

public function getIcon() {
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}

public function showInReports() {
return false;
}
}
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';

private 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()
{
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$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 (!$protector) {
return $fields;
}

if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
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 validateField($data, $form)
{
$formField = $this->getFormField();
if (!$formField->validate($form->getValidator())) {
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
}
}

public function getFieldValidationOptions()
{
return new FieldList();
}

public function getRequired()
{
return false;
}

public function getIcon()
{
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}

public function showInReports()
{
return false;
}
}
}
53 changes: 28 additions & 25 deletions code/SpamProtectorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,36 @@
* @deprecated 1.0
*/

class SpamProtectorManager {
private static $spam_protector = null;
class SpamProtectorManager
{
private static $spam_protector = null;

public static function set_spam_protector($protector) {
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
public static function set_spam_protector($protector)
{
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);

self::$spam_protector = $protector;
}
self::$spam_protector = $protector;
}

public static function get_spam_protector() {
Deprecation::notice('1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
public static function get_spam_protector()
{
Deprecation::notice('1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');

return self::$spam_protector;
}

public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
Deprecation::notice('1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);
return self::$spam_protector;
}

public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
{
Deprecation::notice('1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);

return $form->enableSpamProtection();
}
}
return $form->enableSpamProtection();
}
}
37 changes: 19 additions & 18 deletions code/extensions/CommentSpamProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
* @package spamprotection
*/

class CommentSpamProtection extends Extension {

public function alterCommentForm(&$form) {
$form->enableSpamProtection(array(
'name' => 'IsSpam',
'mapping' => array(
'Name' => 'authorName',
'Email' => 'authorEmail',
'URL' => 'authorUrl',
'Comment' => 'body',
'ReturnURL' => 'contextUrl'
),
'checks' => array(
'spam',
'profanity'
)
));
}
class CommentSpamProtection extends Extension
{
public function alterCommentForm(&$form)
{
$form->enableSpamProtection(array(
'name' => 'IsSpam',
'mapping' => array(
'Name' => 'authorName',
'Email' => 'authorEmail',
'URL' => 'authorUrl',
'Comment' => 'body',
'ReturnURL' => 'contextUrl'
),
'checks' => array(
'spam',
'profanity'
)
));
}
}
Loading

0 comments on commit 965389d

Please sign in to comment.