summaryrefslogtreecommitdiff
path: root/app/controllers/call_histories_controller.rb
blob: edd19cb7adbbcff60dd2aeb55e2a92a5fd8e8b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class CallHistoriesController < ApplicationController
  MAX_CALL_HISTORY_ENTRIES = 10000;

  helper_method :sort_column, :sort_descending

  load_resource :sip_account

  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs
  
  before_filter { |controller|
    if ! params[:type].blank? then
      @type = params[:type].to_s
    end

    if ! params[:page].blank? then
      @pagination_page_number = params[:page].to_i
    end
  }

  def index
    hunt_group_member_ids = PhoneNumber.where(:phone_numberable_type => 'HuntGroupMember', :number => @sip_account.phone_numbers.map {|a| a.number}).map {|a| a.phone_numberable_id}
    hunt_group_ids = HuntGroupMember.where(:id => hunt_group_member_ids, :active => true).map {|a| a.hunt_group_id}
    calls = CallHistory.where('(call_historyable_type = "SipAccount" AND call_historyable_id = ?) OR (call_historyable_type = "HuntGroup" AND call_historyable_id IN (?))', @sip_account.id, hunt_group_ids).order(sort_column + ' ' + (sort_descending ? 'DESC' : 'ASC'))

    @calls_count = calls.count
    @calls_received_count = calls.where(:entry_type => 'received').count
    @calls_dialed_count = calls.where(:entry_type => 'dialed').count
    @calls_missed_count = calls.where(:entry_type => 'missed').count
    @calls_forwarded_count = calls.where(:entry_type => 'forwarded').count

    if ! @type.blank?
      @call_histories = calls.where(:entry_type => @type).limit(MAX_CALL_HISTORY_ENTRIES)
    else
      @call_histories = calls.limit(MAX_CALL_HISTORY_ENTRIES)
    end

    @call_histories =  @call_histories.paginate(
      :page => @pagination_page_number,
      :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
    )
  end


  def destroy
    @call_history = CallHistory.where(:id => params[:id]).first
    if can?(:destroy, @call_history)
      @call_history.destroy
      m = method( :"#{@parent.class.name.underscore}_call_histories_url" )
      respond_to do |format|
        format.html { redirect_to m.(), :notice => t('call_histories.controller.successfuly_destroyed')}
        format.js
      end
    end
  end

  def destroy_multiple
    if ! params[:selected_ids].blank? then
      result = @sip_account.call_histories.where(:id => params[:selected_ids]).destroy_all();
    end

    m = method( :"#{@parent.class.name.underscore}_call_histories_url" )
    if result
      redirect_to m.(), :notice => t('call_histories.controller.successfuly_destroyed')
    else
      redirect_to m.()
    end
  end

  def call
    @call_history = CallHistory.where(:id => params[:id]).first
    if can?(:call, @call_history) && @sip_account.registration
      phone_number = @call_history.display_number
      caller_id_number = phone_number
      if ! phone_number.blank?
        if @sip_account.clir != @call_history.clir
          if @call_history.clir == true
            phone_number = 'f-dcliron-' + phone_number
          elsif call.clir == false
            phone_number = 'f-dcliroff-' + phone_number
          end
        end

        @sip_account.call(phone_number, caller_id_number, @call_history.display_name)
      end
    end
    redirect_to(:back)
  end

  private
  def set_and_authorize_parent
    @parent = @sip_account

    authorize! :read, @parent

    @show_path_method = method( :"#{@parent.class.name.underscore}_call_history_path" )
    @index_path_method = method( :"#{@parent.class.name.underscore}_call_histories_path" )
    @new_path_method = method( :"new_#{@parent.class.name.underscore}_call_history_path" )
    @edit_path_method = method( :"edit_#{@parent.class.name.underscore}_call_history_path" )
  end

  def spread_breadcrumbs
    if @parent.class == SipAccount
     if @sip_account.sip_accountable.class == User
       add_breadcrumb t("#{@sip_account.sip_accountable.class.name.underscore.pluralize}.index.page_title"), method( :"tenant_#{@sip_account.sip_accountable.class.name.underscore.pluralize}_path" ).(@sip_account.tenant)
       add_breadcrumb @sip_account.sip_accountable, method( :"tenant_#{@sip_account.sip_accountable.class.name.underscore}_path" ).(@sip_account.tenant, @sip_account.sip_accountable)
     end
     add_breadcrumb t("sip_accounts.index.page_title"), method( :"#{@sip_account.sip_accountable.class.name.underscore}_sip_accounts_path" ).(@sip_account.sip_accountable)
     add_breadcrumb @sip_account, method( :"#{@sip_account.sip_accountable.class.name.underscore}_sip_account_path" ).(@sip_account.sip_accountable, @sip_account)
     add_breadcrumb t("call_histories.index.page_title"), sip_account_call_histories_path(@sip_account)
     if @call_history && !@call_history.new_record?
       add_breadcrumb @call_history, sip_account_call_history_path(@sip_account, @call_history)
     end
    end
  end

  def sort_descending
    if sort_column == 'start_stamp' && params[:desc].to_s.blank?
      return true
    end
   
    params[:desc].to_s == 'true'
  end

  def sort_column
    CallHistory.column_names.include?(params[:sort]) ? params[:sort] : 'start_stamp'
  end

end