Chris@1494: # encoding: utf-8 Chris@1494: # Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: module WatchersHelper Chris@1494: Chris@1494: def watcher_tag(object, user, options={}) Chris@1494: ActiveSupport::Deprecation.warn "#watcher_tag is deprecated and will be removed in Redmine 3.0. Use #watcher_link instead." Chris@1494: watcher_link(object, user) Chris@1494: end Chris@1494: Chris@1494: def watcher_link(objects, user) Chris@1494: return '' unless user && user.logged? Chris@1494: objects = Array.wrap(objects) Chris@1494: Chris@1494: watched = Watcher.any_watched?(objects, user) Chris@1494: css = [watcher_css(objects), watched ? 'icon icon-fav' : 'icon icon-fav-off'].join(' ') Chris@1494: text = watched ? l(:button_unwatch) : l(:button_watch) Chris@1494: url = watch_path( Chris@1494: :object_type => objects.first.class.to_s.underscore, Chris@1494: :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) Chris@1494: ) Chris@1494: method = watched ? 'delete' : 'post' Chris@1494: Chris@1494: link_to text, url, :remote => true, :method => method, :class => css Chris@1494: end Chris@1494: Chris@1494: # Returns the css class used to identify watch links for a given +object+ Chris@1494: def watcher_css(objects) Chris@1494: objects = Array.wrap(objects) Chris@1494: id = (objects.size == 1 ? objects.first.id : 'bulk') Chris@1494: "#{objects.first.class.to_s.underscore}-#{id}-watcher" Chris@1494: end Chris@1494: Chris@1494: # Returns a comma separated list of users watching the given object Chris@1494: def watchers_list(object) Chris@1494: remove_allowed = User.current.allowed_to?("delete_#{object.class.name.underscore}_watchers".to_sym, object.project) Chris@1494: content = ''.html_safe Chris@1494: lis = object.watcher_users.collect do |user| Chris@1494: s = ''.html_safe Chris@1494: s << avatar(user, :size => "16").to_s Chris@1494: s << link_to_user(user, :class => 'user') Chris@1494: if remove_allowed Chris@1494: url = {:controller => 'watchers', Chris@1494: :action => 'destroy', Chris@1494: :object_type => object.class.to_s.underscore, Chris@1494: :object_id => object.id, Chris@1494: :user_id => user} Chris@1494: s << ' ' Chris@1494: s << link_to(image_tag('delete.png'), url, Chris@1494: :remote => true, :method => 'delete', :class => "delete") Chris@1494: end Chris@1494: content << content_tag('li', s, :class => "user-#{user.id}") Chris@1494: end Chris@1494: content.present? ? content_tag('ul', content, :class => 'watchers') : content Chris@1494: end Chris@1494: Chris@1494: def watchers_checkboxes(object, users, checked=nil) Chris@1494: users.map do |user| Chris@1494: c = checked.nil? ? object.watched_by?(user) : checked Chris@1494: tag = check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil Chris@1494: content_tag 'label', "#{tag} #{h(user)}".html_safe, Chris@1494: :id => "issue_watcher_user_ids_#{user.id}", Chris@1494: :class => "floating" Chris@1494: end.join.html_safe Chris@1494: end Chris@1494: end