From ef0e403fcbf318567b1ffc4a64da5564742528d8 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 31 Jan 2013 12:44:15 +0100 Subject: [PATCH] SONAR-4100 Update "My Profile" screen with "Notifications per project" --- .../resources/org/sonar/l10n/core.properties | 11 ++- .../app/controllers/account_controller.rb | 73 +++++++++++++++-- .../main/webapp/WEB-INF/app/models/user.rb | 13 ++++ .../account/_global_notifications.html.erb | 33 ++++++++ .../_per_project_notifications.html.erb | 78 +++++++++++++++++++ .../WEB-INF/app/views/account/index.html.erb | 39 ++-------- 6 files changed, 207 insertions(+), 40 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties index 81d6e8faa43..1a6859a8378 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties @@ -1551,12 +1551,19 @@ my_profile.password.old=Old value my_profile.password.new=New value my_profile.password.confirm=Confirm new value my_profile.password.submit=Change password -my_profile.notifications.title=Notifications -my_profile.notifications.submit=Save changes my_profile.password.changed=Password changed my_profile.password.empty=Password can not be empty my_profile.password.mismatch=Password mismatch my_profile.password.wrong_old=Wrong old password +my_profile.notifications.title=Notifications +my_profile.notifications.submit=Save changes +my_profile.per_project_notifications.title=Notifications per project +my_profile.add_project=Add project +my_profile.remove_this_line=Remove this line + + +# A ENLEVER !!!!! +notification.channel.TwitterNotificationChannel=Twitter #------------------------------------------------------------------------------ diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb index 875aa77cccc..3d998c1c01a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb @@ -24,10 +24,39 @@ class AccountController < ApplicationController def index notification_service = java_facade.getCoreComponentByClassname('org.sonar.server.notifications.NotificationCenter') @channels = notification_service.getChannels() - @dispatchers = notification_service.getDispatcherKeysForProperty("globalNotification", "true") - @notifications = {} - for property in Property.find(:all, :conditions => ['prop_key like ? AND user_id = ?', 'notification.%', current_user.id]) - @notifications[property.key.sub('notification.', '')] = true + @global_dispatchers = notification_service.getDispatcherKeysForProperty("globalNotification", "true") + @per_project_dispatchers = notification_service.getDispatcherKeysForProperty("perProjectNotification", "true") + + @global_notifications = {} + @per_project_notifications = {} + Property.find(:all, :conditions => ['prop_key like ? AND user_id = ?', 'notification.%', current_user.id]).each do |property| + r_id = property.resource_id + if r_id + # This is a per-project notif + project_notifs = @per_project_notifications[r_id] + unless project_notifs + project_notifs = {} + @per_project_dispatchers.each do |dispatcher| + project_notifs[dispatcher] = [] + end + @per_project_notifications[r_id] = project_notifs + end + parts = property.key.split('.') + dispatcher_key = parts[1] + channel_key = parts[2] + project_notifs[dispatcher_key] << channel_key + else + # This is a global notif + @global_notifications[property.key.sub('notification.', '')] = true + end + end + + if params[:new_project] + new_project = Project.by_key params[:new_project] + unless @per_project_notifications[new_project.id] + @per_project_notifications[new_project.id] = init_project_notifications + end + @selected_project_id = new_project.id end end @@ -54,9 +83,41 @@ class AccountController < ApplicationController def update_notifications notifications = params[:notifications] - Property.delete_all(['prop_key like ? AND user_id = ?', 'notification.%', current_user.id]) - notifications.each_key { |key| current_user.set_property(:prop_key => 'notification.' + key, :text_value => 'true') } unless notifications.nil? + Property.delete_all(['prop_key like ? AND user_id = ? AND resource_id IS NULL', 'notification.%', current_user.id]) + notifications.each_key {|k| puts "===> " + k} + notifications.each_key { |key| current_user.add_property(:prop_key => 'notification.' + key, :text_value => 'true') } unless notifications.nil? redirect_to :action => 'index' end + def update_per_project_notifications + notifications = params[:notifications] + Property.delete_all(['prop_key like ? AND user_id = ? AND resource_id IS NOT NULL', 'notification.%', current_user.id]) + if notifications + notifications.each do |r_id, per_project_notif| + per_project_notif.each do |dispatch, channels| + channels.each do |channel| + current_user.add_property(:prop_key => 'notification.' + dispatch + '.' + channel, :text_value => 'true', :resource_id => r_id) + end + end + end + end + + new_params = {} + unless params[:new_project].blank? + new_params[:new_project] = params[:new_project] + end + + redirect_to :action => 'index', :params => new_params + end + + private + + def init_project_notifications + project_notifs = {} + @per_project_dispatchers.each do |dispatcher| + project_notifs[dispatcher] = [] + end + project_notifs + end + end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/user.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/user.rb index 829589ba362..8813a058064 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/user.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/user.rb @@ -147,6 +147,19 @@ class User < ActiveRecord::Base properties<<%= message('my_profile.notifications.title') -%> + +
+ <% form_tag({:action => 'update_notifications'}, {:method => 'post'}) do %> + + + + <% for channel in @channels %> + + <% end %> + + <% for dispatcher in @global_dispatchers %> + + + <% + for channel in @channels + notification_id = dispatcher + '.' + channel.getKey() + check_box_id = 'notifications[' + notification_id + ']' + check_box_checked = @global_notifications[notification_id] + %> + + <% end %> + + <% end %> + + + + +
<%= message('notification.channel.' + channel.getKey()) -%>
<%= message('notification.dispatcher.' + dispatcher) -%> + <%= check_box_tag check_box_id, 'true', check_box_checked %> +
<%= submit_tag message('my_profile.notifications.submit') %>
+ <% end %> +
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb new file mode 100644 index 00000000000..d00744065fc --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb @@ -0,0 +1,78 @@ +

<%= message('my_profile.per_project_notifications.title') -%>

+ +
+ <% form_tag({:action => 'update_per_project_notifications'}, {:method => 'post', :id => 'per_project_notif_form'}) do %> + + + + + + <% @per_project_dispatchers.each do |dispatcher| %> + + <% end %> + + + <% + index = 0 + @per_project_notifications.each do |r_id, per_project_notif| + index += 1 + %> + + + + + <% + @per_project_dispatchers.each_with_index do |dispatcher, d_index| + subscribed_channels = per_project_notif[dispatcher] + select_id = index.to_s + "_" + dispatcher + %> + + <% end %> + + + <% end %> + + + + +
+ <%= resource_select_tag 'new_project', { + :qualifiers => ['TRK'], + :width => '250px', + :select2_options => {'placeholder' => "'" + message('my_profile.add_project') + "'"} + } -%> + + <%= message('notification.dispatcher.' + dispatcher) -%>
+ + + + + +
<%= Project.by_key(r_id).name -%> + +
+
+ + +
<%= submit_tag message('my_profile.notifications.submit') %>
+ <% end %> +
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb index c6285fe64b6..4b630101a9e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb @@ -55,37 +55,12 @@ <% end -%> -

<%= message('my_profile.notifications.title') -%>

+ <% unless @global_dispatchers.empty? -%> + <%= render "account/global_notifications" -%> + <% end %> -
- <% form_tag({:action => 'update_notifications'}, {:method => 'post'}) do %> - - - - <% for channel in @channels %> - - <% end %> - - <% for dispatcher in @dispatchers %> - - - - - <% end %> - - - - -
<%= message('notification.channel.' + channel.getKey()) -%>
<%= message('notification.dispatcher.' + dispatcher) -%> - <% - for channel in @channels - notification_id = dispatcher + '.' + channel.getKey() - check_box_id = 'notifications[' + notification_id + ']' - check_box_checked = @notifications[notification_id] - %> - <%= check_box_tag check_box_id, 'true', check_box_checked %> - <% end %> -
<%= submit_tag message('my_profile.notifications.submit') %>
- <% end %> -
+ <% unless @per_project_dispatchers.empty? -%> + <%= render "account/per_project_notifications" -%> + <% end %> + \ No newline at end of file -- 2.39.5