/**
  * Save Mailchimp sync settings.
  */
 static function saveSettings($params)
 {
     $transaction = new CRM_Core_Transaction();
     try {
         $existing_mailchimp_sync_setting = self::findByGroupId($params['civicrm_group_id']);
         if ($existing_mailchimp_sync_setting) {
             $params['id'] = $existing_mailchimp_sync_setting->id;
             CRM_CiviMailchimp_BAO_InterestGroupsSyncSettings::deleteAllForSyncSettingsId($existing_mailchimp_sync_setting->id);
         }
         $mailchimp_sync_setting = new CRM_CiviMailchimp_BAO_SyncSettings();
         $mailchimp_sync_setting->copyValues($params);
         $mailchimp_sync_setting->save();
         if (isset($params['mailchimp_interest_groups'])) {
             CRM_CiviMailchimp_BAO_InterestGroupsSyncSettings::saveSettings($mailchimp_sync_setting, $params['mailchimp_interest_groups']);
         }
         if (!$existing_mailchimp_sync_setting) {
             CRM_CiviMailchimp_Utils::addWebhookToMailchimpList($mailchimp_sync_setting->mailchimp_list_id);
         }
     } catch (Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     $transaction->commit();
     return $mailchimp_sync_setting;
 }
 function postProcess()
 {
     $values = $this->exportValues();
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettings::findByGroupId($values['group']);
     $mailchimp_export_url = CRM_CiviMailchimp_Utils::formatMailchimpExportApiUrl($mailchimp_sync_setting->mailchimp_list_id);
     list($contacts, $mailchimp_members) = self::processForcedSync($mailchimp_sync_setting, $mailchimp_export_url);
     parent::postProcess();
     CRM_Core_Session::setStatus(ts("%1 contacts were synced to Mailchimp and %2  Mailchimp members were synced to CiviCRM.", array(1 => count($contacts), 2 => count($mailchimp_members))), ts('CiviMailchimp Force Sync Successful'), 'success');
     CRM_Utils_System::redirect($this->controller->_entryURL);
 }
 static function createTestSettings($group_id, $mailchimp_list_id = 'MailchimpListsTestListA', $mailchimp_interest_groups = array())
 {
     $params = array('civicrm_group_id' => $group_id, 'mailchimp_list_id' => $mailchimp_list_id, 'mailchimp_interest_groups' => $mailchimp_interest_groups);
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettings::saveSettings($params);
     return $mailchimp_sync_setting;
 }
/**
 * Implements hook_civicrm_pre for Group delete.
 */
function civimailchimp_civicrm_pre_Group_delete($group_id, &$group)
{
    $mailchimp_sync_settings = CRM_CiviMailchimp_BAO_SyncSettings::findByGroupId($group_id);
    if ($mailchimp_sync_settings) {
        civimailchimp_static('mailchimp_sync_settings', $mailchimp_sync_settings);
    }
}
 function test_civimailchimp_civicrm_postProcess_no_list()
 {
     $mailchimp_list_id = 'MailchimpListsTestListA';
     $mailchimp_interest_groups = array('MailchimpTestInterestGroupingA_MailchimpTestInterestGroupA', 'MailchimpTestInterestGroupingA_MailchimpTestInterestGroupC');
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group postProcess_CRM_Group_Form_Edit_no_list', $mailchimp_list_id, $mailchimp_interest_groups);
     $formName = 'CRM_Group_Form_Edit';
     $form = $this->getMock('CRM_Core_Form');
     $form->expects($this->once())->method('getVar')->will($this->returnValue($mailchimp_sync_setting->civicrm_group_id));
     $form->expects($this->never())->method('get')->will($this->returnValue(TRUE));
     $form->_elementIndex['mailchimp_list'] = TRUE;
     $form->_submitValues = array('mailchimp_list' => NULL);
     civimailchimp_civicrm_postProcess($formName, $form);
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettings::findByGroupId($mailchimp_sync_setting->civicrm_group_id);
     $this->assertEmpty($mailchimp_sync_setting);
 }
 /**
  * Remove a given contact from the group set to sync with the given
  * Mailchimp list.
  */
 static function removeContactFromGroup($contact, $mailchimp_list_id)
 {
     $contact_ids = array($contact->id);
     $mailchimp_sync_settings = CRM_CiviMailchimp_BAO_SyncSettings::findByListId($mailchimp_list_id);
     CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contact_ids, $mailchimp_sync_settings->civicrm_group_id);
     self::markContactAsNoBulkEmailsIfSettingEnabled($contact);
 }