]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4100 Update "My Profile" screen with "Notifications per project"
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 31 Jan 2013 11:44:15 +0000 (12:44 +0100)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 31 Jan 2013 14:04:07 +0000 (15:04 +0100)
plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/user.rb
sonar-server/src/main/webapp/WEB-INF/app/views/account/_global_notifications.html.erb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/views/account/_per_project_notifications.html.erb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb

index 81d6e8faa437b3f43635b4f92e5ec76c889febb0..1a6859a8378a789031af2f1ea5f087c4e661b0ca 100644 (file)
@@ -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
 
 
 #------------------------------------------------------------------------------
index 875aa77cccc0615fefd419fd2f1571c8d9917274..3d998c1c01a62ac067afdd5c7f68257a4f6057ac 100644 (file)
@@ -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
index 829589ba362cc167bcf28b7737c3c12e289d7a35..8813a058064bdcddafa3203f6761c84df6291001 100644 (file)
@@ -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 (file)
index 0000000..a64938e
--- /dev/null
@@ -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 (file)
index 0000000..d007440
--- /dev/null
@@ -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
index c6285fe64b663033722fa9ec18ddc8d093fa4e6c..4b630101a9e332025ca4aca57956d8a3e07eb9db 100644 (file)
     </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