organization.rb
4.16 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
# Represents any organization of the system
class Organization < Profile
  SEARCH_FILTERS += %w[
    more_popular
    more_active
  ]
  settings_items :closed, :type => :boolean, :default => false
  def closed?
    closed
  end
  before_save do |organization|
    organization.closed = true if !organization.public_profile?
  end
  settings_items :moderated_articles, :type => :boolean, :default => false
  def moderated_articles?
    moderated_articles
  end
  has_one :validation_info
  has_many :validations, :class_name => 'CreateEnterprise', :foreign_key => :target_id
  has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source'
  named_scope :more_popular, :order => 'members_count DESC'
  def validation_methodology
    self.validation_info ? self.validation_info.validation_methodology : nil
  end
  def validation_restrictions
    self.validation_info ? self.validation_info.restrictions : nil
  end
  def pending_validations
    validations.pending
  end
  def find_pending_validation(code)
    validations.pending.find(:first, :conditions => {:code => code})
  end
  def processed_validations
    validations.finished
  end
  def find_processed_validation(code)
    validations.finished.find(:first, :conditions => {:code => code})
  end
  def is_validation_entity?
    !self.validation_info.nil?
  end
  FIELDS = %w[
    display_name
    description
    contact_person
    contact_email
    contact_phone
    legal_form
    economic_activity
    management_information
    address
    zip_code
    city
    state
    country
    tag_list
    template_id
    district
    address_reference
  ]
  def self.fields
    FIELDS
  end
  def required_fields
    []
  end
  def active_fields
    []
  end
  def signup_fields
    []
  end
  N_('Display name'); N_('Description'); N_('Contact person'); N_('Contact email'); N_('Acronym'); N_('Foundation year'); N_('Legal form'); N_('Economic activity'); N_('Management information'); N_('Tag list'); N_('District'); N_('Address reference')
  settings_items :display_name, :description, :contact_person, :contact_email, :acronym, :foundation_year, :legal_form, :economic_activity, :management_information, :district, :address_reference
  validates_format_of :foundation_year, :with => Noosfero::Constants::INTEGER_FORMAT
  validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |org| !org.contact_email.blank? })
  validates_as_cnpj :cnpj
  xss_terminate :only => [ :acronym, :contact_person, :contact_email, :legal_form, :economic_activity, :management_information ], :on => 'validation'
  # Yes, organizations have members.
  #
  # Returns <tt>true</tt>.
  def has_members?
    true
  end
  def default_set_of_blocks
    links = [
      {:name => _("Community's profile"), :address => '/profile/{profile}', :icon => 'ok'},
      {:name => _('Invite Friends'), :address => '/profile/{profile}/invite/friends', :icon => 'send'},
      {:name => _('Agenda'), :address => '/profile/{profile}/events', :icon => 'event'},
      {:name => _('Image gallery'), :address => '/{profile}/gallery', :icon => 'photos'},
      {:name => _('Blog'), :address => '/{profile}/blog', :icon => 'edit'},
    ]
    [
      [MainBlock.new],
      [ProfileImageBlock.new, LinkListBlock.new(:links => links)],
      [MembersBlock.new, RecentDocumentsBlock.new]
    ]
  end
  def default_set_of_articles
    [
      Blog.new(:name => _('Blog')),
      Gallery.new(:name => _('Gallery')),
    ]
  end
  def notification_emails
    [contact_email.blank? ? nil : contact_email].compact + admins.map(&:email)
  end
  def already_request_membership?(person)
    self.tasks.pending.find_by_requestor_id(person.id, :conditions => { :type => 'AddMember' })
  end
  def jid(options = {})
    super({:domain => "conference.#{environment.default_hostname}"}.merge(options))
  end
  def receives_scrap_notification?
    false
  end
  def members_to_json
    members.map { |member| {:id => member.id, :name => member.name} }.to_json
  end
  def members_by_role_to_json(role)
    members_by_role(role).map { |member| {:id => member.id, :name => member.name} }.to_json
  end
  def disable
    self.visible = false
    save!
  end
end