static function forceMailchimpToCiviSync($mailchimp_export_url, $mailchimp_sync_setting)
 {
     $mailchimp_members = CRM_CiviMailchimp_Utils::getAllMembersOfMailchimpList($mailchimp_export_url, $mailchimp_sync_setting->mailchimp_list_id);
     foreach ($mailchimp_members as $mailchimp_member) {
         CRM_CiviMailchimp_Page_Webhook::mailchimpWebhookSubscribe($mailchimp_member);
     }
     return $mailchimp_members;
 }
 static function saveSettings($mailchimp_sync_setting, $interest_groups)
 {
     // Get the interest groups from Mailchimp again so we can associate a
     // name with the interest group id.
     $mailchimp_interest_groups = CRM_CiviMailchimp_Utils::getInterestGroups($mailchimp_sync_setting->mailchimp_list_id);
     foreach ($interest_groups as $interest_group) {
         $interest_group = explode('_', $interest_group);
         $interest_grouping_id = $interest_group[0];
         $interest_group_id = $interest_group[1];
         // Mailchimp expects the interest group name rather than id when
         // making API subscription requests. However, since the name can be
         // changed and is not unique, we're storing both the id and name.
         $interest_group_name = $mailchimp_interest_groups[$interest_grouping_id][$interest_group_id];
         $mailchimp_interest_group_sync_setting = new CRM_CiviMailchimp_BAO_InterestGroupsSyncSettings();
         $mailchimp_interest_group_sync_setting->civimailchimp_sync_settings_id = $mailchimp_sync_setting->id;
         $mailchimp_interest_group_sync_setting->mailchimp_interest_grouping_id = $interest_grouping_id;
         $mailchimp_interest_group_sync_setting->mailchimp_interest_group_id = $interest_group_id;
         $mailchimp_interest_group_sync_setting->mailchimp_interest_group_name = $interest_group_name;
         $mailchimp_interest_group_sync_setting->save();
     }
 }
 function test_civicrm_api3_civi_mailchimp_sync()
 {
     $action = 'subscribeContactToMailchimpList';
     $mailchimp_list_id = 'MailchimpListsTestListA';
     $mailchimp_interest_groups = array('MailchimpTestInterestGroupingA_MailchimpTestInterestGroupA', 'MailchimpTestInterestGroupingA_MailchimpTestInterestGroupC');
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group test_civicrm_api3_civi_mailchimp_sync', $mailchimp_list_id, $mailchimp_interest_groups);
     $merge_fields = CRM_CiviMailchimp_Utils::getMailchimpMergeFields();
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $merge_vars = CRM_CiviMailchimp_Utils::formatMailchimpMergeVars($merge_fields, $contact);
     CRM_CiviMailchimp_Utils::addMailchimpSyncQueueItem($action, $mailchimp_list_id, $params['email'][0]['email'], $merge_vars);
     $action = 'unsubscribeContactFromMailchimpList';
     CRM_CiviMailchimp_Utils::addMailchimpSyncQueueItem($action, $mailchimp_list_id, $params['email'][0]['email'], $merge_vars);
     $job_params['records_to_process_per_run'] = 100;
     civicrm_api3_civi_mailchimp_sync($job_params);
     $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => FALSE));
     $this->assertEquals(0, $queue->numberOfItems());
 }
 /**
  * Create initial list of webhooks.
  */
 protected function defaultWebhooks()
 {
     $webhook_url = CRM_CiviMailchimp_Utils::formatMailchimpWebhookUrl();
     $webhooks = array('MailchimpListsTestListA' => $webhook_url);
     return $webhooks;
 }
 function testClearQueueItem()
 {
     $mailchimp_list_id = 'MailchimpListsTestListA';
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group testClearQueueItem', $mailchimp_list_id);
     $group = CRM_CiviMailchimp_Utils::getGroupById($mailchimp_sync_setting->civicrm_group_id);
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => TRUE));
     civimailchimp_civicrm_contact_removed_from_group($group, $contact);
     $item = $queue->claimItem($lease_time = 0);
     $existing_mailchimp_sync_log = self::createTestLogMessage('This is a test error message', $details = NULL, $direction = 'civicrm_to_mailchimp', $type = 'error', $item->id);
     CRM_CiviMailchimp_BAO_SyncLog::clearQueueItem($item->id);
     $query = "\n      SELECT\n        *\n      FROM\n        civicrm_queue_item\n      WHERE\n        queue_name = 'mailchimp-sync'\n      AND\n        id = %1\n      LIMIT 1\n      ";
     $params = array(1 => array($item->id, 'Integer'));
     $new_item = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
     $mailchimp_sync_log = CRM_CiviMailchimp_BAO_SyncLog::findById($existing_mailchimp_sync_log->id);
     $this->assertFalse($new_item->fetch());
     $this->assertNull($mailchimp_sync_log->civicrm_queue_item_id);
 }
 /**
  * Delete Mailchimp sync settings.
  */
 static function deleteSettings($mailchimp_sync_setting)
 {
     $transaction = new CRM_Core_Transaction();
     try {
         $mailchimp_sync_setting->delete();
         CRM_CiviMailchimp_Utils::deleteWebhookFromMailchimpList($mailchimp_sync_setting->mailchimp_list_id);
     } catch (Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     $transaction->commit();
     return $mailchimp_sync_setting;
 }
 /**
  * Put a Mailchimp subscriber's email On Hold in CiviCRM.
  */
 static function mailchimpWebhookCleaned($request_data)
 {
     $mailchimp_contact = CRM_CiviMailchimp_Utils::getContactInMailchimpListByEmail($request_data['email'], $request_data['list_id']);
     foreach ($mailchimp_contact->email as $email) {
         if ($email->email === $request_data['email']) {
             // We have to go the circuitous route to saving so we can trigger
             // CiviCRM's hooks to allow other extensions to act.
             $params = array();
             CRM_Core_DAO::storeValues($email, $params);
             $params['on_hold'] = 1;
             CRM_Core_BAO_Email::add($params);
         }
     }
 }
 function testMailchimpWebhookCleaned()
 {
     $sample_data = self::sampleRequestSubscribeOrProfileUpdate();
     $contact = CRM_CiviMailchimp_Utils::createContactFromMailchimpRequest($sample_data);
     $sync_settings = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('test_group_mailchimp_webhook_cleaned');
     CRM_Contact_BAO_GroupContact::addContactsToGroup(array($contact->id), $sync_settings->civicrm_group_id);
     $email2 = new CRM_Core_BAO_Email();
     $email2->contact_id = $contact->id;
     $email2->email = 'civimailchimp_cleaned_test' . rand() . '@civimailchimp.org';
     $email2->save();
     $clean_request = self::getCleanRequestFromSampleProfileData($sample_data);
     CRM_CiviMailchimp_Page_Webhook::mailchimpWebhookCleaned($clean_request);
     $mailchimp_contact = CRM_CiviMailchimp_Utils::getContactById($contact->id);
     $this->assertEquals($mailchimp_contact->email[0]->on_hold, 1);
     $this->assertEquals($mailchimp_contact->email[1]->on_hold, 0);
 }
/**
 * Implementation of hook_civicrm_install
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
 */
function civimailchimp_civicrm_install()
{
    _civimailchimp_civix_civicrm_install();
    CRM_CiviMailchimp_Utils::createSyncScheduledJob();
}