/**
  * Look up an array of CiviCRM groups linked to Maichimp groupings.
  *
  * Indexed by CiviCRM groupId, including:
  *
  * - list_id    (MC)
  * - grouping_id(MC)
  * - group_id   (MC)
  * - is_mc_update_grouping (bool) - is the subscriber allowed to update this via MC interface?
  * - group_name (MC)
  * - grouping_name (MC)
  * - civigroup_title
  * - civigroup_uses_cache boolean
  *
  * @param $groupIDs mixed array of CiviCRM group Ids to fetch data for; or empty to return ALL mapped groups.
  * @param $mc_list_id mixed Fetch for a specific Mailchimp list only, or null.
  * @param $membership_only bool. Only fetch mapped membership groups (i.e. NOT linked to a MC grouping).
  *
  */
 static function getGroupsToSync($groupIDs = array(), $mc_list_id = null, $membership_only = FALSE)
 {
     CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils getGroupsToSync $groupIDs', $groupIDs);
     CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils getGroupsToSync $mc_list_id', $mc_list_id);
     CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Utils getGroupsToSync $membership_only', $membership_only);
     $params = $groups = $temp = array();
     foreach ($groupIDs as $value) {
         if ($value) {
             $temp[] = $value;
         }
     }
     $groupIDs = $temp;
     if (!empty($groupIDs)) {
         $groupIDs = implode(',', $groupIDs);
         $whereClause = "entity_id IN ({$groupIDs})";
     } else {
         $whereClause = "1 = 1";
     }
     $whereClause .= " AND mc_list_id IS NOT NULL AND mc_list_id <> ''";
     if ($mc_list_id) {
         // just want results for a particular MC list.
         $whereClause .= " AND mc_list_id = %1 ";
         $params[1] = array($mc_list_id, 'String');
     }
     if ($membership_only) {
         $whereClause .= " AND (mc_grouping_id IS NULL OR mc_grouping_id = '')";
     }
     $query = "\n      SELECT  entity_id, mc_list_id, mc_grouping_id, mc_group_id, is_mc_update_grouping, cg.title as civigroup_title, cg.saved_search_id, cg.children\n FROM    civicrm_value_mailchimp_settings mcs\n      INNER JOIN civicrm_group cg ON mcs.entity_id = cg.id\n      WHERE {$whereClause}";
     $dao = CRM_Core_DAO::executeQuery($query, $params);
     while ($dao->fetch()) {
         $groups[$dao->entity_id] = array('list_id' => $dao->mc_list_id, 'grouping_id' => $dao->mc_grouping_id, 'group_id' => $dao->mc_group_id, 'is_mc_update_grouping' => $dao->is_mc_update_grouping, 'group_name' => CRM_Mailchimp_Utils::getMCGroupName($dao->mc_list_id, $dao->mc_grouping_id, $dao->mc_group_id), 'grouping_name' => CRM_Mailchimp_Utils::getMCGroupingName($dao->mc_list_id, $dao->mc_grouping_id), 'civigroup_title' => $dao->civigroup_title, 'civigroup_uses_cache' => (bool) ($dao->saved_search_id > 0 || (bool) $dao->children));
     }
     CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils getGroupsToSync $groupIDs', $groupIDs);
     CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils getGroupsToSync $mc_list_id', $mc_list_id);
     CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils getGroupsToSync $membership_only', $membership_only);
     CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils getGroupsToSync $groups', $groups);
     return $groups;
 }