/
members_controller.rb
580 lines (458 loc) · 17.4 KB
/
members_controller.rb
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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# Copyright (C) 2009 Pascal Rettig.
class MembersController < CmsController # :nodoc: all
permit 'editor_members', :only => [ :index, :create, :edit, :view, :delete_member ]
layout 'manage'
cms_admin_paths "people",
"People" => { :controller => '/members' },
"Segments" => { :action => 'segments' }
# need to include
include ActiveTable::Controller
protected
def segmentations
return @segmentations if @segmentations
@segmentations = UserSegment.find(:all, :conditions => {:main_page => true, :status => 'finished'}, :order => 'name')
@segmentations << self.segment if self.segment && ! @segmentations.find { |seg| seg.id == self.segment.id }
@segmentations
end
def segment
@segment ||= UserSegment.find_by_id params[:path][0]
end
def count_end_users(opts)
if self.search_results
self.search.total
elsif self.segment
self.segment.last_count
else
EndUser.count :conditions => 'client_user_id IS NULL'
end
end
def find_end_users(opts)
if self.search_results
self.search.users
elsif self.segment
pages, users = self.segment.paginate self.search.page, :per_page => 25, :include => [:user_class, :domain_file]
users
else
EndUser.find(:all, :conditions => 'client_user_id IS NULL', :include => [:user_class, :domain_file], :offset => opts[:offset], :limit => opts[:limit], :order => opts[:order])
end
end
def search
return @search if @search
@search = EndUserSearch.new
@search.terms = params[:terms]
@search.per_page = 25
@search.offset = (params[:offset] || 0).to_i
@search.offsets = (params[:offsets] || [0]).collect { |n| n.to_i }
@search.user_segment = self.segment
@search.page = (params[:page] || 1).to_i
@search.offset = @search.offsets[@search.page-1] if @search.user_segment && @search.offsets.length >= @search.page
@search
end
def search_results
@search_results ||= self.search.search(:include => [:user_class, :domain_file]) if self.search.terms
end
def get_user_classes
UserClass.find(:all,:order => 'name',:conditions => 'editor=0 AND id>3').collect { |cls| [cls.name,cls.id] }
end
def handle_table_actions
if request.post? && params[:table_action] && params[:user].is_a?(Hash)
if params[:table_action] == 'delete'
EndUser.destroy(params[:user].keys)
else
update_users = EndUser.find(:all,:conditions => [ 'end_users.id IN (' + params[:user].keys.collect { |uid| DomainModel.connection.quote(uid) }.join(",") + ")" ])
case params[:table_action]
when 'add_tags'
update_users.each do |user|
user.tag_names_add(params[:added_tags]) if user
end
when 'remove_tags'
update_users.each do |user|
user.tag_remove(params[:removed_tags], :separator => ',') if user
end
when 'clear_tags'
update_users.each do |user|
user.tag_names = '' if user
end
end
end
end
end
def email_targets_table_generate(opts,*find_options)
@generated_active_table_columns = [
ActiveTable::IconHeader.new('check', :width => '16'),
ActiveTable::StaticHeader.new('Profile', :width => '70'),
ActiveTable::StaticHeader.new('Image', :width => '70'),
ActiveTable::StaticHeader.new('Name'),
ActiveTable::StaticHeader.new('Email')
]
@fields = []
if self.segment && self.segment.fields
@fields = self.segment.fields
else self.class.module_options && self.class.module_options.fields
@fields = self.class.module_options.fields
end
@fields.each do |field|
option = UserSegment.fields_options.rassoc(field)
@generated_active_table_columns << ActiveTable::StaticHeader.new(option[1], :label => option[0]) if option
end
active_table_generate('end_users', EndUser, @generated_active_table_columns, {:count_callback => 'count_end_users', :find_callback => 'find_end_users'}, opts, *find_options)
end
public
def lookup_autocomplete
return render :nothing => true if params[:member].blank?
name = params[:member].split(" ").collect { |elm| elm.strip }.find_all { |elm| !elm.blank? }
@users = []
if(!name.blank?)
@users = EndUser.find(:all,:conditions => [' membership_id = ?',params[:member].to_s.strip ])
end
if @users.length == 0 && name.length > 0
if(name.length == 1)
name = "%" + name[0].downcase + "%"
@conditions = [ 'last_name LIKE ? OR first_name LIKE ?',name,name ]
else
if name[0][-1..-1] == "," # Handle rettig, pascal
name = [name[1],name[0][0..-2]]
end
@conditions = [ 'first_name LIKE ? AND last_name LIKE ?',"%" + name[0].downcase + "%","%" + name[1].downcase + "%" ]
end
@users = EndUser.find(:all,:conditions => @conditions, :order => 'last_name, first_name')
end
render :partial => 'lookup_autocomplete'
end
def display_targets_table(display = true)
if params[:table_action] || params[:segment_action]
handle_table_actions
end
@active_table_output = email_targets_table_generate params, :per_page => 25, :include => :tag_cache, :conditions => 'client_user_id IS NULL', :order => self.class.module_options.order_by
if display
@update_tags = true
if @update_segments
segmentations
end
render :partial => 'targets_table'
end
end
# Members editing
def index
cms_page_path [], "People"
return redirect_to(:action => 'segments') if self.segment && ! self.segment.ready?
segmentations
display_targets_table(false)
end
active_table :user_segments_table,
UserSegment,
[ hdr(:icon, 'check', :width => '16'),
hdr(:icon, '', :width => '32'),
:main_page,
:name,
:description,
:status,
:last_count,
:last_ran_at,
:created_at,
:updated_at
]
def user_segments_table(display=true)
active_table_action 'user_segments' do |act,ids|
case act
when 'delete': UserSegment.destroy(ids)
when 'add'
UserSegment.update_all('main_page = 1', :id => ids)
when 'remove'
UserSegment.update_all('main_page = 0', :id => ids)
end
end
@active_table_output = user_segments_table_generate params, :order => 'updated_at DESC'
render :partial => 'user_segments_table' if display
end
def options
cms_page_path ['People'], "Everyone Options"
@options = self.class.module_options(params[:options])
if request.post? && @options.valid?
if params[:commit]
Configuration.set_config_model(@options)
flash[:notice] = "Updated Everyone options".t
end
redirect_to :action => 'index'
return
end
end
def self.module_options(vals=nil)
Configuration.get_config_model(Options,vals)
end
class Options < HashModel
attributes :fields => [], :order_by => 'created_at DESC'
def validate
if self.fields
self.errors.add(:fields, 'is invalid') if self.fields.find { |f| UserSegment.fields_options.rassoc(f).nil? }
end
if self.order_by
self.errors.add(:order_by, 'is invalid') unless UserSegment.order_by_options.rassoc(self.order_by)
end
end
end
def segments
cms_page_path ['People'], 'Segments'
user_segments_table(false)
end
def create_segment
cms_page_path ['People', 'Segments'], 'Create a Segment'
@builder = UserSegment::OperationBuilder.new nil
@segment = UserSegment.new :main_page => true
if request.post? && params[:segment]
return redirect_to :action => 'index' unless params[:commit]
if @segment.update_attributes params[:segment]
@segment.refresh
redirect_to :action => 'segments'
end
end
end
def edit_segment
@segment = UserSegment.find params[:path][0]
cms_page_path ['People', 'Segments'], '%s Segment' / @segment.name
@builder = UserSegment::OperationBuilder.new nil
if request.post? && params[:segment]
return redirect_to :action => 'index', :path => @segment.id unless params[:commit]
if @segment.update_attributes params[:segment]
if @segment.should_refresh?
@segment.refresh
redirect_to :action => 'segments'
else
redirect_to :action => 'index', :path => @segment.id
end
end
end
end
def copy_segment
segment_to_copy = UserSegment.find params[:path][0]
cms_page_path ['People', 'Segments'], 'Copy %s Segment' / segment_to_copy.name
@builder = UserSegment::OperationBuilder.new nil
@segment = UserSegment.new segment_to_copy.attributes.symbolize_keys.slice(:name, :description, :fields, :main_page, :segment_options_text, :order_by)
@segment.name += ' (Copy)' unless request.post?
if request.post? && params[:segment]
return redirect_to :action => 'index', :path => segment_to_copy.id unless params[:commit]
if @segment.update_attributes params[:segment]
@segment.refresh
redirect_to :action => 'segments'
end
end
end
def refresh_segment
@segment = UserSegment.find params[:path][0]
@segment.refresh
redirect_to :action => 'segments'
end
def builder
cms_page_path ['People'], 'Operation Builder'
@builder = UserSegment::OperationBuilder.new nil
if request.post? && params[:builder]
@builder.build(params[:builder])
end
end
def create
cms_page_path ['People'],'Create Contact'
if(params[:user_options] && params[:user_options][:tag_names])
@tag_names = params[:user_options].delete(:tag_names)
end
user_class_id = params[:user_options].delete(:user_class_id) if params[:user_options]
@user = EndUser.new(params[:user_options])
@user.user_class_id = user_class_id || UserClass.default_user_class_id
if request.post?
if params[:commit]
@user.errors.add(:user_clas_id) if @user.user_class.editor? && !myself.has_role?('editor_editors')
@user.source = 'import'
@user.admin_edit=true
@user.valid?
if(@user.save)
@user.tag_names = @tag_names if @tag_names
update_subscriptions(@user,params[:subscription])
flash[:notice] = 'Created User'.t
redirect_to :action => 'index'
return
end
else
redirect_to :action => 'index'
return
end
end
render :action => 'edit'
end
def edit
cms_page_path ['People'],'Edit Contact'
user_id = params[:path][0]
@user = EndUser.find(user_id)
if @user.client_user_id || (@user.user_class.editor? && !myself.has_role?('editor_editors'))
redirect_to :action => 'index'
return
end
if request.post?
if params[:commit]
@user.user_class_id = params[:user_options].delete(:user_class_id)
# Don't let user class be upgrade to editor if the user can edit those
if @user.user_class.editor? && !myself.has_role?('editor_editors')
redirect_to :action => 'index'
return
end
# If it's not an editor - get rid of any editor tokens
if !@user.user_class.editor?
params[:user_options][:tokens] = AccessToken.filter_out_editor_tokens(params[:user_options][:tokens])
end
@user.attributes = params[:user_options]
@user.admin_edit=true
# stupid-complicated for no reason FIXME
%w(address work_address billing_address shipping_address).each do |adr|
address = @user.send(adr)
if address || !params[adr].values.join("").blank?
address ||= @user.send("build_#{adr}")
address.attributes = params[adr]
instance_variable_set("@#{adr}".to_sym,address)
end
end
if @user.save
%w(address work_address billing_address shipping_address).each do |adr|
adr = instance_variable_get("@#{adr}".to_sym)
adr.update_attribute(:end_user_id, @user.id) if adr
end
@user.action('/members/edit',:admin_user_id => myself.id, :level => 0)
update_subscriptions(@user,params[:subscription])
flash[:notice] = 'Saved User: "%s" ' / (@user.email.blank? ? @user.name : @user.email)
redirect_to :action =>'index'
return
end
else
redirect_to :action =>'index'
return
end
end
@address ||= @user.address || @user.build_address
@work_address ||= @user.work_address || @user.build_work_address
@billing_address ||= @user.billing_address || @user.build_billing_address
@shipping_address ||= @user.shipping_address || @user.build_shipping_address
@editing=true
render :action => 'edit'
end
active_table :user_actions_table,
EndUserAction,
[ "Action",
hdr(:options,'level',:options => :level_options,:label => 'Lvl'),
hdr(:string,'identifier'),
hdr(:string,'renderer',:label => 'Category'),
hdr(:string,'action',:label => 'Label'),
hdr(:date_range,'action_at'),
"Admin User"
]
protected
def level_options
EndUserAction.level_select_options
end
public
def display_user_actions_table(display=true)
@user = EndUser.find(params[:path][0]) unless @user
@tbl = user_actions_table_generate params, :order => 'action_at DESC',:conditions => [ 'end_user_id=?',@user.id ],:include => [ :end_user ]
render :partial => 'user_actions_table' if display
end
def view
@user = EndUser.find(params[:path][0])
display_user_actions_table(false)
cms_page_path [ 'People'],['%s',nil,@user.name.to_s.strip.blank? ? @user.email : @user.name ]
handlers = get_handler_info(:members,:view)
tabs_to_display = Configuration.options.member_tabs
if tabs_to_display.length > 0
handlers = handlers.select { |hndl| tabs_to_display.include?(hndl[:identifier].to_s) }
handlers = handlers.select do |hndl|
hndl[:permit] ? myself.has_role?(hndl[:permit]) : true
end
handlers = handlers.select do |hndl|
if hndl[:display]
hndl[:class].send(hndl[:display],@user)
else
true
end
end
end
idx=1
@handler_tabs = [[ "Website Visits", jvh("MemberViewer.loadTab(1,'#{url_for :controller => '/members', :action => 'member_visits', :path => @user.id}');") ]] +
handlers.sort() { |hndler1,hndler2| hndler1[:name] <=> hndler2[:name] }.collect do |handler|
idx+=1
[ handler[:name],
jvh("MemberViewer.loadTab(#{idx},'#{url_for :controller => handler[:controller], :action => handler[:action], :path => @user.id}');") ]
end
end
def login
@user = EndUser.find_by_id(params[:path][0])
if @user.client_user_id.blank? && (!@user.user_class.editor? || myself.has_role?('editor_editors'))
process_login(@user)
redirect_to "/"
else
redirect_to :action => 'view', :path => @user.id
end
end
def member_visits
@user = EndUser.find_by_id(params[:path][0])
@entry_info,@entries = DomainLogEntry.find_user_sessions(@user)
render :partial => 'site_visits'
end
def add_tags_form
@tags = EndUser.tags_count(:order => 'name')
render :partial => 'add_tags_form'
end
def remove_tags_form
@users = EndUser.find(:all,:conditions => [ 'end_users.id IN (' + params[:user_ids].to_a.collect { |uid| DomainModel.connection.quote(uid) }.join(",") + ")" ], :include => :tag_cache)
@existing_tags = []
@users.each do |usr|
if !usr.tag_cache.tags.blank?
@existing_tags += usr.tag_cache.tags.split(",")
end
end
@existing_tags.uniq!
render :partial => 'remove_tags_form'
end
active_table :tags_table, TagNote,
[hdr(:string,'tags.name',:label => 'Name'),'Count','Description' ]
def tags
if TagNote.count == 0
Tag.find(:all).each do |tg|
tg.create_tag_note unless tg.tag_note
end
end
display_tags_table(false)
end
def tag_details
@tag_note = TagNote.find(params[:path][0])
if request.post? && params[:tag_note]
if @tag_note.update_attributes( params[:tag_note])
display_tags_table(false)
render :update do |page|
page << 'RedBox.close();'
page.replace_html 'tags_table', :partial => 'tags_table'
end
return
end
end
render :partial => 'tag_details'
end
def display_tags_table(display=true)
cms_page_path ['People'],'Tag Details'
@tbl = tags_table_generate params, :order =>'name',:include => :tag, :joins => :tag
render :partial => 'tags_table' if display
end
def generate_vip
@vip_number = EndUser.generate_vip
end
private
# update users subscriptions
def update_subscriptions(user,subscriptions)
# Make sure we are updating subscriptions
if subscriptions && subscriptions['0']
user_subscriptions = user.user_subscription_entries.index_by(&:user_subscription_id)
UserSubscription.find(:all).each do |sub|
# Now create an remove subscriptions as necessary
if subscriptions[sub.id.to_s] && !user_subscriptions[sub.id]
sub.admin_subscribe_user(user)
elsif !subscriptions[sub.id.to_s] && user_subscriptions[sub.id]
user_subscriptions[sub.id].destroy
end
end
end
end
end