forked from mholt/ysaward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.php
339 lines (285 loc) · 9.88 KB
/
email.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
require_once "lib/init.php";
protectPage(0, true);
if ($MEMBER != null && $LEADER == null) {
$m = Member::Current();
// Get a list of all members of the ward
$mems = array();
$q = "SELECT ID FROM Members WHERE WardID='$MEMBER->WardID' ORDER BY FirstName,LastName ASC";
$r = DB::Run($q);
while ($row = mysql_fetch_array($r))
array_push($mems, Member::Load($row['ID']));
// Get member's privileges in these matters
$has1 = $m->HasPrivilege(PRIV_EMAIL_ALL); // Email all members
$has2 = $m->HasPrivilege(PRIV_EMAIL_BRO); // Email all brethren
$has3 = $m->HasPrivilege(PRIV_EMAIL_SIS); // Email all sisters
// Get a list of this member's FHE group for convenience
$fheGroupMembers = array();
$r = DB::Run("SELECT ID FROM Members WHERE FheGroup='{$MEMBER->FheGroup}' AND FheGroup != ''");
while ($row = mysql_fetch_array($r))
array_push($fheGroupMembers, $row['ID']);
} else if ($MEMBER == null && $LEADER != null) {
$m = $LEADER;
// Get a list of all members of the stake
$mems = array();
$q = "SELECT ID FROM Members WHERE WardID IN (SELECT ID FROM Wards WHERE StakeID = '{$LEADER->StakeID}') ORDER BY FirstName ASC, LastName ASC";
$r = DB::Run($q);
while ($row = mysql_fetch_array($r))
array_push($mems, Member::Load($row['ID']));
// Get member's privileges in these matters
$has1 = true; // Email all members
$has2 = true; // Email all brethren
$has3 = true; // Email all sisters
// Get a list of this member's FHE group for convenience
$fheGroupMembers = array();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Email — <?php echo $WARD ? $WARD->Name." Ward" : SITE_NAME; ?></title>
<?php include "includes/head.php"; ?>
<style>
fieldset legend {
margin-bottom: 1em;
}
.headers {
font-size: 14px;
margin-bottom: 2em;
}
.memberlist {
overflow-y: scroll;
height: 300px;
display: inline-block;
min-width: 200px;
background: #FFF;
border-radius: 10px;
margin-top: 1em;
border: 1px solid #AAA;
}
.to label {
display: block;
cursor: pointer;
padding: 0 5px;
}
.to label.bold {
width: 150px;
border-radius: 5px;
}
.to label:hover {
background: #EEE;
}
</style>
</head>
<body>
<?php include "includes/header.php"; ?>
<form method="post" action="api/startsendingemails" class="narrow">
<div class="text-center">
<h1>Send email</h1>
</div>
<fieldset>
<legend>From</legend>
<div class="headers">
<b><?php echo $m->FirstName(); ?> <?php echo $m->LastName; ?></b> <<?php echo EMAIL_FROM; ?>>
</div>
</fieldset>
<fieldset>
<legend>Reply-To</legend>
<div class="headers" style="line-height: 1.5em;">
<b><?php echo $m->FirstName(); ?> <?php echo $m->LastName; ?></b> <<?php echo $m->Email; ?>>
<br>
<small><i style="font-weight: 300;">(Replies will be sent directly to you)</i></small>
</div>
</fieldset>
<fieldset class="to">
<legend>To</legend>
<div class="headers">
<?php if ($has1): ?>
<label class="bold"><input type="checkbox" id="sel-all" class="sel standard"> Everyone</label>
<?php endif; if ($has1 || $has2): ?>
<label class="bold"><input type="checkbox" id="sel-bro" class="sel standard"> All brothers</label>
<?php endif; if ($has1 || $has3): ?>
<label class="bold"><input type="checkbox" id="sel-sis" class="sel standard"> All sisters</label>
<?php endif; if ($MEMBER != null && $LEADER == null): ?>
<label class="bold"><input type="checkbox" id="sel-fhe" class="sel standard" name="fhe"> My FHE group</label>
<?php endif; ?>
<div class="memberlist">
<?php foreach ($mems as $mem): ?>
<label>
<input type="checkbox" name="to[]" value="<?php echo $mem->ID(); ?>" data-gender="<?php echo $mem->Gender; ?>" class="standard">
<?php echo $mem->FirstName(); ?> <?php echo $mem->LastName; ?>
</label>
<?php endforeach; ?>
</div>
</div>
</fieldset>
<fieldset>
<legend>Message</legend>
<br>
<input type="text" name="subject" maxlength="255" placeholder="Subject" required>
<br>
<textarea name="msg" rows="5" placeholder="Body" required></textarea>
<small><b>Note:</b> Email is not a secure form of communication.</small>
</fieldset>
<hr>
<div class="text-center">
<button type="submit">Send</button>
<br>
<br>
</div>
</form>
<?php include "includes/footer.php"; ?>
<?php include "includes/nav.php"; ?>
<script type="text/javascript">
$(function() {
var notifyToast;
var checkedBeforeFheChecked = [];
var fheGroup = {<?php
foreach ($fheGroupMembers as $memID)
echo "{$memID}: true, ";
?> 0: false };
// Submit form
$('form').hijax({
before: function() {
if ($('input[type=checkbox]:checked').length < 1)
{
$.sticky("Please select at least one recipient.", { classList: 'error' });
return false;
}
if ($('input[name=subject]').val().length <= 4)
{
$.sticky("Please type a message subject longer than 4 characters.", { classList: 'error' });
return false;
}
if ($('textarea').val().length == 0)
{
$.sticky("You forgot to type a message...", { classList: 'error' });
return false;
}
if ($('textarea').val().length < 10)
{
$.sticky("Please make your message a little bit longer. This will help make sure it doesn't go to spam boxes.", { classList: 'error' });
return false;
}
$('[type=submit]').showSpinner();
},
complete: function(xhr) {
if (xhr.status == 200)
{
_gaq.push(['_trackEvent', 'Feature', 'Email', 'Send']);
if (xhr.responseText == "1")
$.sticky("We also sent a copy of the message to your inbox.");
if ($('input[type=checkbox]:checked').length > 10)
$.sticky("Your emails have been queued up successfully and should be sent soon. Please allow several minutes to an hour for all emails to arrive.");
else
$.sticky("Your email was successfully queued up and should arrive in the next few minutes.");
resetForm();
}
else
{
if (!xhr.responseText && xhr.status != 500)
{
$.sticky("Looks like things are a little slow right now, but your email(s) will be sent momentarily.");
resetForm();
}
else
$.sticky(xhr.responseText || "There was and your message could not be sent. Please report this.", { classList: 'error' });
}
$('[type=submit]').hideSpinner();
}
});
function resetForm()
{
// Resets the form so they don't accidently send more emails
$('input[type=checkbox]').prop('checked', false);
$('.to-fhe').remove();
$('textarea').val('');
$('input[type=text]').val('');
}
// Select all
$('#sel-all').change(function() {
$('input[type=checkbox]').not('#sel-fhe').prop('checked', $(this).prop('checked'));
});
// Select brothers
$('#sel-bro').change(function() {
$('input[type=checkbox]').filter(function() {
return $(this).data('gender') == <?php echo Gender::Male; ?>
}).prop('checked', $(this).prop('checked'));
});
// Select sisters
$('#sel-sis').change(function() {
$('input[type=checkbox]').filter(function() {
return $(this).data('gender') == <?php echo Gender::Female; ?>
}).prop('checked', $(this).prop('checked'));
});
// Select FHE group
$('#sel-fhe').change(function() {
if ($(this).is(':checked'))
{
// Save what's checked...
checkedBeforeFheChecked = $('input[type=checkbox]:checked')
.not('.sel')
.prop('checked', false)
.toArray();
// Disable checkbox fields...
$('input[type=checkbox]').not('#sel-fhe').prop('disabled', $(this).is(':checked'));
// Select the FHE group...
$('input[type=checkbox]').filter(function() {
var isFHE = fheGroup[$(this).val()];
// Since disabled fields don't get sent to the server (apparently),
// we need to inject some input fields manually
if (isFHE)
$('form').append('<input type="hidden" name="to[]" value="'+$(this).val()+'" class="to-fhe">');
return isFHE;
}).prop('checked', $(this).prop('checked'));
}
else
{
// Uncheck everything
$('input[type=checkbox]').prop('disabled', false).not('.sel').prop('checked', false);
// Remove the FHE value "fix" from when we checked the box
$('.to-fhe').remove();
// Restore checked values
$.each(checkedBeforeFheChecked, function(idx, elem) {
$(this).prop('checked', true);
});
}
});
// This block ensures a user can't select more than they're allowed to
$('#memberlist input[type=checkbox]').change(function() {
// Since the fields are disabled, this shouldn't be an issue, but just in case...
if ($('#sel-fhe').is(':checked'))
$('#sel-fhe').click();
<?php if (!$has1 && !$has2 && !$has3): ?>
if ($('.memberlist input[type=checkbox]:checked').length > <?php echo EMAIL_MAX_RECIPIENTS; ?>)
{
$(this).prop('checked', false);
if (!$(notifyToast).is(':visible'))
notifyToast = $.sticky('You can only email up to ' + <?php echo EMAIL_MAX_RECIPIENTS; ?> + ' members at a time.');
}
<?php elseif ($has2): ?>
// May email all brothers but not much more than that
if ($('.memberlist input[type=checkbox]:checked').not(function() {
return $(this).data('gender') == <?php echo Gender::Male; ?>;
}).length > <?php echo EMAIL_MAX_RECIPIENTS; ?>)
{
$(this).prop('checked', false);
if (!$(notifyToast).is(':visible'))
notifyToast = $.sticky('You may email all the brothers, but beyond that you can only email up to <?php echo EMAIL_MAX_RECIPIENTS; ?> members at a time.');
}
<?php elseif ($has3): ?>
// May email all sisters but not much more than that
if ($('.memberlist input[type=checkbox]:checked').not(function() {
return $(this).data('gender') == <?php echo Gender::Female; ?>;
}).length > <?php echo EMAIL_MAX_RECIPIENTS; ?>)
{
$(this).prop('checked', false);
if (!$(notifyToast).is(':visible'))
notifyToast = $.sticky('You may email all the sisters, but beyond that you can only email up to <?php echo EMAIL_MAX_RECIPIENTS; ?> members at a time.');
}
<?php endif; ?>
});
});
</script>
</body>
</html>