-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageEnvelopes.php
234 lines (202 loc) · 7.67 KB
/
ManageEnvelopes.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
<?php
require_once 'Include/Config.php';
require_once 'Include/Functions.php';
require_once 'Include/EnvelopeFunctions.php';
use ChurchCRM\Authentication\AuthenticationManager;
use ChurchCRM\dto\SystemConfig;
use ChurchCRM\model\ChurchCRM\FamilyQuery;
use ChurchCRM\Utils\InputUtils;
use ChurchCRM\Utils\RedirectUtils;
$sPageTitle = gettext('Envelope Manager');
// Security: User must have finance permission to use this form
AuthenticationManager::redirectHomeIfFalse(AuthenticationManager::getCurrentUser()->isFinanceEnabled());
$iClassification = 0;
if (isset($_POST['Classification'])) {
$iClassification = (int) InputUtils::legacyFilterInput($_POST['Classification'], 'int');
$_SESSION['classification'] = $iClassification;
} elseif (isset($_SESSION['classification'])) {
$iClassification = (int) $_SESSION['classification'];
}
$envelopesToWrite = [];
// Get the array of envelopes of interest, indexed by family ID
$envelopesByFamID = getEnvelopes($iClassification);
// Get the array of family name/description strings, also indexed by family ID
$familyArray = getFamilyList(SystemConfig::getValue('sDirRoleHead'), SystemConfig::getValue('sDirRoleSpouse'), $iClassification);
asort($familyArray);
if (isset($_POST['Confirm'])) {
foreach ($familyArray as $fam_ID => $fam_Data) {
$key = 'EnvelopeID_' . $fam_ID;
if (isset($_POST[$key])) {
$newEnvelope = (int) InputUtils::legacyFilterInput($_POST[$key], 'int');
$priorEnvelope = $envelopesByFamID[$fam_ID];
if ($newEnvelope !== $priorEnvelope) {
$envelopesToWrite[$fam_ID] = $newEnvelope;
$envelopesByFamID[$fam_ID] = $newEnvelope;
}
}
}
foreach ($envelopesToWrite as $fam_ID => $envelope) {
$family = FamilyQuery::create()->findOneById($fam_ID);
$family->setEnvelope($envelope);
$family->save();
}
}
if (isset($_POST['SortBy'])) {
$sSortBy = $_POST['SortBy'];
} else {
$sSortBy = 'name';
}
if (isset($_POST['AssignStartNum'])) {
$iAssignStartNum = (int) InputUtils::legacyFilterInput($_POST['AssignStartNum'], 'int');
} else {
$iAssignStartNum = 1;
}
//Get Classifications for the drop-down
$sSQL = 'SELECT * FROM list_lst WHERE lst_ID = 1 ORDER BY lst_OptionSequence';
$rsClassifications = RunQuery($sSQL);
while ($aRow = mysqli_fetch_array($rsClassifications)) {
extract($aRow);
$classification[$lst_OptionID] = $lst_OptionName;
}
require_once 'Include/Header.php';
?>
<div class="card card-body">
<form method="post" action="ManageEnvelopes.php" name="ManageEnvelopes">
<?php
$duplicateEnvelopeHash = [];
$updateEnvelopes = 0;
// Service the action buttons
if (isset($_POST['PrintReport'])) {
RedirectUtils::redirect('Reports/EnvelopeReport.php');
} elseif (isset($_POST['AssignAllFamilies'])) {
$newEnvNum = $iAssignStartNum;
$envelopesToWrite = []; // zero it out
foreach ($familyArray as $fam_ID => $fam_Data) {
$envelopesByFamID[$fam_ID] = $newEnvNum;
$envelopesToWrite[$fam_ID] = $newEnvNum++;
}
} elseif (isset($_POST['ZeroAll'])) {
$envelopesByFamID = []; // zero it out
foreach ($familyArray as $fam_ID => $fam_Data) {
$envelopesByFamID[$fam_ID] = 0;
$envelopesToWrite[$fam_ID] = 0;
}
}
?>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#updateEnvelopesModal"><?= gettext('Update Family Records') ?></button>
<button type="submit" class="btn btn-default" name="PrintReport"><i class="fa fa-print"></i></button>
<br><br>
<!-- Modal -->
<div class="modal fade" id="updateEnvelopesModal" tabindex="-1" role="dialog" aria-labelledby="updateEnvelopesModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="upload-Image-label"><?= gettext('Update Envelopes') ?></h4>
</div>
<div class="modal-body">
<span style="color:red"><?= gettext('This will overwrite the family envelope numbers in the database with those selected on this page. Continue?')?></span>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="<?= gettext('Confirm') ?>" name="Confirm">
<button type="button" class="btn btn-default" data-dismiss="modal"><?= gettext('Cancel') ?></button>
</div>
</div>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>
<b><?= gettext('Family Select')?></b> <?= gettext('with at least one:'); ?>
<select name="Classification">
<option value="0"><?= gettext('All') ?></option>
<?php
foreach ($classification as $lst_OptionID => $lst_OptionName) {
echo '<option value="' . $lst_OptionID . '"';
if ($iClassification == $lst_OptionID) {
echo ' selected';
}
echo '>' . $lst_OptionName . ' ';
}
?>
</select>
<input type="submit" class="btn btn-default" value="<?= gettext('Sort by') ?>" name="Sort">
<input type="radio" Name="SortBy" value="name"
<?php if ($sSortBy === 'name') {
echo ' checked';
} ?>><?= gettext('Last Name') ?>
<input type="radio" Name="SortBy" value="envelope"
<?php if ($sSortBy === 'envelope') {
echo ' checked';
} ?>><?= gettext('Envelope Number') ?>
</th>
<th>
<b>Envelope</b>
<input type="submit" class="btn btn-default" value="<?= gettext('Zero') ?>"
name="ZeroAll">
<input type="submit" class="btn btn-default" value="<?= gettext('Assign starting at #') ?>"
name="AssignAllFamilies">
<input type="text" name="AssignStartNum" value="<?= $iAssignStartNum ?>" maxlength="5">
</th>
</tr>
</thead>
<?php
if ($sSortBy === 'envelope') {
asort($envelopesByFamID);
$arrayToLoop = $envelopesByFamID;
} else {
$arrayToLoop = $familyArray;
}
foreach ($arrayToLoop as $fam_ID => $value) {
if ($sSortBy === 'envelope') {
$envelope = $value;
$fam_Data = $familyArray[$fam_ID];
} else {
$fam_Data = $value;
$envelope = $envelopesByFamID[$fam_ID];
}
echo '<tr>';
echo '<td>' . $fam_Data . ' </td>';
if ($envelope && $duplicateEnvelopeHash && array_key_exists($envelope, $duplicateEnvelopeHash)) {
$tdTag = "<td bgcolor='red'>";
} else {
$duplicateEnvelopeHash[$envelope] = $fam_ID;
$tdTag = '<td>';
}
echo $tdTag; ?><class="TextColumn">
<input type="text" name="EnvelopeID_<?= $fam_ID ?>" value="<?= $envelope ?>" maxlength="10">
</td></tr>
<?php
}
?>
</table><br>
</form>
</div>
<?php
/**
* Make an array of envelopes indexed by family id, subject to the classification filter if specified.
*
* @param int|null $classification
*
* @return array<int, int>
*
* @throws Exception
*/
function getEnvelopes(?int $classification = null): array
{
if ($classification) {
$sSQL = "SELECT fam_ID, fam_Envelope FROM family_fam LEFT JOIN person_per ON fam_ID = per_fam_ID WHERE per_cls_ID='" . $classification . "'";
} else {
$sSQL = 'SELECT fam_ID, fam_Envelope FROM family_fam';
}
$sSQL .= ' ORDER by fam_Envelope';
$dEnvelopes = RunQuery($sSQL);
$envelopes = [];
while ($aRow = mysqli_fetch_array($dEnvelopes)) {
extract($aRow);
$envelopes[(int) $fam_ID] = (int) $fam_Envelope;
}
return $envelopes;
}
require_once 'Include/Footer.php';