Ejemplo n.º 1
0
 /**
  * Save the subscription
  *
  * @return void
  */
 public function save()
 {
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     $appl = JFactory::getApplication();
     $db = JFactory::getDBO();
     $query = $db->getQuery(true);
     $chimp = new cmcHelperChimp();
     $input = JFactory::getApplication()->input;
     $form = $input->get('jform', '', 'array');
     $isAjax = $input->get('ajax');
     $mergeVars = CmcHelperList::mergeVars($form);
     $listId = $form['cmc']['listid'];
     $email = $mergeVars['EMAIL'];
     $chimp->listSubscribe($listId, $email, $mergeVars, 'html', true, true, true, false);
     if ($chimp->errorCode) {
         $response['html'] = $chimp->errorMessage;
         $response['error'] = true;
     } else {
         // Get the member info from mailchimp
         $memberInfo = $chimp->listMemberInfo($listId, $email);
         $status = 'applied';
         // User was found on list
         if ($memberInfo['success']) {
             $status = $memberInfo['data'][0]['status'];
         }
         // Check if the subscription is already present in the db
         if (CmcHelperUsers::getSubscription($email, $listId)) {
             $query->update('#__cmc_users')->set(array('list_id = ' . $db->quote($listId), 'email = ' . $db->quote($email), 'merges = ' . $db->quote(json_encode($mergeVars)), 'status = ' . $db->q($status)))->where('list_id = ' . $db->quote($listId))->where('email = ' . $db->quote($email));
             $html = 'updated';
         } else {
             $query->insert('#__cmc_users')->columns('list_id,email,merges,status')->values($db->quote($listId) . ',' . $db->quote($email) . ',' . $db->quote(json_encode($mergeVars)) . ',' . $db->q($status));
             $html = 'saved';
         }
         $db->setQuery($query);
         $db->execute();
         $response['html'] = $html;
         $response['error'] = false;
     }
     if ($isAjax) {
         echo json_encode($response);
         jexit();
     }
     $appl->enqueueMessage($response['html']);
     $appl->redirect($_SERVER['HTTP_REFERER']);
 }
Ejemplo n.º 2
0
 /**
  * Updates an existing subscription
  *
  * @param   JUser  $user      - The JUser Obj
  * @param   array  $postdata  - The params
  *
  * @return  boolean
  */
 public static function updateSubscription($user, $postdata)
 {
     // We just update the users subscription to mailchimp (not db checking here)
     if (isset($postdata['cmc_groups'])) {
         $postdata['groups'] = $postdata['cmc_groups'];
         unset($postdata['cmc_groups']);
     }
     if (isset($postdata['cmc_interests'])) {
         $postdata['interests'] = $postdata['cmc_interests'];
         unset($postdata['cmc_interests']);
     }
     $chimp = new cmcHelperChimp();
     $userlists = $chimp->listsForEmail($user->email);
     // Hidden field
     $listId = $postdata['listid'];
     if ($userlists && in_array($listId, $userlists)) {
         // Already in list, we don't update here, we update on form send
         return null;
     }
     // Activate E-Mail in mailchimp
     if (isset($params['groups'])) {
         foreach ($params['groups'] as $key => $group) {
             $mergeVars[$key] = $group;
         }
     }
     // Interests
     if (isset($params['interests'])) {
         foreach ($params['interests'] as $key => $interest) {
             // Take care of interests that contain a comma (,)
             array_walk($interest, create_function('&$val', '$val = str_replace(",","\\,",$val);'));
             $mergeVars['GROUPINGS'][] = array('id' => $key, 'groups' => implode(',', $interest));
         }
     }
     $chimp->listSubscribe($listId, $user->email, $mergeVars, 'html', false, true, true, false);
     if (!$chimp->errorCode) {
         return true;
     } else {
         echo "Error: " . $chimp->errorMessage;
         return false;
     }
 }