diff --git a/controllers/gov_user_plugin_controller.rb b/controllers/gov_user_plugin_controller.rb index f9cacf0..d8d5995 100644 --- a/controllers/gov_user_plugin_controller.rb +++ b/controllers/gov_user_plugin_controller.rb @@ -15,6 +15,9 @@ class GovUserPluginController < ApplicationController def create_institution @show_sisp_field = environment.admins.include?(current_user.person) @state_list = get_state_list() + @governmental_sphere = [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]} + @governmental_power = [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]} + @juridical_nature = [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]} params[:community] ||= {} params[:institutions] ||= {} @@ -40,6 +43,9 @@ class GovUserPluginController < ApplicationController def create_institution_admin @show_sisp_field = environment.admins.include?(current_user.person) @state_list = get_state_list() + @governmental_sphere = [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]} + @governmental_power = [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]} + @juridical_nature = [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]} @url_token = split_http_referer request.original_url() @@ -205,9 +211,10 @@ class GovUserPluginController < ApplicationController end def save_institution institution + inst_errors = institution.errors.messages + com_errors = institution.community.errors.messages - inst_errors = institution.errors.full_messages - com_errors = institution.community.errors.full_messages + set_errors institution set_error_css institution @@ -219,7 +226,7 @@ class GovUserPluginController < ApplicationController else { :success => false, :message => _("Institution could not be created!"), - :errors => inst_errors << com_errors + :errors => inst_errors.merge(com_errors) } end end @@ -229,23 +236,24 @@ class GovUserPluginController < ApplicationController redirect_to :controller => "/admin_panel", :action => "index" else flash[:errors] = response_message[:errors] - redirect_to :controller => "gov_user_plugin", :action => "create_institution_admin", :params => params + + redirect_to :controller => "gov_user_plugin", :action => "create_institution_admin" end end - def set_error_css institution - institution.valid? - institution.community.valid? - params[:error_community_name] = institution.community.errors.include?(:name) ? "highlight-error" : "" - params[:error_community_country] = institution.community.errors.include?(:country) ? "highlight-error" : "" - params[:error_community_city] = institution.errors.include?(:city) ? "highlight-error" : "" - params[:error_community_state] = institution.errors.include?(:state) ? "highlight-error" : "" - params[:error_institution_corporate_name] = institution.errors.include?(:corporate_name) ? "highlight-error" : "" - params[:error_institution_acronym] = institution.errors.include?(:acronym) ? "highlight-error" : "" - params[:error_institution_cnpj] = institution.errors.include?(:cnpj) ? "highlight-error" : "" - params[:error_institution_governmental_sphere] = institution.errors.include?(:governmental_sphere) ? "highlight-error" : "" - params[:error_institution_governmental_power] = institution.errors.include?(:governmental_power) ? "highlight-error" : "" - params[:error_institution_juridical_nature] = institution.errors.include?(:juridical_nature) ? "highlight-error" : "" - params[:error_institution_sisp] = institution.errors.include?(:sisp) ? "highlight-error" : "" + def set_errors institution + institution.valid? if institution + institution.community.valid? if institution.community + + flash[:error_community_name] = institution.community.errors.include?(:name) ? "highlight-error" : "" + flash[:error_community_country] = institution.errors.include?(:country) ? "highlight-error" : "" + flash[:error_community_state] = institution.errors.include?(:state) ? "highlight-error" : "" + flash[:error_community_city] = institution.errors.include?(:city) ? "highlight-error" : "" + flash[:error_institution_corporate_name] = institution.errors.include?(:corporate_name) ? "highlight-error" : "" + flash[:error_institution_cnpj] = institution.errors.include?(:cnpj) ? "highlight-error" : "" + flash[:error_institution_governmental_sphere] = institution.errors.include?(:governmental_sphere) ? "highlight-error" : "" + flash[:error_institution_governmental_power] = institution.errors.include?(:governmental_power) ? "highlight-error" : "" + flash[:error_institution_juridical_nature] = institution.errors.include?(:juridical_nature) ? "highlight-error" : "" end + end diff --git a/lib/gov_user_plugin.rb b/lib/gov_user_plugin.rb index be08282..fb32bd2 100644 --- a/lib/gov_user_plugin.rb +++ b/lib/gov_user_plugin.rb @@ -124,6 +124,23 @@ class GovUserPlugin < Noosfero::Plugin percentege end + def stylesheet? + true + end + + def admin_panel_links + [ + { + :title => _('Create Institution'), + :url => { + :controller => 'gov_user_plugin', + :action => 'create_institution_admin' + } + } + ] + end + + def js_files %w( vendor/modulejs-1.5.0.min.js diff --git a/public/views/create-institution.js b/public/views/create-institution.js index e2b3188..e315380 100644 --- a/public/views/create-institution.js +++ b/public/views/create-institution.js @@ -9,7 +9,9 @@ modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'] institution_already_exists: NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/institution_already_exists"), get_institutions: - NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/get_institutions") + NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/get_institutions"), + auto_complete_city: + NoosferoRoot.urlWithSubDirectory("/account/search_cities") }; @@ -96,19 +98,49 @@ modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'] $(".remove-institution").click(remove_institution); } else { - var errors = ""; - if($('#institution_dialog') == 0){ - $('#create_institution_errors').switchClass("show-field", "hide-field"); - $('#errorExplanation').html("

"+response.message+"

"+errors); - }else{ - $("#create_institution_errors").switchClass("hide-field", "show-field").html("

"+response.message+"

"+errors); + show_errors_in_each_field(response.errors); + } + } + + function create_error_list(response){ + var errors = ""; + return errors; + } + + + function show_errors_in_each_field(errors) { + var error_keys = Object.keys(errors); + + // (field)|(field)|... + var verify_error = new RegExp("(" + error_keys.join(")|(") + ")" ); + + var fields_with_errors = $("#institution_dialog .formfield input").filter(function(index, field) { + return verify_error.test(field.getAttribute("name")); + }); + + fields_with_errors.addClass("highlight-error"); + } + + + function adjust_error_key(error_key) { + var text = error_key.replace(/_/, " "); + text = text.charAt(0).toUpperCase() + text.slice(1); + + return text; } @@ -306,6 +338,41 @@ modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'] } } + function autoCompleteCity() { + var country_selected = $('#community_country').val() + + if(country_selected == "BR") + { + $('#community_city').autocomplete({ + source : function(request, response){ + $.ajax({ + type: "GET", + url: AJAX_URL.auto_complete_city, + data: {city_name: request.term, state_name: $("#community_state").val()}, + success: function(result){ + response(result); + + // There are two autocompletes in this page, the last one is modal + // autocomplete just put it above the modal + $(".ui-autocomplete").last().css("z-index", 1000); + }, + error: function(ajax, stat, errorThrown) { + console.log('Link not found : ' + errorThrown); + } + }); + }, + + minLength: 3 + }); + } + else + { + if ($('#community_city').data('autocomplete')) { + $('#community_city').autocomplete("destroy"); + $('#community_city').removeData('autocomplete'); + } + } + } function set_events() { $("#create_institution_link").click(open_create_institution_modal); @@ -330,6 +397,11 @@ modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'] add_mask_to_form_items(); institution_autocomplete(); + + autoCompleteCity(); + $('#community_country').change(function(){ + autoCompleteCity(); + }) } diff --git a/views/gov_user_plugin/_institution.html.erb b/views/gov_user_plugin/_institution.html.erb index 671c4cc..bac8bd0 100644 --- a/views/gov_user_plugin/_institution.html.erb +++ b/views/gov_user_plugin/_institution.html.erb @@ -10,8 +10,11 @@

<%= _("Can`t create new Institution: #{flash[:errors].length} errors") %>

@@ -20,6 +23,10 @@
+
+ <%= required_fields_message %> +
+
<%= labelled_form_for :community, :url => {:action=>"new_institution"}, :html => { :multipart => true, :id=>"institution_form" } do |f| %>
<%= required_fields_message %> @@ -39,7 +46,8 @@
- <%= required f.text_field(:name, :value => params[:community][:name], :size => 55, :class => "#{params[:error_community_name]}" ) %> + + <%= required f.text_field(:name, :class => flash[:error_community_name]) %> <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %>
> @@ -53,49 +61,56 @@
- <%= hidden_field_tag "acronym_translate", _("Acronym") %> - <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %> - <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %> - <%= inst.text_field(:acronym, :value => params[:institutions][:acronym], :class => "#{params[:error_institution_acronym]}") %> + <%= inst.label "corporate_name", _("Corporate Name"), :class=>"formlabel" %> + <%= required inst.text_field(:corporate_name, :class => flash[:error_institution_corporate_name]) %>
- <%= required select_country(_('Country'), 'community', 'country', {:class => "type-select #{params[:error_community_country]}", :id => "community_country"}) %> + <%= required select_country(_('Country'), 'community', 'country', {:class => "type-select #{flash[:error_community_country]}", :id => "community_country"}) %>
- <%= f.select(:state, @state_options, :selected => params[:community][:state], :class => "#{params[:error_community_state]}") %> + <%= f.select(:state, @state_list.collect {|state| [state.name, state.name]}, {}, {:class => flash[:error_community_state]}) %>
- <%= required f.text_field(:city, :value => params[:community][:city], :id => "city_field", :class => "#{params[:error_community_city]}") %> + <%= required f.text_field(:city, :class => flash[:error_community_city]) %>
<%= inst.label("cnpj" ,_("CNPJ"), :class=>"formlabel") %> - <%= required inst.text_field(:cnpj, :value => params[:institutions][:cnpj], :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field #{params[:error_institution_cnpj]}") %> + <%= required inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field #{flash[:error_institution_cnpj]}") %> +
+
+ + +
+ <%= hidden_field_tag "acronym_translate", _("Acronym") %> + <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %> + <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %> + <%= inst.text_field(:acronym) %>
<%= inst.label("governmental_sphere_id" ,_("Governmental Sphere:"), :class=>"formlabel") %> - <%= inst.select(:governmental_sphere, @governmental_sphere_options, :selected => params[:institutions][:governmental_sphere], :class => "#{params[:error_institution_governmental_sphere]}" )%> + <%= inst.select(:governmental_sphere, @governmental_sphere, {:selected=>0})%>
<%= inst.label("governmental_power_id" ,_("Governmental Power:"), :class=>"formlabel") %> - <%= inst.select(:governmental_power, @governmental_power_options, :selected => params[:institutions][:governmental_power], :class => "#{params[:error_institution_governmental_power]}")%> + <%= inst.select(:governmental_power, @governmental_power, {:selected=>0})%>
<%= inst.label("juridical_nature_id" ,_("Juridical Nature:"), :class=>"formlabel") %> - <%= inst.select(:juridical_nature, @juridical_nature_options, :selected => params[:institutions][:juridical_nature], :class => "#{params[:error_institution_juridical_nature]}")%> + <%= inst.select(:juridical_nature, @juridical_nature, {:selected=>0})%>
-- libgit2 0.21.2