function run() { if (CRM_Utils_System::authenticateKey()) { $request_type = CRM_Utils_Request::retrieve('type', 'String'); $request_data = CRM_Utils_Request::retrieve('data', 'String'); $config = CRM_Core_Config::singleton(); if ($config->debug) { $request_data_log = print_r($request_data, TRUE); CRM_Core_Error::debug_log_message("Mailchimp Webhook Request [{$request_type}]: \n{$request_data_log}"); } $function_name = 'self::mailchimpWebhook' . ucwords($request_type); if (is_callable($function_name)) { // Set a canary to prevent CiviMailchimp hooks from firing, which // would trigger updates back to Mailchimp, resulting in an endless // loop. civimailchimp_static('mailchimp_do_not_run_hooks', TRUE); try { call_user_func($function_name, $request_data); } catch (Exception $e) { $error = array('code' => get_class($e), 'message' => $e->getMessage(), 'exception' => $e); $message = "Mailchimp Webhook Request [{$request_type}]: {$error['code']}: {$error['message']}"; CRM_CiviMailchimp_BAO_SyncLog::saveMessage('error', 'mailchimp_to_civicrm', $message, $request_data); CRM_Core_Error::debug_var('Fatal Error Details', $error); CRM_Core_Error::backtrace('backTrace', TRUE); throw $e; } } } parent::run(); }
function civicrm_api3_civi_mailchimp_sync($params) { CRM_CiviMailchimp_BAO_SyncLog::deleteOldMessages(); $records_to_process = $params['records_to_process_per_run']; $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => FALSE)); if ($queue->numberOfItems() > 0) { $runner = new CRM_Queue_Runner(array('title' => ts('Sync Contacts to Mailchimp'), 'queue' => $queue)); $continue_to_next_item = TRUE; $records_processed = 0; while ($continue_to_next_item && $records_processed < $records_to_process) { $record = $runner->runNext(); if ($record['is_error']) { // Get the current Queue Item being worked on to allow for better error // reporting and logging. $query = "\n SELECT\n id,\n data\n FROM\n civicrm_queue_item\n WHERE\n queue_name = 'mailchimp-sync'\n ORDER BY\n weight ASC,\n id ASC\n LIMIT 1\n "; $item = CRM_Core_DAO::executeQuery($query); while ($item->fetch()) { $item_data = unserialize($item->data); $message = "[{$item_data->arguments[0]}] There was an error syncing contacts to Mailchimp."; $exception_name = ''; if (!empty($record['exception'])) { $exception_name = get_class($record['exception']); $message = "[{$item_data->arguments[0]}] {$exception_name}: {$record['exception']->getMessage()}."; } $message .= " Mailchimp List ID: {$item_data->arguments[1]}. {$records_processed} records were successfully synced before this error."; $error = array('code' => $exception_name, 'message' => $message, 'exception' => $record['exception']); CRM_Core_Error::debug_var('Fatal Error Details', $error); CRM_Core_Error::backtrace('backTrace', TRUE); CRM_CiviMailchimp_BAO_SyncLog::saveMessage('error', 'civicrm_to_mailchimp', $message, $item_data, $item->id); return civicrm_api3_create_error($message); } } $continue_to_next_item = $record['is_continue']; $records_processed++; } $message = ts('%1 records were successfully synced to Mailchimp.', array(1 => $records_processed)); CRM_CiviMailchimp_BAO_SyncLog::saveMessage('success', 'civicrm_to_mailchimp', $message); return civicrm_api3_create_success($records_processed); } }
static function forceCiviToMailchimpSync($mailchimp_sync_setting) { $contacts = CRM_CiviMailchimp_Utils::getActiveGroupMembers($mailchimp_sync_setting->civicrm_group_id); $skipped_contacts = 0; foreach ($contacts as $key => $contact_id) { $contact = CRM_CiviMailchimp_Utils::getContactById($contact_id); if ($contact->is_deleted != 1) { $email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($contact); if ($email === NULL) { ++$skipped_contacts; unset($contacts[$key]); } else { $merge_fields = CRM_CiviMailchimp_Utils::getMailchimpMergeFields($mailchimp_sync_setting->mailchimp_list_id); $merge_vars = CRM_CiviMailchimp_Utils::formatMailchimpMergeVars($merge_fields, $contact); CRM_CiviMailchimp_Utils::subscribeContactToMailchimpList($mailchimp_sync_setting->mailchimp_list_id, $email, $merge_vars); } } } if ($skipped_contacts > 0) { $message = ts('%1 records were not synced to Mailchimp because they did not have a valid email address.', array(1 => $skipped_contacts)); CRM_CiviMailchimp_BAO_SyncLog::saveMessage('error', 'civicrm_to_mailchimp', $message); } return $contacts; }
static function createTestLogMessage($message, $details = NULL, $direction = 'civicrm_to_mailchimp', $type = 'error', $civicrm_queue_item_id = NULL) { $mailchimp_sync_log = CRM_CiviMailchimp_BAO_SyncLog::saveMessage($type, $direction, $message, $details, $civicrm_queue_item_id); return $mailchimp_sync_log; }