Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Created August 15, 2011 14:03
Show Gist options
  • Save bjhaid/1146819 to your computer and use it in GitHub Desktop.
Save bjhaid/1146819 to your computer and use it in GitHub Desktop.
ajaxsearch_controller.rb
class AjaxsearchController < ApplicationController
def new
begin
@search = Sugar.find(:all, :conditions => {:first_name => "LIKE '%#{params[:name]}%'"}).collect{ |user| "<tr><td><b><a href='http://10.10.5.104:8080/wakanow/index.php?module=Contacts&action=DetailView&record="+"#{user.id}" + "'>" + "#{user.account_name}"+"</b></td>" + "</a><td>Email address: <b>" + "#{user.email1}"+"</b><a href=" + 'http://10.10.5.104:8080/wakanow/index.php?module=Contacts&action=DetailView&record=' + "#{user.id}" + "><img border=0 src='jquery/images/contacts.gif' title=" + "#{user.account_name}" + "/></a></tr>" }
# @search = Sugar.find(:all, :conditions => {:first_name => "LIKE '%#{params[:name]}'"}).collect{ |user| { :first_name => user.first_name :id => user.account_id, :name => user.account_name }}
rescue
@search = Sugar.find(:all, :conditions => {:first_name => "LIKE '%#{params[:name]}%'"}).collect{ |user| "<tr><td><b><a href='http://10.10.5.104:8080/wakanow/index.php?module=Contacts&action=DetailView&record="+"#{user.id}" + "'>" + "#{user.account_name}"+"</b></td>" + "</a><td>Email address: <b>" + "#{user.email1}"+"</b>&nbsp;<a href=" + 'http://10.10.5.104:8080/wakanow/index.php?module=Contacts&action=DetailView&record=' + "#{user.id}" + "><img border=0 src='jquery/images/contacts.gif' title=" + "#{user.account_name}" + "/></a></tr>" }
# @search = Sugar.find(:all, :conditions => {:first_name => "LIKE '%#{params[:name]}'"}).collect{ |user| { :first_name => user.first_name :id => user.account_id, :name => user.account_name }}
end
@ajaxsearches = Ajaxsearch.create({:name => params[:name], :extenno => params[:extenno]})
if @ajaxsearches.update_attributes(params[:ajaxsearch])
faye = Faye::Client.new('http://10.10.5.96:9292/faye')
msge = JSON.dump('channel' => "/ajaxsearches", 'data' => {'exten_no' => "#{@ajaxsearches.extenno}", 'resarray' => "#{@search* ","}"})
uri = URI.parse('http://0.0.0.0:9292/faye')
Net::HTTP.post_form(uri, :message => msge)
end
end
def create
@ajaxsearches = Ajaxsearch.new(params[:incoming])
end
end
incomings_controller.rb
require 'switchvox'
class IncomingsController < ApplicationController
def index
@incoming = Incoming.all
end
#the new method creates a record of the calls in database, queries sugarCRM for records matching the caller's phone number and publishes the data via the messaging server(faye)
def new
@incoming = Incoming.create({:exten_number => params[:exten_number], :cid_number => params[:cid_number]})
if (params[:cid_number])[0] == "8"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
elsif (params[:cid_number])[0] == "7"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
elsif (params[:cid_number])[0] == "1"
params[:cid_number] = "0" + params[:cid_number]
begin
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
rescue
@contact = Sugar.find_by_phone_fax(params[:cid_number]) || Sugar.find_by_phone_mobile(params[:cid_number]) || Sugar.find_by_phone_other(params[:cid_number]) || Sugar.find_by_phone_work(params[:cid_number])
end
end
if @incoming.update_attributes(params[:incoming])
require 'eventmachine'
#the IP address for the machine hosting the faye messaging server should be passed here
faye = Faye::Client.new('http://10.10.5.208:9292/faye')
unless @contact.nil?
msg = JSON.dump('channel' => "/incomings", 'data' => {'exten_id' => "#{@incoming.exten_number}", 'caller_id' => "#{@contact.id}", 'caller_name' => "#{@contact.account_name.split(' ').map {|w| w.capitalize }.join(' ')}", 'caller_number' => "#{params[:cid_number]}" })
uri = URI.parse('http://0.0.0.0:9292/faye')
Net::HTTP.post_form(uri, :message => msg)
#EM.run {
#faye.publish("/incomings", {'exten_id' => "#{@incoming.exten_number}", 'caller_id' => "#{@contact.id}", 'caller_name' => "#{@contact.account_name.capitalize}", 'caller_number' => "#{params[:cid_number]}" })
#}
else
msg = JSON.dump('channel' => "/incomings", 'data' => {"exten_id" => "#{@incoming.exten_number}", "caller_number" => "#{params[:cid_number]}", "caller_name" => "no details exists for this user"})
uri = URI.parse('http://0.0.0.0:9292/faye')
Net::HTTP.post_form(uri, :message => msg)
#EM.run {
#faye.publish("/incomings", {"exten_id" => "#{@incoming.exten_number}", "caller_number" => "#{params[:cid_number]}", "caller_name" => "no details exists for this user"})
#}
end
# end
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @incoming }
end
end
def create
@incoming = Incoming.new(params[:incoming])
end
# GET /incomings/1
# GET /incomings/1.xml
def call
Switchvox_call.calling(params[:dial_number])
end
def update
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment