You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

avatars_helper.rb 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module AvatarsHelper
  19. include GravatarHelper::PublicMethods
  20. def assignee_avatar(user, options={})
  21. return '' unless user
  22. options[:title] = l(:field_assigned_to) + ": " + user.name
  23. avatar(user, options).to_s.html_safe
  24. end
  25. def author_avatar(user, options={})
  26. return '' unless user
  27. options[:title] = l(:field_author) + ": " + user.name
  28. avatar(user, options).to_s.html_safe
  29. end
  30. # Returns the avatar image tag for the given +user+ if avatars are enabled
  31. # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
  32. def avatar(user, options = {})
  33. if Setting.gravatar_enabled?
  34. options[:default] = Setting.gravatar_default
  35. options[:class] = GravatarHelper::DEFAULT_OPTIONS[:class] + " " + options[:class] if options[:class]
  36. email = nil
  37. if user.respond_to?(:mail)
  38. email = user.mail
  39. options[:title] = user.name unless options[:title]
  40. elsif user.to_s =~ %r{<(.+?)>}
  41. email = $1
  42. end
  43. if email.present?
  44. gravatar(email.to_s.downcase, options) rescue nil
  45. elsif user.is_a?(AnonymousUser)
  46. anonymous_avatar(options)
  47. elsif user.is_a?(Group)
  48. group_avatar(options)
  49. else
  50. nil
  51. end
  52. else
  53. ''
  54. end
  55. end
  56. # Returns a link to edit user's avatar if avatars are enabled
  57. def avatar_edit_link(user, options={})
  58. if Setting.gravatar_enabled?
  59. url = Redmine::Configuration['avatar_server_url']
  60. link_to avatar(user, {:title => l(:button_edit)}.merge(options)), url, :target => '_blank'
  61. end
  62. end
  63. private
  64. def anonymous_avatar(options={})
  65. image_tag 'anonymous.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
  66. end
  67. def group_avatar(options={})
  68. image_tag 'group.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
  69. end
  70. end