profile_helper.rb
6.44 KB
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
module ProfileHelper
  COMMON_CATEGORIES = ActiveSupport::OrderedHash.new
  COMMON_CATEGORIES[:content] = [:blogs, :image_galleries, :events, :article_tags]
  COMMON_CATEGORIES[:interests] = [:interests]
  COMMON_CATEGORIES[:general] = nil
  PERSON_CATEGORIES = ActiveSupport::OrderedHash.new
  PERSON_CATEGORIES[:basic_information] = [:nickname, :sex, :birth_date, :location, :privacy_setting, :created_at]
  PERSON_CATEGORIES[:contact] = [:contact_phone, :cell_phone, :comercial_phone, :contact_information, :email, :personal_website, :jabber_id]
  PERSON_CATEGORIES[:location] = [:address, :address_reference, :zip_code, :city, :state, :district, :country, :nationality]
  PERSON_CATEGORIES[:work] = [:organization, :organization_website, :professional_activity]
  PERSON_CATEGORIES[:study] = [:schooling, :formation, :area_of_study]
  PERSON_CATEGORIES[:network] = [:friends, :communities, :enterprises]
  PERSON_CATEGORIES.merge!(COMMON_CATEGORIES)
  ORGANIZATION_CATEGORIES = ActiveSupport::OrderedHash.new
  ORGANIZATION_CATEGORIES[:basic_information] = [:display_name, :created_at, :foundation_year, :type, :language, :members_count, :location, :address_reference, :historic_and_current_context, :admins]
  ORGANIZATION_CATEGORIES[:contact] = [:contact_person, :contact_phone, :contact_email, :organization_website, :jabber_id]
  ORGANIZATION_CATEGORIES[:economic] = [:business_name, :acronym, :economic_activity, :legal_form, :products, :activities_short_description, :management_information]
  ORGANIZATION_CATEGORIES.merge!(COMMON_CATEGORIES)
  CATEGORY_MAP = ActiveSupport::OrderedHash.new
  CATEGORY_MAP[:person] = PERSON_CATEGORIES
  CATEGORY_MAP[:organization] = ORGANIZATION_CATEGORIES
  FORCE = {
    :person => [:privacy_setting],
    :organization => [:privacy_setting, :location],
  }
  MULTIPLE = {
    :person => [:blogs, :image_galleries, :interests],
    :organization => [:blogs, :image_galleries, :interests],
  }
  CUSTOM_LABELS = {
    :zip_code => _('ZIP code'),
    :email => _('e-Mail'),
    :jabber_id => _('Jabber'),
    :birth_date => _('Date of birth'),
    :created_at => _('Profile created at'),
    :members_count => _('Members'),
    :privacy_setting => _('Privacy setting'),
    :article_tags => _('Tags')
  }
  EXCEPTION = {
    :person => [:image, :preferred_domain, :description, :tag_list],
    :organization => [:image, :preferred_domain, :description, :tag_list, :address, :zip_code, :city, :state, :country, :district]
  }
  def general_fields
    categorized_fields = CATEGORY_MAP[kind].values.flatten
    profile.class.fields.map(&:to_sym) - categorized_fields - EXCEPTION[kind]
  end
  def kind
    if profile.kind_of?(Person)
      :person
    else
      :organization
    end
  end
  def title(field, entry = nil)
    return self.send("#{field}_custom_title", entry) if MULTIPLE[kind].include?(field) && entry.present?
    CUSTOM_LABELS[field.to_sym] || _(field.to_s.humanize)
  end
  def display_field(field)
    force = FORCE[kind].include?(field)
    multiple = MULTIPLE[kind].include?(field)
    unless force || profile.may_display_field_to?(field, user)
      return ''
    end
    value = begin profile.send(field) rescue nil end
    return '' if value.blank?
    if value.kind_of?(Hash)
      content = self.send("treat_#{field}", value)
      content_tag('tr', content_tag('td', title(field), :class => 'field-name') + content_tag('td', content))
    else
      entries = multiple ? value : [] << value
      entries.map do |entry|
        content = self.send("treat_#{field}", entry)
        unless content.blank?
          content_tag('tr', content_tag('td', title(field, entry), :class => 'field-name') + content_tag('td', content))
        end
      end.join("\n")
    end
  end
  def treat_email(email)
    link_to_email(email)
  end
  def treat_organization_website(url)
    link_to(url, url)
  end
  def treat_sex(gender)
    { 'male' => _('Male'), 'female' => _('Female') }[gender]
  end
  def treat_date(date)
    show_date(date.to_date)
  end
  alias :treat_birth_date :treat_date
  alias :treat_created_at :treat_date
  def treat_friends(friends)
     link_to friends.count, :controller => 'profile', :action => 'friends'
  end
  def treat_communities(communities)
    link_to communities.count, :controller => "profile", :action => 'communities'
  end
  def treat_enterprises(enterprises)
    if environment.disabled?('disable_asset_enterprises')
      link_to enterprises.count, :controller => "profile", :action => 'enterprises'
    end
  end
  def treat_members_count(count)
    link_to count, :controller => 'profile', :action => 'members'
  end
  def treat_products(products)
    if profile.kind_of?(Enterprise) && profile.environment.enabled?('products_for_enterprises')
      link_to _('Products/Services'), :controller => 'catalog', :action => 'index'
    end
  end
  def treat_admins(admins)
    profile.admins.map { |admin| link_to(admin.short_name, admin.url)}.join(', ')
  end
  def treat_blogs(blog)
    link_to(n_('One post', '%{num} posts', blog.posts.published.count) % { :num => blog.posts.published.count }, blog.url)
  end
  def treat_image_galleries(gallery)
    link_to(n_('One picture', '%{num} pictures', gallery.images.published.count) % { :num => gallery.images.published.count }, gallery.url)
  end
  def treat_events(events)
    link_to events.published.count, :controller => 'events', :action => 'events'
  end
  def treat_article_tags(tags)
    tag_cloud @tags, :id, { :action => 'tags' }, :max_size => 18, :min_size => 10
  end
  def treat_interests(interest)
    link_to interest.name, :controller => 'search', :action => 'category_index', :category_path => interest.explode_path
  end
  def article_custom_title(article)
    article.name
  end
  alias :blogs_custom_title :article_custom_title
  alias :image_galleries_custom_title :article_custom_title
  def interests_custom_title(interest)
    ''
  end
  def method_missing(method, *args, &block)
    if method.to_s =~ /^treat_(.+)$/
      args[0]
    elsif method.to_s =~ /^display_(.+)$/ && CATEGORY_MAP[kind].has_key?($1.to_sym)
      category = $1.to_sym
      fields = category == :general ? general_fields : CATEGORY_MAP[kind][category]
      contents = []
      fields.each do |field|
        contents << display_field(field).html_safe
      end
      contents = contents.delete_if(&:blank?)
      unless contents.empty?
        content_tag('tr', content_tag('th', title(category), { :colspan => 2 })) + contents.join.html_safe
      else
        ''
      end
    else
      super
    end
  end
end