Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Journal < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. belongs_to :journalized, :polymorphic => true
  20. # added as a quick fix to allow eager loading of the polymorphic association
  21. # since always associated to an issue, for now
  22. belongs_to :issue, :foreign_key => :journalized_id
  23. belongs_to :user
  24. has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
  25. attr_accessor :indice
  26. acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
  27. :description => :notes,
  28. :author => :user,
  29. :group => :issue,
  30. :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
  31. :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
  32. acts_as_activity_provider :type => 'issues',
  33. :author_key => :user_id,
  34. :scope => preload({:issue => :project}, :user).
  35. joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
  36. where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
  37. " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
  38. before_create :split_private_notes
  39. after_create_commit :send_notification
  40. scope :visible, lambda {|*args|
  41. user = args.shift || User.current
  42. options = args.shift || {}
  43. joins(:issue => :project).
  44. where(Issue.visible_condition(user, options)).
  45. where(Journal.visible_notes_condition(user, :skip_pre_condition => true))
  46. }
  47. safe_attributes 'notes',
  48. :if => lambda {|journal, user| journal.new_record? || journal.editable_by?(user)}
  49. safe_attributes 'private_notes',
  50. :if => lambda {|journal, user| user.allowed_to?(:set_notes_private, journal.project)}
  51. # Returns a SQL condition to filter out journals with notes that are not visible to user
  52. def self.visible_notes_condition(user=User.current, options={})
  53. private_notes_permission = Project.allowed_to_condition(user, :view_private_notes, options)
  54. sanitize_sql_for_conditions(["(#{table_name}.private_notes = ? OR #{table_name}.user_id = ? OR (#{private_notes_permission}))", false, user.id])
  55. end
  56. def initialize(*args)
  57. super
  58. if journalized
  59. if journalized.new_record?
  60. self.notify = false
  61. else
  62. start
  63. end
  64. end
  65. end
  66. def save(*args)
  67. journalize_changes
  68. # Do not save an empty journal
  69. (details.empty? && notes.blank?) ? false : super
  70. end
  71. # Returns journal details that are visible to user
  72. def visible_details(user=User.current)
  73. details.select do |detail|
  74. if detail.property == 'cf'
  75. detail.custom_field && detail.custom_field.visible_by?(project, user)
  76. elsif detail.property == 'relation'
  77. Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
  78. else
  79. true
  80. end
  81. end
  82. end
  83. # Returns the JournalDetail for the given attribute, or nil if the attribute
  84. # was not updated
  85. def detail_for_attribute(attribute)
  86. details.detect {|detail| detail.prop_key == attribute}
  87. end
  88. # Returns the new status if the journal contains a status change, otherwise nil
  89. def new_status
  90. s = new_value_for('status_id')
  91. s ? IssueStatus.find_by_id(s.to_i) : nil
  92. end
  93. def new_value_for(prop)
  94. detail_for_attribute(prop).try(:value)
  95. end
  96. def editable_by?(usr)
  97. usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
  98. end
  99. def project
  100. journalized.respond_to?(:project) ? journalized.project : nil
  101. end
  102. def attachments
  103. journalized.respond_to?(:attachments) ? journalized.attachments : []
  104. end
  105. # Returns a string of css classes
  106. def css_classes
  107. s = 'journal'
  108. s << ' has-notes' unless notes.blank?
  109. s << ' has-details' unless details.blank?
  110. s << ' private-notes' if private_notes?
  111. s
  112. end
  113. def notify?
  114. @notify != false
  115. end
  116. def notify=(arg)
  117. @notify = arg
  118. end
  119. def notified_users
  120. notified = journalized.notified_users
  121. if private_notes?
  122. notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
  123. end
  124. notified
  125. end
  126. def recipients
  127. notified_users.map(&:mail)
  128. end
  129. def notified_watchers
  130. notified = journalized.notified_watchers
  131. if private_notes?
  132. notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
  133. end
  134. notified
  135. end
  136. def watcher_recipients
  137. notified_watchers.map(&:mail)
  138. end
  139. # Sets @custom_field instance variable on journals details using a single query
  140. def self.preload_journals_details_custom_fields(journals)
  141. field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
  142. if field_ids.any?
  143. fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
  144. journals.each do |journal|
  145. journal.details.each do |detail|
  146. if detail.property == 'cf'
  147. detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
  148. end
  149. end
  150. end
  151. end
  152. journals
  153. end
  154. # Stores the values of the attributes and custom fields of the journalized object
  155. def start
  156. if journalized
  157. @attributes_before_change = journalized.journalized_attribute_names.inject({}) do |h, attribute|
  158. h[attribute] = journalized.send(attribute)
  159. h
  160. end
  161. @custom_values_before_change = journalized.custom_field_values.inject({}) do |h, c|
  162. h[c.custom_field_id] = c.value
  163. h
  164. end
  165. end
  166. self
  167. end
  168. # Adds a journal detail for an attachment that was added or removed
  169. def journalize_attachment(attachment, added_or_removed)
  170. key = (added_or_removed == :removed ? :old_value : :value)
  171. details << JournalDetail.new(
  172. :property => 'attachment',
  173. :prop_key => attachment.id,
  174. key => attachment.filename
  175. )
  176. end
  177. # Adds a journal detail for an issue relation that was added or removed
  178. def journalize_relation(relation, added_or_removed)
  179. key = (added_or_removed == :removed ? :old_value : :value)
  180. details << JournalDetail.new(
  181. :property => 'relation',
  182. :prop_key => relation.relation_type_for(journalized),
  183. key => relation.other_issue(journalized).try(:id)
  184. )
  185. end
  186. private
  187. # Generates journal details for attribute and custom field changes
  188. def journalize_changes
  189. # attributes changes
  190. if @attributes_before_change
  191. attrs = (journalized.journalized_attribute_names + @attributes_before_change.keys).uniq
  192. attrs.each do |attribute|
  193. before = @attributes_before_change[attribute]
  194. after = journalized.send(attribute)
  195. next if before == after || (before.blank? && after.blank?)
  196. add_attribute_detail(attribute, before, after)
  197. end
  198. end
  199. # custom fields changes
  200. if @custom_values_before_change
  201. values_by_custom_field_id = {}
  202. @custom_values_before_change.each do |custom_field_id, value|
  203. values_by_custom_field_id[custom_field_id] = nil
  204. end
  205. journalized.custom_field_values.each do |c|
  206. values_by_custom_field_id[c.custom_field_id] = c.value
  207. end
  208. values_by_custom_field_id.each do |custom_field_id, after|
  209. before = @custom_values_before_change[custom_field_id]
  210. next if before == after || (before.blank? && after.blank?)
  211. if before.is_a?(Array) || after.is_a?(Array)
  212. before = [before] unless before.is_a?(Array)
  213. after = [after] unless after.is_a?(Array)
  214. # values removed
  215. (before - after).reject(&:blank?).each do |value|
  216. add_custom_field_detail(custom_field_id, value, nil)
  217. end
  218. # values added
  219. (after - before).reject(&:blank?).each do |value|
  220. add_custom_field_detail(custom_field_id, nil, value)
  221. end
  222. else
  223. add_custom_field_detail(custom_field_id, before, after)
  224. end
  225. end
  226. end
  227. start
  228. end
  229. # Adds a journal detail for an attribute change
  230. def add_attribute_detail(attribute, old_value, value)
  231. add_detail('attr', attribute, old_value, value)
  232. end
  233. # Adds a journal detail for a custom field value change
  234. def add_custom_field_detail(custom_field_id, old_value, value)
  235. add_detail('cf', custom_field_id, old_value, value)
  236. end
  237. # Adds a journal detail
  238. def add_detail(property, prop_key, old_value, value)
  239. details << JournalDetail.new(
  240. :property => property,
  241. :prop_key => prop_key,
  242. :old_value => old_value,
  243. :value => value
  244. )
  245. end
  246. def split_private_notes
  247. if private_notes?
  248. if notes.present?
  249. if details.any?
  250. # Split the journal (notes/changes) so we don't have half-private journals
  251. journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
  252. journal.details = details
  253. journal.save
  254. self.details = []
  255. self.created_on = journal.created_on
  256. end
  257. else
  258. # Blank notes should not be private
  259. self.private_notes = false
  260. end
  261. end
  262. true
  263. end
  264. def send_notification
  265. if notify? && (Setting.notified_events.include?('issue_updated') ||
  266. (Setting.notified_events.include?('issue_note_added') && notes.present?) ||
  267. (Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
  268. (Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) ||
  269. (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?)
  270. )
  271. Mailer.deliver_issue_edit(self)
  272. end
  273. end
  274. end