summaryrefslogtreecommitdiff
path: root/app/controllers/tenants_controller.rb
blob: 7bb8ecdb9f5fa423345f219c68722ac08a8e2fdb (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
class TenantsController < ApplicationController
  load_and_authorize_resource :tenant
  
  def index
  end

  def show
  end

  def new
    @tenant.name = generate_a_new_name(@tenant)
    @tenant.sip_domain = SipDomain.last
    @tenant.country  = GemeinschaftSetup.first.country
    @tenant.language = GemeinschaftSetup.first.language
    @tenant.internal_extension_ranges = '10-99'
    @tenant.from_field_voicemail_email = 'admin@localhost'
    @tenant.from_field_pin_change_email = 'admin@localhost'
  end

  def create
    if @tenant.save
      # Become a member of this tenant.
      #
      @tenant.tenant_memberships.create(:user_id => @current_user.id)
      
      # Groups
      #
      admin_group = @tenant.user_groups.create(:name => t('gemeinschaft_setups.initial_setup.admin_group_name'))
      admin_group.users << @current_user
      
      user_group = @tenant.user_groups.create(:name => t('gemeinschaft_setups.initial_setup.user_group_name'))
      user_group.users << @current_user
      
      @current_user.update_attributes!(:current_tenant_id => @tenant.id)
      
      # Generate the internal_extensions
      #
      if !@tenant.internal_extension_ranges.blank?
        if @tenant.array_of_internal_extension_numbers.count < 105
          # This can be done more or less quick.
          @tenant.generate_internal_extensions
        else
          # Better be on the save side and start a delayed job for this.
          @tenant.delay.generate_internal_extensions
        end
      end
      
      # Generate the external numbers (DIDs)
      #
      if !@tenant.did_list.blank?
        if @tenant.array_of_dids.count < 105
          # This can be done more or less quick.
          @tenant.generate_dids
        else
          # Better be on the save side and start a delayed job for this.
          @tenant.delay.generate_dids
        end
      end
       
      if Delayed::Job.count > 0
        if SipAccount.any? || Phone.any?
          redirect_to @tenant, :notice => t('tenants.controller.successfuly_created_plus_delayed_jobs', 
                                          :resource => @tenant, 
                                          :amount_of_numbers => @tenant.array_of_internal_extension_numbers.count + @tenant.array_of_dids.count
                                          )
        else
          redirect_to page_beginners_intro_path, :notice => t('tenants.controller.successfuly_created_plus_delayed_jobs', 
                                          :resource => @tenant, 
                                          :amount_of_numbers => @tenant.array_of_internal_extension_numbers.count + @tenant.array_of_dids.count
                                          )
        end
      else
        if SipAccount.any? || Phone.any?
          redirect_to @tenant, :notice => t('tenants.controller.successfuly_created', 
                                            :resource => @tenant
                                            )
        else
          redirect_to page_beginners_intro_path, :notice => t('tenants.controller.successfuly_created', 
                                            :resource => @tenant
                                            )
        end
      end
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @tenant.update_attributes(params[:tenant])
      redirect_to @tenant, :notice  => t('tenants.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @tenant.destroy
    redirect_to tenants_url, :notice => t('tenants.controller.successfuly_destroyed')
  end
  
end