-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug/mailchip ajax cleanup #5267
Changes from 19 commits
f3df571
8ca99a6
5b6efeb
cd54417
f8244a9
1ebbe6d
b62d447
a2e8fba
2e8a6f8
405c3c2
f017455
62d65d1
48162e9
64c61b4
a554f6f
9fa6bcc
9f96f3b
83b51ad
ea1b24c
541b6e9
50dcc5d
6922833
d3da5e6
86766d0
f99f832
07e0ff8
8f29a88
c58233a
34459cd
a634767
49a1fbe
243db13
3272963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,103 +6,101 @@ | |
use \DrewM\MailChimp\MailChimp; | ||
use ChurchCRM\Utils\LoggerUtils; | ||
use ChurchCRM\Utils\ExecutionTime; | ||
|
||
class ListEmailFilter { | ||
private $email; | ||
|
||
function __construct($emailAddress) | ||
{ | ||
$this->email = $emailAddress; | ||
} | ||
public function isEmailInList($list) { | ||
foreach ($list['members'] as $listMember) { | ||
if (strcmp(strtolower($listMember['email_address']), strtolower($this->email)) == 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
use PHPMailer\PHPMailer\Exception; | ||
|
||
class MailChimpService | ||
{ | ||
private $hasKey = false; | ||
private $isActive = false; | ||
private $myMailchimp; | ||
private $lists; | ||
|
||
public function __construct() | ||
{ | ||
if (!empty(SystemConfig::getValue('sMailChimpApiKey'))) { | ||
$this->isActive = true; | ||
$this->hasKey = true; | ||
$this->myMailchimp = new MailChimp(SystemConfig::getValue('sMailChimpApiKey')); | ||
} | ||
} | ||
|
||
public function isActive() | ||
{ | ||
return $this->isActive; | ||
if ($this->isActive) { | ||
return true; | ||
} | ||
if ($this->hasKey) { | ||
$rootAPI = $this->myMailchimp->get(""); | ||
if ( $rootAPI["total_subscribers"] > 0 ) { | ||
$this->isActive = true; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
private function getListsFromCache(){ | ||
if (!isset($_SESSION['MailChimpLists'])){ | ||
LoggerUtils::getAppLogger()->debug("Updating MailChimp List Cache"); | ||
$time = new ExecutionTime; | ||
$lists = $this->myMailchimp->get("lists")['lists']; | ||
LoggerUtils::getAppLogger()->debug("MailChimp list enumeration took: ". $time->getMiliseconds(). " ms. Found ".count($lists)." lists"); | ||
foreach($lists as &$list) { | ||
$listmembers = $this->myMailchimp->get('lists/'.$list['id'].'/members',['count' => 100000]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This was the issue as 100000 was kicking back 0 subs |
||
$list['members'] = $listmembers['members']; | ||
|
||
private function getListsFromCache() | ||
{ | ||
if (!isset($_SESSION['MailChimpLists'])) { | ||
LoggerUtils::getAppLogger()->debug("Updating MailChimp List Cache"); | ||
$time = new ExecutionTime; | ||
$lists = $this->myMailchimp->get("lists")['lists']; | ||
LoggerUtils::getAppLogger()->debug("MailChimp list enumeration took: " . $time->getMiliseconds() . " ms. Found " . count($lists) . " lists"); | ||
foreach ($lists as &$list) { | ||
$list['members'] = []; | ||
$listmembers = $this->myMailchimp->get('lists/' . $list['id'] . '/members', | ||
[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra filters |
||
'count' => $list['stats']["member_count"], | ||
"fields" => "members.email_address", | ||
"status" => "subscribed" | ||
]); | ||
foreach ($listmembers['members'] as $member) { | ||
array_push($list['members'], strtolower($member["email_address"])); | ||
} | ||
LoggerUtils::getAppLogger()->debug("MailChimp list ". $list['id'] . " membership ". count($list['members'])); | ||
|
||
} | ||
LoggerUtils::getAppLogger()->debug("MailChimp list and membership update took: " . $time->getMiliseconds() . " ms"); | ||
$_SESSION['MailChimpLists'] = $lists; | ||
} else { | ||
LoggerUtils::getAppLogger()->debug("Using cached MailChimp List"); | ||
} | ||
LoggerUtils::getAppLogger()->debug("MailChimp list and membership update took: ". $time->getMiliseconds(). " ms"); | ||
$_SESSION['MailChimpLists'] = $lists; | ||
} | ||
else{ | ||
LoggerUtils::getAppLogger()->debug("Using cached MailChimp List"); | ||
} | ||
return $_SESSION['MailChimpLists']; | ||
return $_SESSION['MailChimpLists']; | ||
} | ||
|
||
public function isEmailInMailChimp($email) | ||
{ | ||
if (!$this->isActive) { | ||
return 'Mailchimp is not active'; | ||
if (empty($email)) { | ||
return new Exception(gettext('No email passed in')); | ||
} | ||
if ($email == '') { | ||
return 'No email'; | ||
|
||
if (!$this->isActive()) { | ||
return new Exception(gettext('Mailchimp is not active')); | ||
} | ||
|
||
try { | ||
$lists = $this->getListsFromCache(); | ||
$lists = array_filter($lists, array(new ListEmailFilter($email),'isEmailInList')); | ||
$listNames = array_map(function ($list) { return $list['name']; }, $lists); | ||
$listMemberships = implode(', ', $listNames); | ||
LoggerUtils::getAppLogger()->debug($email. "is a member of ".$listMemberships); | ||
|
||
return $listMemberships; | ||
} catch (\Mailchimp_Invalid_ApiKey $e) { | ||
return 'Invalid ApiKey'; | ||
} catch (\Mailchimp_List_NotSubscribed $e) { | ||
return ''; | ||
} catch (\Mailchimp_Email_NotExists $e) { | ||
return ''; | ||
} catch (\Exception $e) { | ||
return $e; | ||
$lists = $this->getListsFromCache(); | ||
$listsStatus = []; | ||
foreach ($lists as $list) { | ||
$data = $this->myMailchimp->get("lists/" . $list["id"] . "/members/" . md5($email)); | ||
LoggerUtils::getAppLogger()->debug($email . " is " . $data["status"] . " to " . $list["name"]); | ||
array_push($listsStatus, ["name" => $list["name"], "status" => $data["status"], "stats" => $data["stats"]]); | ||
} | ||
return $listsStatus; | ||
} | ||
|
||
public function getLists() | ||
{ | ||
if (!$this->isActive) { | ||
return 'Mailchimp is not active'; | ||
if (!$this->isActive()) { | ||
return new Exception(gettext('Mailchimp is not active')); | ||
} | ||
try { | ||
$result = $this->getListsFromCache(); | ||
|
||
return $result; | ||
} catch (\Mailchimp_Invalid_ApiKey $e) { | ||
return 'Invalid ApiKey'; | ||
} catch (\Exception $e) { | ||
return $e->getMessage(); | ||
return $this->getListsFromCache(); | ||
} | ||
|
||
public function getListMembers($listId) { | ||
$mailchimpLists = $this->getLists(); | ||
foreach ($mailchimpLists as $mailchimpList) { | ||
if ($listId == $mailchimpList["id"]) { | ||
LoggerUtils::getAppLogger()->debug("MailChimp list ". $listId . " has ". count($mailchimpList["members"]) . " members"); | ||
return $mailchimpList["members"]; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace ChurchCRM\Slim\Middleware; | ||
|
||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
use ChurchCRM\Service\MailChimpService; | ||
|
||
class MailChimpMiddleware { | ||
|
||
public function __invoke( Request $request, Response $response, callable $next ) | ||
{ | ||
$mailchimpService = new MailChimpService(); | ||
|
||
if (! $mailchimpService->isActive()) { | ||
return $response->withStatus(412)->withJson(["message" => gettext('Mailchimp is not active')]); | ||
} | ||
$request = $request->withAttribute("mailchimpService", $mailchimpService); | ||
return $next( $request, $response ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
use ChurchCRM\PersonQuery; | ||
use ChurchCRM\Utils\LoggerUtils; | ||
use ChurchCRM\Slim\Middleware\MailChimpMiddleware; | ||
use ChurchCRM\Slim\Middleware\Request\PersonAPIMiddleware; | ||
use ChurchCRM\Slim\Middleware\Request\FamilyAPIMiddleware; | ||
use Propel\Runtime\ActiveQuery\Criteria; | ||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
|
||
$app->group('/mailchimp/', function () { | ||
$this->get('{id}/missing', 'getMailchimpEmailNotInCRM'); | ||
$this->get('{id}/not-subscribed', 'getMailChimpMissingSubscribed'); | ||
$this->get('person/{personId}', 'getPersonStatus')->add(new PersonAPIMiddleware()); | ||
$this->get('family/{familyId}', 'getFamilyStatus')->add(new FamilyAPIMiddleware()); | ||
})->add(new MailChimpMiddleware()); | ||
|
||
function getMailchimpEmailNotInCRM(Request $request, Response $response, array $args) { | ||
|
||
$listId = $args['id']; | ||
|
||
$mailchimpService = $request->getAttribute("mailchimpService"); | ||
$mailchimpListMembers = $mailchimpService->getListMembers($listId); | ||
|
||
$People = PersonQuery::create() | ||
->filterByEmail(null, Criteria::NOT_EQUAL) | ||
->_or() | ||
->filterByWorkEmail(null, Criteria::NOT_EQUAL) | ||
->find(); | ||
|
||
|
||
foreach($People as $Person) | ||
{ | ||
$key = array_search(strtolower($Person->getEmail()), $mailchimpListMembers); | ||
if ($key > 0) { | ||
LoggerUtils::getAppLogger()->debug("found " . $Person->getEmail()); | ||
array_splice($mailchimpListMembers, $key, 1); | ||
} | ||
$key = array_search($Person->getWorkEmail(), $mailchimpListMembers); | ||
if ($key > 0) { | ||
array_splice($mailchimpListMembers, $key, 1); | ||
} | ||
} | ||
LoggerUtils::getAppLogger()->debug("MailChimp list ". $listId . " now has ". count($mailchimpListMembers) . " members"); | ||
|
||
return $response->withJson($mailchimpListMembers); | ||
} | ||
|
||
function getMailChimpMissingSubscribed(Request $request, Response $response, array $args) { | ||
$listId = $args['id']; | ||
|
||
$mailchimpService = $request->getAttribute("mailchimpService"); | ||
$mailchimpListMembers = $mailchimpService->getListMembers($listId); | ||
|
||
$People = PersonQuery::create() | ||
->filterByEmail(null, Criteria::NOT_EQUAL) | ||
->_or() | ||
->filterByWorkEmail(null, Criteria::NOT_EQUAL) | ||
->find(); | ||
|
||
$personsNotInMailchimp = []; | ||
foreach($People as $Person) | ||
{ | ||
$found = false; | ||
$key = array_search(strtolower($Person->getEmail()), $mailchimpListMembers); | ||
if ($key > 0 ) { | ||
$found= true; | ||
} | ||
$key = array_search($Person->getWorkEmail(), $mailchimpListMembers); | ||
if ($key > 0) { | ||
$found= true; | ||
} | ||
|
||
if (!$found) { | ||
$emails = []; | ||
if (!empty($Person->getEmail())) { | ||
array_push($emails, $Person->getEmail()); | ||
} | ||
if (!empty($Person->getWorkEmail())) { | ||
array_push($emails, $Person->getWorkEmail()); | ||
} | ||
array_push($personsNotInMailchimp, ["id" => $Person->getId(), | ||
"name" => $Person->getFullName(), | ||
"emails" => $emails | ||
]); | ||
} | ||
} | ||
LoggerUtils::getAppLogger()->debug("MailChimp list ". $listId . " now has ". count($mailchimpListMembers) . " members"); | ||
|
||
return $response->withJson($personsNotInMailchimp); | ||
} | ||
|
||
function getFamilyStatus(Request $request, Response $response, array $args) { | ||
$family = $request->getAttribute("family"); | ||
$mailchimpService = $request->getAttribute("mailchimpService"); | ||
$emailToLists = []; | ||
if (!empty($family->getEmail())) { | ||
array_push($emailToLists, ["email" => $family->getEmail(), "emailMD5" => md5($family->getEmail()), | ||
"list" => $mailchimpService->isEmailInMailChimp($family->getEmail())]); | ||
} | ||
return $response->withJson($emailToLists); | ||
} | ||
|
||
function getPersonStatus(Request $request, Response $response, array $args) { | ||
$person = $request->getAttribute("person"); | ||
$mailchimpService = $request->getAttribute("mailchimpService"); | ||
$emailToLists = []; | ||
if (!empty($person->getEmail())) { | ||
array_push($emailToLists, ["email" => $person->getEmail(), "emailMD5" => md5($person->getEmail()), | ||
"list" => $mailchimpService->isEmailInMailChimp($person->getEmail())]); | ||
} | ||
if (!empty($person->getWorkEmail())) { | ||
array_push($emailToLists, ["email" => $person->getWorkEmail(), "emailMD5" => md5($person->getWorkEmail()), | ||
"list" => $mailchimpService->isEmailInMailChimp($person->getWorkEmail())]); | ||
} | ||
return $response->withJson($emailToLists); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure we have an active mailchimp with subscribers in it