From 7f2134396246ff937d1c856c4bb4123b66512fb2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 6 Nov 2014 14:05:53 -0500 Subject: [PATCH] benefits: Add email alias CRUD. * CRM/Memberdashboard/BAO/EmailAlias.php: New file. * CRM/Memberdashboard/Page/Benefits.php (CRM_Memberdashboard_Page_Benefits) [loadEmailAliases]: Delegate to BAO. [postProcess, add, edit]: New methods. [padEmailAliases]: Delete. [run]: Handle POST requests. * templates/CRM/Memberdashboard/Page/Benefits.tpl: Update edit/add forms. --- CRM/Memberdashboard/BAO/EmailAlias.php | 141 ++++++++++++++++++ CRM/Memberdashboard/Page/Benefits.php | 128 +++++++++++++--- .../CRM/Memberdashboard/Page/Benefits.tpl | 63 +++++--- 3 files changed, 288 insertions(+), 44 deletions(-) create mode 100644 CRM/Memberdashboard/BAO/EmailAlias.php diff --git a/CRM/Memberdashboard/BAO/EmailAlias.php b/CRM/Memberdashboard/BAO/EmailAlias.php new file mode 100644 index 0000000..c782c56 --- /dev/null +++ b/CRM/Memberdashboard/BAO/EmailAlias.php @@ -0,0 +1,141 @@ +. + */ + +// Safe array reference with default value. +function array_ref($array, $key, $default = NULL) { + return isset($array[$key]) ? $array[$key] : $default; +} + +class CRM_Memberdashboard_BAO_EmailAlias { + public $id = NULL; + public $contactId = NULL; + public $localpart = ''; + public $forward = ''; + + static function create($params) { + $alias = new CRM_Memberdashboard_BAO_EmailAlias($params); + $alias->save(); + return $alias; + } + + static function destroy($id) { + $dao = CRM_Core_DAO::executeQuery( + 'DELETE FROM member_email_alias WHERE id=%1', + array( 1 => array($id, 'Integer') ) + ); + + $dao->free(); + } + + static function find($ids) { + $dao = CRM_Core_DAO::executeQuery( + "SELECT * FROM member_email_alias WHERE id IN (" . + CRM_Core_DAO::escapeString(implode(',', $ids)) . ")" + ); + $aliases = array(); + + while($dao->fetch()) { + $aliases[$dao->id] = new CRM_Memberdashboard_BAO_EmailAlias( + array( + 'id' => $dao->id, + 'contact_id' => $dao->contact_id, + 'localpart' => $dao->localpart, + 'forward' => $dao->forward + ) + ); + } + + $dao->free(); + + return $aliases; + } + + static function countForContact($contactId) { + $dao = CRM_Core_DAO::executeQuery( + 'SELECT COUNT(*) AS count FROM member_email_alias WHERE contact_id=%1', + array( 1 => array($contactId, 'Integer') ) + ); + + $dao->fetch(); + $count = $dao->count; + $dao->free(); + + return $count; + } + + static function allForContact($contactId) { + $dao = CRM_Core_DAO::executeQuery( + 'SELECT * FROM member_email_alias WHERE contact_id=%1', + array( 1 => array($contactId, 'Integer') ) + ); + $aliases = array(); + + while($dao->fetch()) { + $aliases[] = new CRM_Memberdashboard_BAO_EmailAlias( + array( + 'id' => $dao->id, + 'contact_id' => $dao->contact_id, + 'localpart' => $dao->localpart, + 'forward' => $dao->forward + ) + ); + } + + $dao->free(); + + return $aliases; + } + + function __construct($params) { + $this->id = array_ref($params, 'id'); + $this->contactId = array_ref($params, 'contact_id'); + $this->localpart = array_ref($params, 'localpart'); + $this->forward = array_ref($params, 'forward'); + } + + function insert() { + CRM_Core_DAO::executeQuery( + 'INSERT INTO member_email_alias (contact_id, localpart, forward) VALUES (%1, %2, %3)', + array(1 => array($this->contactId, 'Integer'), + 2 => array($this->localpart, 'String'), + 3 => array($this->forward, 'String')) + ); + } + + function update() { + CRM_Core_DAO::executeQuery( + 'UPDATE member_email_alias SET contact_id=%1, localpart=%2, forward=%3 WHERE id=%4', + array(1 => array($this->contactId, 'Integer'), + 2 => array($this->localpart, 'String'), + 3 => array($this->forward, 'String'), + 4 => array($this->id, 'Integer')) + ); + } + + function save() { + if($this->id) { + $this->update(); + } else { + $this->insert(); + } + } +} diff --git a/CRM/Memberdashboard/Page/Benefits.php b/CRM/Memberdashboard/Page/Benefits.php index 286ecd6..9c82490 100644 --- a/CRM/Memberdashboard/Page/Benefits.php +++ b/CRM/Memberdashboard/Page/Benefits.php @@ -1,38 +1,113 @@ . + */ require_once 'CRM/Memberdashboard/Page.php'; class CRM_Memberdashboard_Page_Benefits extends CRM_Memberdashboard_Page { function loadEmailAliases() { - $dao = CRM_Core_DAO::executeQuery( - 'SELECT * FROM member_email_alias WHERE contact_id=%1', - array( 1 => array($this->contact['id'], 'Integer') ) - ); - $rows = array(); - - while($dao->fetch()) { - $rows[] = array( - 'id' => $dao->id, - 'contact_id' => $dao->contact_id, - 'localpart' => $dao->localpart, - 'forward' => $dao->forward - ); + return CRM_Memberdashboard_BAO_EmailAlias::allForContact($this->contact['id']); + } + + function postProcess() { + $action = $_REQUEST['action']; + + switch($action) { + case 'add': + $this->add(); + break; + + case 'edit': + $this->edit(); + break; } + } - $dao->free(); + function add() { + $contactId = $this->contact['id']; + $localpart = $_POST['localpart']; + $forward = $_POST['forward']; + $count = CRM_Memberdashboard_BAO_EmailAlias::countForContact($contactId); - return $rows; + if(CRM_Utils_Rule::email($forward)) { + 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('Invalid email forwarding address', + 'Error', 'error'); + } } - function padEmailAliases($emailAliases) { - $nil = array( - 'id' => NULL, - 'contact_id' => NULL, - 'localpart' => '', - 'forward' => '', - ); + function edit() { + $ids = $_POST['ids']; + $localparts = $_POST['localparts']; + $forwards = $_POST['forwards']; + $delete = isset($_POST['delete']) ? $_POST['delete'] : array(); + $aliases = CRM_Memberdashboard_BAO_EmailAlias::find($ids); + $error = ''; + + for($i = 0; $i < count($ids); $i++) { + $id = $ids[$i]; + $localpart = $localparts[$i]; + $forward = $forwards[$i]; - return array_pad($emailAliases, MEMBERDASHBOARD_MAX_EMAIL_ALIASES, $nil); + if(isset($delete[$id]) && $delete[$id] == 'delete') { + CRM_Memberdashboard_BAO_EmailAlias::destroy($id); + } else { + $alias = $aliases[$id]; + + // Make sure sneaky users can't edit other people's aliases. + if($alias->contactId != $this->contact['id']) { + $error .= '
  • You cannot alter email aliases that don\'t belong to you!
  • '; + } else if(CRM_Utils_Rule::email($forward)) { + $alias->localpart = $localpart; + $alias->forward = $forward; + $alias->save(); + } else { + $error .= "
  • Invalid email forwarding address: $forward
  • "; + } + } + } + + if(empty($error)) { + CRM_Core_Session::setStatus("Email aliases updated!", 'Success', 'success'); + } else { + CRM_Core_Session::setStatus("", 'Error', 'error'); + } } function run() { @@ -40,7 +115,12 @@ class CRM_Memberdashboard_Page_Benefits extends CRM_Memberdashboard_Page { // Ugly global variable for the current Drupal user. global $user; - $emailAliases = $this->padEmailAliases($this->loadEmailAliases()); + // create/update/delete email aliases. + if($this->isPOST()) { + $this->postProcess(); + } + + $emailAliases = $this->loadEmailAliases(); CRM_Utils_System::setTitle(ts('Benefits')); diff --git a/templates/CRM/Memberdashboard/Page/Benefits.tpl b/templates/CRM/Memberdashboard/Page/Benefits.tpl index e40c777..8cfa196 100644 --- a/templates/CRM/Memberdashboard/Page/Benefits.tpl +++ b/templates/CRM/Memberdashboard/Page/Benefits.tpl @@ -63,26 +63,49 @@ take effect.

    - - - - - - - - - {foreach from=$emailAliases item=alias} - - - - - {/foreach} - -
    Local PartForward
    - - - -
    +{if !empty($emailAliases)} +
    + + + + + + + + + + {foreach from=$emailAliases item=alias} + + + + + + {/foreach} + +
    DeleteLocal PartForward
    + + + + + + +
    + + +
    +{/if} + +

    Add New Alias

    + +
    + + + + + +
    Member Forums
    -- 2.25.1