88use ChurchCRM \model \ChurchCRM \Person2group2roleP2g2r ;
99use ChurchCRM \model \ChurchCRM \PersonQuery ;
1010use ChurchCRM \Service \AuthService ;
11+ use ChurchCRM \Utils \Functions ;
1112use Propel \Runtime \Propel ;
1213
1314class 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