1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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
18 module Redmine #:nodoc:
19
20 class PluginNotFound < StandardError; end
21 class PluginRequirementError < StandardError; end
22
23 # Base class for Redmine plugins.
24 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
25 #
26 # Redmine::Plugin.register :example do
27 # name 'Example plugin'
28 # author 'John Smith'
29 # description 'This is an example plugin for Redmine'
30 # version '0.0.1'
31 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
32 # end
33 #
34 # === Plugin attributes
35 #
36 # +settings+ is an optional attribute that let the plugin be configurable.
37 # It must be a hash with the following keys:
38 # * <tt>:default</tt>: default value for the plugin settings
39 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
40 # Example:
41 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
42 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
43 #
44 # When rendered, the plugin settings value is available as the local variable +settings+
45 class Plugin
46 cattr_accessor :public_directory
47 self.public_directory = File.join(Rails.root, 'public', 'plugin_assets')
48
49 @registered_plugins = {}
50 class << self
51 attr_reader :registered_plugins
52 private :new
53
54 def def_field(*names)
55 class_eval do
56 names.each do |name|
57 define_method(name) do |*args|
58 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
59 end
60 end
61 end
62 end
63 end
64 def_field :name, :description, :url, :author, :author_url, :version, :settings
65 attr_reader :id
66
67 # Plugin constructor
68 def self.register(id, &block)
69 p = new(id)
70 p.instance_eval(&block)
71 # Set a default name if it was not provided during registration
72 p.name(id.to_s.humanize) if p.name.nil?
73 # Adds plugin locales if any
74 # YAML translation files should be found under <plugin>/config/locales/
75 ::I18n.load_path += Dir.glob(File.join(Rails.root, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml'))
76 registered_plugins[id] = p
77 end
78
79 # Returns an array of all registered plugins
80 def self.all
81 registered_plugins.values.sort
82 end
83
84 # Finds a plugin by its id
85 # Returns a PluginNotFound exception if the plugin doesn't exist
86 def self.find(id)
87 registered_plugins[id.to_sym] || raise(PluginNotFound)
88 end
89
90 # Clears the registered plugins hash
91 # It doesn't unload installed plugins
92 def self.clear
93 @registered_plugins = {}
94 end
95
96 # Checks if a plugin is installed
97 #
98 # @param [String] id name of the plugin
99 def self.installed?(id)
100 registered_plugins[id.to_sym].present?
101 end
102
103 def initialize(id)
104 @id = id.to_sym
105 end
106
107 def <=>(plugin)
108 self.id.to_s <=> plugin.id.to_s
109 end
110
111 # Sets a requirement on Redmine version
112 # Raises a PluginRequirementError exception if the requirement is not met
113 #
114 # Examples
115 # # Requires Redmine 0.7.3 or higher
116 # requires_redmine :version_or_higher => '0.7.3'
117 # requires_redmine '0.7.3'
118 #
119 # # Requires a specific Redmine version
120 # requires_redmine :version => '0.7.3' # 0.7.3 only
121 # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
122 def requires_redmine(arg)
123 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
124 arg.assert_valid_keys(:version, :version_or_higher)
125
126 current = Redmine::VERSION.to_a
127 arg.each do |k, v|
128 v = [] << v unless v.is_a?(Array)
129 versions = v.collect {|s| s.split('.').collect(&:to_i)}
130 case k
131 when :version_or_higher
132 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
133 unless (current <=> versions.first) >= 0
134 raise PluginRequirementError.new("#{id} plugin requires Redmine #{v} or higher but current is #{current.join('.')}")
135 end
136 when :version
137 unless versions.include?(current.slice(0,3))
138 raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{v.join(', ')} but current is #{current.join('.')}")
139 end
140 end
141 end
142 true
143 end
144
145 # Sets a requirement on a Redmine plugin version
146 # Raises a PluginRequirementError exception if the requirement is not met
147 #
148 # Examples
149 # # Requires a plugin named :foo version 0.7.3 or higher
150 # requires_redmine_plugin :foo, :version_or_higher => '0.7.3'
151 # requires_redmine_plugin :foo, '0.7.3'
152 #
153 # # Requires a specific version of a Redmine plugin
154 # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only
155 # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
156 def requires_redmine_plugin(plugin_name, arg)
157 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
158 arg.assert_valid_keys(:version, :version_or_higher)
159
160 plugin = Plugin.find(plugin_name)
161 current = plugin.version.split('.').collect(&:to_i)
162
163 arg.each do |k, v|
164 v = [] << v unless v.is_a?(Array)
165 versions = v.collect {|s| s.split('.').collect(&:to_i)}
166 case k
167 when :version_or_higher
168 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
169 unless (current <=> versions.first) >= 0
170 raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
171 end
172 when :version
173 unless versions.include?(current.slice(0,3))
174 raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
175 end
176 end
177 end
178 true
179 end
180
181 # Adds an item to the given +menu+.
182 # The +id+ parameter (equals to the project id) is automatically added to the url.
183 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
184 #
185 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
186 #
187 def menu(menu, item, url, options={})
188 Redmine::MenuManager.map(menu).push(item, url, options)
189 end
190 alias :add_menu_item :menu
191
192 # Removes +item+ from the given +menu+.
193 def delete_menu_item(menu, item)
194 Redmine::MenuManager.map(menu).delete(item)
195 end
196
197 # Defines a permission called +name+ for the given +actions+.
198 #
199 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
200 # permission :destroy_contacts, { :contacts => :destroy }
201 # permission :view_contacts, { :contacts => [:index, :show] }
202 #
203 # The +options+ argument can be used to make the permission public (implicitly given to any user)
204 # or to restrict users the permission can be given to.
205 #
206 # Examples
207 # # A permission that is implicitly given to any user
208 # # This permission won't appear on the Roles & Permissions setup screen
209 # permission :say_hello, { :example => :say_hello }, :public => true
210 #
211 # # A permission that can be given to any user
212 # permission :say_hello, { :example => :say_hello }
213 #
214 # # A permission that can be given to registered users only
215 # permission :say_hello, { :example => :say_hello }, :require => :loggedin
216 #
217 # # A permission that can be given to project members only
218 # permission :say_hello, { :example => :say_hello }, :require => :member
219 def permission(name, actions, options = {})
220 if @project_module
221 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
222 else
223 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
224 end
225 end
226
227 # Defines a project module, that can be enabled/disabled for each project.
228 # Permissions defined inside +block+ will be bind to the module.
229 #
230 # project_module :things do
231 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
232 # permission :destroy_contacts, { :contacts => :destroy }
233 # end
234 def project_module(name, &block)
235 @project_module = name
236 self.instance_eval(&block)
237 @project_module = nil
238 end
239
240 # Registers an activity provider.
241 #
242 # Options:
243 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
244 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
245 #
246 # A model can provide several activity event types.
247 #
248 # Examples:
249 # register :news
250 # register :scrums, :class_name => 'Meeting'
251 # register :issues, :class_name => ['Issue', 'Journal']
252 #
253 # Retrieving events:
254 # Associated model(s) must implement the find_events class method.
255 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
256 #
257 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
258 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
259 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
260 #
261 # Note that :view_scrums permission is required to view these events in the activity view.
262 def activity_provider(*args)
263 Redmine::Activity.register(*args)
264 end
265
266 # Registers a wiki formatter.
267 #
268 # Parameters:
269 # * +name+ - human-readable name
270 # * +formatter+ - formatter class, which should have an instance method +to_html+
271 # * +helper+ - helper module, which will be included by wiki pages
272 def wiki_format_provider(name, formatter, helper)
273 Redmine::WikiFormatting.register(name, formatter, helper)
274 end
275
276 # Returns +true+ if the plugin can be configured.
277 def configurable?
278 settings && settings.is_a?(Hash) && !settings[:partial].blank?
279 end
280 end
281 end