mpog_software_plugin_controller.rb
9.17 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
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
require 'csv'
class MpogSoftwarePluginController < ApplicationController
include InstitutionHelper
def check_reactivate_account
if request.xhr? && params[:email]
result = ""
user = User.where(:email => params[:email])
if user.length == 1
result = "<span id='forgot_link'><a href='/account/forgot_password'> Reactive account</a></span>" unless user[0].person.visible
end
render :json => result.to_json
end
end
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)
@estate_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)
@estate_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 download
respond_to do |format|
format.html
format_xml_download format
format_csv_download format
end
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
return render :json=>{} if !request.xhr? || params[:query].nil? || params[:field].nil?
model = case params[:field]
when "database"
DatabaseDescription
when "software_language"
ProgrammingLanguage
else
DatabaseDescription
end
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
protected
def get_state_list
redirect_to "/" unless request.xhr?
NationalRegion.find(:all, :conditions=>["national_region_type_id = ?", 2], :order=>"name")
end
def private_create_institution
community = Community.new(params[:community])
community.environment = environment
institution = if params[:institutions][:type] == "PublicInstitution"
PublicInstitution::new params[:institutions].except(:governmental_power, :governmental_sphere, :juridical_nature)
else
PrivateInstitution::new params[:institutions].except(:governmental_power, :governmental_sphere, :juridical_nature)
end
institution.name = community[:name]
institution.community = community
if institution.type == "PublicInstitution"
begin
govPower = GovernmentalPower.find params[:institutions][:governmental_power]
govSphere = GovernmentalSphere.find params[:institutions][:governmental_sphere]
jur_nature = JuridicalNature.find params[:institutions][:juridical_nature]
institution.juridical_nature = jur_nature
institution.governmental_power = govPower
institution.governmental_sphere = govSphere
rescue
institution.errors.add(:governmental_fields, _("Could not find Governmental Power or Governmental Sphere"))
end
end
InstitutionHelper.register_institution_modification institution
institution
end
def software_list_to_correct_format software_list=[]
if !software_list.empty?
software_list.each do |software|
software[:name] = Community.find(software.community_id).name
software[:languages_list] = []
software.software_languages.each do |sl|
software[:languages_list] << {}
index = software[:languages_list].count - 1
software[:languages_list][index][:name] = ProgrammingLanguage.find(sl.programming_language_id).name
software[:languages_list][index][:version] = sl.version
software[:languages_list][index][:operating_system] = sl.operating_system
end
software[:database_list] = []
software.software_databases.each do |dd|
software[:database_list] << {}
index = software[:database_list].count - 1
software[:database_list][index][:name] = DatabaseDescription.find(dd.database_description_id).name
software[:database_list][index][:version] = dd.version
software[:database_list][index][:operating_system] = dd.operating_system
end
end
end
software_list
end
def add_template_in_params institution_template
if (!institution_template.blank? && institution_template.is_template)
params[:community][:template_id] = institution_template.id unless params[:community].blank?
end
end
def add_environment_admins_to_institution institution
if environment.admins.include?(current_user.person) && params[:edit_institution_page] == false
environment.admins.each do |adm|
institution.community.add_admin(adm)
end
end
end
def save_institution institution
if institution.errors.full_messages.empty? and institution.community.errors.full_messages.empty? and institution.valid? and 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 => institution.errors.full_messages << institution.community.errors.full_messages}
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 => "mpog_software_plugin", :action => "create_institution_admin"
end
end
def format_xml_download format
format.xml do
softwares = software_list_to_correct_format(SoftwareInfo.all)
send_data(
softwares.to_xml(
:skip_types => true,
:only => %w[name acronym demonstration_url e_arq e_mag e_ping features icp_brasil objectives operating_platform languages_list database_list]
),
:type => 'text/xml',
:disposition => "attachment; filename=softwares.xml")
end
end
def format_csv_download format
format.csv do
softwares = software_list_to_correct_format(SoftwareInfo.all)
csv_content = ""
softwares.each do |s|
csv_content << "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"
csv_content << CSV.generate_line([s['name'], s['acronym'], s['demonstration_url'], s['e_arq'], s['e_mag'], s['e_ping'], s['features'], s['icp_brasil'], s['objectives'], s['operating_platform']], {:col_sep => ';'})
csv_content << "\nlanguage_name;language_version;language_operating_system\n"
s[:languages_list].each { |sl|
csv_content << CSV.generate_line([sl[:name], sl[:version], sl[:operating_system]], {:col_sep => ';'})
}
csv_content << "\ndatabase_name;database_version;database_operating_system\n"
s[:database_list].each { |dl|
csv_content << CSV.generate_line([dl[:name], dl[:version], dl[:operating_system]], {:col_sep => ';'})
}
csv_content << "\n\n"
end
if csv_content.blank?
csv_content = "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"
end
render :text => csv_content, :content_type => 'text/csv', :layout => false
end
end
end