| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2015-01-11 09:09:50 +00:00
										 |  |  | # Copyright (C) 2006-2015  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Redmine | 
					
						
							|  |  |  |   module Hook | 
					
						
							|  |  |  |     @@listener_classes = [] | 
					
						
							|  |  |  |     @@listeners = nil | 
					
						
							|  |  |  |     @@hook_listeners = {} | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     class << self | 
					
						
							|  |  |  |       # Adds a listener class. | 
					
						
							|  |  |  |       # Automatically called when a class inherits from Redmine::Hook::Listener. | 
					
						
							|  |  |  |       def add_listener(klass) | 
					
						
							|  |  |  |         raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton) | 
					
						
							|  |  |  |         @@listener_classes << klass | 
					
						
							|  |  |  |         clear_listeners_instances | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 08:05:35 +00:00
										 |  |  |       # Returns all the listener instances. | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       def listeners | 
					
						
							|  |  |  |         @@listeners ||= @@listener_classes.collect {|listener| listener.instance} | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 08:05:35 +00:00
										 |  |  |       # Returns the listener instances for the given hook. | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       def hook_listeners(hook) | 
					
						
							|  |  |  |         @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       # Clears all the listeners. | 
					
						
							|  |  |  |       def clear_listeners | 
					
						
							|  |  |  |         @@listener_classes = [] | 
					
						
							|  |  |  |         clear_listeners_instances | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       # Clears all the listeners instances. | 
					
						
							|  |  |  |       def clear_listeners_instances | 
					
						
							|  |  |  |         @@listeners = nil | 
					
						
							|  |  |  |         @@hook_listeners = {} | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       # Calls a hook. | 
					
						
							|  |  |  |       # Returns the listeners response. | 
					
						
							|  |  |  |       def call_hook(hook, context={}) | 
					
						
							| 
									
										
										
										
											2010-11-14 16:24:21 +00:00
										 |  |  |         [].tap do |response| | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |           hls = hook_listeners(hook) | 
					
						
							|  |  |  |           if hls.any? | 
					
						
							|  |  |  |             hls.each {|listener| response << listener.send(hook, context)} | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Base class for hook listeners. | 
					
						
							|  |  |  |     class Listener | 
					
						
							|  |  |  |       include Singleton | 
					
						
							| 
									
										
										
										
											2009-02-21 11:04:50 +00:00
										 |  |  |       include Redmine::I18n | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Registers the listener | 
					
						
							|  |  |  |       def self.inherited(child) | 
					
						
							|  |  |  |         Redmine::Hook.add_listener(child) | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2009-02-25 07:25:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2009-02-25 07:25:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     # Listener class used for views hooks. | 
					
						
							|  |  |  |     # Listeners that inherit this class will include various helpers by default. | 
					
						
							|  |  |  |     class ViewListener < Listener | 
					
						
							|  |  |  |       include ERB::Util | 
					
						
							|  |  |  |       include ActionView::Helpers::TagHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::FormHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::FormTagHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::FormOptionsHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::JavaScriptHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::NumberHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::UrlHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::AssetTagHelper | 
					
						
							|  |  |  |       include ActionView::Helpers::TextHelper | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |       include Rails.application.routes.url_helpers | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       include ApplicationHelper | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 07:25:01 +00:00
										 |  |  |       # Default to creating links using only the path.  Subclasses can | 
					
						
							|  |  |  |       # change this default as needed | 
					
						
							|  |  |  |       def self.default_url_options | 
					
						
							|  |  |  |         {:only_path => true } | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-18 08:09:31 +00:00
										 |  |  |       # Helper method to directly render using the context, | 
					
						
							|  |  |  |       # render_options must be valid #render options. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |       #   class MyHook < Redmine::Hook::ViewListener | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |       #     render_on :view_issues_show_details_bottom, :partial => "show_more_data" | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |       #   end | 
					
						
							|  |  |  |       # | 
					
						
							| 
									
										
										
										
											2014-10-18 08:09:31 +00:00
										 |  |  |       #   class MultipleHook < Redmine::Hook::ViewListener | 
					
						
							|  |  |  |       #     render_on :view_issues_show_details_bottom, | 
					
						
							|  |  |  |       #       {:partial => "show_more_data"}, | 
					
						
							|  |  |  |       #       {:partial => "show_even_more_data"} | 
					
						
							|  |  |  |       #   end | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       def self.render_on(hook, *render_options) | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |         define_method hook do |context| | 
					
						
							| 
									
										
										
										
											2014-10-18 08:09:31 +00:00
										 |  |  |           render_options.map do |options| | 
					
						
							|  |  |  |             if context[:hook_caller].respond_to?(:render) | 
					
						
							|  |  |  |               context[:hook_caller].send(:render, {:locals => context}.merge(options)) | 
					
						
							|  |  |  |             elsif context[:controller].is_a?(ActionController::Base) | 
					
						
							|  |  |  |               context[:controller].send(:render_to_string, {:locals => context}.merge(options)) | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}" | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2012-06-09 09:19:15 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |        | 
					
						
							|  |  |  |       def controller | 
					
						
							|  |  |  |         nil | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |       def config | 
					
						
							|  |  |  |         ActionController::Base.config | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-21 09:47:31 +00:00
										 |  |  |     # Helper module included in ApplicationHelper and ActionController so that | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |     # hooks can be called in views like this: | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     #   <%= call_hook(:some_hook) %> | 
					
						
							| 
									
										
										
										
											2011-09-21 09:47:31 +00:00
										 |  |  |     #   <%= call_hook(:another_hook, :foo => 'bar') %> | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |     # Or in controllers like: | 
					
						
							|  |  |  |     #   call_hook(:some_hook) | 
					
						
							| 
									
										
										
										
											2011-09-21 09:47:31 +00:00
										 |  |  |     #   call_hook(:another_hook, :foo => 'bar') | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2011-09-21 09:47:31 +00:00
										 |  |  |     # Hooks added to views will be concatenated into a string. Hooks added to | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |     # controllers will return an array of results. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Several objects are automatically added to the call context: | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |     # * project => current project | 
					
						
							|  |  |  |     # * request => Request instance | 
					
						
							|  |  |  |     # * controller => current Controller instance | 
					
						
							| 
									
										
										
										
											2012-06-09 09:19:15 +00:00
										 |  |  |     # * hook_caller => object that called the hook | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |     module Helper | 
					
						
							|  |  |  |       def call_hook(hook, context={}) | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |         if is_a?(ActionController::Base) | 
					
						
							| 
									
										
										
										
											2012-06-09 09:19:15 +00:00
										 |  |  |           default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self} | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:45 +00:00
										 |  |  |           Redmine::Hook.call_hook(hook, default_context.merge(context)) | 
					
						
							| 
									
										
										
										
											2009-02-10 03:12:40 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2012-06-09 09:19:15 +00:00
										 |  |  |           default_context = { :project => @project, :hook_caller => self } | 
					
						
							| 
									
										
										
										
											2011-12-18 08:40:04 +00:00
										 |  |  |           default_context[:controller] = controller if respond_to?(:controller) | 
					
						
							|  |  |  |           default_context[:request] = request if respond_to?(:request) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |           Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe | 
					
						
							| 
									
										
										
										
											2011-09-20 02:12:17 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ApplicationHelper.send(:include, Redmine::Hook::Helper) | 
					
						
							| 
									
										
										
										
											2008-11-06 05:37:29 +00:00
										 |  |  | ActionController::Base.send(:include, Redmine::Hook::Helper) |