Example #1
0
function civicrm_api3_speakcivi_sendconfirm($params)
{
    $confirmationBlock = $params['confirmation_block'];
    $contactId = $params['contact_id'];
    $campaignId = $params['campaign_id'];
    $activityId = $params['activity_id'];
    $campaignObj = new CRM_Speakcivi_Logic_Campaign($campaignId);
    $locale = $campaignObj->getLanguage();
    $params['from'] = $campaignObj->getSenderMail();
    $params['format'] = null;
    if ($confirmationBlock) {
        $params['subject'] = $campaignObj->getSubjectNew();
        $message = $campaignObj->getMessageNew();
    } else {
        $params['subject'] = $campaignObj->getSubjectCurrent();
        $message = $campaignObj->getMessageCurrent();
    }
    if (!$message) {
        if ($confirmationBlock) {
            $message = CRM_Speakcivi_Tools_Dictionary::getMessageNew($locale);
            $campaignObj->setCustomFieldBySQL($campaignId, $campaignObj->fieldMessageNew, $message);
        } else {
            $message = CRM_Speakcivi_Tools_Dictionary::getMessageCurrent($locale);
            $campaignObj->setCustomFieldBySQL($campaignId, $campaignObj->fieldMessageCurrent, $message);
        }
    }
    $contact = array();
    $params_contact = array('id' => $contactId, 'sequential' => 1);
    $result = civicrm_api3('Contact', 'get', $params_contact);
    if ($result['count'] == 1) {
        $contact = $result['values'][0];
    }
    /* CONFIRMATION_BLOCK */
    $hash = sha1(CIVICRM_SITE_KEY . $contactId);
    $utm_content = 'version_' . $contactId % 2;
    $utm_campaign = $campaignObj->getUtmCampaign();
    $url_confirm_and_keep = CRM_Utils_System::url('civicrm/speakcivi/confirm', "id={$contactId}&aid={$activityId}&cid={$campaignId}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign={$utm_campaign}&utm_content={$utm_content}", true);
    $url_confirm_and_not_receive = CRM_Utils_System::url('civicrm/speakcivi/optout', "id={$contactId}&aid={$activityId}&cid={$campaignId}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign={$utm_campaign}&utm_content={$utm_content}", true);
    $template = CRM_Core_Smarty::singleton();
    $template->assign('url_confirm_and_keep', $url_confirm_and_keep);
    $template->assign('url_confirm_and_not_receive', $url_confirm_and_not_receive);
    $locales = getLocale($locale);
    $confirmation_block_html = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['html'] . '.html.tpl');
    $confirmation_block_text = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['text'] . '.text.tpl');
    /* SHARING_BLOCK */
    $template->clearTemplateVars();
    $template->assign('url_campaign', $campaignObj->getUrlCampaign());
    $template->assign('url_campaign_fb', prepareCleanUrl($campaignObj->getUrlCampaign()));
    $template->assign('utm_campaign', $campaignObj->getUtmCampaign());
    $template->assign('share_facebook', CRM_Speakcivi_Tools_Dictionary::getShareFacebook($locale));
    $template->assign('share_twitter', CRM_Speakcivi_Tools_Dictionary::getShareTwitter($locale));
    $template->assign('twitter_share_text', urlencode($campaignObj->getTwitterShareText()));
    $sharing_block_html = $template->fetch('../templates/CRM/Speakcivi/Page/SharingBlock.html.tpl');
    $template->clearTemplateVars();
    $template->assign('contact', $contact);
    $message = $template->fetch('string:' . $message);
    $message_html = str_replace("#CONFIRMATION_BLOCK", html_entity_decode($confirmation_block_html), $message);
    $message_text = str_replace("#CONFIRMATION_BLOCK", html_entity_decode($confirmation_block_text), $message);
    $message_html = str_replace("#SHARING_BLOCK", html_entity_decode($sharing_block_html), $message_html);
    $message_text = str_replace("#SHARING_BLOCK", html_entity_decode($sharing_block_html), $message_text);
    $params['html'] = $message_html;
    $params['text'] = convertHtmlToText($message_text);
    $sent = CRM_Utils_Mail::send($params);
    return civicrm_api3_create_success($sent, $params);
}
Example #2
0
function civicrm_api3_speakcivi_remind($params)
{
    // how old not confirmed petitions
    $start = microtime(true);
    $days = $params['days'];
    $daysContact = $params['days_contact'];
    if ($daysContact < $days) {
        $daysContact = $days;
    }
    $groupId = CRM_Core_BAO_Setting::getItem('Speakcivi API Preferences', 'group_id');
    $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', 'Petition', 'name', 'String', 'value');
    $adminId = 1;
    $query = "SELECT acp.activity_id, ap.campaign_id, acp.contact_id\n            FROM civicrm_activity ap\n              JOIN civicrm_activity_contact acp ON acp.activity_id = ap.id\n              JOIN civicrm_contact c ON c.id = acp.contact_id\n              LEFT JOIN civicrm_group_contact gc ON gc.contact_id = acp.contact_id AND gc.group_id = %1 AND gc.status = 'Added'\n            WHERE ap.activity_type_id = %2 AND ap.status_id = 1 AND ap.activity_date_time <= date_add(current_date, INTERVAL -%3 DAY)\n                AND c.created_date >= date_add(current_date, INTERVAL -%4 DAY)\n                AND c.is_opt_out = 0 AND c.is_deleted = 0 AND c.is_deceased = 0 AND c.do_not_email = 0 AND gc.id IS NULL";
    $params = array(1 => array($groupId, 'Integer'), 2 => array($activityTypeId, 'Integer'), 3 => array($days, 'Integer'), 4 => array($daysContact, 'Integer'));
    $dao = CRM_Core_DAO::executeQuery($query, $params);
    $contacts = array();
    $campaigns = array();
    while ($dao->fetch()) {
        $contacts[$dao->campaign_id][$dao->contact_id] = $dao->contact_id;
        $campaigns[$dao->campaign_id] = $dao->campaign_id;
    }
    $message = array();
    $subject = array();
    $utmCampaign = array();
    $locale = array();
    $language = array();
    $email = array();
    foreach ($campaigns as $cid) {
        $campaignObj = new CRM_Speakcivi_Logic_Campaign($cid);
        $message[$cid] = $campaignObj->getMessageNew();
        $subject[$cid] = substr(removeSmartyIfClause(convertTokens($campaignObj->getSubjectNew())), 0, 128);
        $utmCampaign[$cid] = $campaignObj->getUtmCampaign();
        $locale[$cid] = $campaignObj->getLanguage();
        $language[$cid] = strtoupper(substr($locale[$cid], 0, 2));
        $email[$cid] = parseSenderEmail($campaignObj->getSenderMail());
    }
    // fetch confirmation block
    $messageHtml = array();
    $messageText = array();
    foreach ($message as $cid => $msg) {
        $url_confirm_and_keep = CRM_Utils_System::url('civicrm/speakcivi/confirm', null, true) . "?id={contact.contact_id}&cid={$cid}&hash={speakcivi.confirmation_hash}&utm_source=civicrm&utm_medium=email&utm_campaign=" . $utmCampaign[$cid];
        $url_confirm_and_not_receive = CRM_Utils_System::url('civicrm/speakcivi/optout', null, true) . "?id={contact.contact_id}&cid={$cid}&hash={speakcivi.confirmation_hash}&utm_source=civicrm&utm_medium=email&utm_campaign=" . $utmCampaign[$cid];
        $locales = getLocale($locale[$cid]);
        $confirmationBlockHtml = implode('', file(dirname(__FILE__) . '/../../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['html'] . '.html.tpl'));
        $confirmationBlockText = implode('', file(dirname(__FILE__) . '/../../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['text'] . '.text.tpl'));
        $confirmationBlockHtml = str_replace('{$url_confirm_and_keep}', $url_confirm_and_keep, $confirmationBlockHtml);
        $confirmationBlockHtml = str_replace('{$url_confirm_and_not_receive}', $url_confirm_and_not_receive, $confirmationBlockHtml);
        $confirmationBlockText = str_replace('{$url_confirm_and_keep}', $url_confirm_and_keep, $confirmationBlockText);
        $confirmationBlockText = str_replace('{$url_confirm_and_not_receive}', $url_confirm_and_not_receive, $confirmationBlockText);
        $messageHtml[$cid] = removeSmartyIfClause(convertTokens(removeDelim(str_replace("#CONFIRMATION_BLOCK", $confirmationBlockHtml, $msg))));
        $messageText[$cid] = convertHtmlToText(removeSmartyIfClause(convertTokens(removeDelim(str_replace("#CONFIRMATION_BLOCK", $confirmationBlockText, $msg)))));
    }
    foreach ($campaigns as $cid) {
        $sentContacts = findSentContacts($cid);
        $contacts[$cid] = excludeContacts($contacts[$cid], $sentContacts);
        if (is_array($contacts[$cid]) && count($contacts[$cid]) > 0) {
            if ($mailingId = findNotCompletedMailing($cid)) {
                if ($linkedGroupId = findLinkedGroup($mailingId)) {
                    addContactsToGroup($contacts[$cid], $linkedGroupId);
                } else {
                    $includeGroupId = createGroup($cid, $language[$cid]);
                    addContactsToGroup($contacts[$cid], $includeGroupId);
                    includeGroup($mailingId, $includeGroupId);
                }
            } else {
                $name = determineMailingName($cid, $language[$cid]);
                $params = array('name' => $name, 'subject' => $subject[$cid], 'body_text' => $messageText[$cid], 'body_html' => $messageHtml[$cid], 'created_id' => $adminId, 'created_date' => date('YmdHis'), 'campaign_id' => $cid, 'mailing_type' => 'standalone', 'unsubscribe_id' => 5, 'resubscribe_id' => 6, 'optout_id' => 7, 'reply_id' => 8, 'open_tracking' => 1, 'url_tracking' => 1, 'dedupe_email' => 1, 'from_name' => $email[$cid]['from_name'], 'from_email' => $email[$cid]['from_email'], 'footer_id' => chooseFooter($language[$cid]));
                $mailing = new CRM_Mailing_BAO_Mailing();
                $mm = $mailing->add($params);
                excludeGroup($mm->id, $groupId);
                if ($existingGroupId = findExistingGroup($cid)) {
                    cleanGroup($existingGroupId);
                    addContactsToGroup($contacts[$cid], $existingGroupId);
                    includeGroup($mm->id, $existingGroupId);
                } else {
                    $includeGroupId = createGroup($cid, $language[$cid]);
                    addContactsToGroup($contacts[$cid], $includeGroupId);
                    includeGroup($mm->id, $includeGroupId);
                }
            }
        }
    }
    $results = array('time' => microtime(true) - $start);
    return civicrm_api3_create_success($results, $params);
}