From 24544fc999c82666f0a248b1f8b109ef5cb9105e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 24 Oct 2014 16:44:19 -0400 Subject: [PATCH] communication: Add custom group subscription form. * CRM/Memberdashboard/Page/Communications.php (CRM_Memberdashboard_Page_Communications) [getGroups]: New method. [postProcess]: Add contact to given group. [run]: Add groups for rendering. * templates/CRM/Memberdashboard/Page/Communications.tpl: Render group select box. --- CRM/Memberdashboard/Page/Communications.php | 48 +++++++++++++++++-- .../Memberdashboard/Page/Communications.tpl | 15 ++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CRM/Memberdashboard/Page/Communications.php b/CRM/Memberdashboard/Page/Communications.php index 36a5439..0cb1b07 100644 --- a/CRM/Memberdashboard/Page/Communications.php +++ b/CRM/Memberdashboard/Page/Communications.php @@ -23,15 +23,49 @@ require_once 'CRM/Core/Page.php'; class CRM_Memberdashboard_Page_Communications extends CRM_Memberdashboard_Page { + function getGroups() { + // Get all public groups. + $allGroups = CRM_Core_PseudoConstant::staticGroup(true); + + // Arrange groups into hierarchical listing (child groups follow + // their parents and have indentation spacing in title) + $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, + NULL, + '  ', + TRUE); + + // Get the list of groups contact is currently in ("Added") or + // unsubscribed ("Removed"). + $id = $this->contact['id']; + $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($id); + + // Remove groups that the user is already subscribed to. + return array_diff_key($groupHierarchy, $currentGroups); + } + function postProcess($params) { // Update member's preferred mail format (plain text, HTML) - $key = 'preferred_mail_format'; - $value = $params[$key]; - civicrm_api3('contact', 'create', array( 'id' => $this->contact['id'], - $key => $value + 'preferred_mail_format' => $params['preferred_mail_format'] )); + + // Subscribe to a new group (if present) + if(array_key_exists('group_id', $params) && $params['group_id'] != '') { + $groupID = $params['group_id']; + $contactID = array($this->contact['id']); + $method = 'Web'; + $groupContact = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactID, + $groupID, + $method); + + if($groupContact) { + $groups = CRM_Core_PseudoConstant::group(); + CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", + array(1 => $groups[$groupID])), + ts('Added to Group'), 'success'); + } + } } function run() { @@ -45,9 +79,15 @@ class CRM_Memberdashboard_Page_Communications extends CRM_Memberdashboard_Page { $this->assign('dashboardElements', $helper->buildDashboardElements()); $gContact = new CRM_Contact_Page_View_UserDashBoard_GroupContact(); + // Call browse instead of run in order to prevent additional form + // elements appearing on the page that don't play nice with the rest + // of the page. This group subscription stuff is a big hack. $gContact->browse(); + $groups = array('' => ts('Select a group')) + $this->getGroups(); + $this->assign('contact', $this->contact); + $this->assign('groups', $groups); $this->assign('mailFormats', array('Both', 'HTML', 'Text')); CRM_Utils_System::setTitle('Communications'); diff --git a/templates/CRM/Memberdashboard/Page/Communications.tpl b/templates/CRM/Memberdashboard/Page/Communications.tpl index 389e31c..4ea3255 100644 --- a/templates/CRM/Memberdashboard/Page/Communications.tpl +++ b/templates/CRM/Memberdashboard/Page/Communications.tpl @@ -24,6 +24,21 @@ {ts}Your Group(s){/ts} {include file="CRM/Contact/Page/View/UserDashBoard/GroupContact.tpl"} + +
+ + + + + +
-- 2.25.1