##// END OF EJS Templates
Added Redmine::Plugin::Hook::Manager.clear_listeners to remove all hook listeners.
Eric Davis -
r1704:cb485c92efb4
parent child
Show More
@@ -1,242 +1,249
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine #:nodoc:
19 19
20 20 # Base class for Redmine plugins.
21 21 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
22 22 #
23 23 # Redmine::Plugin.register :example do
24 24 # name 'Example plugin'
25 25 # author 'John Smith'
26 26 # description 'This is an example plugin for Redmine'
27 27 # version '0.0.1'
28 28 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
29 29 # end
30 30 #
31 31 # === Plugin attributes
32 32 #
33 33 # +settings+ is an optional attribute that let the plugin be configurable.
34 34 # It must be a hash with the following keys:
35 35 # * <tt>:default</tt>: default value for the plugin settings
36 36 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
37 37 # Example:
38 38 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
39 39 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
40 40 #
41 41 # When rendered, the plugin settings value is available as the local variable +settings+
42 42 class Plugin
43 43 @registered_plugins = {}
44 44 class << self
45 45 attr_reader :registered_plugins
46 46 private :new
47 47
48 48 def def_field(*names)
49 49 class_eval do
50 50 names.each do |name|
51 51 define_method(name) do |*args|
52 52 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
53 53 end
54 54 end
55 55 end
56 56 end
57 57 end
58 58 def_field :name, :description, :author, :version, :settings
59 59
60 60 # Plugin constructor
61 61 def self.register(name, &block)
62 62 p = new
63 63 p.instance_eval(&block)
64 64 Plugin.registered_plugins[name] = p
65 65 end
66 66
67 67 # Adds an item to the given +menu+.
68 68 # The +id+ parameter (equals to the project id) is automatically added to the url.
69 69 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
70 70 #
71 71 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
72 72 #
73 73 def menu(name, item, url, options={})
74 74 Redmine::MenuManager.map(name) {|menu| menu.push item, url, options}
75 75 end
76 76
77 77 # Defines a permission called +name+ for the given +actions+.
78 78 #
79 79 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
80 80 # permission :destroy_contacts, { :contacts => :destroy }
81 81 # permission :view_contacts, { :contacts => [:index, :show] }
82 82 #
83 83 # The +options+ argument can be used to make the permission public (implicitly given to any user)
84 84 # or to restrict users the permission can be given to.
85 85 #
86 86 # Examples
87 87 # # A permission that is implicitly given to any user
88 88 # # This permission won't appear on the Roles & Permissions setup screen
89 89 # permission :say_hello, { :example => :say_hello }, :public => true
90 90 #
91 91 # # A permission that can be given to any user
92 92 # permission :say_hello, { :example => :say_hello }
93 93 #
94 94 # # A permission that can be given to registered users only
95 95 # permission :say_hello, { :example => :say_hello }, :require => loggedin
96 96 #
97 97 # # A permission that can be given to project members only
98 98 # permission :say_hello, { :example => :say_hello }, :require => member
99 99 def permission(name, actions, options = {})
100 100 if @project_module
101 101 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
102 102 else
103 103 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
104 104 end
105 105 end
106 106
107 107 # Defines a project module, that can be enabled/disabled for each project.
108 108 # Permissions defined inside +block+ will be bind to the module.
109 109 #
110 110 # project_module :things do
111 111 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
112 112 # permission :destroy_contacts, { :contacts => :destroy }
113 113 # end
114 114 def project_module(name, &block)
115 115 @project_module = name
116 116 self.instance_eval(&block)
117 117 @project_module = nil
118 118 end
119 119
120 120 # Registers a +method+ to be called when Redmine runs a hook called
121 121 # +hook_name+
122 122 #
123 123 # # Run puts whenever the issue_show hook is called
124 124 # add_hook :issue_show, Proc.new { puts 'Hello' }
125 125 #
126 126 # # Call the class method +my_method+ passing in all the context
127 127 # add_hook :issue_show, Proc.new {|context| MyPlugin.my_method(context)}
128 128 def add_hook(hook_name, method)
129 129 Redmine::Plugin::Hook::Manager.add_listener(hook_name, method)
130 130 end
131 131
132 132 # Registers an activity provider.
133 133 #
134 134 # Options:
135 135 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
136 136 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
137 137 #
138 138 # A model can provide several activity event types.
139 139 #
140 140 # Examples:
141 141 # register :news
142 142 # register :scrums, :class_name => 'Meeting'
143 143 # register :issues, :class_name => ['Issue', 'Journal']
144 144 #
145 145 # Retrieving events:
146 146 # Associated model(s) must implement the find_events class method.
147 147 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
148 148 #
149 149 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
150 150 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
151 151 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
152 152 #
153 153 # Note that :view_scrums permission is required to view these events in the activity view.
154 154 def activity_provider(*args)
155 155 Redmine::Activity.register(*args)
156 156 end
157 157
158 158 # Returns +true+ if the plugin can be configured.
159 159 def configurable?
160 160 settings && settings.is_a?(Hash) && !settings[:partial].blank?
161 161 end
162 162
163 163 # Hook is used to allow plugins to hook into Redmine at specific sections
164 164 # to change it's behavior. See +Redmine::Plugin.add_hook+ for details.
165 165 class Hook
166 166 class Manager
167 167 # Hooks and the procs added
168 168 @@hooks = {
169 169 :issue_show => [],
170 170 :issue_edit => [],
171 171 :issue_bulk_edit => [],
172 172 :issue_bulk_edit_save => [],
173 173 :issue_update => [],
174 174 :project_member_list_header => [],
175 175 :project_member_list_column_three => [],
176 176 :issues_helper_show_details => []
177 177 }
178 178
179 179 cattr_reader :hooks
180 180
181 181 class << self
182 182
183 183 def valid_hook?(hook_name)
184 184 return @@hooks.has_key?(hook_name)
185 185 end
186 186
187 187 # Add +method+ to +hook_name+
188 188 def add_listener(hook_name, method)
189 189 if valid_hook?(hook_name)
190 190 @@hooks[hook_name.to_sym] << method
191 191 puts "Listener added for #{hook_name.to_s}"
192 192 end
193 193 end
194 194
195 # Removes all listeners
196 def clear_listeners()
197 @@hooks.each do |hook, registrations|
198 @@hooks[hook] = []
199 end
200 end
201
195 202 # Run all the hooks for +hook_name+ passing in +context+
196 203 def call_hook(hook_name, context = { })
197 204 response = ''
198 205 @@hooks[hook_name.to_sym].each do |method|
199 206 response += method.call(context)
200 207 end
201 208 response
202 209 end
203 210
204 211 # Are hooks registered for +hook_name+
205 212 def hook_registered?(hook_name)
206 213 return @@hooks[hook_name.to_sym].size > 0
207 214 end
208 215 end
209 216 end
210 217
211 218 # Base class for Redmin Plugin hooks.
212 219 class Base
213 220
214 221 # Class level access to Rails' helper methods.
215 222 def self.help
216 223 Helper.instance
217 224 end
218 225
219 226 # Includes several Helper methods to be used in the class
220 227 class Helper # :nodoc:
221 228 include Singleton
222 229 include ERB::Util
223 230 include ActionView::Helpers::TagHelper
224 231 include ActionView::Helpers::FormHelper
225 232 include ActionView::Helpers::FormTagHelper
226 233 include ActionView::Helpers::FormOptionsHelper
227 234 include ActionView::Helpers::JavaScriptHelper
228 235 include ActionView::Helpers::PrototypeHelper
229 236 include ActionView::Helpers::NumberHelper
230 237 include ActionView::Helpers::UrlHelper
231 238
232 239 include ActionController::UrlWriter
233 240
234 241 def protect_against_forgery? # :nodoc:
235 242 false
236 243 end
237 244
238 245 end
239 246 end
240 247 end
241 248 end
242 249 end
General Comments 0
You need to be logged in to leave comments. Login now