예제 #1
0
 function ajax_sync_hotness()
 {
     $elements = JRequest::getVar('elements', '', 'request', 'string');
     $elements = json_decode($elements);
     if ($elements->done == 0) {
         $_SESSION['abortAJAX'] = 0;
         unset($_SESSION['addedUsers']);
         unset($_SESSION['HotnessExists']);
     }
     if ($_SESSION['abortAJAX'] != 1) {
         $db =& JFactory::getDBO();
         $model =& $this->getModel('sync');
         $params =& JComponentHelper::getParams('com_joomailermailchimpintegration');
         $paramsPrefix = version_compare(JVERSION, '1.6.0', 'ge') ? 'params.' : '';
         $MCapi = $params->get($paramsPrefix . 'MCapi');
         $MC = new joomlamailerMCAPI($MCapi);
         $MCerrorHandler = new MCerrorHandler();
         $list_id = $elements->listid;
         $step = $elements->step;
         $offset = $elements->offset;
         // retrieve hotness rating
         require_once JPATH_COMPONENT_ADMINISTRATOR . DS . '/libraries/joomailer/hotActivityComposite.php';
         $composite = new hotActivityComposite();
         $hotnessRating = $composite->getAllUserHotnessValue($list_id);
         if (isset($_SESSION['addedUsers'])) {
             $exclude = $_SESSION['addedUsers'];
         } else {
             $exclude = array();
         }
         if (!$elements->failed) {
             $elements->failed = array();
         }
         $exclude = array_merge($exclude, $elements->failed);
         $exclude = implode('","', $exclude);
         $exclude = '"' . $exclude . '"';
         if (isset($exclude[0])) {
             $exclude = 'AND j.userid NOT IN (' . $exclude . ') ';
         } else {
             $exclude = '';
         }
         //		$data = array();
         //		$run = true;
         //		$page = 0;
         //		while($run){
         //		    $result = $MC->listMembers( $list_id, '', '', $page, '15000');
         //		    $page++;
         //		    if($result){
         //			$run = true;
         //			$data = array_merge($data, $result);
         //		    } else {
         //			$run = false;
         //		    }
         //		}
         $data = $MC->listMembers($list_id, '', '', $offset, $step);
         if (count($data) > 0) {
             // determine if the interest group Hotness already exists, if not: create it
             if (!isset($_SESSION['HotnessExists'])) {
                 $query = "SELECT value FROM #__joomailermailchimpintegration_misc WHERE type = 'hotness' AND listid = '" . $list_id . "' ";
                 $db->setQuery($query);
                 $hotnessId = $db->loadResult();
                 if ($hotnessId == NULL) {
                     $result = $MC->listInterestGroupingAdd($list_id, JText::_('JM_HOTNESS_RATING'), 'hidden', array(1, 2, 3, 4, 5));
                     if (is_int($result)) {
                         $query = "INSERT INTO #__joomailermailchimpintegration_misc (type, listid, value) VALUES ('hotness', '" . $list_id . "', '" . $result . "') ";
                         $db->setQuery($query);
                         $db->query();
                         $_SESSION['HotnessExists'] = $result;
                     }
                 } else {
                     $_SESSION['HotnessExists'] = $hotnessId;
                 }
             }
             /*
             		    $userIds = array();
             		    foreach($data as $dat){
             			$userIds[$dat->email] = $dat->id;
             		    }
             */
             $addedUsers = $elements->addedUsers;
             $m = 0;
             $successCount = 0;
             $errorcount = $msgErrorsCount = 0;
             $msg = $msgErrors = false;
             $counter = 0;
             $ids = '';
             $errorMsg = $elements->errorMsg;
             for ($x = 0; $x < count($data); $x += $step) {
                 if ($_SESSION['abortAJAX'] == 1) {
                     unset($_SESSION['addedUsers']);
                     break;
                 }
                 $k = 0;
                 $batch = array();
                 $errorcount = $msgErrorsCount = 0;
                 for ($y = $x; $y < $x + $step; $y++) {
                     if ($_SESSION['abortAJAX'] == 1) {
                         unset($_SESSION['addedUsers']);
                         break;
                     }
                     if (isset($data[$y])) {
                         $dat = $data[$y];
                     } else {
                         $dat = false;
                     }
                     if ($dat) {
                         $addedUsers[] = $dat['email'];
                         $batch[$k]['EMAIL'] = $dat['email'];
                         if (!isset($hotnessRating[$dat['email']])) {
                             $hotnessRating[$dat['email']] = 2;
                         }
                         $batch[$k]['GROUPINGS'][] = array('id' => $_SESSION['HotnessExists'], 'groups' => $hotnessRating[$dat['email']]);
                         $k++;
                     } else {
                         break;
                     }
                 }
                 if ($batch) {
                     $optin = false;
                     //yes, send optin emails
                     $up_exist = true;
                     // yes, update currently subscribed users
                     $replace_int = true;
                     // false = add interest, don't replace
                     $result = $MC->listBatchSubscribe($list_id, $batch, $optin, $up_exist, $replace_int);
                     $successCount = $successCount + $result['success_count'];
                     if ($result['error_count']) {
                         foreach ($result['errors'] as $e) {
                             $tmp = new stdClass();
                             $tmp->errorCode = $e['code'];
                             $tmp->errorMessage = $e['message'];
                             $errorMsg .= '"' . $MCerrorHandler->getErrorMsg($tmp) . ' => ' . $e['row']['EMAIL'] . '", ';
                             //  $addedUsers = array_diff($addedUsers, array($userIds[$e['row']['EMAIL']]));
                             //  $elements->failed[] = $userIds[$e['row']['EMAIL']];
                             $errorcount++;
                         }
                         $msgErrorsCount += $result['error_count'];
                     }
                 }
             }
             $addedUsers = array_unique($addedUsers);
             if (!count($data)) {
                 $done = $elements->total;
                 unset($_SESSION['addedUsers']);
                 $percent = 100;
             } else {
                 $done = count($addedUsers);
                 $_SESSION['addedUsers'] = $addedUsers;
                 $percent = $done / $elements->total * 100;
             }
             $response['msg'] = '<div id="bg"></div>' . '<div style="background:#FFFFFF none repeat scroll 0 0;border:10px solid #000000;height:100px;left:37%;position:relative;text-align:center;top:37%;width:300px; ">' . '<div style="margin: 35px auto 3px; width: 300px; text-align: center;">' . JText::_('adding users') . ' ( ' . $done . '/' . $elements->total . ' ' . JText::_('done') . ' )</div>' . '<div style="margin: auto; background: transparent url(' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/images/progress_bar_grey.gif) repeat scroll 0% 0%; width: 190px; height: 14px; display: block;">' . '<div style="width: ' . $percent . '%; overflow: hidden;">' . '<img src="' . JURI::root() . 'administrator/components/com_joomailermailchimpintegration/assets/images/progress_bar.gif" style="margin: 0 5px 0 0;"/>' . '</div>' . '<div style="width: 190px; text-align: center; position: relative;top:-13px; font-weight:bold;">' . round($percent, 0) . ' %</div>' . '</div>' . '<a id="sbox-btn-close" style="text-indent:-5000px;right:-20px;top:-18px;outline:none;" href="javascript:abortAJAXnoRefresh();">abort</a>' . '</div>';
             $response['done'] = $done;
             $response['errors'] = count($elements->failed);
             $response['errorMsg'] = $errorMsg;
             $response['addedUsers'] = array_values(array_unique($addedUsers));
             //	    $response['failed']	    = $elements->failed;
             if ($done + count($elements->failed) + $elements->errors >= $elements->total) {
                 $response['finished'] = 1;
                 if ($errorMsg) {
                     $errorMsg = substr($errorMsg, 0, -2);
                     $msgErrors = ' ( ' . count($elements->failed) . ' ' . JText::_('Errors') . ': ' . $errorMsg . ' )';
                 }
                 if (!$msg) {
                     $msg = $done . ' ' . JText::_('JM_USERS_SYNCHRONIZED') . '.';
                 }
                 if ($msgErrors) {
                     $msg .= $msgErrors;
                 }
                 $response['finalMessage'] = $msg;
             } else {
                 $response['finished'] = 0;
                 $response['finalMessage'] = '';
             }
             $response['abortAJAX'] = $_SESSION['abortAJAX'];
         } else {
             unset($_SESSION['addedUsers']);
             $response['addedUsers'] = '';
             $response['finalMessage'] = JText::_('JM_NO_USERS_FOUND');
             $response['finished'] = 1;
             $response['abortAJAX'] = $_SESSION['abortAJAX'];
         }
         echo json_encode($response);
     } else {
         unset($_SESSION['addedUsers']);
         $response['addedUsers'] = '';
         $response['finished'] = 1;
         $response['abortAJAX'] = $_SESSION['abortAJAX'];
         echo json_encode($response);
     }
 }