_similar_enterprises.html.erb 2.31 KB
<div id="similar-enterprises" style="display: none; border-bottom: 2px solid #babdb6;">
  <p><b><%= _('Existing enterprises:') %></b></p>
  <p><i style="text-align: center;"><%= _('Were found %{count} enterprises with similar names on the same city, you can decide to associate one of them or create the new enterprise confirming the informations you typed in.') % {:count => content_tag('span', '', :id => 'similar-enterprises-count')} %></i></p>
  <ul id="create-enterprise-similar-enterprises"></ul>
  <% query = bsc.enterprises.map(&:id).join(',') %>
  <script type="text/javascript">
    update_enterprises = function(enterprises){
      var association_url = <%= url_for({:controller => 'bsc_plugin_myprofile', :action => 'save_associations', :profile => bsc.identifier}).to_json %>;
      jQuery('#create-enterprise-similar-enterprises').empty();
      jQuery.each(enterprises, function(index, enterprise){
        var id = enterprise[0]
        var name = enterprise[1]
        var query = <%= query.to_json %>;
        if(query)
          query += ',';
        query += id.toString();
        query = '?q='+query;
        var url = association_url + query;
        jQuery('#create-enterprise-similar-enterprises').append(
          '<li>' + name + ' - <a href="' + url +'">' + <%= _('Associate').to_json %> + '</a></li>'
        );
      });
      if (jQuery(enterprises).length > 0){
        jQuery('#similar-enterprises-count').text(jQuery(enterprises).length.toString());
        jQuery('#similar-enterprises').fadeIn();
      }
      else
        jQuery('#similar-enterprises').fadeOut();
    }
    jQuery('#create_enterprise_name,#create_enterprise_city').change(function() {
      var parameters = {};
      if(jQuery('#create_enterprise_name').length)
        parameters.name = jQuery('#create_enterprise_name').val();
      if(jQuery('#create_enterprise_city').length)
        parameters.city = jQuery('#create_enterprise_city').val();

      jQuery.ajax({
        url: <%= url_for({:controller => 'bsc_plugin_myprofile', :action => 'similar_enterprises', :profile => bsc.identifier}).to_json %>,
        dataType: 'json',
        data: parameters,
        success: function(data){ update_enterprises(data); },
        error: function(ajax, stat, errorThrown) {
          alert(stat+': '+errorThrown);
        }
      });
    });
  </script>
</div>