diff --git a/code/EditableSpamProtectionField.php b/code/EditableSpamProtectionField.php index 7d1efbe..649bd9a 100644 --- a/code/EditableSpamProtectionField.php +++ b/code/EditableSpamProtectionField.php @@ -4,42 +4,25 @@ * 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 + * @package spamprotection */ class EditableSpamProtectionField extends EditableFormField { static $singular_name = 'Spam Protection Field'; + static $plural_name = 'Spam Protection Fields'; - function __construct( $record = null, $isSingleton = false ) { - - parent::__construct( $record, $isSingleton ); - } - function getFormField() { - return $this->createField(); - } - - function getFilterField() { - return $this->createField(true); - } - - function createField() { if($protector = SpamProtectorManager::get_spam_protector()) { if($protector) { $protector = new $protector(); - if($class = $protector->getFieldName()) { - return new $class($class, $this->Title); - } + return $protector->getFormField($this->Name, $this->Title, null); } } return false; } - - /** - * @return string - */ + public function Icon() { return 'spamprotection/images/' . strtolower($this->class) . '.png'; } diff --git a/code/SpamProtector.php b/code/SpamProtector.php index 52d8836..2312e7f 100644 --- a/code/SpamProtector.php +++ b/code/SpamProtector.php @@ -2,18 +2,17 @@ /** * Spam Protector base interface. All Protectors should implement this interface - * to ensure that they contain all the correct methods and we do not get too many - * odd missing function errors + * to ensure that they contain all the correct methods. * - * @package SpamProtection + * @package spamprotection */ interface SpamProtector { /** - * Return the name of the Field Associated with this protector + * Return the Field Associated with this protector */ - public function getFieldName(); + public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null); /** * Function required to handle dynamic feedback of the system. @@ -21,14 +20,4 @@ interface SpamProtector { */ 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); -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/code/SpamProtectorField.php b/code/SpamProtectorField.php index a2057d7..cc63146 100644 --- a/code/SpamProtectorField.php +++ b/code/SpamProtectorField.php @@ -1,44 +1,34 @@ spanControlCallbackObj = $callbackObject; - } - - /** - * Tell the callback object the submission is spam - */ - protected function markAsSpam() { - if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) { - $this->spanControlCallbackObj->markAsSpam($this->getForm()); - } - } + private $spamFieldMapping = array(); + /** - * Tell the callback object the submission is ham + * Set the fields to map spam protection too + * + * @param Array array of Field Names */ - protected function markAsHam() { - if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) { - $this->spanControlCallbackObj->markAsHam($this->getForm()); - } + public function setFieldMapping($array) { + $this->spamFieldMapping = $array; } - -} -?> + /** + * Get the fields that are mapped via spam protection + * + * @return Array + */ + public function getFieldMapping() { + return $this->spamFieldMapping; + } +} \ No newline at end of file diff --git a/code/SpamProtectorManager.php b/code/SpamProtectorManager.php index 7fdd1b9..22cf019 100644 --- a/code/SpamProtectorManager.php +++ b/code/SpamProtectorManager.php @@ -1,6 +1,7 @@ updateForm($form, $before, $fieldsToSpamServiceMapping); + $protector = new $protectorClass(); + $field = $protector->getFormField("Captcha", $title, null, $form, $rightTitle); + + if($field) { + // update the mapping + $field->setFieldMapping($fieldsToSpamServiceMapping); + + // add the form field + if($before && $form->Fields()->fieldByName($before)) { + $form->Fields()->insertBefore($field, $before); + } + else { + $form->Fields()->push($field); + } + } + } catch (Exception $e) { - user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING); + return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING); } - - if(!$check) return null; - - return $protector; } /** @@ -72,5 +85,4 @@ class SpamProtectorManager { $protector = new self::$spam_protector(); return $protector->sendFeedback($object, $feedback); } -} -?> +} \ No newline at end of file