typo
[org.fsf.memberdashboard.git] / CRM / Memberdashboard / Page / Benefits.php
CommitLineData
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
23require_once 'CRM/Memberdashboard/Page.php';
24
25class 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,
2c762923
IK
32 // we only test that they have a membership, and not that its pending,
33 // because pending members havent yet paid us yet.
7d701e33 34 $params = array(
2c762923
IK
35 'contact_id' => $this->contact['id'],
36 'status_id' => ['!=' => "Pending"]
7d701e33
DT
37 );
38
39 try {
a0b007c2 40 $result = civicrm_api3('membership', 'get', $params);
7d701e33
DT
41 return $result['count'] > 0;
42 } catch (CiviCRM_API3_Exception $e) {
43 return false;
44 }
45 }
46
7f213439
DT
47 function postProcess() {
48 $action = $_REQUEST['action'];
49
7d701e33
DT
50 // Short-circuit if user doesn't have a membership.
51 if($this->hasValidMembership()) {
52 switch($action) {
53 case 'add':
54 $this->add();
55 break;
7f213439 56
7d701e33
DT
57 case 'edit':
58 $this->edit();
59 break;
60 }
61 } else {
62 CRM_Core_Session::setStatus('Only members can have email aliases',
63 'Error', 'error');
73f5f860 64 }
7f213439 65 }
73f5f860 66
7f213439
DT
67 function add() {
68 $contactId = $this->contact['id'];
69 $localpart = $_POST['localpart'];
70 $forward = $_POST['forward'];
71 $count = CRM_Memberdashboard_BAO_EmailAlias::countForContact($contactId);
73f5f860 72
7f213439 73 if(CRM_Utils_Rule::email($forward)) {
2297524f 74 if(CRM_Utils_Rule::email("$localpart@example.com")) {
2a3d840b
DT
75 if(CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart) == NULL) {
76 if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
77 try {
78 $params = array(
79 'contact_id' => $contactId,
80 'localpart' => $localpart,
81 'forward' => $forward
82 );
83 CRM_Memberdashboard_BAO_EmailAlias::create($params);
84 CRM_Core_Session::setStatus('Email alias added!',
85 'Success', 'success');
86 } catch (Exception $e) {
87 CRM_Core_Session::setStatus('Failed to create email alias',
88 'Error', 'error');
89 }
90 } else {
91 CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
2297524f
DT
92 'Error', 'error');
93 }
94 } else {
2a3d840b 95 CRM_Core_Session::setStatus("Local part is already in use: $localpart",
7f213439
DT
96 'Error', 'error');
97 }
98 } else {
2297524f 99 CRM_Core_Session::setStatus('Invalid local part',
7f213439
DT
100 'Error', 'error');
101 }
102 } else {
103 CRM_Core_Session::setStatus('Invalid email forwarding address',
104 'Error', 'error');
105 }
73f5f860
DT
106 }
107
7f213439
DT
108 function edit() {
109 $ids = $_POST['ids'];
110 $localparts = $_POST['localparts'];
111 $forwards = $_POST['forwards'];
112 $delete = isset($_POST['delete']) ? $_POST['delete'] : array();
113 $aliases = CRM_Memberdashboard_BAO_EmailAlias::find($ids);
114 $error = '';
115
116 for($i = 0; $i < count($ids); $i++) {
117 $id = $ids[$i];
118 $localpart = $localparts[$i];
119 $forward = $forwards[$i];
73f5f860 120
7f213439
DT
121 if(isset($delete[$id]) && $delete[$id] == 'delete') {
122 CRM_Memberdashboard_BAO_EmailAlias::destroy($id);
123 } else {
124 $alias = $aliases[$id];
125
126 // Make sure sneaky users can't edit other people's aliases.
127 if($alias->contactId != $this->contact['id']) {
128 $error .= '<li>You cannot alter email aliases that don\'t belong to you!</li>';
129 } else if(CRM_Utils_Rule::email($forward)) {
2297524f 130 if(CRM_Utils_Rule::email("$localpart@example.com")) {
2a3d840b
DT
131 $existingAlias = CRM_Memberdashboard_BAO_EmailAlias::findByLocalpart($localpart);
132 if($existingAlias == NULL || $existingAlias->id == $id) {
133 $alias->localpart = $localpart;
134 $alias->forward = $forward;
135 $alias->save();
136 } else {
137 $error .= "<li>Local part already in use: $localpart</li>";
138 }
2297524f
DT
139 } else {
140 $error .= "<li>Invalid local part: $localpart</li>";
141 }
7f213439
DT
142 } else {
143 $error .= "<li>Invalid email forwarding address: $forward</li>";
144 }
145 }
146 }
147
148 if(empty($error)) {
149 CRM_Core_Session::setStatus("Email aliases updated!", 'Success', 'success');
150 } else {
151 CRM_Core_Session::setStatus("<ul>$error</ul>", 'Error', 'error');
152 }
73f5f860
DT
153 }
154
3346ef47 155 function run() {
fa2dc954
DT
156 // FIXME: Assumes CiviCRM is running on Drupal.
157 // Ugly global variable for the current Drupal user.
158 global $user;
159
7f213439
DT
160 // create/update/delete email aliases.
161 if($this->isPOST()) {
162 $this->postProcess();
163 }
164
165 $emailAliases = $this->loadEmailAliases();
f5310d9e
DT
166 $buttonUrl = civicrm_api3('setting', 'getvalue', array(
167 'name' => 'memberdashboard_button_static_url',
168 'group' => MEMBERDASHBOARD_SETTINGS_GROUP
169 ));
73f5f860 170
3346ef47
DT
171 CRM_Utils_System::setTitle(ts('Benefits'));
172
f5310d9e 173 $this->assign('contact', $this->contact);
fa2dc954 174 $this->assign('user', $user);
73f5f860 175 $this->assign('emailAliases', $emailAliases);
7d701e33 176 $this->assign('hasValidMembership', $this->hasValidMembership());
f5310d9e 177 $this->assign('buttonUrl', $buttonUrl);
3346ef47
DT
178
179 parent::run();
180 }
181}