function civicrm_api3_mailchimp_synchronize($params)
{
    $return = $tags = $createdContact = array();
    $apiKey = CRM_Core_BAO_Setting::getItem('MailChimp Preferences', 'api_key', NULL, FALSE);
    $tagsCount = civicrm_api3('Tag', 'getcount', array('sequential' => 1));
    $tagsGet = civicrm_api3('Tag', 'get', array('sequential' => 1, 'rowCount' => $tagsCount));
    foreach ($tagsGet['values'] as $tagIds => $tagsValues) {
        $tags[$tagsValues['id']] = $tagsValues['name'];
    }
    $mcClient = new Mailchimp($apiKey);
    $mcLists = new Mailchimp_Lists($mcClient);
    $lists = $mcLists->getList();
    foreach ($lists['data'] as $listsDetails) {
        if (!in_array($listsDetails['name'], $tags)) {
            $tagsCreate = civicrm_api3('Tag', 'create', array('sequential' => 1, 'name' => $listsDetails['name']));
        }
        $tagsCount = civicrm_api3('Tag', 'getcount', array('sequential' => 1));
        $tagsGet = civicrm_api3('Tag', 'get', array('sequential' => 1, 'rowCount' => $tagsCount));
        foreach ($tagsGet['values'] as $tagIds => $tagsValues) {
            $tags[$tagsValues['id']] = $tagsValues['name'];
        }
        if (in_array($listsDetails['name'], $tags)) {
            $keyTags = array_search($listsDetails['name'], $tags);
            $members = $mcLists->members($listsDetails['id']);
            foreach ($members['data'] as $key => $value) {
                try {
                    $createdContact = civicrm_api3('Contact', 'create', array('sequential' => 1, 'contact_type' => "Individual", 'first_name' => $value['merges']['FNAME'], 'last_name' => $value['merges']['LNAME'], 'email' => $value['merges']['EMAIL'], 'api.EntityTag.create' => array('entity_id' => "\$value.id", 'entity_table' => "civicrm_contact", 'tag_id' => $keyTags), 'dupe_check' => true));
                } catch (CiviCRM_API3_Exception $e) {
                    $result = civicrm_api3('EntityTag', 'create', array('contact_id' => $e->getExtraParams()['ids'][0], 'tag_id' => $keyTags));
                }
            }
        }
    }
    return civicrm_api3_create_success();
}
/**
 * Mailchimp Get all Mailchimp Lists & Groups API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_mailchimp_getlistsandgroups($params)
{
    $mcLists = new Mailchimp_Lists(CRM_Mailchimp_Utils::mailchimp());
    $results = $mcLists->getList();
    $lists = array();
    foreach ($results['data'] as $list) {
        $lists[$list['id']]['name'] = $list['name'];
        $lists[$list['id']]['id'] = $list['id'];
        $params = array('id' => $list['id']);
        $group_results = civicrm_api3_mailchimp_getgroups($params);
        if (!empty($group_results)) {
            $lists[$list['id']]['grouping'] = $group_results['values'];
        }
    }
    return civicrm_api3_create_success($lists);
}
示例#3
0
 //$request_token loginradius token get from social/traditional interface after success authentication.
 $accesstoken = $accesstoken->access_token;
 $userProfileData = $socialLoginObject->getUserProfiledata($accesstoken);
 if (!empty($userProfileData->Email)) {
     $responseArr = ['status' => "success", "msg" => "Successfully logged in"];
     /*
      * Call MailChimp here
      */
     $userData = ['FNAME' => $userProfileData->FirstName, 'LNAME' => $userProfileData->LastName];
     foreach ($userProfileData->Email as $email) {
         $userData['EMAIL'] = $email->Value;
     }
     $mailChimpObj = new Mailchimp(MAILCHIMP_API_KEY);
     $mailChimpListsObj = new Mailchimp_Lists($mailChimpObj);
     //(MAILCHIMP_API_KEY);
     $allList = $mailChimpListsObj->getList();
     if (intval($allList["total"]) >= 1) {
         $subscriberListId = null;
         foreach ($allList['data'] as $lists) {
             if (stristr($lists['name'], "test")) {
                 $subscriberListId = $lists['id'];
             }
             if (!empty($subscriberListId)) {
                 break;
             }
         }
         if (!empty($subscriberListId)) {
             foreach ($userProfileData->Email as $email) {
                 try {
                     $data = $mailChimpListsObj->Subscribe($subscriberListId, array("email" => trim($email->Value)), $userData);
                 } catch (Exception $exp) {