*/ WBClass::load('WBPAT_Forms_Rule'); /** * PatForms Rule Duplicate Check For E-Mail-Adresses * * Helps to avoid duplicate e-mail tables or user data * * @version 0.3.0 * @package Wombat * @subpackage patForms */ class patForms_Rule_EmailDuplicate extends WBPAT_Forms_Rule { /** * default config values * - table * - id * * @var array */ protected $config = array( 'table' => '__user', 'id' => null ); /** * Initialize validation codes using gettext * * @access protected * @return bool $success Always returns true. * @see $attributeDefaults */ public function loadValidatiorErrorCodes() { $this->validatorErrorCodes = array( 1 => patI18n::dgettext('wombat', 'The entered value must not be empty.'), 2 => patI18n::dgettext('wombat', 'The @ sign is missing.'), 3 => patI18n::dgettext('wombat', 'This email address is already in use, try another one..'), ); return true; } /** * Method called by patForms or any patForms_Element to validate the * element or the form. * * @param patForms_Element $form object * @return bool true on sucess */ public function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION) { $value = $element->getValue(); if (empty($value)) { $this->addValidationError(1); return false; } // check for @ if (strstr($value, '@') === false) { $this->addValidationError(2); return false; } switch ($this->config['table']) { case '__user': return $this->applyRule4UserStorage($value); break; default: break; } return $this->applyRule4Table($value); } private function applyRule4Table($value) { list($local, $domain) = explode('@', $value, 2); /** @var $table WBDatasource_Table */ $table = WBClass::create('WBDatasource_Table'); $clause = array(); $clause[] = array( 'field' => 'emaillocal', 'value' => $local ); $clause[] = array( 'field' => 'emaildomain', 'value' => $domain ); if (!empty($this->config['id'])) { $clause[] = array( 'field' => $table->getIdentifier($this->config['table'], true), 'relation' => 'not', 'value' => $this->config['id'] ); } $others = $table->getIds($this->config['table'], null, $clause); if (count($others)) { $this->addValidationError(3); return false; } return true; } private function applyRule4UserStorage($value) { // user storage module to check WBClass::load('WBUser'); /** @var WBUser_Storage */ $stor = WBUser::getCurrent()->getStorageModule(); $check = array( 'email' => $value ); $id = $stor->find($check, false); if (empty($id)) { return true; } if (empty($this->config['id'])) { $this->addValidationError(3); return false; } if (count($id) > 1) { $this->addValidationError(3); return false; } if ($this->config['id'] == $id[0]) { return true; } $this->addValidationError(3); return false; } }