diff --git a/db_objects/person_query.class.php b/db_objects/person_query.class.php
index 25ca548b..3e434821 100644
--- a/db_objects/person_query.class.php
+++ b/db_objects/person_query.class.php
@@ -444,8 +444,55 @@ class="select-rule-toggle"
+
+
who have a family member in one or more of these groups:
+
For example, find the family of Youth Group members. In the "Show me" section, you may add 'Names of family member in the specified group' to see the family member who is in the picked group.
+
+
+
+ havePerm(PERM_VIEWNOTE)) {
?>
who have a person/family note containing the phrase:
@@ -545,6 +592,7 @@ class="select-rule-toggle"
$options['all_members'] = 'Names of all their family members';
$options['adult_members'] = 'Names of their adult family members';
+ $options['familymember_group_members'] = 'Names of family member in the specified group';
if ($GLOBALS['system']->featureEnabled('PHOTOS')) {
$options['photo'] = 'Photo';
}
@@ -814,6 +862,16 @@ function processForm($prefix='', $fields=NULL)
$params['exclude_group_membership_status'] = Array();
}
+ // FAMILY MEMBER GROUP RULES
+ $params['include_familymember_groups'] = array_remove_empties(array_get($_POST, 'include_familymember_groupids', Array()));
+ if (!empty($_REQUEST['enable_familymember_group_membership_status'])) {
+ $params['familymember_group_membership_status'] = array_get($_POST, 'familymember_group_membership_status');
+ } else {
+ $params['familymember_group_membership_status'] = Array();
+ }
+ $params['familymember_group_join_date_from'] = empty($_POST['enable_familymember_group_join_date']) ? NULL : process_widget('familymember_group_join_date_from', Array('type' => 'date'));
+ $params['familymember_group_join_date_to'] = empty($_POST['enable_familymember_group_join_date']) ? NULL : process_widget('familymember_group_join_date_to', Array('type' => 'date'));
+
// NOTE RULES
$params['note_phrase'] = array_get($_POST, 'note_phrase');
@@ -1122,6 +1180,32 @@ function getSQL($custom_select_fields=NULL)
WHERE ('.$include_groupids_clause.')';
$query['where'][] = 'p.id IN ('.$group_members_sql.')';
}
+
+ if (!empty($params['include_familymember_groups'])) {
+ // "have a family member in one or more of these groups". E.g. family members (e.g. parents) of Youth Group members. #1104
+
+ // Logic identical to 'include_groups' above
+ $include_familymember_groupids_clause = $this->_getGroupAndCategoryRestrictionSQL(
+ $params['include_familymember_groups'],
+ array_get($params, 'familymember_group_join_date_from'),
+ array_get($params, 'familymember_group_join_date_to'),
+ array_get($params, 'familymember_group_membership_status'));
+
+ // subquery returning
for every familymember in the groups.
+ // E.g. if little Jimmy (id 3) in Youth Group has parents with ids 1 and 2, this returns (1,"Jimmy"),(2,"Jimmy").
+ // Note, Jimmy himself is excluded, i.e. we treat "being the family member in the group" (Jimmy) as different to "having a family member in the group" (his parents), and assume we usually want the latter.
+ $familymember_group_members_sql = "SELECT p.id AS personid, concat(familyperson.first_name, ' ',familyperson.last_name) AS familymember_name
+ FROM person p
+ JOIN person familyperson USING (familyid)
+ JOIN person_group_membership pgm ON pgm.personid = familyperson.id
+ JOIN person_group pg ON pgm.groupid = pg.id
+ WHERE
+ p.id <> familyperson.id
+ AND (".$include_familymember_groupids_clause.')';
+
+ $query['from'] .= ' JOIN ('.$familymember_group_members_sql.') familymember_in_required_group ON (familymember_in_required_group.personid = p.id)';
+// $query['where'][] = 'familymember_in_required_group IN ('.$familymember_group_members_sql.')';
+ }
if (!empty($params['exclude_groups'])) {
@@ -1297,6 +1381,9 @@ function getSQL($custom_select_fields=NULL)
';
$query['select'][] = 'all_members.names as `All Family Members`';
break;
+ case 'familymember_group_members':
+ $query['select'][] = 'familymember_in_required_group.familymember_name as `Family Member In Group`';
+ break;
case 'adult_members':
/*
* For a left join to be efficient we need to