Exemple #1
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);
}
            $liveDB->executeNoresSQL($sql, $affectedRows);
        } catch (DBAdapter2Exception $e) {
            print "\n" . date("d/m/y : H:i:s", time()) . " AUTO MYSQLl: Error: " . $e->getMessage() . " *** QUERY: {$sql}";
        }
    }
}
// the main execution
while (true) {
    echo "\nNext round ";
    $groups = fetchGroupsByUrl(300);
    if (count($groups) == 0) {
        echo "\n No more groups to find by url \n";
        break;
    }
    foreach ($groups as $group) {
        //echo "\n".$group['id_list']."\n\n";
        cleanGroup($group);
    }
}
while (true) {
    echo "\nNext round ";
    $groups = fetchGroups(300);
    if (count($groups) == 0) {
        echo "\n No more to find  by uniqueness \n";
        break;
    }
    foreach ($groups as $group) {
        //echo "\n".$group['id_list']."\n\n";
        cleanGroup($group);
    }
}
        }
    }
}
// the main execution
foreach ($l_countries as $oneC) {
    echo "\n Anti duplicating jobs in " . $oneC['reg'] . " \n ";
    $liveDB = new DBAdapter2(array('host' => $dbConf->live->host, 'username' => $dbConf->live->username, 'password' => $dbConf->live->password, 'schema' => $oneC['schemali']));
    while (true) {
        echo "\nNext round(by URL) ";
        $groups = fetchGroupsBySameUrl(500, $oneC);
        if (count($groups) < 10) {
            echo "\n No more to find \n";
            break;
        }
        foreach ($groups as $group) {
            cleanGroup($group, $oneC);
        }
    }
    while (true) {
        echo "\nNext round ";
        $groups = fetchGroups(500, $oneC);
        if (count($groups) < 10) {
            echo "\n No more to find \n";
            break;
        }
        foreach ($groups as $group) {
            cleanGroup($group, $oneC);
        }
    }
    unset($liveDB);
}