/** * Get Mailchimp email groups * * @param \Joomla\Registry\Registry $params Params * * @throws RuntimeException * * @return array groups */ protected function getGroups($params) { $listId = $params->get('mailchimp_listid'); $apiKey = $params->get('mailchimp_apikey'); if ($apiKey == '') { throw new RuntimeException('Mailchimp: no api key specified'); } if ($listId == '') { throw new RuntimeException('Mailchimp: no list id specified'); } $api = new MCAPI($params->get('mailchimp_apikey')); $groups = $api->listInterestGroupings($listId); return $groups; }
/** * get_interest_groupings function. * * @access public * @return void */ public function get_interest_groupings($listid = 'false') { if ($listid == 'false') { $listid = $this->list; } $interest_groupings = array(); $interest_groups = array(); $api = new MCAPI($this->api_key); $retval = $api->listInterestGroupings($listid); if ($mailchimp->errorCode) { echo $this->get_message(sprintf(__('Unable to load listInterestGroupings() from MailChimp: (%s) %s', 'ss_wc_mailchimp'), $mailchimp->errorCode, $mailchimp->errorMessage)); return false; } else { if (sizeof($retval) > 0) { foreach ($retval as $interest_grouping) { $interest_groupings[$interest_grouping['id']] = $interest_grouping['name']; foreach ($interest_grouping['groups'] as $group) { $interest_groups[$group['bit']] = $group['name']; } } if (sizeof($interest_groupings) > 0) { // set transients for cache set_transient('wc_mailchimp_list_' . md5($this->api_key) . '_' . $listid, $interest_groupings, 60 * 60 * 1); set_transient('wc_mailchimp_list_' . md5($this->api_key) . '_' . $listid . '_groups', $interest_groups, 60 * 60 * 1); $this->interest_groupings = $interest_groupings; $this->groups[$listid] = $interest_groups; } } } return $interest_groupings; }
/** * Efficient wrapper for MCAPI:listInterestGroupings() * @see application/libraries/MCAPI#listInterestGroupings() */ function listInterestGroupings($id) { if (isset($this->lists_groups[$id])) { return $this->lists_groups[$id]; } return parent::listInterestGroupings($id); }
/** * Action to process subscriptions * @param array $raw_data * @param Form $form * @return SilverChimpSubscriptionPage_Controller */ function SubscribeAction($raw_data, $form) { $config = SiteConfig::current_site_config(); $data = Convert::raw2sql($raw_data); // get list of mergeVars to check for from API $mergeVars = $this->api->listMergeVars($this->ListID); // initialise data container $postedVars = array(); // loop through merge vars and only poopulate data required foreach ($mergeVars as $var) { if (isset($data[$var['tag']])) { $postedVars[$var['tag']] = $data[$var['tag']]; } } // get all groups for list $groupData = $this->api->listInterestGroupings($this->ListID); // get all defaults for list $groupDefaults = unserialize($this->DefaultGroupSelections); if (is_array($groupData) || $groupData instanceof Traversable) { // loop through groups foreach ($groupData as $gr) { // initialise valiable containing the key for defaults test $defaultsName = 'SCG-' . preg_replace('/[^0-9A-Za-z]/', '-', $gr['name']); // if a GROUPINGS value for the current group exists if (isset($data['GROUPINGS'][$gr['name']])) { $newGroups = array(); // check current group is in submitted values foreach ($gr['groups'] as $gd) { if (in_array($gd['bit'], $data['GROUPINGS'][$gr['name']])) { $newGroups[] = $gd['name']; } } // add groups to data for subscription $postedVars['GROUPINGS'][] = array('name' => $gr['name'], 'groups' => implode(',', $newGroups)); } else { if (isset($groupDefaults[$defaultsName])) { // if defaults present $newGroups = array(); // loop through groups and check in defaults foreach ($gr['groups'] as $gd) { if (in_array($gd['bit'], $groupDefaults[$defaultsName])) { $newGroups[] = $gd['name']; } } // add groups to data for subscription $postedVars['GROUPINGS'][] = array('name' => $gr['name'], 'groups' => implode(',', $newGroups)); } } } } $this->extend('updateSilverChimpSignupAction', $data, $postedVars); // send subscription data to MailChimp $result = $this->api->listSubscribe($this->ListID, $postedVars['EMAIL'], $postedVars, $config->ChimpEmailType, $config->ChimpDoubleOptIn, $this->AllowUpdateExisting, $config->ChimpReplaceGroups, $config->ChimpSendWelcome); if (true === $result) { // success! Session::set('SilverChimp.SUCCESS', true); return $this->subscribeSuccess(); } else { // failure! $form->sessionMessage($this->api->errorMessage, 'warning'); return $this; } }
function addUserToList($user_id, $product_id) { require_once JPATH_COMPONENT . '/helpers/MCAPI.class.php'; $db = JFactory::getDBO(); $sql = "select p.mailchimpapi, p.mailchimplist, p.mailchimpregister, p.mailchimpgroupid, u.email, c.firstname, c.lastname from #__users u, #__digicom_products p, #__digicom_customers c where u.id=c.id and u.id=" . intval($user_id) . " and p.id=" . intval($product_id); $db->setQuery($sql); $db->query(); $result = $db->loadAssocList(); if (isset($result) && count($result) > 0) { $mc_username = $result["0"]["mailchimpapi"]; $mc_listid = $result["0"]["mailchimplist"]; $mc_autoregister = $result["0"]["mailchimpregister"] == 0 ? FALSE : TRUE; $mc_groupid = $result["0"]["mailchimpgroupid"]; $mc_email = $result["0"]["email"]; if (trim($mc_username) == "") { $sql = "select `mailchimpapi` from #__digicom_settings"; $db->setQuery($sql); $db->query(); $mc_username = $db->loadResult(); } if (trim($mc_listid) == "") { $sql = "select `mailchimplist` from #__digicom_settings"; $db->setQuery($sql); $db->query(); $mc_listid = $db->loadResult(); } if (trim($mc_username) != "" && trim($mc_listid) != "") { $api = new MCAPI($mc_username); $user_info = $api->listMemberInfo($mc_listid, $mc_email); if ($user_info === FALSE) { //add new user $mergeVars = array('FNAME' => $result["0"]["firstname"], 'LNAME' => $result["0"]["lastname"]); if (trim($mc_groupid) != "") { $mergeVars["INTERESTS"] = trim($mc_groupid); } $api->listSubscribe($mc_listid, $mc_email, $mergeVars, 'html', $mc_autoregister, true); } else { //already exist and update user group if (trim($mc_groupid) != "") { $user_group_string = $user_info["merges"]["INTERESTS"]; $user_group_array = explode(",", $user_group_string); $exist = FALSE; foreach ($user_group_array as $key => $group) { if (trim($group) == trim($mc_groupid)) { $exist = TRUE; } } if ($exist === FALSE) { $new_group_list = trim($user_group_string); if (trim($new_group_list) != "") { $new_group_list = trim($user_group_string) . ", " . trim($mc_groupid); } else { $new_group_list = trim($mc_groupid); } $mergeVars = array('INTERESTS' => $new_group_list); $name = ""; $groups = $api->listInterestGroupings($mc_listid); if (isset($groups) && count($groups) > 0) { foreach ($groups as $key_group => $group) { if (isset($group["groups"]) && count($group["groups"]) > 0) { foreach ($group["groups"] as $key_subgroup => $subgroup) { if (trim($subgroup["name"]) == trim($mc_groupid)) { $name = $group["name"]; break; } } } } } if (trim($name) != "") { $mergeVars = array('INTERESTS' => $new_group_list, 'GROUPINGS' => array(array('name' => $name, 'groups' => $new_group_list))); } $api->listUpdateMember($mc_listid, $mc_email, $mergeVars, 'html', false); } } } } } }