Commit d50d9438612fa07c6a5326cf5c72bd31f0162e14

Authored by Francisco Júnior
1 parent 3193dfa1

bug fixes

app/helpers/application_helper.rb
... ... @@ -1301,11 +1301,23 @@ module ApplicationHelper
1301 1301 end
1302 1302  
1303 1303 def template_options(kind, field_name)
1304   - templates = environment.send(kind).templates
  1304 + templates = environment.send(kind).templates.order('name')
1305 1305 return '' if templates.count == 0
1306   - return hidden_field_tag("#{field_name}[template_id]", templates.first.id) if templates.count == 1
1307   - options = options_for_select(templates.collect{ |template| [template.name, template.id]})
1308   - content_tag('div', content_tag('label', _('Profile organization'), :class => 'formlabel') + (select_tag 'profile_data[template_id]', options, :onchange => 'show_fields_for_template(this);'))
  1306 + if templates.count == 1
  1307 + if templates.first.custom_fields == {}
  1308 + return hidden_field_tag("#{field_name}[template_id]", templates.first.id)
  1309 + else
  1310 + custom_fields = ""
  1311 + templates.first.custom_fields.each { |field, value|
  1312 + custom_fields += content_tag('div', content_tag('label', value[:title].capitalize, :class => 'formlabel') +
  1313 + content_tag('div', text_field_tag( "profile_data[custom_fields][#{field}][title]", ''), :class => 'formfield type-text'), :class => "formfieldline" ) if value[:signup] == 'on'
  1314 + }
  1315 + content_tag('div', custom_fields)
  1316 + end
  1317 + else
  1318 + options = options_for_select(templates.collect{ |template| [template.name, template.id]})
  1319 + content_tag('div', content_tag('label', _('Profile organization'), :class => 'formlabel') + (select_tag 'profile_data[template_id]', options, :onchange => 'show_fields_for_template(this);'))
  1320 + end
1309 1321 end
1310 1322  
1311 1323 def expirable_content_reference(content, action, text, url, options = {})
... ...
app/models/profile.rb
... ... @@ -26,7 +26,11 @@ class Profile < ActiveRecord::Base
26 26 settings_items :custom_fields, :default => {}
27 27  
28 28 def custom_field_value(field)
29   - self.custom_fields[field][:value]
  29 + if !self.custom_fields.blank?
  30 + self.custom_fields[field][:value]
  31 + else
  32 + ''
  33 + end
30 34 end
31 35  
32 36 def custom_field_title(field)
... ...
app/views/account/_signup_form.html.erb
... ... @@ -103,41 +103,9 @@
103 103  
104 104 <div id="signup-form-profile">
105 105  
106   - <script>
107   - function show_fields_for_template(element) {
108   - jQuery('div#signup-form-profile div.formfieldline').remove();
109   - var selected_template = element.options[element.selectedIndex].value;
110   - jQuery.ajax({
111   - type: "GET",
112   - url: "<%= url_for :controller=>'account', :action=>'custom_fields_for_template' %>",
113   - dataType: 'json',
114   - data: { template_id : selected_template },
115   - success: function(data) {
116   - if (data.ok) {
117   - data.custom_fields.each(function(field) {
118   - html = '<div class="formfieldline">' +
119   - '<label class="formlabel" for="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}">{#CUSTOM_FIELD_NAME#}</label>' +
120   - '<input type="hidden" name="profile_data[custom_fields][{#CUSTOM_FIELD_ID#}][title]" id="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}_title" value="{#CUSTOM_FIELD_NAME#}" />' +
121   - '<div class="formfield type-text">' +
122   - '<input type="text" name="profile_data[custom_fields][{#CUSTOM_FIELD_ID#}][value]" id="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}_value" />' +
123   - '</div>' +
124   - '</div>';
125   -
126   - html = html.replace( /{#CUSTOM_FIELD_ID#}/g, field.name );
127   - html = html.replace( /{#CUSTOM_FIELD_NAME#}/g, field.title );
128   - jQuery('div#signup-form-profile').append(html);
129   - });
130   - };
131   - }
132   - });
133   - }
134   - </script>
135   -
136   - <%= template_options(:people, 'profile_data') %>
137   -
138   - <%#= labelled_fields_for :profile_data, @person do |f| %>
139   - <%#= render :partial => 'profile_editor/person_form', :locals => {:f => f} %>
140   - <%# end %>
  106 + <%= labelled_fields_for :profile_data, @person do |f| %>
  107 + <%= render :partial => 'profile_editor/person_form', :locals => {:f => f} %>
  108 + <% end %>
141 109  
142 110 <%= @plugins.dispatch(:signup_extra_contents).collect { |content| instance_eval(&content) }.join("") %>
143 111  
... ... @@ -155,6 +123,11 @@
155 123 <% end %>
156 124 </div>
157 125  
  126 +<div id="signup-form-custom-fields">
  127 + <%= javascript_include_tag "manage-custom-fields" %>
  128 + <%= template_options(:people, 'profile_data') %>
  129 +</div>
  130 +
158 131 <%= recaptcha_tags :ajax => true, :display => {:theme => 'clean'} if @block_bot %>
159 132  
160 133 <p style="text-align: center">
... ...
app/views/profile_editor/_person.html.erb
... ... @@ -20,6 +20,8 @@
20 20  
21 21 <%= render :partial => 'person_form', :locals => {:f => f} %>
22 22  
  23 + <%= render :partial => 'person_form_custom_fields', :locals => {:f => f} %>
  24 +
23 25 <h2><%= _('Notification options') %></h2>
24 26 <div>
25 27 <%= select_tag 'profile_data[notification_time]', options_for_select([[_('Disabled'), 0], [_('Hourly'), 1], [_('Half Day'), 12], [_('Daily'), 24]], @profile.notification_time) %>
... ...
app/views/profile_editor/_person_form.html.erb
... ... @@ -64,74 +64,3 @@
64 64 <%= optional_field(@person, 'professional_activity', f.text_field(:professional_activity, :rel => _('Professional activity'))) %>
65 65 <%= optional_field(@person, 'organization', f.text_field(:organization, :rel => _('Organization'))) %>
66 66 <%= optional_field(@person, 'organization_website', f.text_field(:organization_website, :rel => _('Organization website'))) %>
67   -
68   -<br />
69   -
70   -<div id="custom-fields-container">
71   -
72   - <h2><%= _('Custom fields') %></h2>
73   -
74   - <% if @person.is_template? %>
75   -
76   - <%= javascript_include_tag "manage-custom-fields.js" %>
77   -
78   - <table border="0" style="display: none;">
79   - <tr>
80   - <th align="left"><%= _('Field name') %></th>
81   - <th><%= _('Display on signup?') %></th>
82   - <th>&nbsp;</th>
83   - </tr>
84   -
85   - <% @person.custom_fields.each { |field,value| %>
86   - <tr>
87   - <td>
88   - <%= text_field_tag( "profile_data[custom_fields][#{field}][title]", value[:title], :style => "display:block") %>
89   - </td>
90   - <td align="center">
91   - <%= check_box_tag "profile_data[custom_fields][#{field}][signup]", value['signup'], value['signup'], :onclick => "signup_action('profile_data[custom_fields][#{field}][active]','profile_data[custom_fields][#{field}][required]', 'profile_data[custom_fields][#{field}][signup]')" %>
92   - </td>
93   - <td align="center">
94   - <%= link_to content_tag(:span, _('Delete')), '#', onclick: "return remove_custom_field(this);", title: "Delete", class: "button icon-delete" %>
95   - </td>
96   - </tr>
97   - <% } %>
98   -
99   - </table>
100   -
101   - <% if @person.custom_fields.length > 0 %>
102   - <script>
103   - jQuery('#custom-fields-container table').css('display', 'table');
104   - </script>
105   - <% end %>
106   -
107   - <span>
108   - <%= link_to_function(_('Add field'), "add_new_field('person');", :class => 'button icon-add with-text') %>
109   - </span>
110   -
111   - <% else %>
112   -
113   - <% @person.custom_fields_template.each { |field,value| %>
114   -
115   - <div class="field-with-privacy-selector">
116   - <div class="formfieldline">
117   -
118   - <span style="display: block;">
119   - <%= label_tag @person.custom_fields_template_title(field) %>
120   - </span>
121   -
122   - <div class="formfield type-text" style="display: inline-block;">
123   - <%= hidden_field_tag "profile_data[custom_fields][#{field}[title]", @person.custom_fields_template_title(field) %>
124   - <%= text_field_tag( "profile_data[custom_fields][#{field}][value]", @person.custom_field_value(field), :size => 30 ) %>
125   - </div>
126   -
127   - </div>
128   -
129   - <%= profile_field_privacy_selector @person, field %>
130   -
131   - </div>
132   -
133   - <% } %>
134   -
135   - <% end %>
136   -
137   -</div>
... ...
app/views/profile_editor/_person_form_custom_fields.html.erb 0 → 100644
... ... @@ -0,0 +1,76 @@
  1 +<% @person ||= @profile %>
  2 +
  3 +<% if @person.custom_fields_template != {} || @person.is_template? %>
  4 +
  5 +<br />
  6 +
  7 +<div id="custom-fields-container">
  8 +
  9 + <h2><%= _('Custom fields') %></h2>
  10 +
  11 + <% if @person.is_template? %>
  12 +
  13 + <%= javascript_include_tag "manage-custom-fields.js" %>
  14 +
  15 + <table border="0" style="display: none;">
  16 + <tr>
  17 + <th align="left"><%= _('Field name') %></th>
  18 + <th><%= _('Display on signup?') %></th>
  19 + <th>&nbsp;</th>
  20 + </tr>
  21 +
  22 + <% @person.custom_fields.each { |field,value| %>
  23 + <tr>
  24 + <td>
  25 + <%= text_field_tag( "profile_data[custom_fields][#{field}][title]", value[:title], :style => "display:block", :size => 30, :maxlength => 30) %>
  26 + </td>
  27 + <td align="center">
  28 + <%= check_box_tag "profile_data[custom_fields][#{field}][signup]", value['signup'], value['signup'], :onclick => "signup_action('profile_data[custom_fields][#{field}][active]','profile_data[custom_fields][#{field}][required]', 'profile_data[custom_fields][#{field}][signup]')" %>
  29 + </td>
  30 + <td align="center">
  31 + <%= link_to content_tag(:span, _('Delete')), '#', onclick: "return remove_custom_field(this);", title: "Delete", class: "button icon-delete" %>
  32 + </td>
  33 + </tr>
  34 + <% } %>
  35 +
  36 + </table>
  37 +
  38 + <% if @person.custom_fields.length > 0 %>
  39 + <script>
  40 + jQuery('#custom-fields-container table').css('display', 'table');
  41 + </script>
  42 + <% end %>
  43 +
  44 + <span>
  45 + <%= link_to_function(_('Add field'), "add_new_field('person');", :class => 'button icon-add with-text') %>
  46 + </span>
  47 +
  48 + <% else %>
  49 +
  50 + <% @person.custom_fields_template.each { |field,value| %>
  51 +
  52 + <div class="field-with-privacy-selector">
  53 + <div class="formfieldline">
  54 +
  55 + <span style="display: block;">
  56 + <%= label_tag @person.custom_fields_template_title(field) %>
  57 + </span>
  58 +
  59 + <div class="formfield type-text" style="display: inline-block;">
  60 + <%= hidden_field_tag "profile_data[custom_fields][#{field}[title]", @person.custom_fields_template_title(field) %>
  61 + <%= text_field_tag( "profile_data[custom_fields][#{field}][value]", @person.custom_field_value(field), :size => 30) %>
  62 + </div>
  63 +
  64 + </div>
  65 +
  66 + <%= profile_field_privacy_selector @person, field %>
  67 +
  68 + </div>
  69 +
  70 + <% } %>
  71 +
  72 + <% end %>
  73 +
  74 +</div>
  75 +
  76 +<% end %>
... ...
public/javascripts/manage-custom-fields.js
... ... @@ -58,7 +58,7 @@ function add_new_field() {
58 58 new_custom_field = '' +
59 59 '<tr>' +
60 60 '<td>' +
61   - '<input id="profile_data_custom_fields_custom_field_' + next_custom_field_id + '_title" name="profile_data[custom_fields][custom_field_' + next_custom_field_id + '][title]" style="display:block" type="text" value="">' +
  61 + '<input id="profile_data_custom_fields_custom_field_' + next_custom_field_id + '_title" name="profile_data[custom_fields][custom_field_' + next_custom_field_id + '][title]" style="display:block" type="text" value="" maxlength="30" size="30">' +
62 62 '</td>' +
63 63 '<td align="center">' +
64 64 '<input id="profile_data_custom_fields_custom_field_' + next_custom_field_id + '_signup" name="profile_data[custom_fields][custom_field_' + next_custom_field_id + '][signup]" onclick="signup_action("profile_data[custom_fields][custom_field_' + next_custom_field_id + '][active]","profile_data[custom_fields][custom_field_' + next_custom_field_id + '][required]", "profile_data[custom_fields][custom_field_' + next_custom_field_id + '][signup]")" type="checkbox">' +
... ... @@ -70,3 +70,32 @@ function add_new_field() {
70 70  
71 71 jQuery('#custom-fields-container tbody').append(new_custom_field);
72 72 }
  73 +
  74 +function show_fields_for_template(element) {
  75 + jQuery('div#signup-form-custom-fields div.formfieldline').remove();
  76 + var selected_template = element.options[element.selectedIndex].value;
  77 + jQuery.ajax({
  78 + type: "GET",
  79 + url: "/account/custom_fields_for_template",
  80 + dataType: 'json',
  81 + data: { template_id : selected_template },
  82 + success: function(data) {
  83 + if (data.ok) {
  84 + data.custom_fields.each(function(field) {
  85 + html = '<div class="formfieldline">' +
  86 + '<label class="formlabel" for="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}">{#CUSTOM_FIELD_NAME#}</label>' +
  87 + '<input type="hidden" name="profile_data[custom_fields][{#CUSTOM_FIELD_ID#}][title]" id="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}_title" value="{#CUSTOM_FIELD_NAME#}" />' +
  88 + '<div class="formfield type-text">' +
  89 + '<input type="text" name="profile_data[custom_fields][{#CUSTOM_FIELD_ID#}][value]" id="profile_data_custom_fields_{#CUSTOM_FIELD_ID#}_value" />' +
  90 + '</div>' +
  91 + '</div>';
  92 +
  93 + html = html.replace( /{#CUSTOM_FIELD_ID#}/g, field.name );
  94 + html = html.replace( /{#CUSTOM_FIELD_NAME#}/g, field.title );
  95 + jQuery('div#signup-form-custom-fields').append(html);
  96 + });
  97 + };
  98 + }
  99 + });
  100 +}
  101 +
... ...