function TraceIncomingCall() { require_once 'include/utils/utils.php'; global $adb, $current_user; global $theme, $app_strings; $theme_path = "themes/" . $theme . "/"; $image_path = $theme_path . "images/"; $sql = "select * from vtiger_asteriskextensions where userid = " . $current_user->id; $result = $adb->query($sql); $asterisk_extension = $adb->query_result($result, 0, "asterisk_extension"); $query = "select * from vtiger_asteriskincomingcalls where to_number = " . $asterisk_extension; $result = $adb->query($query); if ($adb->num_rows($result) > 0) { $callerNumber = $adb->query_result($result, 0, "from_number"); $callerName = $adb->query_result($result, 0, "from_name"); $callerType = $adb->query_result($result, 0, "callertype"); if (!empty($callerType)) { $caller = getCallerName("{$callerType}:" . $callerNumber); } else { $caller = getCallerName($callerNumber); } $adb->query("delete from vtiger_asteriskincomingcalls where to_number = " . $asterisk_extension); //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>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>Caller Information</b>\n\t\t\t\t\t\t\t<br><b>Number:</b> {$callerNumber}\n\t\t\t\t\t\t\t<br><b>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>Information from vtigerCRM</b>\n\t\t\t\t\t\t\t<br>{$caller}\n\t\t\t\t\t\t</td></tr>\n\t\t\t\t\t</table>"; require_once 'modules/Calendar/Activity.php'; $focus = new Activity(); $focus->column_fields['subject'] = "Incoming call from {$callerName} ({$callerNumber})"; $focus->column_fields['activitytype'] = "Call"; $focus->column_fields['date_start'] = date('Y-m-d'); $focus->column_fields['due_date'] = date('Y-m-d'); $focus->column_fields['time_start'] = date('H:i'); $focus->column_fields['time_end'] = date('H:i'); $focus->column_fields['eventstatus'] = "Held"; $focus->column_fields['assigned_user_id'] = $current_user->id; $focus->save('Calendar'); $callerInfo = getCallerInfo("{$callerType}:" . $callerNumber); if ($callerInfo == false) { $callerInfo = getCallerInfo(getStrippedNumber($callerNumber)); } if ($callerInfo != false) { $tablename = array('Contacts' => 'vtiger_cntactivityrel', 'Accounts' => 'vtiger_seactivityrel', 'Leads' => 'vtiger_seactivityrel'); $sql = "insert into " . $tablename[$callerInfo['module']] . " values (?,?)"; $params = array($callerInfo[id], $focus->id); $adb->pquery($sql, $params); } } else { $status = "failure"; } return $status; }
/** * this function returns the caller name based on the phone number that is passed to it * @param $from - the number which is calling * returns caller information in name(type) format :: for e.g. Mary Smith (Contact) * if no information is present in database, it returns :: Unknown Caller (Unknown) */ function getTraceIncomingCallerInfo($from) { global $adb; // Grab all possible caller informations (lookup for number as well stripped number) $callerInfos = getCallerInfo($from); $callerLinks = ''; if ($callerInfos !== false) { $callerName = decode_html($callerInfos['name']); $module = $callerInfos['module']; $callerModule = " [{$module}]"; $callerID = $callerInfos['id']; $callerLinks = $callerLinks . "<a href='index.php?module={$module}&action=DetailView&record={$callerID}'>{$callerName}</a>{$callerModule}<br>"; } else { $callerLinks = $callerLinks . "<br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Leads&action=EditView&extra_action=addToCallHistory&phone={$from}'>" . getTranslatedString('LBL_CREATE_LEAD') . "</a><br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Contacts&action=EditView&extra_action=addToCallHistory&phone={$from}'>" . getTranslatedString('LBL_CREATE_CONTACT') . "</a><br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Accounts&action=EditView&extra_action=addToCallHistory&phone={$from}'>" . getTranslatedString('LBL_CREATE_ACCOUNT') . "</a>"; } return array('callerInfos' => $callerInfos, 'callerLinks' => $callerLinks); }
/** * this function returns a formatted link with the caller's name if found or links to create a new record if not * @param $from - the number which is calling * @param $fromname - the name found in the call to create the record * @param $createActivityInfo - info to create activity after new create */ function getTraceIncomingCallerInfo($from, $fromname, $createActivityInfo) { global $adb; // Grab all possible caller informations (lookup for number as well stripped number) $callerInfos = getCallerInfo($from); $createActivityInfo = urlencode(serialize($createActivityInfo)); if ($callerInfos !== false) { $callerName = decode_html($callerInfos['name']); $module = $callerInfos['module']; $callerModule = " [{$module}]"; $callerID = $callerInfos['id']; $callerLinks = "<a href='index.php?module={$module}&action=DetailView&record={$callerID}'>{$callerName}</a>{$callerModule}<br>"; $callerLinks .= "<br>\n\t\t\t<a target='_blank' href='index.php?module=HelpDesk&action=EditView&parent_id={$callerID}&ticket_title={$callerName}&cbcustominfo1={$createActivityInfo}'>" . getTranslatedString('LBL_CREATE_TICKET') . "</a><br>\n\t\t\t<a target='_blank' href='index.php?module=Potentials&action=EditView&related_to={$callerID}&potentialname={$callerName}&cbcustominfo1={$createActivityInfo}'>" . getTranslatedString('LBL_CREATE') . ' ' . getTranslatedString('SINGLE_Potentials', 'Potentials') . "</a><br>"; } else { $from = urlencode($from); $fromname = urlencode($fromname); $callerLinks = "<br>\n\t\t\t<a target='_blank' href='index.php?module=Leads&action=EditView&lastname=" . $fromname . "&phone={$from}&cbcustominfo1={$createActivityInfo}'>" . getTranslatedString('LBL_CREATE_LEAD') . "</a><br>\n\t\t\t<a target='_blank' href='index.php?module=Contacts&action=EditView&lastname=" . $fromname . "&phone={$from}&cbcustominfo1={$createActivityInfo}'>" . getTranslatedString('LBL_CREATE_CONTACT') . "</a><br>\n\t\t\t<a target='_blank' href='index.php?module=Accounts&action=EditView&accountname=" . $fromname . "&phone={$from}&cbcustominfo1={$createActivityInfo}'>" . getTranslatedString('LBL_CREATE_ACCOUNT') . "</a>"; } return array('callerInfos' => $callerInfos, 'callerLinks' => $callerLinks); }
/** * this function returns the caller name based on the phone number that is passed to it * @param $from - the number which is calling * returns caller information in name(type) format :: for e.g. Mary Smith (Contact) * if no information is present in database, it returns :: Unknown Caller (Unknown) */ function getCallerName($from) { global $adb; //information found $callerInfo = getCallerInfo($from); if ($callerInfo != false) { $callerName = decode_html($callerInfo['name']); $module = $callerInfo['module']; $callerModule = " (<a href='index.php?module={$module}&action=index'>{$module}</a>)"; $callerID = $callerInfo['id']; $caller = $caller . "<a href='index.php?module={$module}&action=DetailView&record={$callerID}'>{$callerName}</a>{$callerModule}"; } else { $caller = $caller . "<br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Leads&action=EditView&phone={$from}'>" . getTranslatedString('LBL_CREATE_LEAD') . "</a><br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Contacts&phone={$from}'>" . getTranslatedString('LBL_CREATE_CONTACT') . "</a><br>\n\t\t\t\t\t\t<a target='_blank' href='index.php?module=Accounts&action=EditView&phone={$from}'>" . getTranslatedString('LBL_CREATE_ACCOUNT') . "</a>"; } return $caller; }
function asterisk_handleResponse3($mainresponse, $adb, $asterisk) { $uid = false; $receiver_callerinfo = false; // Asterisk 1.4 (Event: Link), Asterisk 1.6 (Event: Bride, Bridgestate: Link) if ($mainresponse['Event'] == 'Link' || $mainresponse['Event'] == 'Bridge' && $mainresponse['Bridgestate'] == 'Link') { $uid = $mainresponse['Uniqueid1']; $uid2 = $mainresponse['Uniqueid2']; $callerNumber = $mainresponse['CallerID1']; $extensionCalled = $mainresponse['CallerID2']; // Ignore the case wheren CallerIDs are same! if ($callerNumber == $extensionCalled) { // case handled but we ignored. return false; } $callerType = ''; $status = "received"; $sourceChannel = $mainresponse['Channel1']; // Check if Popup has already been shown to user? // Due to (asterisk 1.4 bug: https://issues.asterisk.org/view.php?id=11757) // Popup display for Call made to queue is defered and will be handled below // So we need to pick up events with (flag = 0, asterisk 1.6) or (flag = -1, asterisk 1.4) // asterisk 1.4 - from_number is NULL, // TODO check the state of from_number in asterisk 1.6 $checkres = $adb->pquery("SELECT * FROM vtiger_asteriskincomingevents WHERE uid=? and (flag = 0 or flag = -1) and (from_number is NULL or from_number = 0)", array($uid)); if ($adb->num_rows($checkres) > 0) { if (empty($checkresrow['from_name'])) { $checkresrow['from_name'] = "Unknown"; } $checkresrow = $adb->fetch_array($checkres); $sql = "UPDATE vtiger_asteriskincomingevents SET from_number=?, to_number=?, timer=?, flag=? WHERE uid=?"; $adb->pquery($sql, array($callerNumber, $extensionCalled, time(), 0, $uid)); // Check if the user has checked Incoming Calls in My Preferences if (checkExtension($extensionCalled, $adb)) { $query = "INSERT INTO vtiger_asteriskincomingcalls (refuid, from_number, from_name, to_number, callertype, flag, timer) VALUES(?,?,?,?,?,?,?)"; $adb->pquery($query, array($uid, $callerNumber, $checkresrow['from_name'], $extensionCalled, '', 0, time())); } } // END } else { if ($mainresponse['Event'] == 'Newexten' && $mainresponse['AppData'] == "DIALSTATUS=CONGESTION" || $mainresponse['Event'] == 'Hangup') { $status = "missed"; $uid = $mainresponse['Uniqueid']; $extensionCalled = false; } } // TODO Need to detect the caller number using the Event Information $callerNumberInfo = $adb->pquery("SELECT from_number, callertype FROM vtiger_asteriskincomingevents WHERE uid=? AND from_number is not NULL LIMIT 1", array($uid)); if ($callerNumberInfo && $adb->num_rows($callerNumberInfo)) { $callerNumber = $adb->query_result($callerNumberInfo, 0, 'from_number'); $receiver_callerinfo = getCallerInfo($callerNumber); } if ($uid !== false) { // Create Record if not yet done and link to the event for further use $eventResult = $adb->pquery("SELECT * FROM vtiger_asteriskincomingevents WHERE uid = ? and pbxrecordid is NULL AND flag =0", array($uid)); if ($adb->num_rows($eventResult)) { $eventResultRow = $adb->fetch_array($eventResult); $callerNumber = $eventResultRow['from_number']; if ($extensionCalled === false) { $extensionCalled = $eventResultRow['to_number']; } // If we are not knowing the caller informatio (Asterisk 1.4, Event: Link not yet called) if ($callerNumber != 'Unknown' && $callerNumber != '0') { $pbxrecordid = addToCallHistory($extensionCalled, $callerNumber, $extensionCalled, "incoming-{$status}", $adb, $receiver_callerinfo); $adb->pquery("UPDATE vtiger_asteriskincomingevents SET pbxrecordid = ? WHERE uid = ?", array($pbxrecordid, $uid)); if (!empty($receiver_callerinfo['id'])) { $adb->pquery("UPDATE vtiger_asteriskincomingevents SET relcrmid = ? WHERE uid = ?", array($receiver_callerinfo['id'], $uid)); } } return false; } } return true; }
/** * this function adds a record to the callhistory module * * @param string $userExtension - the extension of the current user * @param string $callfrom - the caller number * @param string $callto - the called number * @param string $status - the status of the call (outgoing/incoming/missed) * @param object $adb - the peardatabase object */ function addToCallHistory($userExtension, $callfrom, $callto, $status, $adb) { $sql = "select * from vtiger_asteriskextensions where asterisk_extension=" . $userExtension; $result = $adb->pquery($sql, array()); $userID = $adb->query_result($result, 0, "userid"); $crmID = $adb->getUniqueID('vtiger_crmentity'); $timeOfCall = date('Y-m-d H:i:s'); $sql = "insert into vtiger_crmentity values (?,?,?,?,?,?,?,?,?,?,?,?,?)"; $params = array($crmID, $userID, $userID, 0, "PBXManager", "", $timeOfCall, $timeOfCall, NULL, NULL, 0, 1, 0); $adb->pquery($sql, $params); if (empty($callfrom)) { $callfrom = "Unknown"; } if (empty($callto)) { $callto = "Unknown"; } if ($status == 'outgoing') { //call is from user to record $sql = "select * from vtiger_asteriskextensions where asterisk_extension=?"; $result = $adb->pquery($sql, array($callfrom)); if ($adb->num_rows($result) > 0) { $userid = $adb->query_result($result, 0, "userid"); $callerName = getUserFullName($userid); } $receiver = getCallerInfo($callto); if ($receiver == false) { $receiver = getCallerInfo(getStrippedNumber($callto)); } if (empty($receiver)) { $receiver = "Unknown"; } else { $receiver = "<a href='index.php?module=" . $receiver[module] . "&action=DetailView&record=" . $receiver[id] . "'>" . $receiver[name] . "</a>"; } } else { //call is from record to user $sql = "select * from vtiger_asteriskextensions where asterisk_extension=?"; $result = $adb->pquery($sql, array($callto)); if ($adb->num_rows($result) > 0) { $userid = $adb->query_result($result, 0, "userid"); $receiver = getUserFullName($userid); } $callerName = getCallerInfo($callfrom); if ($callerName == false) { $callerName = getCallerInfo(getStrippedNumber($callfrom)); } if (empty($callerName)) { $callerName = "Unknown"; } else { $callerName = "<a href='index.php?module=" . $callerName[module] . "&action=DetailView&record=" . $callerName[id] . "'>" . decode_html($callerName[name]) . "</a>"; } } $sql = "insert into vtiger_pbxmanager (pbxmanagerid,callfrom,callto,timeofcall,status)values (?,?,?,?,?)"; $params = array($crmID, $callerName, $receiver, $timeOfCall, $status); $adb->pquery($sql, $params); }
/** * this function adds the call information to the actvity history * @param string $callerName - the caller name * @param string $callerNumber - the callers' number * @param string $callerType - the caller type (SIP/PSTN...) * @param object $adb - the peardatabase object * @param object $current_user - the current user * @return string $status - on success - string success * on failure - string failure */ function asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $userid, $relcrmid, $callerInfo = false) { global $log, $current_user; // Reset date format for a while $date = new DateTimeField(null); $currentDate = $date->getDisplayDate(); $currentTime = $date->getDisplayTime(); require_once 'modules/Calendar/Activity.php'; $focus = new Activity(); $focus->column_fields['subject'] = "Incoming call from {$callerName} ({$callerNumber})"; $focus->column_fields['activitytype'] = "Call"; $focus->column_fields['date_start'] = $currentDate; $focus->column_fields['due_date'] = $currentDate; $focus->column_fields['time_start'] = $currentTime; $focus->column_fields['time_end'] = $currentTime; $focus->column_fields['eventstatus'] = "Held"; $focus->column_fields['assigned_user_id'] = $userid; $focus->save('Calendar'); $focus->setActivityReminder('off'); // Restore dateformat $current_user->date_format = $old_userdate_format; if (empty($relcrmid)) { if (empty($callerInfo)) { $callerInfo = getCallerInfo($callerNumber); } } else { $callerInfo = array(); $callerInfo['module'] = getSalesEntityType($relcrmid); $callerInfo['id'] = $relcrmid; } if ($callerInfo != false) { $tablename = array('Contacts' => 'vtiger_cntactivityrel', 'Accounts' => 'vtiger_seactivityrel', 'Leads' => 'vtiger_seactivityrel'); $sql = "insert into " . $tablename[$callerInfo['module']] . " values (?,?)"; $params = array($callerInfo['id'], $focus->id); $adb->pquery($sql, $params); } return $focus->id; }
/** * this function adds the call information to the actvity history * @param string $callerName - the caller name * @param string $callerNumber - the callers' number * @param string $callerType - the caller type (SIP/PSTN...) * @param object $adb - the peardatabase object * @param object $current_user - the current user * @return string $status - on success - string success * on failure - string failure */ function asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $current_user) { global $log; require_once 'modules/Calendar/Activity.php'; $focus = new Activity(); $focus->column_fields['subject'] = "Incoming call from {$callerName} ({$callerNumber})"; $focus->column_fields['activitytype'] = "Call"; $focus->column_fields['date_start'] = date('Y-m-d'); $focus->column_fields['due_date'] = date('Y-m-d'); $focus->column_fields['time_start'] = date('H:i'); $focus->column_fields['time_end'] = date('H:i'); $focus->column_fields['eventstatus'] = "Held"; $focus->column_fields['assigned_user_id'] = $current_user->id; $focus->save('Calendar'); $focus->setActivityReminder('off'); // $callerInfo = getCallerInfo("$callerType:".$callerNumber); // if($callerInfo == false){ $callerInfo = getCallerInfo(getStrippedNumber($callerNumber)); //} if ($callerInfo != false) { $tablename = array('Contacts' => 'vtiger_cntactivityrel', 'Accounts' => 'vtiger_seactivityrel', 'Leads' => 'vtiger_seactivityrel'); $sql = "insert into " . $tablename[$callerInfo['module']] . " values (?,?)"; $params = array($callerInfo[id], $focus->id); $adb->pquery($sql, $params); $status = "success"; } else { $status = "failure"; } return $status; }