File: /home/nexper/public_html/crm/custom/modules/Schedulers/_AddJobsHere.php
<?php
/**
*
* @package Advanced OpenPortal
* @copyright SalesAgility Ltd http://www.salesagility.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with this program; if not, see http://www.gnu.org/licenses
* or write to the Free Software Foundation,Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Salesagility Ltd <support@salesagility.com>
*/
$job_strings[] = 'pollMonitoredInboxesCustomAOP';
$GLOBALS['log']->info('Custom add jobs here loaded');
function getDistributionMethod($ieX){
global $sugar_config;
$method = $sugar_config['aop']['distribution_method'];
//Check if there is a portal setting for the distribution method.
if($method){
return $method;
}else{
return $ieX->get_stored_options("distrib_method", "");
}
}
function pollMonitoredInboxesCustomAOP() {
$_bck_up = array('team_id' => $GLOBALS['current_user']->team_id, 'team_set_id' => $GLOBALS['current_user']->team_set_id);
$GLOBALS['log']->info('----->Scheduler fired job of type pollMonitoredInboxesCustomAOP()');
global $dictionary;
global $app_strings;
global $sugar_config;
require_once('modules/Configurator/Configurator.php');
require_once('modules/Emails/EmailUI.php');
$ie = new InboundEmail();
$emailUI = new EmailUI();
$r = $ie->db->query('SELECT id, name FROM inbound_email WHERE is_personal = 0 AND deleted=0 AND status=\'Active\' AND mailbox_type != \'bounce\'');
$GLOBALS['log']->debug('Just got Result from get all Inbounds of Inbound Emails');
while($a = $ie->db->fetchByAssoc($r)) {
$GLOBALS['log']->debug('In while loop of Inbound Emails');
$ieX = new InboundEmail();
$ieX->retrieve($a['id']);
$GLOBALS['current_user']->team_id = $ieX->team_id;
$GLOBALS['current_user']->team_set_id = $ieX->team_set_id;
$mailboxes = $ieX->mailboxarray;
foreach($mailboxes as $mbox) {
$ieX->mailbox = $mbox;
$newMsgs = array();
$msgNoToUIDL = array();
$connectToMailServer = false;
if ($ieX->isPop3Protocol()) {
$msgNoToUIDL = $ieX->getPop3NewMessagesToDownloadForCron();
// get all the keys which are msgnos;
$newMsgs = array_keys($msgNoToUIDL);
}
if($ieX->connectMailserver() == 'true') {
$connectToMailServer = true;
} // if
$GLOBALS['log']->debug('Trying to connect to mailserver for [ '.$a['name'].' ]');
if($connectToMailServer) {
$GLOBALS['log']->debug('Connected to mailserver');
if (!$ieX->isPop3Protocol()) {
$newMsgs = $ieX->getNewMessageIds();
}
if(is_array($newMsgs)) {
$current = 1;
$total = count($newMsgs);
require_once("include/SugarFolders/SugarFolders.php");
$sugarFolder = new SugarFolder();
$groupFolderId = $ieX->groupfolder_id;
$isGroupFolderExists = false;
$users = array();
if ($groupFolderId != null && $groupFolderId != "") {
$sugarFolder->retrieve($groupFolderId);
$isGroupFolderExists = true;
} // if
$messagesToDelete = array();
if ($ieX->isMailBoxTypeCreateCase()) {
$users[] = $sugarFolder->assign_to_id;
$distributionMethod = getDistributionMethod($ieX);
if ($distributionMethod == 'singleUser') {
$distributionUserId = $sugar_config['aop']['distribution_user_id'];
}elseif ($distributionMethod != 'roundRobin') {
$counts = $emailUI->getAssignedEmailsCountForUsers($users);
} else {
$lastRobin = $emailUI->getLastRobin($ieX);
}
$GLOBALS['log']->debug('distribution method id [ '.$distributionMethod.' ]');
}
foreach($newMsgs as $k => $msgNo) {
$uid = $msgNo;
if ($ieX->isPop3Protocol()) {
$uid = $msgNoToUIDL[$msgNo];
} else {
$uid = imap_uid($ieX->conn, $msgNo);
} // else
if ($isGroupFolderExists) {
if ($ieX->importOneEmail($msgNo, $uid)) {
// add to folder
$sugarFolder->addBean($ieX->email);
if ($ieX->isPop3Protocol()) {
$messagesToDelete[] = $msgNo;
} else {
$messagesToDelete[] = $uid;
}
if ($ieX->isMailBoxTypeCreateCase()) {
$userId = "";
if ($distributionMethod == 'singleUser') {
$userId = $distributionUserId;
}elseif ($distributionMethod == 'roundRobin') {
if (sizeof($users) == 1) {
$userId = $users[0];
$lastRobin = $users[0];
} else {
$userIdsKeys = array_flip($users); // now keys are values
$thisRobinKey = $userIdsKeys[$lastRobin] + 1;
if(!empty($users[$thisRobinKey])) {
$userId = $users[$thisRobinKey];
$lastRobin = $users[$thisRobinKey];
} else {
$userId = $users[0];
$lastRobin = $users[0];
}
} // else
} else {
if (sizeof($users) == 1) {
foreach($users as $k => $value) {
$userId = $value;
} // foreach
} else {
asort($counts); // lowest to highest
$countsKeys = array_flip($counts); // keys now the 'count of items'
$leastBusy = array_shift($countsKeys); // user id of lowest item count
$userId = $leastBusy;
$counts[$leastBusy] = $counts[$leastBusy] + 1;
}
} // else
$GLOBALS['log']->debug('userId [ '.$userId.' ]');
$ieX->handleCreateCase($ieX->email, $userId);
} // if
} // if
} else {
if($ieX->isAutoImport()) {
$ieX->importOneEmail($msgNo, $uid);
} else {
/*If the group folder doesn't exist then download only those messages
which has caseid in message*/
$ieX->getMessagesInEmailCache($msgNo, $uid);
$email = new Email();
$header = imap_headerinfo($ieX->conn, $msgNo);
$email->name = $ieX->handleMimeHeaderDecode($header->subject);
$email->from_addr = $ieX->convertImapToSugarEmailAddress($header->from);
$email->reply_to_email = $ieX->convertImapToSugarEmailAddress($header->reply_to);
if(!empty($email->reply_to_email)) {
$contactAddr = $email->reply_to_email;
} else {
$contactAddr = $email->from_addr;
}
$mailBoxType = $ieX->mailbox_type;
$ieX->handleAutoresponse($email, $contactAddr);
} // else
} // else
$GLOBALS['log']->debug('***** On message [ '.$current.' of '.$total.' ] *****');
$current++;
} // foreach
// update Inbound Account with last robin
if ($ieX->isMailBoxTypeCreateCase() && $distributionMethod == 'roundRobin') {
$emailUI->setLastRobin($ieX, $lastRobin);
} // if
} // if
if ($isGroupFolderExists) {
$leaveMessagesOnMailServer = $ieX->get_stored_options("leaveMessagesOnMailServer", 0);
if (!$leaveMessagesOnMailServer) {
if ($ieX->isPop3Protocol()) {
$ieX->deleteMessageOnMailServerForPop3(implode(",", $messagesToDelete));
} else {
$ieX->deleteMessageOnMailServer(implode($app_strings['LBL_EMAIL_DELIMITER'], $messagesToDelete));
}
}
}
} else {
$GLOBALS['log']->fatal("SCHEDULERS: could not get an IMAP connection resource for ID [ {$a['id']} ]. Skipping mailbox [ {$a['name']} ].");
// cn: bug 9171 - continue while
} // else
} // foreach
imap_expunge($ieX->conn);
imap_close($ieX->conn, CL_EXPUNGE);
} // while
$GLOBALS['current_user']->team_id = $_bck_up['team_id'];
$GLOBALS['current_user']->team_set_id = $_bck_up['team_set_id'];
return true;
}