return $aliases;
}
+ static function findByLocalpart($localpart) {
+ $dao = CRM_Core_DAO::executeQuery(
+ 'SELECT * FROM member_email_alias WHERE localpart=%1',
+ array( 1 => array($localpart, 'String') )
+ );
+
+ if($dao->fetch()) {
+ $alias = new CRM_Memberdashboard_BAO_EmailAlias(
+ array(
+ 'id' => $dao->id,
+ 'contact_id' => $dao->contact_id,
+ 'localpart' => $dao->localpart,
+ 'forward' => $dao->forward
+ )
+ );
+ } else {
+ $alias = NULL;
+ }
+
+ $dao->free();
+
+ return $alias;
+ }
+
static function countForContact($contactId) {
$dao = CRM_Core_DAO::executeQuery(
'SELECT COUNT(*) AS count FROM member_email_alias WHERE contact_id=%1',
if(CRM_Utils_Rule::email($forward)) {
if(CRM_Utils_Rule::email("$localpart@example.com")) {
- if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
- try {
- $params = array(
- 'contact_id' => $contactId,
- 'localpart' => $localpart,
- 'forward' => $forward
- );
- CRM_Memberdashboard_BAO_EmailAlias::create($params);
- CRM_Core_Session::setStatus('Email alias added!',
- 'Success', 'success');
- } catch (Exception $e) {
- CRM_Core_Session::setStatus('Failed to create email alias',
+ if(CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart) == NULL) {
+ if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
+ try {
+ $params = array(
+ 'contact_id' => $contactId,
+ 'localpart' => $localpart,
+ 'forward' => $forward
+ );
+ CRM_Memberdashboard_BAO_EmailAlias::create($params);
+ CRM_Core_Session::setStatus('Email alias added!',
+ 'Success', 'success');
+ } catch (Exception $e) {
+ CRM_Core_Session::setStatus('Failed to create email alias',
+ 'Error', 'error');
+ }
+ } else {
+ CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
'Error', 'error');
}
} else {
- CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
+ CRM_Core_Session::setStatus("Local part is already in use: $localpart",
'Error', 'error');
}
} else {
$error .= '<li>You cannot alter email aliases that don\'t belong to you!</li>';
} else if(CRM_Utils_Rule::email($forward)) {
if(CRM_Utils_Rule::email("$localpart@example.com")) {
- $alias->localpart = $localpart;
- $alias->forward = $forward;
- $alias->save();
+ $existingAlias = CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart);
+ if($existingAlias == NULL || $existingAlias->id == $id) {
+ $alias->localpart = $localpart;
+ $alias->forward = $forward;
+ $alias->save();
+ } else {
+ $error .= "<li>Local part already in use: $localpart</li>";
+ }
} else {
$error .= "<li>Invalid local part: $localpart</li>";
}