| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2024-02-26 22:55:54 +00:00
										 |  |  | # Copyright (C) 2006-  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) | 
					
						
							| 
									
										
										
										
											2020-11-19 13:30:00 +00:00
										 |  |  |         unless klass.included_modules.include?(Singleton) | 
					
						
							|  |  |  |           raise "Hooks must include Singleton module." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 10:31:06 +00:00
										 |  |  |         @@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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:43:28 +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 |