Skip to content

Commit fe097f8

Browse files
authored
Fix: Sunday School dashboard and group role management issues (#7704)
2 parents a0a2b60 + 2c02ee2 commit fe097f8

File tree

9 files changed

+185
-121
lines changed

9 files changed

+185
-121
lines changed

src/ChurchCRM/Service/DashboardService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ChurchCRM\dto\SystemConfig;
66
use ChurchCRM\model\ChurchCRM\FamilyQuery;
77
use ChurchCRM\model\ChurchCRM\PersonQuery;
8+
use ChurchCRM\Utils\Functions;
89
use Propel\Runtime\ActiveQuery\Criteria;
910

1011
const GENDER_STATS_UNASSIGNED = 0;
@@ -108,7 +109,7 @@ public function getGroupStats(): array
108109
p2g2r_rle_ID = 2 and grp_Type = 4) as SundaySchoolKidsCount
109110
from dual ;
110111
';
111-
$rsQuickStat = RunQuery($sSQL);
112+
$rsQuickStat = Functions::runQuery($sSQL);
112113
$row = mysqli_fetch_array($rsQuickStat);
113114

114115
return ['groups' => $row['Group_cnt'], 'sundaySchoolClasses' => $row['SundaySchoolClasses'], 'sundaySchoolkids' => $row['SundaySchoolKidsCount']];

src/ChurchCRM/Service/DepositService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use ChurchCRM\model\ChurchCRM\Deposit;
55
use ChurchCRM\Service\AuthService;
6+
use ChurchCRM\Utils\Functions;
67

78
class DepositService {
89
/**
@@ -64,7 +65,7 @@ public function setDeposit(string $depositType, string $depositComment, string $
6465
if ($depositClosed && ($depositType === 'CreditCard' || $depositType === 'BankDraft')) {
6566
// Delete any failed transactions on this deposit slip now that it is closing
6667
$q = 'DELETE FROM pledge_plg WHERE plg_depID = ' . $iDepositSlipID . ' AND plg_PledgeOrPayment="Payment" AND plg_aut_Cleared=0';
67-
\RunQuery($q);
68+
Functions::runQuery($q);
6869
}
6970
} else {
7071
$deposit = new \ChurchCRM\model\ChurchCRM\Deposit();

src/ChurchCRM/Service/FinancialService.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ChurchCRM\model\ChurchCRM\FamilyQuery;
1212
use ChurchCRM\model\ChurchCRM\Pledge;
1313
use ChurchCRM\model\ChurchCRM\PledgeQuery;
14+
use ChurchCRM\Utils\Functions;
1415
use ChurchCRM\Utils\InputUtils;
1516
use Propel\Runtime\ActiveQuery\Criteria;
1617
use ChurchCRM\Service\AuthService;
@@ -79,7 +80,7 @@ public function getMemberByScanString(string $tScanString): array
7980
throw new \Exception('error in locating family');
8081
}
8182
$sSQL = 'SELECT fam_ID, fam_Name FROM family_fam WHERE fam_scanCheck="' . $routeAndAccount . '"';
82-
$rsFam = RunQuery($sSQL);
83+
$rsFam = Functions::runQuery($sSQL);
8384
$row = mysqli_fetch_array($rsFam);
8485
$iCheckNo = $micrObj->findCheckNo($tScanString);
8586

@@ -105,7 +106,7 @@ public function setDeposit(string $depositType, string $depositComment, string $
105106
if ($depositClosed && ($depositType === 'CreditCard' || $depositType === 'BankDraft')) {
106107
// Delete any failed transactions on this deposit slip now that it is closing
107108
$q = 'DELETE FROM pledge_plg WHERE plg_depID = ' . $iDepositSlipID . ' AND plg_PledgeOrPayment="Payment" AND plg_aut_Cleared=0';
108-
RunQuery($q);
109+
Functions::runQuery($q);
109110
}
110111
} else {
111112
$deposit = new Deposit();
@@ -131,7 +132,7 @@ public function getDepositTotal($id, $type = null)
131132
}
132133
// Get deposit total
133134
$sSQL = "SELECT SUM(plg_amount) AS deposit_total FROM pledge_plg WHERE plg_depID = '$id' AND plg_PledgeOrPayment = 'Payment' " . $sqlClause;
134-
$rsDepositTotal = RunQuery($sSQL);
135+
$rsDepositTotal = Functions::runQuery($sSQL);
135136
[$deposit_total] = mysqli_fetch_row($rsDepositTotal);
136137

137138
return $deposit_total;
@@ -193,7 +194,7 @@ public function locateFamilyCheck(string $checkNumber, string $fam_ID)
193194
$sSQL = 'SELECT count(plg_FamID) from pledge_plg
194195
WHERE plg_CheckNo = ' . $checkNumber . ' AND
195196
plg_FamID = ' . $fam_ID;
196-
$rCount = RunQuery($sSQL);
197+
$rCount = Functions::runQuery($sSQL);
197198

198199
return mysqli_fetch_array($rCount)[0];
199200
}
@@ -230,7 +231,7 @@ public function processCurrencyDenominations(object $payment, string $groupKey):
230231
}
231232
$sSQL = "INSERT INTO pledge_denominations_pdem (pdem_plg_GroupKey, plg_depID, pdem_denominationID, pdem_denominationQuantity)
232233
VALUES ('" . $groupKey . "','" . $payment->DepositID . "','" . $cdom->currencyID . "','" . $cdom->Count . "')";
233-
RunQuery($sSQL);
234+
Functions::runQuery($sSQL);
234235
unset($sSQL);
235236
}
236237
}
@@ -312,7 +313,7 @@ public function getPledgeorPayment(string $GroupKey): string
312313
AuthService::requireUserGroupMembership('bFinance');
313314
$total = 0;
314315
$sSQL = 'SELECT plg_plgID, plg_FamID, plg_date, plg_fundID, plg_amount, plg_NonDeductible,plg_comment, plg_FYID, plg_method, plg_EditedBy from pledge_plg where plg_GroupKey="' . $GroupKey . '"';
315-
$rsKeys = RunQuery($sSQL);
316+
$rsKeys = Functions::runQuery($sSQL);
316317
$payment = new \stdClass();
317318
$payment->funds = [];
318319
while ($aRow = mysqli_fetch_array($rsKeys)) {
@@ -378,7 +379,7 @@ public function getCurrencyTypeOnDeposit(string $currencyID, string $depositID)
378379
where plg_depID = ' . $depositID . '
379380
AND
380381
pdem_denominationID = ' . $currencyID;
381-
$rscurrencyDenomination = RunQuery($sSQL);
382+
$rscurrencyDenomination = Functions::runQuery($sSQL);
382383

383384
return mysqli_fetch_array($rscurrencyDenomination)[0];
384385
}
@@ -391,7 +392,7 @@ public function getCurrency(): array
391392
$currencies = [];
392393
// Get the list of Currency denominations
393394
$sSQL = 'SELECT * FROM currency_denominations_cdem';
394-
$rscurrencyDenomination = RunQuery($sSQL);
395+
$rscurrencyDenomination = Functions::runQuery($sSQL);
395396
mysqli_data_seek($rscurrencyDenomination, 0);
396397
while ($row = mysqli_fetch_array($rscurrencyDenomination)) {
397398
$currency = new \stdClass();
@@ -414,7 +415,7 @@ public function getActiveFunds(): array
414415
$funds = [];
415416
$sSQL = 'SELECT fun_ID,fun_Name,fun_Description,fun_Active FROM donationfund_fun';
416417
$sSQL .= " WHERE fun_Active = 'true'"; // New donations should show only active funds.
417-
$rsFunds = RunQuery($sSQL);
418+
$rsFunds = Functions::runQuery($sSQL);
418419
mysqli_data_seek($rsFunds, 0);
419420
while ($aRow = mysqli_fetch_array($rsFunds)) {
420421
$fund = new \stdClass();

src/ChurchCRM/Service/GroupService.php

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ChurchCRM\model\ChurchCRM\Person2group2roleP2g2r;
99
use ChurchCRM\model\ChurchCRM\PersonQuery;
1010
use ChurchCRM\Service\AuthService;
11+
use ChurchCRM\Utils\Functions;
1112
use Propel\Runtime\Propel;
1213

1314
class GroupService
@@ -38,33 +39,33 @@ public function removeUserFromGroup(int $groupID, int $personID): void
3839
{
3940
AuthService::requireUserGroupMembership('bManageGroups');
4041
$sSQL = 'DELETE FROM person2group2role_p2g2r WHERE p2g2r_per_ID = ' . $personID . ' AND p2g2r_grp_ID = ' . $groupID;
41-
RunQuery($sSQL);
42+
Functions::runQuery($sSQL);
4243

4344
// Check if this group has special properties
4445
$sSQL = 'SELECT grp_hasSpecialProps FROM group_grp WHERE grp_ID = ' . $groupID;
45-
$rsTemp = RunQuery($sSQL);
46+
$rsTemp = Functions::runQuery($sSQL);
4647
$rowTemp = mysqli_fetch_row($rsTemp);
4748
$bHasProp = $rowTemp[0];
4849

4950
if ($bHasProp == 'true') {
5051
$sSQL = 'DELETE FROM groupprop_' . $groupID . " WHERE per_ID = '" . $personID . "'";
51-
RunQuery($sSQL);
52+
Functions::runQuery($sSQL);
5253
}
5354

5455
// Reset any group specific property fields of type "Person from Group" with this person assigned
5556
$sSQL = 'SELECT grp_ID, prop_Field FROM groupprop_master WHERE type_ID = 9 AND prop_Special = ' . $groupID;
56-
$result = RunQuery($sSQL);
57+
$result = Functions::runQuery($sSQL);
5758
while ($aRow = mysqli_fetch_array($result)) {
5859
$sSQL = 'UPDATE groupprop_' . $aRow['grp_ID'] . ' SET ' . $aRow['prop_Field'] . ' = NULL WHERE ' . $aRow['prop_Field'] . ' = ' . $personID;
59-
RunQuery($sSQL);
60+
Functions::runQuery($sSQL);
6061
}
6162

6263
// Reset any custom person fields of type "Person from Group" with this person assigned
6364
$sSQL = 'SELECT custom_Field FROM person_custom_master WHERE type_ID = 9 AND custom_Special = ' . $groupID;
64-
$result = RunQuery($sSQL);
65+
$result = Functions::runQuery($sSQL);
6566
while ($aRow = mysqli_fetch_array($result)) {
6667
$sSQL = 'UPDATE person_custom SET ' . $aRow['custom_Field'] . ' = NULL WHERE ' . $aRow['custom_Field'] . ' = ' . $personID;
67-
RunQuery($sSQL);
68+
Functions::runQuery($sSQL);
6869
}
6970
}
7071

@@ -165,7 +166,7 @@ public function getGroupRoles(int $groupID): array
165166
LEFT JOIN list_lst ON
166167
list_lst.lst_ID = group_grp.grp_RoleListID
167168
WHERE group_grp.grp_ID = ' . $groupID;
168-
$rsList = RunQuery($sSQL);
169+
$rsList = Functions::runQuery($sSQL);
169170

170171
// Validate that this list ID is really for a group roles list. (for security)
171172
if (mysqli_num_rows($rsList) === 0) {
@@ -188,7 +189,7 @@ public function setGroupRoleOrder(string $groupID, string $groupRoleID, string $
188189
SET list_lst.lst_OptionSequence = "' . $groupRoleOrder . '"
189190
WHERE group_grp.grp_ID = "' . $groupID . '"
190191
AND list_lst.lst_OptionID = ' . $groupRoleID;
191-
RunQuery($sSQL);
192+
Functions::runQuery($sSQL);
192193
}
193194

194195
public function getGroupRoleOrder(string $groupID, string $groupRoleID)
@@ -199,7 +200,7 @@ public function getGroupRoleOrder(string $groupID, string $groupRoleID)
199200
WHERE group_grp.grp_ID = "' . $groupID . '"
200201
AND list_lst.lst_OptionID = ' . $groupRoleID;
201202

202-
$rsPropList = RunQuery($sSQL);
203+
$rsPropList = Functions::runQuery($sSQL);
203204
$rowOrder = mysqli_fetch_array($rsPropList);
204205

205206
return $rowOrder[0];
@@ -212,7 +213,7 @@ public function deleteGroupRole(string $groupID, string $groupRoleID): array
212213
INNER JOIN group_grp
213214
ON group_grp.grp_RoleListID = list_lst.lst_ID
214215
WHERE group_grp.grp_ID = "' . $groupID . '"';
215-
$rsPropList = RunQuery($sSQL);
216+
$rsPropList = Functions::runQuery($sSQL);
216217
$numRows = mysqli_num_rows($rsPropList);
217218
// Make sure we never delete the only option
218219
if ($numRows > 1) {
@@ -223,22 +224,22 @@ public function deleteGroupRole(string $groupID, string $groupRoleID): array
223224
WHERE group_grp.grp_ID = "' . $groupID . '"
224225
AND lst_OptionID = ' . $groupRoleID;
225226

226-
RunQuery($sSQL);
227+
Functions::runQuery($sSQL);
227228

228229
//check if we've deleted the old group default role. If so, reset default to role ID 1
229230
// Next, if any group members were using the deleted role, reset their role to the group default.
230231
// Reset if default role was just removed.
231232
$sSQL = "UPDATE group_grp SET grp_DefaultRole = 1 WHERE grp_ID = $groupID AND grp_DefaultRole = $groupRoleID";
232-
RunQuery($sSQL);
233+
Functions::runQuery($sSQL);
233234

234235
// Get the current default role and Group ID (so we can update the p2g2r table)
235236
// This seems backwards, but grp_RoleListID is unique, having a 1-1 relationship with grp_ID.
236237
$sSQL = "SELECT grp_ID,grp_DefaultRole FROM group_grp WHERE grp_ID = $groupID";
237-
$rsTemp = RunQuery($sSQL);
238+
$rsTemp = Functions::runQuery($sSQL);
238239
$aTemp = mysqli_fetch_array($rsTemp);
239240

240241
$sSQL = "UPDATE person2group2role_p2g2r SET p2g2r_rle_ID = 1 WHERE p2g2r_grp_ID = $groupID AND p2g2r_rle_ID = $groupRoleID";
241-
RunQuery($sSQL);
242+
Functions::runQuery($sSQL);
242243

243244
//Shift the remaining rows IDs up by one
244245

@@ -249,7 +250,7 @@ public function deleteGroupRole(string $groupID, string $groupRoleID): array
249250
WHERE group_grp.grp_ID = ' . $groupID . '
250251
AND list_lst.lst_OptionID >= ' . $groupRoleID;
251252

252-
RunQuery($sSQL);
253+
Functions::runQuery($sSQL);
253254

254255
//Shift up the remaining row Sequences by one
255256

@@ -262,7 +263,7 @@ public function deleteGroupRole(string $groupID, string $groupRoleID): array
262263

263264
//echo $sSQL;
264265

265-
RunQuery($sSQL);
266+
Functions::runQuery($sSQL);
266267

267268
return $this->getGroupRoles($groupID);
268269
} else {
@@ -275,45 +276,51 @@ public function addGroupRole(string $groupID, string $groupRoleName): array
275276
AuthService::requireUserGroupMembership('bManageGroups');
276277
if (strlen($groupRoleName) === 0) {
277278
throw new \Exception('New field name cannot be blank');
278-
} else {
279-
// Check for a duplicate option name
280-
$sSQL = 'SELECT \'\' FROM list_lst
281-
INNER JOIN group_grp
282-
ON group_grp.grp_RoleListID = list_lst.lst_ID
283-
WHERE group_grp.grp_ID = "' . $groupID . '" AND
284-
lst_OptionName = "' . $groupRoleName . '"';
285-
$rsCount = RunQuery($sSQL);
286-
if (mysqli_num_rows($rsCount) > 0) {
287-
throw new \Exception('Field ' . $groupRoleName . ' already exists');
288-
} else {
289-
$sSQL = "SELECT grp_RoleListID FROM group_grp WHERE grp_ID = $groupID";
290-
$rsTemp = RunQuery($sSQL);
291-
$listIDTemp = mysqli_fetch_array($rsTemp);
292-
$listID = $listIDTemp[0];
293-
// Get count of the options
294-
$sSQL = "SELECT '' FROM list_lst WHERE lst_ID = $listID";
295-
$rsTemp = RunQuery($sSQL);
296-
$numRows = mysqli_num_rows($rsTemp);
297-
$newOptionSequence = $numRows + 1;
298-
299-
// Get the new OptionID
300-
$sSQL = "SELECT MAX(lst_OptionID) FROM list_lst WHERE lst_ID = $listID";
301-
$rsTemp = RunQuery($sSQL);
302-
$aTemp = mysqli_fetch_array($rsTemp);
303-
$newOptionID = $aTemp[0] + 1;
304-
305-
// Insert into the appropriate options table
306-
$listOption = new ListOption();
307-
$listOption
308-
->setId($listID)
309-
->setOptionId($newOptionID)
310-
->setOptionName($groupRoleName)
311-
->setOptionSequence($newOptionSequence);
312-
$listOption->save();
313-
314-
$iNewNameError = 0;
279+
}
280+
281+
// Get the group to find its role list ID
282+
$group = GroupQuery::create()->findOneById((int)$groupID);
283+
if ($group === null) {
284+
throw new \Exception('Group not found');
285+
}
286+
287+
$listID = $group->getRoleListId();
288+
289+
// Check for duplicate option name using Propel
290+
$existingOption = \ChurchCRM\model\ChurchCRM\ListOptionQuery::create()
291+
->filterById($listID)
292+
->filterByOptionName($groupRoleName)
293+
->findOne();
294+
295+
if ($existingOption !== null) {
296+
throw new \Exception('Field ' . $groupRoleName . ' already exists');
297+
}
298+
299+
// Get count of options and max option ID
300+
$options = \ChurchCRM\model\ChurchCRM\ListOptionQuery::create()
301+
->filterById($listID)
302+
->find();
303+
304+
$numRows = count($options);
305+
$newOptionSequence = $numRows + 1;
306+
307+
// Find max OptionID
308+
$maxOptionID = 0;
309+
foreach ($options as $option) {
310+
if ($option->getOptionId() > $maxOptionID) {
311+
$maxOptionID = $option->getOptionId();
315312
}
316313
}
314+
$newOptionID = $maxOptionID + 1;
315+
316+
// Insert into the appropriate options table
317+
$listOption = new ListOption();
318+
$listOption
319+
->setId($listID)
320+
->setOptionId($newOptionID)
321+
->setOptionName($groupRoleName)
322+
->setOptionSequence($newOptionSequence);
323+
$listOption->save();
317324

318325
return [
319326
'newRole' => [
@@ -329,36 +336,36 @@ public function enableGroupSpecificProperties(string $groupID): void
329336
AuthService::requireUserGroupMembership('bManageGroups');
330337
$sSQL = 'UPDATE group_grp SET grp_hasSpecialProps = true
331338
WHERE grp_ID = ' . $groupID;
332-
RunQuery($sSQL);
339+
Functions::runQuery($sSQL);
333340
$sSQLp = 'CREATE TABLE groupprop_' . $groupID . " (
334341
per_ID mediumint(8) unsigned NOT NULL default '0',
335342
PRIMARY KEY (per_ID),
336343
UNIQUE KEY per_ID (per_ID)
337344
) ENGINE=InnoDB;";
338-
RunQuery($sSQLp);
345+
Functions::runQuery($sSQLp);
339346

340347
$groupMembers = $this->getGroupMembers($groupID);
341348

342349
foreach ($groupMembers as $member) {
343350
$sSQLr = 'INSERT INTO groupprop_' . $groupID . " ( per_ID ) VALUES ( '" . $member['per_ID'] . "' );";
344-
RunQuery($sSQLr);
351+
Functions::runQuery($sSQLr);
345352
}
346353
}
347354

348355
public function disableGroupSpecificProperties(string $groupID): void
349356
{
350357
AuthService::requireUserGroupMembership('bManageGroups');
351358
$sSQLp = 'DROP TABLE groupprop_' . $groupID;
352-
RunQuery($sSQLp);
359+
Functions::runQuery($sSQLp);
353360

354361
// need to delete the master index stuff
355362
$sSQLp = 'DELETE FROM groupprop_master WHERE grp_ID = ' . $groupID;
356-
RunQuery($sSQLp);
363+
Functions::runQuery($sSQLp);
357364

358365
$sSQL = 'UPDATE group_grp SET grp_hasSpecialProps = false
359366
WHERE grp_ID = ' . $groupID;
360367

361-
RunQuery($sSQL);
368+
Functions::runQuery($sSQL);
362369
}
363370

364371
/**

0 commit comments

Comments
 (0)