-
Notifications
You must be signed in to change notification settings - Fork 11
/
application_helper.rb
1315 lines (1136 loc) · 51.6 KB
/
application_helper.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Controls needed for Gravatar support throughout the site
require 'avatar/view/action_view_support'
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
include ExtendedFieldsHelpers
include ExtendedContentHelpers
include OaiDcHelpers
include ZoomHelpers
# Controls needed for Gravatar support throughout the site
include Avatar::View::ActionViewSupport
def avatar_for(user, options = {})
# New installs use strings for the small_sq value, but we have to handle legacy settings containing arrays
image_dimension = IMAGE_SIZES[:small_sq].is_a?(String) ? \
IMAGE_SIZES[:small_sq].gsub(/(!|>|<)/, '').split('x').first.to_i : \
IMAGE_SIZES[:small_sq].first
default_options = { :width => image_dimension, :height => image_dimension, :alt => "#{user.user_name}'s Avatar. " }
options = default_options.merge(options)
return nil if options[:return_portrait] && (!ENABLE_USER_PORTRAITS || user.portraits.empty?)
if ENABLE_USER_PORTRAITS && !user.portraits.empty? && !user.portraits.first.thumbnail_file.file_private
if options[:return_portrait]
return user.portraits.first
else
return image_tag(user.portraits.first.thumbnail_file.public_filename, options)
end
elsif ENABLE_USER_PORTRAITS && !ENABLE_GRAVATAR_SUPPORT
return image_tag('no-avatar.png', options)
end
if ENABLE_GRAVATAR_SUPPORT
return avatar_tag(user, { :size => 50, :rating => 'G', :gravatar_default_url => "#{SITE_URL}images/no-avatar.png" }, options)
end
return ''
end
# Adds the necessary javascript to update a div with the id of user_avat
def avatar_updater_js(options = {})
options = options.merge({ :email_id => 'user_email', :avatar_id => 'user_avatar_img', :spinner_id => 'user_avatar_spinner' })
javascript_tag("
$('#{options[:email_id]}').observe('blur', function(event) {
new Ajax.Request('#{url_for(:controller => 'account', :action => 'fetch_gravatar')}', {
method: 'get',
parameters: { email: $('#{options[:email_id]}').value, avatar_id: '#{options[:avatar_id]}' },
onLoading: function(loading) { $('#{options[:spinner_id]}').show(); },
onComplete: function(complete) { $('#{options[:spinner_id]}').hide(); }
});
});
")
end
def page_keywords
return DEFAULT_PAGE_KEYWORDS if current_item.nil? || current_item.tags.blank?
current_item.tags.join(",").gsub(" ", "_").gsub("\"", "")
end
def page_description
return DEFAULT_PAGE_DESCRIPTION if current_item.nil?
description_text = (current_item.respond_to?(:short_summary) && !current_item.short_summary.blank?) ? current_item.short_summary : current_item.description
return DEFAULT_PAGE_DESCRIPTION if description_text.blank?
strip_tags(truncate(description_text, 180)).gsub("\"", "").squish
end
def header_links_to_baskets
html = '<ul id="basket-list" class="nav-list">'
except_certain_baskets = @standard_baskets
except_certain_baskets += [@current_basket] if @current_basket != @site_basket
except_certain_baskets_args = { :conditions => ["id not in (?) AND status = 'approved'", except_certain_baskets] }
baskets_limit = 2
total_baskets_count = Basket.count(except_certain_baskets_args)
except_certain_baskets_args[:limit] = baskets_limit
basket_count = 0
Basket.find(:all, except_certain_baskets_args).each do |basket|
basket_count += 1
html += li_with_correct_class(basket_count) + link_to_index_for(basket) + '</li>'
end
if baskets_limit < total_baskets_count
html += '<li>' + link_to_unless_current('more...',
url_for(:urlified_name => @site_basket.urlified_name,
:controller => 'baskets' ), {:tabindex => '2'}) + '</li>'
end
html += '</ul>'
if basket_count > 0
return html
else
return ''
end
end
def header_link_to_current_basket
html = String.new
html += ': ' + link_to_index_for(@current_basket, { :class => 'basket', :tabindex => '2' }) if @current_basket != @site_basket
end
def search_link_to_searched_basket
html = String.new
html += ' ' + link_to_index_for(@current_basket, { :class => 'basket' }) if @current_basket != @site_basket
end
def link_to_index_for(basket, options = { })
link_to basket.name, basket_index_url(basket.urlified_name), options
end
def header_browse_links
html = '<li>'
pre_text = String.new
site_link_text = String.new
current_basket_html = String.new
if @current_basket != @site_basket
pre_text = 'Browse: '
site_link_text = @site_basket.name
privacy_type = (@current_basket.private_default_with_inheritance? && permitted_to_view_private_items?) ? 'private' : nil
current_basket_html = " or " + link_to_unless_current( @current_basket.name,
{:controller => 'search',
:action => 'all',
:urlified_name => @current_basket.urlified_name,
:controller_name_for_zoom_class => 'topics',
:trailing_slash => true,
:privacy_type => privacy_type}, {:tabindex => '2'} )
else
site_link_text = 'Browse'
end
html += pre_text + link_to_unless_current( site_link_text,
{:controller => 'search',
:action => 'all',
:urlified_name => @site_basket.urlified_name,
:controller_name_for_zoom_class => 'topics',
:trailing_slash => true}, {:tabindex => '2'} ) + current_basket_html + '</li>'
end
def header_add_links(options={})
return unless current_user_can_see_add_links?
options = { :link_text => 'Add Item' }.merge(options)
link_text = options.delete(:link_text)
li_class = options.delete(:class) || ''
html = "<li class='#{li_class}'>"
html += link_to_unless_current(link_text,
{ :controller => 'baskets',
:action => 'choose_type',
:urlified_name => @current_basket.urlified_name }.merge(options),
{ :tabindex => '2' })
html += '</li>'
end
def users_baskets_list(user=current_user, options ={})
# if the user is the current user, use the basket_access_hash instead of fetching them again
@baskets = (user == current_user) ? @basket_access_hash : user.basket_permissions
row1 = 'user_basket_list_row1'
row2 = 'user_basket_list_row2'
css_class = row1
if user == current_user || @site_admin
Basket.find_all_by_status_and_creator_id('requested', user).each do |basket|
@baskets << [basket.urlified_name, nil] if @baskets[basket.urlified_name.to_sym].blank?
end
end
html = String.new
@baskets.each do |basket_name, role|
basket = Basket.find_by_urlified_name(basket_name.to_s)
next unless user == current_user || current_user_can_see_memberlist_for?(basket)
pending = (basket.status == 'requested') ? " (pending)" : ''
link = link_to(basket.name + pending, basket_index_url(:urlified_name => basket_name))
link += " - #{role[:role_name].humanize}" if options[:show_roles] && !role.blank?
basket_options = options[:show_options] ? link_to_actions_available_for(basket, options) : ''
basket_options = '<div class="profile_basket_options">[<ul>' + basket_options + '</ul>]</div>' unless basket_options.blank?
html += content_tag('li', basket_options + link, :class => css_class)
css_class = css_class == row1 ? row2 : row1
end
html
end
def header_add_basket_link
return unless current_user_can_add_or_request_basket?
if basket_policy_request_with_permissions?
basket_text = 'Request basket'
else
basket_text = 'Add basket'
end
link_to_unless_current( basket_text,
:controller => 'baskets',
:action => 'new',
:urlified_name => @site_basket.urlified_name)
end
def render_baskets_as_menu
html = '<ul id="sub-menu" class="menu basket-list-menu">'
except_certain_baskets_args = { :conditions => ["id not in (?) AND status = 'approved'", @standard_baskets] }
basket_count = 0
Basket.find(:all, except_certain_baskets_args).each do |basket|
basket_count += 1
if basket == @current_basket
html += li_with_correct_class(basket_count) + link_to_index_for(basket)
html += '<ul>'
topic_count = 0
order_with_inheritence = basket.settings[:side_menu_ordering_of_topics] || @site_basket.settings[:side_menu_ordering_of_topics]
direction_with_inheritence = basket.settings[:side_menu_direction_of_topics] || @site_basket.settings[:side_menu_direction_of_topics]
order = case order_with_inheritence
when "alphabetical"
case direction_with_inheritence
when "reverse"
"title DESC"
else
"title ASC"
end
else
case direction_with_inheritence
when "reverse"
"updated_at ASC"
else
"updated_at DESC"
end
end
if !basket.settings[:side_menu_number_of_topics].blank?
limit = basket.settings[:side_menu_number_of_topics].to_i
elsif !@site_basket.settings[:side_menu_number_of_topics].blank?
limit = @site_basket.settings[:side_menu_number_of_topics].to_i
else
limit = 10
end
basket_topic_count = 0
for topic in basket.topics.find(:all, :limit => limit, :order => order).reject { |t| t.disputed_or_not_available? }
if topic != basket.index_topic
html += li_with_correct_class(topic_count) + link_to_item(topic) + '</li>'
basket_topic_count += 1
end
end
if basket.topics.count > basket_topic_count && basket_topic_count > 0
html += content_tag("li", link_to("More..",
{:controller => 'search',
:action => 'all',
:urlified_name => basket.urlified_name,
:controller_name_for_zoom_class => 'topics'},
{:tabindex => '2'}))
end
html += '</ul>'
else
html += li_with_correct_class(basket_count) + link_to_index_for(basket)
end
html += '</li>'
end
html += '</ul>'
end
def current_user_can_see_flagging?
if @current_basket.settings[:show_flagging] == "at least moderator"
can_see_flagging = logged_in? && @at_least_a_moderator
else
can_see_flagging = true
end
can_see_flagging
end
def current_user_can_see_add_links?
if @current_basket.settings[:show_add_links] == "at least moderator"
can_see_add_links = logged_in? && @at_least_a_moderator
else
can_see_add_links = true
end
can_see_add_links
end
def current_user_can_see_action_menu?
if @current_basket.settings[:show_action_menu] == "at least moderator"
can_see_action_menu = logged_in? && @at_least_a_moderator
else
can_see_action_menu = true
end
can_see_action_menu
end
def current_user_can_see_discussion?
if @current_basket.settings[:show_discussion] == "at least moderator"
can_see_discussion = logged_in? && @at_least_a_moderator
else
can_see_discussion = true
end
return_value = can_see_discussion
end
# TODO: may want to replace this with better history plugin
def link_to_last_stored_location
if session[:return_to_title].blank?
return link_to("<< Back to Kete Home", '/')
else
return link_to("<< Back to \"#{session[:return_to_title]}\"", session[:return_to])
end
end
def link_to_members_of(basket, options={})
options = { :viewable_text => "Members",
:unavailable_text => "" }.merge(options)
if current_user_can_see_memberlist_for?(basket)
content_tag("li", link_to(options[:viewable_text],
:urlified_name => basket.urlified_name,
:controller => 'members',
:action => 'list'),
:class => options[:class] )
elsif !options[:unavailable_text].blank?
content_tag("li", options[:unavailable_text],
:class => options[:class])
else
''
end
end
def link_to_membership_request_of(basket, options={})
return '' unless logged_in?
options = { :join_text => "Join",
:request_text => "Request membership",
:closed_text => "",
:as_list_element => true,
:plus_divider => "",
:pending_text => "Membership pending",
:rejected_text => "Membership rejected",
:current_role => "You're a |role|.",
:leave_text => "Leave" }.merge(options)
show_roles = options[:show_roles].nil? ? true : options[:show_roles]
location_hash = { :urlified_name => basket.urlified_name,
:controller => 'members',
:action => 'join' }
html = String.new
if @basket_access_hash[basket.urlified_name.to_sym].blank?
case basket.join_policy_with_inheritance
when 'open'
html += link_to(options[:join_text], location_hash)
when 'request'
html += link_to(options[:request_text], location_hash)
else
return '' if options[:closed_text].blank?
html += options[:closed_text]
end
else
role = @basket_access_hash[basket.urlified_name.to_sym][:role_name].humanize
case role
when "Membership requested"
html += options[:pending_text]
when "Membership rejected"
html += options[:rejected_text]
else
html += options[:current_role].gsub('|role|', role) if show_roles
# no one can remove themselves from the site basket
# and there needs to be at least one basket admin remaining if the user removed him/herself
if basket != @site_basket && @current_basket.more_than_one_basket_admin?
html += " " + link_to(options[:leave_text], location_hash.merge({:action => 'remove', :id => current_user}))
end
end
end
html = "<li class='#{options[:class]}'>#{html}</li>" if !html.blank? && options[:as_list_element]
html += options[:plus_divider]
end
def link_to_basket_contact_for(basket, include_name = true)
link_text = 'Contact'
link_text += ' ' + basket.name if include_name
link_to link_text, basket_contact_path(:urlified_name => basket.urlified_name)
end
def link_to_actions_available_for(basket, options={})
options[:class] = 'first'
html = ''
html += link_to_membership_request_of(basket, options)
options[:class] = nil unless html.blank?
html += link_to_members_of(basket, options)
html += "<li>" + link_to_basket_contact_for(basket, false) + "</li>" if @current_basket.allows_contact_with_inheritance?
html
end
def link_to_cancel(from_form = "")
html = "<div id=\"cancel#{from_form}\" style=\"display:inline\">"
if session[:return_to].blank?
html += link_to("Cancel", :action => 'list', :tabindex => '1')
else
html += link_to("Cancel", url_for(session[:return_to]), :tabindex => '1')
end
html += "</div>"
end
def link_to_item(item)
link_to h(item.title), :controller => zoom_class_controller(item.class.name),
:urlified_name => item.basket.urlified_name,
:action => :show, :id => item
end
def url_for_contributions_of(user, zoom_class)
url_for(:controller => 'search',
:urlified_name => @site_basket.urlified_name,
:controller_name_for_zoom_class => zoom_class_controller(zoom_class),
:action => :all, :contributor => user, :trailing_slash => true, :only_path => false)
end
def link_to_contributions_of(user, zoom_class, options = {})
if options[:with_avatar]
display_html = avatar_for(user, { :class => 'user_contribution_link_avatar' })
display_html += h(user.user_name)
display_html += '<div class="clear"></div>'
else
display_html = h(user.user_name)
end
link_to display_html, url_for_contributions_of(user, zoom_class)
end
def stylish_link_to_contributions_of(user, zoom_class, options = {})
options = { :with_avatar => true }.merge(options)
div_classes = (['stylish_user_contribution_link'] + options[:additional_classes].to_a).flatten.compact.join(' ')
display_html = "<div class=\"#{div_classes}\">"
if options[:with_avatar]
avatar = avatar_for(user)
display_html += '<div class="stylish_user_contribution_link_avatar">' + avatar_for(user) + '</div>' unless avatar.blank?
end
user_link = link_to(h(user.user_name), url_for_contributions_of(user, zoom_class))
link_text = (options[:link_text] || user_link).gsub('|user_name_link|', user_link)
display_html += content_tag('div', link_text, :class => 'stylish_user_contribution_link_extra')
display_html += options[:additional_html] if options[:additional_html]
display_html += '<div style="clear:both;"></div>'
display_html += '</div>'
display_html
end
def add_to_stylish_display_with(content)
"<div class=\"stylish_user_contribution_link_extra\">#{content}</div>"
end
def url_for_profile_of(user)
url_for(:controller => 'account',
:urlified_name => @site_basket.urlified_name,
:action => :show, :id => user, :only_path => false)
end
def link_to_profile_for(user, phrase = nil)
phrase ||= h(user.user_name)
link_to phrase, url_for_profile_of(user)
end
def link_to_add_item(options={})
phrase = options[:phrase]
item_class = options[:item_class]
phrase += ' ' + zoom_class_humanize(item_class)
if @current_basket != @site_basket
phrase += ' in ' + @current_basket.name
end
return link_to(phrase, {:controller => zoom_class_controller(item_class), :action => :new}, :tabindex => '1')
end
#
# START RELATED ITEM HELPERS
#
# Public items need to be sorted based on the acts_as_list position in the database so
# we can't use zebra to find public items in this case, so pull it from the database
def public_related_items_for(item, options={})
@items = Hash.new
item_classes = options[:topics_only] ? ['Topic'] : ITEM_CLASSES
item_classes.each do |item_class|
if options[:count_only]
items = find_related_items_for(item, item_class, { :start_record => nil, :end_record => nil, :dont_parse_results => true })
@items[item_class] = items.size
else
if item_class != 'Topic' || options[:topics_only]
items = item.send(item_class.tableize)
items = items.find_all_public_non_pending if items.size > 0
else
items = item.related_topics(:only_non_pending => true)
end
@items[item_class] = items
end
end
@items
end
# We use a method in lib/zoom_search.rb to find all private items the current user has access to
# (should be faster than a bunch of mysql queries - might be an interesting thing to benchmark)
def private_related_items_for(item, options={})
@items = Hash.new
item_classes = options[:topics_only] ? ['Topic'] : ITEM_CLASSES
item_classes.each do |item_class|
items = find_private_related_items_for(item, item_class, { :start_record => nil, :end_record => nil, :dont_parse_results => options[:count_only] })
@items[item_class] = options[:count_only] ? items.size : items
end
@items
end
# Link to the related items of a certain item
def link_to_related_items_of(item, zoom_class, options={}, location={})
options = { :link_text => "View items related to #{item.title}" }.merge(options)
location = { :urlified_name => @site_basket.urlified_name,
:controller_name_for_zoom_class => zoom_class_controller(zoom_class),
:source_controller_singular => zoom_class_controller(item.class.name).singularize,
:source_item => item }.merge(location)
related_item_url = (location[:privacy_type] == 'private') ? basket_all_private_related_to_path(location) :
basket_all_related_to_path(location)
link_to options[:link_text], related_item_url, { :class => 'small' }
end
# Creates the item list for display
def related_items_display_of(items, options={})
return '' if items.blank?
unless options[:display_num].nil?
display_num = options[:display_num]
items = Array.new if display_num == 0
items = items[0..(display_num - 1)] if display_num > 0
end
display_html = String.new
display_html += options[:are_still_images] ? '<ul class="results-list images-list">' : '<ul>'
items.each_with_index do |related_item,index|
li_class = index == 0 ? 'first' : ''
if related_item.is_a?(Hash)
if related_item.is_a?(Hash) && !related_item[:thumbnail].blank?
display_html += "<li class='#{li_class}'>#{related_image_link_for(related_item, options)}</li>"
else
display_html += "<li class='#{li_class}'>#{link_to(related_item[:title], related_item[:url])}</li>"
end
elsif related_item.is_a?(StillImage)
display_html += "<li class='#{li_class}'>#{related_image_link_for(related_item, options)}</li>"
else
display_html += "<li class='#{li_class}'>#{link_to_item(related_item)}</li>"
end
end
if (options[:item] && options[:zoom_class] && options[:display_num] && options[:total_num]) &&
(options[:total_num] > options[:display_num])
display_html += "<li>"
more_num = options[:total_num] - options[:display_num]
display_html += link_to_related_items_of(options[:item], options[:zoom_class], { :link_text => "#{more_num.to_s} more like this >>" }, { :privacy_type => options[:privacy_type] })
display_html += "</li>"
end
display_html += "</ul>"
display_html
end
# Creates an image and wraps in within a link tag
def related_image_link_for(still_image, options={})
options = { :privacy_type => 'public' }.merge(options)
if still_image.is_a?(StillImage)
if !still_image.thumbnail_file.nil?
thumb_src_value = still_image.already_at_blank_version? ? '/images/pending.jpg' :
still_image.thumbnail_file.public_filename
link_text = image_tag(thumb_src_value, { :size => still_image.thumbnail_file.image_size,
:alt => "#{still_image.title}. " })
else
link_text = 'only available as original'
end
link_location = { :urlified_name => still_image.basket.urlified_name,
:controller => 'images', :action => 'show', :id => still_image,
:private => (options[:privacy_type] == 'private') }
else
thumb_src_value = still_image[:thumbnail][:src]
link_text = image_tag(thumb_src_value, { :width => still_image[:thumbnail][:width],
:height => still_image[:thumbnail][:height],
:alt => "#{still_image[:title]}. " })
link_location = still_image[:url]
end
link_to(link_text, link_location)
end
def link_to_related_item_function(options={})
options = { :link_text => "#{options[:function].capitalize} an Existing Related Item" }.merge(options)
link_text = options.delete(:link_text)
disabled = false
disabled = true if options[:function] == 'remove' && @total_item_counts < 1
if options[:function] == 'restore'
restore_count = ContentItemRelation::Deleted.count(:conditions => { :topic_id => options[:relate_to_topic] })
disabled = true if restore_count < 1
link_text += " (#{restore_count})"
end
link = disabled ? link_text : link_to(link_text, { :controller => 'search', :action => 'find_related' }.merge(options),
{ :popup => ['links', 'height=500,width=500,scrollbars=yes,top=100,left=100,resizable=yes'] })
content_tag('li', link)
end
def link_to_add_set_of_related_items(options={})
options = { :link_text => "Import Set of Related Items" }.merge(options)
link_text = options.delete(:link_text)
link = link_to(link_text, { :controller => 'importers',
:action => 'new_related_set_from_archive_file',
:relate_to_topic => options[:relate_to_topic] })
content_tag('li', link)
end
#
# END RELATED ITEM HELPERS
#
# tag related helpers
def link_to_tagged(tag, zoom_class = nil, basket = @site_basket)
zoom_class = zoom_class || tag[:zoom_class]
link_to h(tag[:name]),
{ :controller => 'search',
:action => 'all',
:tag => tag[:id],
:trailing_slash => true,
:controller_name_for_zoom_class => zoom_class_controller(zoom_class),
:urlified_name => basket.urlified_name,
:privacy_type => get_acceptable_privacy_type_for(nil, nil, "private") },
:class => tag[:css_class]
end
alias :link_to_tagged_in_basket :link_to_tagged
def tag_cloud(tags, classes)
logger.info("using this")
max, min = 0, 0
tags.each { |t|
t_count = t[:total_taggings_count].to_i
max = t_count if t_count > max
min = t_count if t_count < min
}
divisor = ((max - min) / classes.size) + 1
tags.each { |t|
t_count = t[:total_taggings_count].to_i
yield t[:id], t[:name], classes[(t_count - min) / divisor]
}
end
def tags_for(item)
html_string = String.new
return html_string if item.tags.blank?
html_string = "<p>Tags: "
item_tags = item.tags
item_tags.each_with_index do |tag,index|
html_string += link_to_tagged(tag, item.class.name)
html_string += ", " unless item_tags.size == (index + 1)
end
html_string += "</p>"
html_string
end
def tags_input_field(form,label_for)
"<div class=\"form-element\"><label for=\"#{label_for}\">Tags (separated by commas):</label>
#{form.text_field :tag_list, :tabindex => '1'}</div>"
end
# if extended_field is passed in, use that to limit choices
# else if @all_choices is true, we provide them all
def limit_search_to_choice_control
options_array = Array.new
if @extended_field
options_array = @extended_field.choices.find_top_level.inject([]) do |memo, choice|
memo + option_for_choice_control(choice, :level => 0)
end
elsif @all_choices && categories_field
options_array = categories_field.choices.find_top_level.reject { |c| c.extended_fields.empty? }.inject([]) do |memo, choice|
memo + option_for_choice_control(choice, :level => 0)
end
else
return
end
html_options_for_select = ([['', '']] + options_array).map do |k, v|
attrs = { :value => v }
attrs.merge!(:selected => "selected") if params[:limit_to_choice] == v
content_tag("option", k, attrs)
end.join
# Don't print out the SELECT tag unless there are choices available.
options_array.flatten.empty? ? "" : select_tag("limit_to_choice", html_options_for_select)
end
def option_for_choice_control(choice, options = {})
level = options[:level] || 0
array = [[(" "*level) + choice.label, choice.value]]
choice.children.reject { |c| c.extended_fields.empty? }.inject(array) { |a, c| a + option_for_choice_control(c, :level => level + 1) }
end
#---- related to extended_fields for either topic_types or content_types
def display_xml_attributes(item)
raq = " » "
html = []
mappings = item.is_a?(Topic) ? item.all_field_mappings : \
ContentType.find_by_class_name(item.class.name).content_type_to_field_mappings
content = item.extended_content_pairs
mappings.each do |mapping|
field = mapping.extended_field
# value = content[qualified_name_for_field(field)]
field_name = field.multiple? ? qualified_name_for_field(field) + "_multiple" : qualified_name_for_field(field)
value = content.select { |pair| pair[0] == field_name }.first.last rescue nil
next if value.to_s.blank?
value = formatted_extended_content_value(field, field_name, value, item)
if field.ftype == 'map' || field.ftype == 'map_address'
next if value.blank?
td = content_tag("td", "#{field.label}:<br />#{value}", :class => "detail-extended-field-label", :colspan => 2)
else
td = content_tag("td", "#{field.label}:", :class => "detail-extended-field-label") +
content_tag("td", value)
end
html << content_tag("tr", td)
end
unless html.empty?
content_tag("table", content_tag("tbody", html.join), :class => "detail-extended-field-table", :summary => "Extended details")
end
end
def formatted_extended_content_value(field, field_name, value, item)
# handle if the field is multiple
values = Array.new
if field.multiple?
values = value
else
values << value
end
# create an array of the result from processing each value
# that way, if we need to, we can join on a bit of html code
# or do "to_sentence" on the array
output_array = Array.new
values.each do |value_input|
value_output = \
if field.ftype == 'map'
extended_field_map_editor(field_name, value_input, field, { :style => 'width:220px;' }, { :style => 'width:220px;' }, false, true, false)
elsif field.ftype == 'map_address'
extended_field_map_editor(field_name, value_input, field, { :style => 'width:220px;' }, { :style => 'width:220px;' }, false, true, true)
else
formatted_value_from_xml(value_input, field, item)
end
value_output = value_output.to_s
# we prepend base_url to the value here
# for the extended_field if it is set
# but only if it hasn't been done previously in other formatting
base_url = field.base_url
unless base_url.blank? || %w(map map_address choice autocomplete).include?(field.ftype)
value_output = link_to(value_output, base_url + value_output)
end
output_array << value_output
end
if output_array.size > 1
unless %w(map map_address).include?(field.ftype)
output_array.to_sentence
else
# TODO: look into how best to present multiple maps
# they may not need any extra formatting
output_array.join('<br\>')
end
else
output_array.first
end
end
def formatted_value_from_xml(value, ef = nil, item = nil)
if ef && %w(autocomplete choice).member?(ef.ftype)
base_url = ef.base_url
# If the extended field type is a choice, then link the value to the search page for the EF.
url_hash = {
:controller_name_for_zoom_class => item.nil? ? 'topics' : zoom_class_controller(item.class.name),
:controller => 'search',
:extended_field => ef.label_for_params
}
if item.respond_to?(:private?) && item.private?
method = 'basket_all_private_of_category_url'
url_hash.merge!(:privacy_type => 'private')
else
method = 'basket_all_of_category_url'
end
# make the hash nested in an array so the map command does the right thing
if value.is_a?(Hash) && value['label']
value = [value]
end
value.map do |v|
# use passed label if present
# otherwise value is label
l = v
if v.is_a?(Hash) && v['label']
l = v['label']
v = v['value']
end
# the extended field's base_url takes precedence over
# normal behavior creating a link to results
# limited to choice for an extended field (a.k.a category_url in method names)
unless base_url.blank?
link_to(l, base_url + v)
else
link_to(l, send(method, url_hash.merge(:limit_to_choice => v)))
end
end.join(" » ")
else
value = value.first if value.is_a?(Array)
label = value
# use passed label if present
# otherwise we use value as label
if value.is_a?(Hash) && value['label']
label = value['label']
value = value['value']
end
case value
when /^(.+)\((.+)\)$/
# something (url)
link_to($1, $2)
when /^\w+:\/\/[^ ]+/
# this is a url protocal of some sort, make link
link_to(label, value)
when /^\w+[^ ]*\@\w+\.\w/
mail_to(label, value, :encode => "hex")
else
sanitize(value)
end
end
end
#---- end related to extended_fields for either topic_types or content_types
# return an array of hashes of related items
# where class is the key
# and id is the value
def items_to_rebuild(item)
# first entry is self
items_to_rebuild = [ "#{item.class.name}-#{item.id}" ]
# grab all zoom_classes for topics
# everything else is just related topics and comments
if item.class.name == 'Topic'
ZOOM_CLASSES.each do |zoom_class|
if zoom_class == 'Topic'
item.related_topics.each do |related_topic|
items_to_rebuild << "Topic-#{related_topic.id}"
end
else
item.send(zoom_class.tableize).each do |related_item|
items_to_rebuild << "#{zoom_class}-#{related_item.id}"
end
end
end
else
item.topics.each do |related_item|
items_to_rebuild << "Topic-#{related_item.id}"
end
item.comments.each do |related_item|
items_to_rebuild << "Comment-#{related_item.id}"
end
end
return items_to_rebuild.join(",")
end
# related to comments
def show_comments_for(item)
html_string = "<p>There are #{@comments.size} comments in this discussion.</p>\n<p>"
logger.debug("what are comments: " + @comments.inspect)
if @comments.size > 0
html_string += "Read and "
end
html_string += link_to("join this discussion",
{:action => :new,
:controller => 'comments',
:commentable_id => item,
:commentable_type => item.class.name,
:commentable_private => (item.respond_to?(:private) && item.private?) ? 1 : 0
},
:method => :post)
html_string += "</p>\n"
if @comments.size > 0
@comments.each do |comment|
comment_string = "<div class=\"comment-wrapper\">"
comment_string += "<div class=\"comment-wrapper-header-wrapper\"><div class=\"comment-wrapper-header\"></div></div>"
comment_string += "<div class=\"comment-content\">"
comment_string += "#{comment.description}\n" unless comment.description.blank?
tags_for_comment = tags_for(comment)
comment_string += "#{tags_for_comment}\n" unless tags_for_comment.blank?
comment_string += pending_review(comment) + "\n"
comment_string += "<div class=\"comment-tools\">\n"
comment_string += flagging_links_for(comment,true,'comments')
if logged_in? and @at_least_a_moderator
comment_string += "<ul>"
comment_string += "<li class='first'>" + link_to("Edit",
:controller => 'comments',
:action => :edit,
:id => comment) + "</li>\n"
comment_string += "<li>" + link_to("History",
:controller => 'comments',
:action => :history,
:id => comment) + "</li>\n"
comment_string += "<li>" + link_to("Delete",
{:action => :destroy,
:controller => 'comments',
:id => comment,
:authenticity_token => form_authenticity_token},
:method => :post,
:confirm => 'Are you sure?') + "</li>\n"
comment_string += "</ul>\n"
end
comment_string += "</div>" # comment-tools
comment_string += "</div>" # comment-content
comment_string += "<div class=\"comment-wrapper-footer-wrapper\"><div class=\"comment-wrapper-footer\"></div></div>"
comment_string += "</div>" # comment-wrapper
html_string += '<div class="comment-outer-wrapper">'
html_string += stylish_link_to_contributions_of(comment.creators.first, 'Comment',
:link_text => "<h3>|user_name_link|</h3><div class=\"stylish_user_contribution_link_extra\"><h3> said <a name=\"comment-#{comment.id}\">#{h(comment.title)}</a></h3></div>",
:additional_html => comment_string)
html_string += '</div>' # comment-outer-wrapper
end
html_string += "<p>" + link_to("join this discussion",
{:action => :new,
:controller => 'comments',
:commentable_id => item,
:commentable_type => item.class.name,
:commentable_private => (item.respond_to?(:private) && item.private?) ? 1 : 0,
:authenticity_token => form_authenticity_token
},
:method => :post) + "</p>"
end
return html_string
end
def flagging_links_for(item, first = false, controller = nil)
html_string = String.new
if FLAGGING_TAGS.size > 0 and !item.already_at_blank_version?
if first
html_string = " <ul><li class=\"first flag\">Flag as:</li>\n"
else
html_string = " <ul><li class=\"flag\">Flag as:</li>\n"
end
html_string += "<li><ul>\n"
flag_count = 1
FLAGGING_TAGS.each do |flag|
if flag_count == 1
html_string += "<li class=\"first\">"
else
html_string += "<li>"
end
if !controller.nil?
html_string += link_to(flag,
{ :controller => controller,
:action => 'flag_form',
:flag => flag,
:id => item,
:version => item.version },