mirror of
https://github.com/silverstripe/silverstripe-spamprotection.git
synced 2024-10-22 14:05:59 +02:00
FEATURE: added ability to create a spam protector field in user defined forms
This commit is contained in:
parent
ca8c02b531
commit
05208f0aba
6
INSTALL
6
INSTALL
@ -48,6 +48,6 @@ $protector = SpamProtecterManager::update_form($form, 'Message');
|
||||
What does this do?
|
||||
------------------
|
||||
|
||||
This code add an instance of a 'SpamProtecterField' class specified in SETTING UP THE MODULE section. The newly created field will have MollomField field. The first parameter is a Form object in which the field will be added into and the second parameter tells SpamProtecterManager to place the new field before a field named 'Message'. The last parameter is an object that get notified when the spam verification is done and given the form object.
|
||||
|
||||
The purpose of having callback object is we can process the submission differently according to the spam status of the submission. For example, put any submission with 'spam' status into spam queue.
|
||||
This code add an instance of a 'SpamProtecterField' class specified in SETTING UP THE MODULE section. The newly created field will have
|
||||
MollomField field. The first parameter is a Form object in which the field will be added into and the second parameter tells
|
||||
SpamProtecterManager to place the new field before a field named 'Message'.
|
||||
|
84
code/EditableSpamProtectionField.php
Normal file
84
code/EditableSpamProtectionField.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Editable Spam Protecter Field. Used with the User Defined Forms module (if
|
||||
* installed) to allow the user to have captcha fields with their custom forms
|
||||
*
|
||||
* @package SpamProtection
|
||||
*/
|
||||
|
||||
class EditableSpamProtectionField extends EditableFormField {
|
||||
|
||||
static $db = array(
|
||||
|
||||
);
|
||||
|
||||
static $singular_name = 'Spam Protection Field';
|
||||
static $plural_name = 'Spam Protection Fields';
|
||||
|
||||
function __construct( $record = null, $isSingleton = false ) {
|
||||
|
||||
parent::__construct( $record, $isSingleton );
|
||||
}
|
||||
|
||||
function ExtraOptions() {
|
||||
|
||||
// eventually replace hard-coded "Fields"?
|
||||
$baseName = "Fields[$this->ID]";
|
||||
|
||||
$extraFields = new FieldSet();
|
||||
|
||||
foreach( parent::ExtraOptions() as $extraField )
|
||||
$extraFields->push( $extraField );
|
||||
|
||||
if( $this->readonly )
|
||||
$extraFields = $extraFields->makeReadonly();
|
||||
|
||||
return $extraFields;
|
||||
}
|
||||
|
||||
function populateFromPostData($data) {
|
||||
parent::populateFromPostData($data);
|
||||
}
|
||||
|
||||
function getFormField() {
|
||||
return $this->createField();
|
||||
}
|
||||
|
||||
function getFilterField() {
|
||||
return $this->createField(true);
|
||||
}
|
||||
|
||||
function createField() {
|
||||
if($protector = SpamProtecterManager::get_spam_protecter()) {
|
||||
if($protector) {
|
||||
$protector = new $protector();
|
||||
if($class = $protector->getFieldName()) {
|
||||
$spamProtection = new $class($class, $this->Title);
|
||||
if($spamProtection) {
|
||||
// set field mapping for all the fields in this form.
|
||||
// fields should have the same ParentID as this
|
||||
$fields = DataObject::get("EditableTextField", "ParentID = '$this->ParentID'");
|
||||
$fields = ($fields) ? $fields->toArray('Name') : null;
|
||||
|
||||
// @TODO get FieldMapping Working.
|
||||
$spamProtection->setFieldMapping(null, $fields);
|
||||
return $spamProtection;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Populates the default fields.
|
||||
*/
|
||||
function DefaultField() {
|
||||
return "";
|
||||
}
|
||||
|
||||
function EditSegment() {
|
||||
return $this->renderWith( $this->class );
|
||||
}
|
||||
}
|
||||
?>
|
@ -15,6 +15,13 @@ class SpamProtecterManager {
|
||||
self::$spam_protecter = $protecter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the spam protector class
|
||||
*/
|
||||
static function get_spam_protecter() {
|
||||
return self::$spam_protecter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the spam protecter field to a form
|
||||
* @param Form the form that the protecter field added into
|
||||
|
@ -10,10 +10,25 @@
|
||||
|
||||
interface SpamProtector {
|
||||
|
||||
/**
|
||||
* Return the name of the Field Associated with this protector
|
||||
*/
|
||||
public function getFieldName();
|
||||
|
||||
/**
|
||||
* Function required to handle dynamic feedback of the system.
|
||||
* if unneeded just return true
|
||||
*/
|
||||
public function sendFeedback($object = null, $feedback = "");
|
||||
|
||||
/**
|
||||
* Updates the form with the given protection
|
||||
*/
|
||||
public function updateForm($form, $before = null, $fieldsToSpamServiceMapping = null);
|
||||
|
||||
/**
|
||||
* Set which fields need to be mapped for protection
|
||||
*/
|
||||
public function setFieldMapping($fieldToPostTitle, $fieldsToPostBody = null, $fieldToAuthorName = null, $fieldToAuthorUrl = null, $fieldToAuthorEmail = null, $fieldToAuthorOpenId = null);
|
||||
}
|
||||
?>
|
BIN
images/editablespamprotectionfield.png
Normal file
BIN
images/editablespamprotectionfield.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
20
templates/EditableSpamProtectionField.ss
Normal file
20
templates/EditableSpamProtectionField.ss
Normal file
@ -0,0 +1,20 @@
|
||||
<div class="EditableSpamProtectionField EditableFormField" id="$Name.Attr">
|
||||
<div class="FieldInfo">
|
||||
<img class="handle" src="sapphire/images/drag.gif" alt="<% _t('DRAG', 'Drag to rearrange order of fields') %>" />
|
||||
<img class="icon" src="spamprotection/images/editablespamprotectionfield.png" alt="<% _t('SPAMPROTECTIONFIELD', 'Spam Protection Field') %>" />
|
||||
$TitleField
|
||||
<a class="toggler" href="#" title="<% _t('MORE', 'More options') %>"><img src="cms/images/edit.gif" alt="<% _t('MORE', 'More options') %>" /></a>
|
||||
<a class="delete" href="#" title="<% _t('DELETE', 'Delete this field') %>"><img src="cms/images/delete.gif" alt="<% _t('DELETE', 'Delete this field') %>" /></a>
|
||||
</div>
|
||||
<div class="ExtraOptions" id="$Name.Attr-extraOptions">
|
||||
<div class="FieldDefault" id="$Name.Attr-fieldDefault">
|
||||
$DefaultField
|
||||
</div>
|
||||
<% control ExtraOptions %>
|
||||
$FieldHolder
|
||||
<% end_control %>
|
||||
</div>
|
||||
<input type="hidden" name="$Name.Attr[CustomParameter]" value="$CustomParameter" />
|
||||
<input type="hidden" name="$Name.Attr[Type]" value="EditableSpamProtectionField" />
|
||||
<input type="hidden" name="$Name.Attr[Sort]" value="-1" />
|
||||
</div>
|
Loading…
Reference in New Issue
Block a user