Пример #1
0
function findCallByAsteriskDestId($asteriskDestId)
{
    global $soapClient, $soapSessionId, $verbose_logging;
    logLine("# +++ findCallByAsteriskDestId({$asteriskDestId})\n");
    //
    // First, fetch row in asterisk_log...
    //
    $sql = sprintf("SELECT * from asterisk_log WHERE asterisk_dest_id='{$asteriskDestId}'", $asteriskDestId);
    $queryResult = db_checked_query($sql);
    if ($queryResult === FALSE) {
        return FALSE;
    }
    while ($row = $GLOBALS['db']->fetchByAssoc($queryResult)) {
        $callRecId = $row['call_record_id'];
        logLine("! FindCallByAsteriskDestId - Found entry in asterisk_log recordId={$callRecId}\n");
        //
        // ... then locate Object in Calls module:
        //
        $soapResult = $soapClient->call('get_entry', array('session' => $soapSessionId, 'module_name' => 'Calls', 'id' => $callRecId));
        $resultDecoded = decode_name_value_list($soapResult['entry_list'][0]['name_value_list']);
        // echo ("# ** Soap call successfull, dumping result ******************************\n");
        // var_dump($soapResult);
        if ($verbose_logging) {
            var_dump($resultDecoded);
        }
        // var_dump($row);
        // echo ("# ***********************************************************************\n");
        //
        // also store raw sql data in case we need it later...
        //
        return array('bitter' => $row, 'sweet' => $resultDecoded);
    }
    logLine("! Warning, FindCallByAsteriskDestId results s\n    et was empty!\n");
    return FALSE;
}
function findUserByAsteriskExtension($aExtension)
{
    global $soapClient, $soapSessionId;
    print "# +++ findUserByAsteriskExtension({$aExtension})\n";
    $soapArgs = array('session' => $soapSessionId, 'module_name' => 'Users', 'query' => sprintf("(users_cstm.asterisk_ext_c='%s')", $aExtension));
    $soapResult = $soapClient->call('get_entry_list', $soapArgs);
    // var_dump($soapResult);
    if ($soapResult['error']['number'] != 0) {
        echo "! Warning: SOAP error " . $soapResult['error']['number'] . " " . $soapResult['error']['string'] . "\n";
    } else {
        $resultDecoded = decode_name_value_list($soapResult['entry_list'][0]['name_value_list']);
        // print "--- SOAP get_entry_list() ----- RESULT --------------------------------------\n";
        // var_dump($resultDecoded);
        // print "-----------------------------------------------------------------------------\n";
        return $resultDecoded['id'];
    }
    return FALSE;
}
Пример #3
0
function findSugarObjectByPhoneNumber($aPhoneNumber)
{
    global $soapClient, $soapSessionId;
    logLine("### +++ find ContactByPhoneNumber({$aPhoneNumber})\n");
    // Add if phonenumber .length == 10
    $searchPattern = $aPhoneNumber;
    //$searchPattern = regexify($aPhoneNumber);
    //
    // Plan A: Attempt to locate an object in Contacts
    //        $soapResult = $soapClient->call('get_entry' , array('session' => $soapSessionId, 'module_name' => 'Calls', 'id' => $callRecId));
    //
    //************ CID.agi
    // =~ <--- use the left side as input string to regex.
    // s -- replace.
    //
    //$number =~ s/\D//g;
    //$number =~ m/([0-9]{7})$/;
    //$number = $1 if ($1);
    //
    //if ($1) {
    //  $regje = $number;
    //  $regje =~ s/(\d)/$1\[^\\d\]*/g;
    //  $regje = '(' . $regje . ')' . '$';
    //} elsif($number) {
    //  $regje = '^' . $number . '$';
    //} else {
    //  debugAndQuit("No caller ID found for this call");
    //}
    //
    //debug("Searching for regexp $regje",5);
    //
    //# lookup the number @ contacts
    //$result = $service->get_entry_list($sid,"Contacts","contacts.phone_home REGEXP '$regje' OR contacts.phone_mobile REGEXP '$regje' OR contacts.phone_work REGEXP '$regje' OR contacts.phone_other REGEXP '$regje' OR contacts.phone_fax REGEXP '$regje'","",0,{a=>"first_name",b=>"last_name",c=>"account_name"},1,0)->result;
    //$id = $result->{entry_list}[0]{id};
    // TODO figure out what that 2nd case could be the elseif part...
    $aPhoneNumber = preg_replace('/\\D/', '', $aPhoneNumber);
    // removes everything that isn't a digit.
    // TODO make the '7' below a configurable parameter... some may prefer to match on 10.
    if (preg_match('/([0-9]{7})$/', $aPhoneNumber, $matches)) {
        $aPhoneNumber = $matches[1];
    }
    $regje = preg_replace('/(\\d)/', '$1\\[^\\d\\]*', $aPhoneNumber);
    $regje = '(' . $regje . ')$';
    logLine("  findSugarObjectByPhoneNumber: Contact query components- Phone: {$aPhoneNumber}   RegEx: {$regje}\n");
    //*******/
    $soapArgs = array('session' => $soapSessionId, 'module_name' => 'Contacts', 'select_fields' => array('id', 'account_id', 'last_name'), 'query' => "contacts.phone_home REGEXP '{$regje}' OR contacts.phone_mobile REGEXP '{$regje}' OR contacts.phone_work REGEXP '{$regje}' OR contacts.phone_other REGEXP '{$regje}' OR contacts.phone_fax REGEXP '{$regje}'");
    // print "--- SOAP get_entry_list() ----- ARGS ----------------------------------------\n";
    // var_dump($soapArgs);
    // print "-----------------------------------------------------------------------------\n";
    $soapResult = $soapClient->call('get_entry_list', $soapArgs);
    //print "--- SOAP get_entry_list() FOR GET CONTACT ------------------------------------\n";
    //var_dump($soapResult);
    //print "-----------------------------------------------------------------------------\n";
    if (!isSoapResultAnError($soapResult)) {
        // TODO implement a working array_unique
        //$uniqueEntryList = array_unique($soapResult['entry_list'] );
        $uniqueEntryList = $soapResult['entry_list'];
        $resultDecoded = decode_name_value_list($uniqueEntryList[0]['name_value_list']);
        if (count($uniqueEntryList) > 1) {
            $foundMultipleAccounts = FALSE;
            $account_id = $resultDecoded['account_id'];
            //logLine(print_r($resultDecoded,true));
            // TODO I had 43 entries returned for 2 contacts with matching number... need better distinct support.  Apparently, no way to do this via soap... probably need to create a new service endpoint.
            // TODO (continued) if you have a contact with no account associated... this will not associate it with the contact b/c it thinks there are 43 unique contacts being returned.
            // Perhaps we should just do a database call instead and use the same logic in place in callListener.
            for ($i = 1; $i < count($uniqueEntryList); $i++) {
                $resultDecoded = decode_name_value_list($uniqueEntryList[$i]['name_value_list']);
                // logLine(print_r($resultDecoded,true));
                if ($account_id != $resultDecoded['account_id']) {
                    $foundMultipleAccounts = TRUE;
                }
            }
            if (!$foundMultipleAccounts) {
                $result = array();
                $result['id'] = $account_id;
                // TODO there is an error case here where you could have multiple contacts with same number and none of them are assigned to ANY account.
                logLine("Found multiple contacts (" . count($uniqueEntryList) . ") -- all belong to same account, associating call with account {$result['id']}\n");
                return array('type' => 'Accounts', 'values' => $result);
            } else {
                logLine("Multiple contacts matched multiple accounts, Not associating\n");
                return FALSE;
            }
        }
        //print "--- Decoded get_entry_list() FOR GET CONTACT --------------------------------------\n";
        //var_dump($resultDecoded);
        //print "-----------------------------------------------------------------------------\n";
        return array('type' => 'Contacts', 'values' => $resultDecoded);
    }
    // Oops nothing found :-(
    return FALSE;
}