summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>2013-01-31 12:44:15 +0100
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>2013-01-31 15:04:07 +0100
commitef0e403fcbf318567b1ffc4a64da5564742528d8 (patch)
tree12db12ff794f4bcc391c40db9f029d0700b5eecd
parenteb332bfe8941407fabdc35b33c51309f984ee5bb (diff)
downloadsonarqube-ef0e403fcbf318567b1ffc4a64da5564742528d8.tar.gz
sonarqube-ef0e403fcbf318567b1ffc4a64da5564742528d8.zip
SONAR-4100 Update "My Profile" screen with "Notifications per project"
-rw-r--r--plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties11
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb73
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/user.rb13
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb33
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb78
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb39
6 files changed, 207 insertions, 40 deletions
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<<prop
end
end
+
+ #
+ # This method is different from "set_property(options)" which can also add a new property:
+ # it "really" adds a property and does not try to update a existing one with the same key.
+ # This is used in the account controller to be able to add notification properties both on
+ # a resource (resource_id != nil) or globally (resource_id = nil) - which was not possible
+ # with "set_property(options)".
+ #
+ def add_property(options)
+ prop=Property.new(options)
+ prop.user_id=id
+ properties<<prop
+ end
def delete_property(key)
prop=property(key)
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb
new file mode 100644
index 00000000000..a64938efdf6
--- /dev/null
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb
@@ -0,0 +1,33 @@
+<h2><%= message('my_profile.notifications.title') -%></h2>
+
+<div class="admin marginbottom10">
+ <% form_tag({:action => 'update_notifications'}, {:method => 'post'}) do %>
+ <table class="form">
+ <tr>
+ <td></td>
+ <% for channel in @channels %>
+ <td><b><%= message('notification.channel.' + channel.getKey()) -%></b></td>
+ <% end %>
+ </tr>
+ <% for dispatcher in @global_dispatchers %>
+ <tr>
+ <td><%= message('notification.dispatcher.' + dispatcher) -%></td>
+ <%
+ for channel in @channels
+ notification_id = dispatcher + '.' + channel.getKey()
+ check_box_id = 'notifications[' + notification_id + ']'
+ check_box_checked = @global_notifications[notification_id]
+ %>
+ <td>
+ <%= check_box_tag check_box_id, 'true', check_box_checked %>
+ </td>
+ <% end %>
+ </tr>
+ <% end %>
+ <tr>
+ <td></td>
+ <td><%= submit_tag message('my_profile.notifications.submit') %></td>
+ </tr>
+ </table>
+ <% end %>
+</div> \ 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 @@
+<h2><%= message('my_profile.per_project_notifications.title') -%></h2>
+
+<div class="admin marginbottom10">
+ <% form_tag({:action => 'update_per_project_notifications'}, {:method => 'post', :id => 'per_project_notif_form'}) do %>
+
+ <table class="form">
+ <tr>
+ <td>
+ <%= resource_select_tag 'new_project', {
+ :qualifiers => ['TRK'],
+ :width => '250px',
+ :select2_options => {'placeholder' => "'" + message('my_profile.add_project') + "'"}
+ } -%>
+ <script>
+ $j('#new_project').change(function (event) {
+ var id = event.target.value;
+ if (id != null) {
+ //window.location = '<%= ApplicationController.root_context -%>/account/index?new_project=' + id;
+ $j('#per_project_notif_form').submit();
+ }
+ });
+ </script>
+ </td>
+
+ <% @per_project_dispatchers.each do |dispatcher| %>
+ <td style="max-width: 200px;" class="center"><b><%= message('notification.dispatcher.' + dispatcher) -%></b></td>
+ <% end %>
+ </tr>
+
+ <%
+ index = 0
+ @per_project_notifications.each do |r_id, per_project_notif|
+ index += 1
+ %>
+
+ <tr id="row_<%= index -%>">
+ <td class="middle">
+ <table style="width: 100%">
+ <tr>
+ <td class="nowrap" style="vertical-align: bottom; width: 100%;"><b><%= Project.by_key(r_id).name -%></b></td>
+ <td class="nowrap" style="padding-left: 10px">
+ <a href="#" onclick="$j('#row_<%= index -%>').detach(); return false;" id="remove-<%= index -%>"><img src="<%= ApplicationController.root_context -%>/images/cross-gray.png" title="<%= message('my_profile.remove_this_line') -%>"/></a>
+ </td>
+ </tr>
+ </table>
+ </td>
+
+ <%
+ @per_project_dispatchers.each_with_index do |dispatcher, d_index|
+ subscribed_channels = per_project_notif[dispatcher]
+ select_id = index.to_s + "_" + dispatcher
+ %>
+ <td>
+ <select id="<%= select_id -%>" name="notifications[<%= r_id.to_s -%>][<%= dispatcher-%>][]" multiple style="width: 100%">
+ <% @channels.each do |channel| -%>
+ <option value="<%= channel.getKey() -%>" <%= 'selected' if per_project_notif[dispatcher].include?(channel.getKey()) -%>><%= message('notification.channel.' + channel.getKey()) -%></option>
+ <% end %>
+ </select>
+ <script>
+ $j('#<%= select_id -%>').select2();
+ <% if @selected_project_id == r_id && d_index == 0 -%>
+ // focus the input field and scroll the page to it
+ $j('#<%= select_id -%>').select2("focus");
+ $j(document).scrollTop( $j('#row_<%= index -%>').offset().top );
+ <% end %>
+ </script>
+ </td>
+ <% end %>
+ </tr>
+
+ <% end %>
+
+ <tr>
+ <td colspan="<%= @per_project_dispatchers.size + 1 -%>"><%= submit_tag message('my_profile.notifications.submit') %></td>
+ </tr>
+ </table>
+ <% end %>
+</div> \ 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 @@
</div>
<% end -%>
- <h2><%= message('my_profile.notifications.title') -%></h2>
+ <% unless @global_dispatchers.empty? -%>
+ <%= render "account/global_notifications" -%>
+ <% end %>
- <div class="admin marginbottom10">
- <% form_tag({:action => 'update_notifications'}, {:method => 'post'}) do %>
- <table class="form">
- <tr>
- <td></td>
- <% for channel in @channels %>
- <td><b><%= message('notification.channel.' + channel.getKey()) -%></b></td>
- <% end %>
- </tr>
- <% for dispatcher in @dispatchers %>
- <tr>
- <td><%= message('notification.dispatcher.' + dispatcher) -%></td>
- <td>
- <%
- 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 %>
- </td>
- </tr>
- <% end %>
- <tr>
- <td></td>
- <td><%= submit_tag message('my_profile.notifications.submit') %></td>
- </tr>
- </table>
- <% end %>
- </div>
+ <% unless @per_project_dispatchers.empty? -%>
+ <%= render "account/per_project_notifications" -%>
+ <% end %>
+
</div> \ No newline at end of file