function MailChimpUpdateListFromDB($con, $listid) { $list = SqlResultArray($con, "select listname from ctcweb9_ctc.mailchimp_lists where listid='{$listid}'"); $listname = $list[0]["listname"]; //$list = MailChimpSqlResultToArray($con,"select listname from ctcweb9_ctc.mailchimp_lists where listid='$listid'"); //$listname = $list[0]["listname"]; // Get the current state from the table $sql = "SELECT UCASE(primaryEmail) as K, primaryEmail as EMAIL,trim(firstName) as FNAME,trim(lastName) as LNAME, memberid, 'create' as Action\n from ctcweb9_ctc.members m\n join ctcweb9_ctc.mailchimp_subscriptions ms on ms.memberid = m.id\n where listid='{$listid}' and primaryEmail != '' and not (primaryEmail is null)\n order by primaryEmail"; $list = SqlResultArray($con, $sql, "K"); $changed = array(); $changed[] = "listname={$listname} listid={$listid} sql returned " . count($list) . " items"; // Get the current state from MailChimp for ($start = 0;; $start++) { $data = MailChimpRequest("lists/members", array("id" => $listid, "opts" => array("start" => $start, "limit" => 50))); $changed[] = "lists/members listname={$listname} listid={$listid} start={$start} returned " . count($data["data"]) . " items"; if (count($data["data"]) == 0) { break; } foreach ($data["data"] as $member) { $email = $member["merges"]["EMAIL"]; $fname = $member["merges"]["FNAME"]; $lname = $member["merges"]["LNAME"]; $key = strtoupper($email); if (!array_key_exists($key, $list)) { $list[$key] = $member["merges"]; $list[$key]["Action"] = "delete"; } else { if ($list[$key]["FNAME"] == $fname && $list[$key]["LNAME"] == $lname) { $list[$key]["Action"] = "ignore"; } else { $list[$key]["Action"] = "update"; $list[$key]["old"] = "{$fname} {$lname}"; } } } } $actions = array(); // Generate subscribe or unsubscribe actions as necessary foreach ($list as $item) { $email = $item["EMAIL"]; $fname = $item["FNAME"]; $lname = $item["LNAME"]; $changed[] = json_encode($item); if ($item["Action"] == 'delete' || $item["Action"] == 'update') { $changed[] = "remove {$email} from {$listname}"; $actions[] = array("method" => "lists/unsubscribe", "args" => array("id" => $listid, "email" => array("email" => $email), "delete_member" => true, "send_goodbye" => false, "send_notify" => false)); } if ($item["Action"] == 'create' || $item["Action"] == 'update') { $changed[] = "add {$email} to {$listname}"; $actions[] = array("method" => "lists/subscribe", "args" => array("id" => $listid, "email" => array("email" => $email), "merge_vars" => array("FNAME" => $fname, "LNAME" => $lname), "double_optin" => false, "update_existing" => true)); } } // Send all the actions to Mailchimp foreach ($actions as $action) { MailChimpRequest($action["method"], $action["args"]); } return $changed; }
<?php // Set flag that this is a parent file define('_VALID_MOS', 1); require_once 'mailchimp.connect.php'; $method = $_POST['method'] ? $_POST['method'] : 'lists/list'; $replace = $_POST['replace'] ? $_POST['replace'] : ''; $data = $_POST['data'] ? $_POST['data'] : '{}'; $result = ''; if ($_POST['send'] == 'MailChimpAPI') { $result = json_encode(MailChimpRequest($method, json_decode($data, true))); } else { if ($_POST['send'] == 'MailChimpUpdateLists') { MailChimpUpdateLists($con); $result = json_encode(MailChimpSqlResultToArray($con, 'select * from ctcweb9_ctc.mailchimp_lists')); } else { if ($_POST['send'] == 'MailChimpResetSubscription') { MailChimpResetSubscription($con); $result = json_encode($_POST['send']); } else { if ($_POST['send'] == 'MailChimpUpdateListFromDB') { $result = json_encode(MailChimpUpdateListFromDB($con, "054eb834c8")); } else { if ($_POST['send'] == 'SQL') { $result = json_encode(MailChimpSqlResultToArray($con, $data)); } else { if ($_POST['send'] == 'preg_replace') { $result = json_encode(preg_replace($method, $replace, $data)); } } }
<style> </style> <script> </script> </head> <body> <form method="POST" action="send.php"> <table> <tr> <td>List</td> <td> <select name="listid"> <?php $lists = MailChimpRequest("lists/list"); foreach ($lists['data'] as $item) { echo "<option value='" . $item['id'] . "' >" . $item['name'] . "</option>"; } ?> </select> </td> </tr> <tr> <td>Subject</td> <td><input name="subject" type="text" value="<?php echo $subject; ?> "/></td> </tr> <tr>
} echo "{$userid} " . join(",", $listids) . "<br>"; $sql = "DELETE from ctcweb9_ctc.mailchimp_subscriptions where memberId = {$userid} and listid not in (" . join(",", $listids) . ")"; echo "<pre>{$sql}</pre>"; echo mysql_query($sql, $con) or die('delete'); $sql = "INSERT into ctcweb9_ctc.mailchimp_subscriptions(listid,memberid)\n\t\t\tSELECT listid,{$userid} \n\t\t\tFROM ctcweb9_ctc.mailchimp_lists\n\t\t\tWHERE listid in (" . join(",", $listids) . ")\n\t\t\tAND listid not in (select listid from ctcweb9_ctc.mailchimp_subscriptions where memberid = {$userid})"; echo "<pre>{$sql}</pre>"; echo mysql_query($sql, $con) or die('insert'); $result = MailChimpUpdateListsFromDB($con); } else { $result = array("Waiting"); } echo json_encode(MailChimpSqlResultToArray($con, "select ml.listname, ms.* \n\t\t\t\t\t\tfrom ctcweb9_ctc.mailchimp_subscriptions ms\n\t\t\t\t\t\tjoin ctcweb9_ctc.mailchimp_lists ml on ml.listid = ms.listid\n\t\t\t\t\t\twhere memberId = {$userid}")); foreach ($lists['data'] as &$list) { $memberinfoargs = array("id" => $list['id'], "emails" => array(array("email" => $email))); $memberinfo = MailChimpRequest("lists/member-info", $memberinfoargs); $list['is_subscribed'] = sizeof($memberinfo['data']) > 0; } ?> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <style> </style> <script> </script> </head> <body>