/**
 * This function traces an incoming call and adds it to the databse for vtiger to pickup,
 * it also adds an entry to the activity history of the related Contact/Lead/Account
 * only these three modules are supported for now
 */
function TraceIncomingCall()
{
    require_once 'modules/PBXManager/AsteriskUtils.php';
    global $adb, $current_user;
    global $theme, $app_strings, $log;
    $theme_path = "themes/" . $theme . "/";
    $image_path = $theme_path . "images/";
    $asterisk_extension = false;
    if (isset($current_user->column_fields)) {
        $asterisk_extension = $current_user->column_fields['asterisk_extension'];
    } else {
        $sql = "select asterisk_extension from vtiger_asteriskextensions where userid = ?";
        $result = $adb->pquery($sql, array($current_user->id));
        $asterisk_extension = $adb->query_result($result, 0, "asterisk_extension");
    }
    $query = "select * from vtiger_asteriskincomingcalls where to_number = ?";
    $result = $adb->pquery($query, array($asterisk_extension));
    if ($adb->num_rows($result) > 0) {
        $flag = $adb->query_result($result, 0, "flag");
        $oldTime = $adb->query_result($result, 0, "timer");
        $callerNumber = $adb->query_result($result, 0, "from_number");
        $callerName = $adb->query_result($result, 0, "from_name");
        $callerType = $adb->query_result($result, 0, "callertype");
        $refuid = $adb->query_result($result, 0, "refuid");
        if (!empty($callerNumber)) {
            $tracedCallerInfo = getTraceIncomingCallerInfo($callerNumber);
        }
        $callerLinks = $tracedCallerInfo['callerLinks'];
        $firstCallerInfo = false;
        if (!empty($tracedCallerInfo['callerInfos'])) {
            $firstCallerInfo = $tracedCallerInfo['callerInfos'];
        }
        $newTime = time();
        if ($newTime - $oldTime >= 3 && $flag == 1) {
            $adb->pquery("delete from vtiger_asteriskincomingcalls where to_number = ?", array($asterisk_extension));
        } else {
            if ($flag == 0) {
                $flag = 1;
                // Trying to get the Related CRM ID for the Event (if already desired by popup click)
                $relcrmid = false;
                if (!empty($refuid)) {
                    $refuidres = $adb->pquery('SELECT relcrmid FROM vtiger_asteriskincomingevents WHERE uid=?', array($refuid));
                    if ($adb->num_rows($refuidres)) {
                        $relcrmid = $adb->query_result($refuidres, 0, 'relcrmid');
                    }
                }
                $adb->pquery("update vtiger_asteriskincomingcalls set flag = ? where to_number = ?", array($flag, $asterisk_extension));
                $activityid = asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $current_user->id, $relcrmid, $firstCallerInfo);
            }
            //prepare the div for incoming calls
            $status = "\t<table  border='0' cellpadding='5' cellspacing='0'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td style='padding:10px;' colspan='2'><b>" . $app_strings['LBL_INCOMING_CALL'] . "</b></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t\t<table  border='0' cellpadding='0' cellspacing='0' class='hdrNameBg'>\n\t\t\t\t\t\t<tr><td style='padding:10px;' colspan='2'><b>" . $app_strings['LBL_CALLER_INFORMATION'] . "</b>\n\t\t\t\t\t\t\t<br><b>" . $app_strings['LBL_CALLER_NUMBER'] . "</b> {$callerNumber}\n\t\t\t\t\t\t\t<br><b>" . $app_strings['LBL_CALLER_NAME'] . "</b> {$callerName}\n\t\t\t\t\t\t</td></tr>\n\t\t\t\t\t\t<tr><td style='padding:10px;' colspan='2'><b>" . $app_strings['LBL_INFORMATION_VTIGER'] . "</b>\n\t\t\t\t\t\t\t<br> {$callerLinks}\n\t\t\t\t\t\t</td></tr>\n\t\t\t\t\t</table>";
        }
    } else {
        $status = "failure";
    }
    return $status;
}
Пример #2
0
 function handleEvent($eventName, $entityData)
 {
     global $log, $adb, $current_user;
     $moduleName = $entityData->getModuleName();
     if ($eventName == 'vtiger.entity.aftersave' and isset($_REQUEST['cbcustominfo1']) and in_array($moduleName, array('Accounts', 'Contacts', 'Leads', 'HelpDesk', 'Potentials'))) {
         $act = urldecode($_REQUEST['cbcustominfo1']);
         $act = unserialize($act);
         if (is_array($act) and isset($act['action']) and $act['action'] == 'asterisk_addToActivityHistory') {
             require_once 'modules/PBXManager/AsteriskUtils.php';
             $callerName = $act['callerName'];
             $callerNumber = $act['callerNumber'];
             $callerType = $act['callerType'];
             $relcrmid = $entityData->getId();
             $activityid = asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $current_user->id, $relcrmid);
         }
     }
 }