Class: Widgets::ManageProfile::Base

Inherits:
ErpApp::Widgets::Base
  • Object
show all
Defined in:
app/widgets/manage_profile/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_layoutObject



287
288
289
290
291
292
293
294
# File 'app/widgets/manage_profile/base.rb', line 287

def base_layout
  begin
    file = File.join(File.dirname(__FILE__), "/views/layouts/base.html.erb")
    IO.read(file)
  rescue
    return nil
  end
end

.titleObject



279
280
281
# File 'app/widgets/manage_profile/base.rb', line 279

def title
  "Manage Profile"
end

.widget_nameObject



283
284
285
# File 'app/widgets/manage_profile/base.rb', line 283

def widget_name
  File.basename(File.dirname(__FILE__))
end

Instance Method Details

#contact_purpose_in_use?(contacts, purpose) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
270
# File 'app/widgets/manage_profile/base.rb', line 261

def contact_purpose_in_use?(contacts, purpose)
  result = false
  contacts.each do |e|
    if e.contact.contact_purposes[0].internal_identifier == purpose
      result = true
      break
    end
  end
  result
end

#indexObject



33
34
35
36
37
# File 'app/widgets/manage_profile/base.rb', line 33

def index
  load_profile_data

  render
end

#load_profile_dataObject



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
# File 'app/widgets/manage_profile/base.rb', line 5

def load_profile_data
  @user = User.find(current_user)
  @individual = @user.party.business_party
  @email_addresses = @user.party.find_all_contacts_by_contact_mechanism(EmailAddress)
  @phone_numbers = @user.party.find_all_contacts_by_contact_mechanism(PhoneNumber)
  @postal_addresses = @user.party.find_all_contacts_by_contact_mechanism(PostalAddress)

  contact_purposes = ContactPurpose.all
  @purpose_hash={}
  contact_purposes.each do |p|
    @purpose_hash[p.description]=p.internal_identifier
  end

  countries= GeoCountry.all
  @countries_id=[]
  @countries_id << ["Country", "default"]
  countries.each do |c|
    @countries_id << [c.name, c.id]
  end

  states= GeoZone.all
  @states_id=[]
  @states_id << ["State", "default"]
  states.each do |s|
    @states_id << [s.zone_name, s.id]
  end
end

#locateObject

should not be modified modify at your own risk



274
275
276
# File 'app/widgets/manage_profile/base.rb', line 274

def locate
  File.dirname(__FILE__)
end

#update_contact_informationObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/widgets/manage_profile/base.rb', line 122

def update_contact_information
  @user= User.find(current_user)
  @email_addresses= @user.party.find_all_contacts_by_contact_mechanism(EmailAddress)
  @phone_numbers= @user.party.find_all_contacts_by_contact_mechanism(PhoneNumber)
  @postal_addresses= @user.party.find_all_contacts_by_contact_mechanism(PostalAddress)
  contact_type_in_use=false

  #### Updates email records ####
  @email_addresses.each_with_index do |e, i|
    if e.email_address != params[:email_addresses][i.to_s]
      email_address_args={:email_address => params[:email_addresses][i.to_s]}
      @user.party.update_or_add_contact_with_purpose(EmailAddress,
                                                     ContactPurpose.find_by_internal_identifier(params[:email_address_contact_purposes][i.to_s]),
                                                     email_address_args)
    end
  end

  #### Updates Phone Numbers ####
  @phone_numbers.each_with_index do |p, i|
    if p.phone_number != params[:phone_numbers][i.to_s]
      phone_number_args={:phone_number => params[:phone_numbers][i.to_s]}
      @user.party.update_or_add_contact_with_purpose(PhoneNumber,
                                                     ContactPurpose.find_by_internal_identifier(params[:phone_number_contact_purposes][i.to_s]),
                                                     phone_number_args)
    end
  end

  #### Updates Postal Addresses
  @postal_addresses.each_with_index do |a, i|
    postal_address_args= {}

    if a.address_line_1 != params[:postal_addresses][i.to_s][:address_line_1]
      postal_address_args[:address_line_1]= params[:postal_addresses][i.to_s][:address_line_1]
    end

    if a.address_line_2 != params[:postal_addresses][i.to_s][:address_line_2]
      postal_address_args[:address_line_2]= params[:postal_addresses][i.to_s][:address_line_2]
    end

    if a.city != params[:postal_addresses][i.to_s][:city]
      postal_address_args[:city]= params[:postal_addresses][i.to_s][:city]
    end

    if a.geo_zone_id != params[:postal_addresses][i.to_s][:state_id].to_i
      postal_address_args[:geo_zone_id]= params[:postal_addresses][i.to_s][:state_id].to_i
      postal_address_args[:state]= GeoZone.find(params[:postal_addresses][i.to_s][:state_id]).zone_name
    end

    if a.zip != params[:postal_addresses][i.to_s][:zip]
      postal_address_args[:zip]= params[:postal_addresses][i.to_s][:zip]
    end

    if a.geo_country_id != params[:postal_addresses][i.to_s][:country_id].to_i
      postal_address_args[:geo_country_id]= params[:postal_addresses][i.to_s][:country_id].to_i
      postal_address_args[:country]= GeoCountry.find(params[:postal_addresses][i.to_s][:country_id]).name
    end

    if !postal_address_args.empty?
      @user.party.update_or_add_contact_with_purpose(PostalAddress,
                                                     ContactPurpose.find_by_internal_identifier(params[:postal_address_contact_purposes][i.to_s]),
                                                     postal_address_args)
    end
  end

  #### Adds new email address ####
  if params[:new_email_address] != nil && params[:new_email_address] != ""
    if !contact_purpose_in_use?(@email_addresses, params[:new_email_address_contact_purpose])
      @user.party.update_or_add_contact_with_purpose(EmailAddress,
                                                     ContactPurpose.find_by_internal_identifier(params[:new_email_address_contact_purpose]),
                                                     :email_address => params[:new_email_address])
    else
      contact_type_in_use=true
    end
  end

  #### Adds new phone number ####
  if params[:new_phone_number] != nil && params[:new_phone_number] != ""
    if !contact_purpose_in_use?(@phone_numbers, params[:new_phone_number_contact_purpose])
      @user.party.update_or_add_contact_with_purpose(PhoneNumber,
                                                     ContactPurpose.find_by_internal_identifier(params[:new_phone_number_contact_purpose]),
                                                     :phone_number => params[:new_phone_number])
    else
      contact_type_in_use=true
    end
  end

  #### Adds new postal address ####
  new_postal_address_args= {}

  if params[:new_postal_address][:address_line_1] != "Address Line 1"
    new_postal_address_args[:address_line_1]= params[:new_postal_address][:address_line_1]
  end

  if params[:new_postal_address][:address_line_2] != "Address Line 2"
    new_postal_address_args[:address_line_2]= params[:new_postal_address][:address_line_2]
  end

  if params[:new_postal_address][:city] != "City"
    new_postal_address_args[:city]= params[:new_postal_address][:city]
  end

  if !params[:new_postal_address][:state_id].blank? and params[:new_postal_address][:state_id] != "default"
    new_postal_address_args[:geo_zone_id]= params[:new_postal_address][:state_id].to_i
    new_postal_address_args[:state]= GeoZone.find(params[:new_postal_address][:state_id]).zone_name
  end

  if params[:new_postal_address][:zip] != "Zipcode"
    new_postal_address_args[:zip]= params[:new_postal_address][:zip]
  end

  if !params[:new_postal_address][:country_id].blank? and params[:new_postal_address][:country_id] != "default"
    new_postal_address_args[:geo_country_id]= params[:new_postal_address][:country_id].to_i
    new_postal_address_args[:country]= GeoCountry.find(params[:new_postal_address][:country_id]).name
  end

  if !new_postal_address_args.empty?
    if !contact_purpose_in_use?(@postal_addresses, params[:new_postal_address_contact_purpose])
      @user.party.update_or_add_contact_with_purpose(PostalAddress,
                                                     ContactPurpose.find_by_internal_identifier(params[:new_postal_address_contact_purpose]),
                                                     new_postal_address_args)
    else
      contact_type_in_use=true
    end
  end

  load_profile_data

  #### Renders proper error or success message ####
  if contact_type_in_use
    @message = "New contacts cannot have the same contact type as old contacts"
    @message_cls = 'sexyerror'
    render :update => {:id => "#{@uuid}_result", :view => :index}
  else
    @message = "Your Contact Information Was Updated"
    render :update => {:id => "#{@uuid}_result", :view => :index}
  end

end

#update_passwordObject



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
# File 'app/widgets/manage_profile/base.rb', line 87

def update_password
  if @user = User.authenticate(current_user.username, params[:old_password])
    load_profile_data

    if !params[:new_password].blank? && !params[:password_confirmation].blank? && params[:new_password] == params[:password_confirmation]

      @user.password_confirmation= params[:password_confirmation]

      if @user.change_password!(params[:new_password])
        @message = "Password Updated"
        
        render :update => {:id => "#{@uuid}_result", :view => :index}
      else
        @message = "Error Updating Password"
        @message_cls = 'sexyerror'
        #### validation failed ####
        render :update => {:id => "#{@uuid}_result", :view => :index}
      end

    else
      #### password and password confirmation cant be blank or unequal ####
      @message = "Password Confirmation Must Match Password"
      @message_cls = 'sexyerror'
      render :update => {:id => "#{@uuid}_result", :view => :index}
    end
  else
    load_profile_data
    #### old password wrong ####
    @message = "Invalid Old Password"
    @message_cls = 'sexyerror'
    render :update => {:id => "#{@uuid}_result", :view => :index}
  end

end

#update_user_informationObject



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
# File 'app/widgets/manage_profile/base.rb', line 39

def update_user_information
  #### Get appropriate models ####

  @user=User.find(current_user)
  @individual= @user.party.business_party

  #### Formating the date for sqlite. Will probably need diffrent formating for production ####
  if params[:date][:day].to_i < 10
    day="0#{params[:date][:day]}"
  else
    day= params[:date][:day]
  end

  if params[:date][:month].to_i < 10
    month="0#{params[:date][:month]}"
  else
    month= params[:date][:month]
  end

  formated_date= "#{params[:date][:year]}-#{month}-#{day}"

  @individual.current_first_name= params[:first_name]
  @individual.current_last_name= params[:last_name]
  @individual.current_middle_name= params[:middle_name]
  @individual.gender= params[:gender]
  @individual.birth_date= formated_date
  @user.email = params[:email]

  #### check validation then save and render message ####
  if @user.changed? || @individual.changed?
    if @user.valid? && @individual.valid?
      @user.save
      @individual.save
      @message = "User Information Updated"
      load_profile_data
      render :update => {:id => "#{@uuid}_result", :view => :index}
    else
      @message_cls = 'sexyerror'
      @message = "Error Updating User Information"
      render :update => {:id => "#{@uuid}_result", :view => :index}
    end
  else
    render :update => {:id => "#{@uuid}_result", :view => :index}
  end

end