software_communities_plugin_controller.rb
6.5 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
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
require 'csv'
class SoftwareCommunitiesPluginController < ApplicationController
def hide_registration_incomplete_percentage
response = false
if request.xhr? && params[:hide]
session[:hide_incomplete_percentage] = true
response = session[:hide_incomplete_percentage]
end
render :json=>response.to_json
end
def create_institution
@show_sisp_field = environment.admins.include?(current_user.person)
@state_list = get_state_list()
if request.xhr?
render :layout=>false
else
redirect_to "/"
end
end
def split_http_referer http_referer
split_list = []
split_list = http_referer.split("/")
@url_token = split_list.last
return @url_token
end
def create_institution_admin
@show_sisp_field = environment.admins.include?(current_user.person)
@state_list = get_state_list()
@url_token = split_http_referer request.original_url()
end
def new_institution
redirect_to "/" if params[:community].blank? || params[:institutions].blank?
response_message = {}
institution_template = Community["institution"]
add_template_in_params institution_template
institution = private_create_institution
add_environment_admins_to_institution institution
response_message = save_institution institution
if request.xhr? #User create institution
render :json => response_message.to_json
else #Admin create institution
session[:notice] = response_message[:message] # consume the notice
redirect_depending_on_institution_creation response_message
end
end
def institution_already_exists
redirect_to "/" if !request.xhr? || params[:name].blank?
already_exists = !Community.where(:name=>params[:name]).empty?
render :json=>already_exists.to_json
end
def get_institutions
redirect_to "/" if !request.xhr? || params[:query].blank?
list = Institution.search_institution(params[:query]).map{ |institution|
{:value=>institution.name, :id=>institution.id}
}
render :json => list.to_json
end
def get_categories
redirect_to "/" if !request.xhr? || params[:query].blank?
list = []
Category.where("name ILIKE ?", "%#{params[:query]}%").collect { |c|
list << {:label=>c.name, :id=>c.id} if c.name != "Software"
}
render :json => list.to_json
end
def get_brazil_states
redirect_to "/" unless request.xhr?
state_list = get_state_list()
render :json=>state_list.collect {|state| state.name }.to_json
end
def get_field_data
condition = !request.xhr? || params[:query].nil? || params[:field].nil?
return render :json=>{} if condition
model = get_model_by_params_field
data = model.where("name ILIKE ?", "%#{params[:query]}%").select("id, name")
.collect { |db|
{:id=>db.id, :label=>db.name}
}
other = [model.select("id, name").last].collect { |db|
{:id=>db.id, :label=>db.name}
}
# Always has other in the list
data |= other
render :json=> data
end
def get_license_data
return render :json=>{} if !request.xhr? || params[:query].nil?
data = if params[:query].empty?
LicenseInfo.all
else
LicenseInfo.where("version ILIKE ?", "%#{params[:query]}%").select("id, version")
end
render :json=> data.collect { |license|
{:id=>license.id, :label=>license.version}
}
end
protected
def get_state_list
NationalRegion.find(
:all,
:conditions=>["national_region_type_id = ?", 2],
:order=>"name"
)
end
def set_institution_type
institution_params = params[:institutions].except(
:governmental_power,
:governmental_sphere,
:juridical_nature
)
if params[:institutions][:type] == "PublicInstitution"
PublicInstitution::new institution_params
else
PrivateInstitution::new institution_params
end
end
def set_public_institution_fields institution
inst_fields = params[:institutions]
begin
gov_power = GovernmentalPower.find inst_fields[:governmental_power]
gov_sphere = GovernmentalSphere.find inst_fields[:governmental_sphere]
jur_nature = JuridicalNature.find inst_fields[:juridical_nature]
institution.juridical_nature = jur_nature
institution.governmental_power = gov_power
institution.governmental_sphere = gov_sphere
rescue
institution.errors.add(
:governmental_fields,
_("Could not find Governmental Power or Governmental Sphere")
)
end
end
def private_create_institution
community = Community.new(params[:community])
community.environment = environment
institution = set_institution_type
institution.name = community[:name]
institution.community = community
if institution.type == "PublicInstitution"
set_public_institution_fields institution
end
institution.date_modification = DateTime.now
institution.save
institution
end
def add_template_in_params institution_template
com_fields = params[:community]
if !institution_template.blank? && institution_template.is_template
com_fields[:template_id]= institution_template.id unless com_fields.blank?
end
end
def add_environment_admins_to_institution institution
edit_page = params[:edit_institution_page] == false
if environment.admins.include?(current_user.person) && edit_page
environment.admins.each do |adm|
institution.community.add_admin(adm)
end
end
end
def save_institution institution
inst_errors = institution.errors.full_messages
com_errors = institution.community.errors.full_messages
if inst_errors.empty? && com_errors.empty? && institution.valid? && institution.save
{ :success => true,
:message => _("Institution successful created!"),
:institution_data => {:name=>institution.name, :id=>institution.id}
}
else
{ :success => false,
:message => _("Institution could not be created!"),
:errors => inst_errors << com_errors
}
end
end
def redirect_depending_on_institution_creation response_message
if response_message[:success]
redirect_to :controller => "/admin_panel", :action => "index"
else
flash[:errors] = response_message[:errors]
redirect_to :controller => "software_communities_plugin", :action => "create_institution_admin"
end
end
def get_model_by_params_field
case params[:field]
when "software_language"
return ProgrammingLanguage
else
return DatabaseDescription
end
end
end