benefits: Add email alias CRUD.
[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
30 function postProcess() {
31 $action = $_REQUEST['action'];
32
33 switch($action) {
34 case 'add':
35 $this->add();
36 break;
37
38 case 'edit':
39 $this->edit();
40 break;
73f5f860 41 }
7f213439 42 }
73f5f860 43
7f213439
DT
44 function add() {
45 $contactId = $this->contact['id'];
46 $localpart = $_POST['localpart'];
47 $forward = $_POST['forward'];
48 $count = CRM_Memberdashboard_BAO_EmailAlias::countForContact($contactId);
73f5f860 49
7f213439
DT
50 if(CRM_Utils_Rule::email($forward)) {
51 if($count < MEMBERDASHBOARD_MAX_EMAIL_ALIASES) {
52 try {
53 $params = array(
54 'contact_id' => $contactId,
55 'localpart' => $localpart,
56 'forward' => $forward
57 );
58 CRM_Memberdashboard_BAO_EmailAlias::create($params);
59 CRM_Core_Session::setStatus('Email alias added!',
60 'Success', 'success');
61 } catch (Exception $e) {
62 CRM_Core_Session::setStatus('Failed to create email alias',
63 'Error', 'error');
64 }
65 } else {
66 CRM_Core_Session::setStatus('You cannot have more than 5 email aliases',
67 'Error', 'error');
68 }
69 } else {
70 CRM_Core_Session::setStatus('Invalid email forwarding address',
71 'Error', 'error');
72 }
73f5f860
DT
73 }
74
7f213439
DT
75 function edit() {
76 $ids = $_POST['ids'];
77 $localparts = $_POST['localparts'];
78 $forwards = $_POST['forwards'];
79 $delete = isset($_POST['delete']) ? $_POST['delete'] : array();
80 $aliases = CRM_Memberdashboard_BAO_EmailAlias::find($ids);
81 $error = '';
82
83 for($i = 0; $i < count($ids); $i++) {
84 $id = $ids[$i];
85 $localpart = $localparts[$i];
86 $forward = $forwards[$i];
73f5f860 87
7f213439
DT
88 if(isset($delete[$id]) && $delete[$id] == 'delete') {
89 CRM_Memberdashboard_BAO_EmailAlias::destroy($id);
90 } else {
91 $alias = $aliases[$id];
92
93 // Make sure sneaky users can't edit other people's aliases.
94 if($alias->contactId != $this->contact['id']) {
95 $error .= '<li>You cannot alter email aliases that don\'t belong to you!</li>';
96 } else if(CRM_Utils_Rule::email($forward)) {
97 $alias->localpart = $localpart;
98 $alias->forward = $forward;
99 $alias->save();
100 } else {
101 $error .= "<li>Invalid email forwarding address: $forward</li>";
102 }
103 }
104 }
105
106 if(empty($error)) {
107 CRM_Core_Session::setStatus("Email aliases updated!", 'Success', 'success');
108 } else {
109 CRM_Core_Session::setStatus("<ul>$error</ul>", 'Error', 'error');
110 }
73f5f860
DT
111 }
112
3346ef47 113 function run() {
fa2dc954
DT
114 // FIXME: Assumes CiviCRM is running on Drupal.
115 // Ugly global variable for the current Drupal user.
116 global $user;
117
7f213439
DT
118 // create/update/delete email aliases.
119 if($this->isPOST()) {
120 $this->postProcess();
121 }
122
123 $emailAliases = $this->loadEmailAliases();
73f5f860 124
3346ef47
DT
125 CRM_Utils_System::setTitle(ts('Benefits'));
126
fa2dc954 127 $this->assign('user', $user);
73f5f860 128 $this->assign('emailAliases', $emailAliases);
3346ef47
DT
129
130 parent::run();
131 }
132}