Warn before leaving page.
[redakcja.git] / redmine / redmine_publications / lib / issue_publication_hook.rb
1 # Provides a link to the document on the platform
2 class IssuesPublicationHook < Redmine::Hook::ViewListener
3
4   def self.render_on(hook, options={})
5     define_method hook do |context|
6       if !options.include?(:if) || evaluate_if_option(options[:if], context)
7         context[:controller].send(:render_to_string, {:locals => context}.merge(options))
8       end
9     end
10   end
11
12   private
13
14   def evaluate_if_option(if_option, context)
15     case if_option
16     when Symbol
17       send(if_option, context)
18     when Method, Proc
19       if_option.call(context)
20     end
21   end
22
23   def is_pticket?(context)
24     context[:issue].project_id == Setting.plugin_redmine_publications[:project].to_i
25   end
26
27   public
28
29   render_on :view_issues_show_details_bottom, :partial => 'issue_view_pub', :if => :is_pticket?
30   render_on :view_issues_form_details_bottom, :partial => 'issue_form_pub', :if => :is_pticket?
31
32   #  names = context[:issue].publication_names {|name| "<span>" + name + "</span>"}
33   #  result << names.join(', ')
34
35   def controller_issues_edit_before_save(context)
36     if is_pticket?context
37       old_value = context[:issue].publication_names
38       new_value = context[:params][:publications].split(',').map { |n| n.strip }
39       context[:journal].details << JournalDetail.new(
40         :property => 'attr', :prop_key => "publications",
41         :old_value => old_value.join(', '),
42         :value => new_value.join(', ') ) unless new_value==old_value
43       context[:issue].publication_names = new_value
44     end
45   end
46
47
48   def controller_issues_new_after_save(context)
49     if is_pticket?context
50       value = context[:params][:publications].split(',').map { |n| n.strip }
51       context[:issue].publication_names = value
52       context[:issue].save
53     end
54   end
55 end