summaryrefslogtreecommitdiff
path: root/app/controllers/phones_controller.rb
blob: 77e7b5efd8745bbd0af0f72915a41271a7e33ddc (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
class PhonesController < ApplicationController
  load_resource :tenant
  load_resource :user
  load_and_authorize_resource :phone, :through => [:tenant, :user]

  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs

  helper_method :sort_column, :sort_descending

  def index
    if @parent.class == Tenant
      @phones = @parent.tenant_user_phones.order(sort_column + ' ' + (sort_descending ? 'DESC' : 'ASC')).paginate(
        :page => params[:page],
        :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
      )
    else
      @phones = @parent.phones.order(sort_column + ' ' + (sort_descending ? 'DESC' : 'ASC')).paginate(
        :page => params[:page],
        :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
      )
    end
  end

  def show
  end

  def new
    @phone = @phoneable.phones.build()

    set_fallback_sip_accounts
    
    # Use the last phone.phone_model as the default.
    #
    @phone.phone_model_id = Phone.last.try(:phone_model).try(:id)
  end

  def create
    @phone = @phoneable.phones.build(params[:phone])
    if !@tenant
      @tenant = @user.current_tenant
    end
    @phone.tenant = @tenant
    if @phone.save
      m = method( :"#{@phoneable.class.name.underscore}_phone_path" )
      redirect_to m.( @phoneable, @phone ), :notice => t('phones.controller.successfuly_created')
    else
      set_fallback_sip_accounts
      render :new
    end
  end

  def edit
    set_fallback_sip_accounts
  end

  def update
    if @phone.update_attributes(params[:phone])
      m = method( :"#{@phoneable.class.name.underscore}_phone_path" )
      redirect_to m.( @phoneable, @phone ), :notice => t('phones.controller.successfuly_updated')
    else
      set_fallback_sip_accounts
      render :edit
    end
  end

  def destroy
    @phone.destroy
    m = method( :"#{@phoneable.class.name.underscore}_phones_url" )
    redirect_to m.( @phoneable ), :notice => t('phones.controller.successfuly_destroyed')
  end

  private
  def set_and_authorize_parent
    @phoneable = (@user || @tenant)
    @parent = @phoneable
    authorize! :read, @parent
    @nesting_prefix = @phoneable ? "#{@phoneable.class.name.underscore}_" : ''
  end

  def spread_breadcrumbs
    if @user
      add_breadcrumb t('users.index.page_title'), tenant_users_path(@user.current_tenant) 
      add_breadcrumb @user, tenant_user_path(@user.current_tenant, @user) 
      add_breadcrumb t('phones.index.page_title'), user_phones_path(@user) 
    elsif @tenant
      add_breadcrumb t('phones.index.page_title'), tenant_phones_path(@tenant) 
    end
    if @phone && !@phone.new_record?
      add_breadcrumb @phone, method( :"#{@phone.phoneable.class.name.underscore}_phone_path" ).(@phone.phoneable, @phone)
    end
  end

  def set_fallback_sip_accounts
    used_sip_account_ids = Phone.pluck(:fallback_sip_account_id) + PhoneSipAccount.pluck(:sip_account_id)
    if @phone
      used_sip_account_ids = used_sip_account_ids - [ @phone.fallback_sip_account_id ]
    end
    @fallback_sip_accounts = SipAccount.where(:sip_accountable_type => 'Tenant') - SipAccount.where(:id => used_sip_account_ids)
  end

  def sort_descending
    params[:desc].to_s == 'true'
  end

  def sort_column
    Phone.column_names.include?(params[:sort]) ? params[:sort] : 'id'
  end
  
end