예제 #1
0
function findUserByAsteriskExtension($aExtension)
{
    logLine("### +++ findUserByAsteriskExtension({$aExtension})\n");
    $qry = "select id from users join users_cstm on users.id = users_cstm.id_c where users_cstm.asterisk_ext_c={$aExtension} and status='Active'";
    $result = mysql_checked_query($qry);
    if ($result) {
        $row = mysql_fetch_array($result);
        return $row['id'];
    }
    return FALSE;
    ///// OLD WAY OF DOING IT IS WITH SOAP... DIDN"T WORK FOR ME... so reverted to db query.
    /*
        global $soapClient, $soapSessionId;
        print("# +++ findUserByAsteriskExtension($aExtension)\n");
    
    		//'select_fields'=>array('id', 'first_name', 'last_name'),
    	//'deleted' => 0,
        $soapArgs = array(
            'session' => $soapSessionId,
            'module_name' => 'Users',
    	'query' => '(users_cstm.asterisk_ext_c=\'710\')',
       // 	'query' => sprintf("(users_cstm.asterisk_ext_c='%s')", $aExtension),
    'select_fields'=>array('id', 'first_name', 'last_name'),
        );
    //var_dump($soapArgs);
    
        $soapResult = $soapClient->call('get_entry_list', $soapArgs);
    
         var_dump($soapResult);
    
        if ($soapResult['error']['number'] != 0) {
            logLine("! Warning: SOAP error " . $soapResult['error']['number'] . " " . $soapResult['error']['string'] . "\n");
        }
    else if( $soapResult['result_count'] == 0 ) {
    	logLine("! No results returned\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;
    */
}
function findCallByAsteriskId($asteriskId)
{
    global $soapClient, $soapSessionId;
    print "# +++ findCallByAsteriskId({$asteriskId})\n";
    //
    // First, fetch row in asterisk_log...
    //
    $sql = sprintf("SELECT * from asterisk_log WHERE asterisk_id='{$asteriskId}'", $asteriskId);
    $queryResult = mysql_checked_query($sql);
    if ($queryResult === FALSE) {
        return FALSE;
    }
    while ($row = mysql_fetch_assoc($queryResult)) {
        $callRecId = $row['call_record_id'];
        echo "! 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);
        // 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);
    }
    echo "! Warning, results set was empty!\n";
    return FALSE;
}
function mysql_checked_query($aQuery)
{
    global $mysql_loq_queries;
    global $mysql_log_results;
    global $sql_connection, $sql_db, $sugar_config;
    print "# +++ mysql_checked_query()\n";
    $query = trim($aQuery);
    if ($mysql_loq_queries) {
        print "! SQL: {$query}\n";
    }
    // Is this is a SELECT ?
    $isSelect = preg_match("#^select#i", $query);
    $sqlResult = mysql_query($query);
    if ($mysql_log_results) {
        if (!$sqlResult) {
            // Error occured
            print "! SQL error " . mysql_errno() . " (" . mysql_error() . ")\n";
            if (mysql_errno() == 2006) {
                // Server has gone away
                print "! Trying to reconnect";
                @mysql_close($sql_connection);
                $sql_connection = mysql_connect($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password']);
                $sql_db = mysql_select_db($sugar_config['dbconfig']['db_name']);
                print "! Trying the query again";
                mysql_checked_query($aQuery);
            }
        } else {
            // SQL succeeded
            if ($isSelect) {
                print "# Rows in result set: " . mysql_num_rows($sqlResult) . "\n";
            } else {
                print "# Rows affected: " . mysql_affected_rows() . "\n";
            }
        }
    }
    // Pass original result to caller
    print "# --- mysql_checked_query()\n";
    return $sqlResult;
}