Commit | Line | Data |
---|---|---|
3346ef47 | 1 | <?php |
7f213439 DT |
2 | /** |
3 | * FSF Member Dashboard | |
4 | * Copyright © 2014 Free Software Foundation, Inc. | |
5 | * | |
6 | * This file is a part of FSF Member Dashboard. | |
7 | * | |
8 | * FSF Member Dashboard is free software; you can copy, modify, and | |
9 | * distribute it under the terms of the GNU Affero General Public | |
10 | * License Version 3, 19 November 2007 and the CiviCRM Licensing | |
11 | * Exception. | |
12 | * | |
13 | * FSF Member Dashboard is distributed in the hope that it will be | |
14 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Affero General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with FSF Member Dashboard. If not, see | |
20 | * <http://www.gnu.org/licenses/>. | |
21 | */ | |
3346ef47 DT |
22 | |
23 | require_once 'CRM/Memberdashboard/Page.php'; | |
24 | ||
25 | class CRM_Memberdashboard_Page_Benefits extends CRM_Memberdashboard_Page { | |
73f5f860 | 26 | function loadEmailAliases() { |
7f213439 DT |
27 | return CRM_Memberdashboard_BAO_EmailAlias::allForContact($this->contact['id']); |
28 | } | |
29 | ||
7d701e33 DT |
30 | function hasValidMembership() { |
31 | // Since we don't currently shut off an expired member's benefits, | |
32 | // we only test that they have a membership, not that it is current. | |
33 | $params = array( | |
34 | 'contact_id' => $this->contact['id'] | |
35 | ); | |
36 | ||
37 | try { | |
38 | $result = civicrm_api3('membership', 'get', $params); | |
39 | return $result['count'] > 0; | |
40 | } catch (CiviCRM_API3_Exception $e) { | |
41 | return false; | |
42 | } | |
43 | } | |
44 | ||
7f213439 DT |
45 | function postProcess() { |
46 | $action = $_REQUEST['action']; | |
47 | ||
7d701e33 DT |
48 | // Short-circuit if user doesn't have a membership. |
49 | if($this->hasValidMembership()) { | |
50 | switch($action) { | |
51 | case 'add': | |
52 | $this->add(); | |
53 | break; | |
7f213439 | 54 | |
7d701e33 DT |
55 | case 'edit': |
56 | $this->edit(); | |
57 | break; | |
58 | } | |
59 | } else { | |
60 | CRM_Core_Session::setStatus('Only members can have email aliases', | |
61 | 'Error', 'error'); | |
73f5f860 | 62 | } |
7f213439 | 63 | } |
73f5f860 | 64 | |
7f213439 DT |
65 | function add() { |
66 | $contactId = $this->contact['id']; | |
67 | $localpart = $_POST['localpart']; | |
68 | $forward = $_POST['forward']; | |
69 | $count = CRM_Memberdashboard_BAO_EmailAlias::countForContact($contactId); | |
73f5f860 | 70 | |
7f213439 | 71 | if(CRM_Utils_Rule::email($forward)) { |
2297524f | 72 | if(CRM_Utils_Rule::email("$localpart@example.com")) { |
2a3d840b DT |
73 | if(CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart) == NULL) { |
74 | if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) { | |
75 | try { | |
76 | $params = array( | |
77 | 'contact_id' => $contactId, | |
78 | 'localpart' => $localpart, | |
79 | 'forward' => $forward | |
80 | ); | |
81 | CRM_Memberdashboard_BAO_EmailAlias::create($params); | |
82 | CRM_Core_Session::setStatus('Email alias added!', | |
83 | 'Success', 'success'); | |
84 | } catch (Exception $e) { | |
85 | CRM_Core_Session::setStatus('Failed to create email alias', | |
86 | 'Error', 'error'); | |
87 | } | |
88 | } else { | |
89 | CRM_Core_Session::setStatus('You cannot have more than 5 email aliases', | |
2297524f DT |
90 | 'Error', 'error'); |
91 | } | |
92 | } else { | |
2a3d840b | 93 | CRM_Core_Session::setStatus("Local part is already in use: $localpart", |
7f213439 DT |
94 | 'Error', 'error'); |
95 | } | |
96 | } else { | |
2297524f | 97 | CRM_Core_Session::setStatus('Invalid local part', |
7f213439 DT |
98 | 'Error', 'error'); |
99 | } | |
100 | } else { | |
101 | CRM_Core_Session::setStatus('Invalid email forwarding address', | |
102 | 'Error', 'error'); | |
103 | } | |
73f5f860 DT |
104 | } |
105 | ||
7f213439 DT |
106 | function edit() { |
107 | $ids = $_POST['ids']; | |
108 | $localparts = $_POST['localparts']; | |
109 | $forwards = $_POST['forwards']; | |
110 | $delete = isset($_POST['delete']) ? $_POST['delete'] : array(); | |
111 | $aliases = CRM_Memberdashboard_BAO_EmailAlias::find($ids); | |
112 | $error = ''; | |
113 | ||
114 | for($i = 0; $i < count($ids); $i++) { | |
115 | $id = $ids[$i]; | |
116 | $localpart = $localparts[$i]; | |
117 | $forward = $forwards[$i]; | |
73f5f860 | 118 | |
7f213439 DT |
119 | if(isset($delete[$id]) && $delete[$id] == 'delete') { |
120 | CRM_Memberdashboard_BAO_EmailAlias::destroy($id); | |
121 | } else { | |
122 | $alias = $aliases[$id]; | |
123 | ||
124 | // Make sure sneaky users can't edit other people's aliases. | |
125 | if($alias->contactId != $this->contact['id']) { | |
126 | $error .= '<li>You cannot alter email aliases that don\'t belong to you!</li>'; | |
127 | } else if(CRM_Utils_Rule::email($forward)) { | |
2297524f | 128 | if(CRM_Utils_Rule::email("$localpart@example.com")) { |
2a3d840b DT |
129 | $existingAlias = CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart); |
130 | if($existingAlias == NULL || $existingAlias->id == $id) { | |
131 | $alias->localpart = $localpart; | |
132 | $alias->forward = $forward; | |
133 | $alias->save(); | |
134 | } else { | |
135 | $error .= "<li>Local part already in use: $localpart</li>"; | |
136 | } | |
2297524f DT |
137 | } else { |
138 | $error .= "<li>Invalid local part: $localpart</li>"; | |
139 | } | |
7f213439 DT |
140 | } else { |
141 | $error .= "<li>Invalid email forwarding address: $forward</li>"; | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | if(empty($error)) { | |
147 | CRM_Core_Session::setStatus("Email aliases updated!", 'Success', 'success'); | |
148 | } else { | |
149 | CRM_Core_Session::setStatus("<ul>$error</ul>", 'Error', 'error'); | |
150 | } | |
73f5f860 DT |
151 | } |
152 | ||
3346ef47 | 153 | function run() { |
fa2dc954 DT |
154 | // FIXME: Assumes CiviCRM is running on Drupal. |
155 | // Ugly global variable for the current Drupal user. | |
156 | global $user; | |
157 | ||
7f213439 DT |
158 | // create/update/delete email aliases. |
159 | if($this->isPOST()) { | |
160 | $this->postProcess(); | |
161 | } | |
162 | ||
163 | $emailAliases = $this->loadEmailAliases(); | |
f5310d9e DT |
164 | $buttonUrl = civicrm_api3('setting', 'getvalue', array( |
165 | 'name' => 'memberdashboard_button_static_url', | |
166 | 'group' => MEMBERDASHBOARD_SETTINGS_GROUP | |
167 | )); | |
73f5f860 | 168 | |
3346ef47 DT |
169 | CRM_Utils_System::setTitle(ts('Benefits')); |
170 | ||
f5310d9e | 171 | $this->assign('contact', $this->contact); |
fa2dc954 | 172 | $this->assign('user', $user); |
73f5f860 | 173 | $this->assign('emailAliases', $emailAliases); |
7d701e33 | 174 | $this->assign('hasValidMembership', $this->hasValidMembership()); |
f5310d9e | 175 | $this->assign('buttonUrl', $buttonUrl); |
3346ef47 DT |
176 | |
177 | parent::run(); | |
178 | } | |
179 | } |