Survey form - use number widget for number fields
[civicrm-core.git] / Civi / ActionSchedule / RecipientBuilder.php
index 7e568aae329511390328766cad85c89b87d36661..b72bbdada5ff386d4999721238034df8ab4646b8 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -314,7 +314,7 @@ class RecipientBuilder {
 
     $addlCheck = \CRM_Utils_SQL_Select::from($query['casAddlCheckFrom'])
       ->select('*')
-      ->merge($query, array('params', 'wheres'))// why only where? why not the joins?
+      ->merge($query, array('params', 'wheres', 'joins'))
       ->merge($this->prepareRepetitionEndFilter($query['casDateField']))
       ->limit(1)
       ->strict()
@@ -419,18 +419,31 @@ class RecipientBuilder {
     $actionSchedule = $this->actionSchedule;
 
     if ($actionSchedule->group_id) {
-      if ($this->isSmartGroup($actionSchedule->group_id)) {
-        // Check that the group is in place in the cache and up to date
-        \CRM_Contact_BAO_GroupContactCache::check($actionSchedule->group_id);
-        return \CRM_Utils_SQL_Select::fragment()
-          ->join('grp', "INNER JOIN civicrm_group_contact_cache grp ON {$contactIdField} = grp.contact_id")
-          ->where(" grp.group_id IN ({$actionSchedule->group_id})");
+      $regularGroupIDs = $smartGroupIDs = $groupWhereCLause = array();
+      $query = \CRM_Utils_SQL_Select::fragment();
+
+      // get child group IDs if any
+      $childGroupIDs = \CRM_Contact_BAO_Group::getChildGroupIds($actionSchedule->group_id);
+      foreach (array_merge(array($actionSchedule->group_id), $childGroupIDs) as $groupID) {
+        if ($this->isSmartGroup($groupID)) {
+          // Check that the group is in place in the cache and up to date
+          \CRM_Contact_BAO_GroupContactCache::check($groupID);
+          $smartGroupIDs[] = $groupID;
+        }
+        else {
+          $regularGroupIDs[] = $groupID;
+        }
       }
-      else {
-        return \CRM_Utils_SQL_Select::fragment()
-          ->join('grp', " INNER JOIN civicrm_group_contact grp ON {$contactIdField} = grp.contact_id AND grp.status = 'Added'")
-          ->where(" grp.group_id IN ({$actionSchedule->group_id})");
+
+      if (!empty($smartGroupIDs)) {
+        $query->join('sg', "LEFT JOIN civicrm_group_contact_cache sg ON {$contactIdField} = sg.contact_id");
+        $groupWhereCLause[] = " sg.group_id IN ( " . implode(', ', $smartGroupIDs) . " ) ";
+      }
+      if (!empty($regularGroupIDs)) {
+        $query->join('rg', " LEFT JOIN civicrm_group_contact rg ON {$contactIdField} = rg.contact_id AND rg.status = 'Added'");
+        $groupWhereCLause[] = " rg.group_id IN ( " . implode(', ', $regularGroupIDs) . " ) ";
       }
+      return $query->where(implode(" OR ", $groupWhereCLause));
     }
     elseif (!empty($actionSchedule->recipient_manual)) {
       $rList = \CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String');