You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menu_manager.rb 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module MenuManager
  20. # @private
  21. class MenuError < StandardError
  22. end
  23. module MenuController
  24. def self.included(base)
  25. base.class_attribute :main_menu
  26. base.main_menu = true
  27. base.extend(ClassMethods)
  28. end
  29. module ClassMethods
  30. @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}}
  31. mattr_accessor :menu_items
  32. # Set the menu item name for a controller or specific actions
  33. # Examples:
  34. # * menu_item :tickets # => sets the menu name to :tickets for the whole controller
  35. # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
  36. # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
  37. #
  38. # The default menu item name for a controller is controller_name by default
  39. # Eg. the default menu item name for ProjectsController is :projects
  40. def menu_item(id, options = {})
  41. if actions = options[:only]
  42. actions = [] << actions unless actions.is_a?(Array)
  43. actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id}
  44. else
  45. menu_items[controller_name.to_sym][:default] = id
  46. end
  47. end
  48. end
  49. def menu_items
  50. self.class.menu_items
  51. end
  52. def current_menu(project)
  53. if project && !project.new_record?
  54. :project_menu
  55. elsif self.class.main_menu
  56. :application_menu
  57. end
  58. end
  59. # Returns the menu item name according to the current action
  60. def current_menu_item
  61. @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
  62. menu_items[controller_name.to_sym][:default]
  63. end
  64. # Redirects user to the menu item
  65. # Returns false if user is not authorized
  66. def redirect_to_menu_item(name)
  67. redirect_to_project_menu_item(nil, name)
  68. end
  69. # Redirects user to the menu item of the given project
  70. # Returns false if user is not authorized
  71. def redirect_to_project_menu_item(project, name)
  72. menu = project.nil? ? :application_menu : :project_menu
  73. item = Redmine::MenuManager.items(menu).detect {|i| i.name.to_s == name.to_s}
  74. if item && item.allowed?(User.current, project)
  75. url = item.url
  76. url = {item.param => project}.merge(url) if project
  77. redirect_to url
  78. return true
  79. end
  80. false
  81. end
  82. end
  83. module MenuHelper
  84. # Returns the current menu item name
  85. def current_menu_item
  86. controller.current_menu_item
  87. end
  88. # Renders the application main menu
  89. def render_main_menu(project)
  90. if menu_name = controller.current_menu(project)
  91. render_menu(menu_name, project)
  92. end
  93. end
  94. def display_main_menu?(project)
  95. menu_name = controller.current_menu(project)
  96. menu_name.present? && Redmine::MenuManager.items(menu_name).children.present?
  97. end
  98. def render_menu(menu, project=nil)
  99. links = []
  100. menu_items_for(menu, project) do |node|
  101. links << render_menu_node(node, project)
  102. end
  103. links.empty? ? nil : content_tag('ul', links.join.html_safe)
  104. end
  105. def render_menu_node(node, project=nil)
  106. if node.children.present? || !node.child_menus.nil?
  107. return render_menu_node_with_children(node, project)
  108. else
  109. caption, url, selected = extract_node_details(node, project)
  110. return content_tag('li',
  111. render_single_menu_node(node, caption, url, selected))
  112. end
  113. end
  114. def render_menu_node_with_children(node, project=nil)
  115. caption, url, selected = extract_node_details(node, project)
  116. html = [].tap do |html|
  117. html << '<li>'
  118. # Parent
  119. html << render_single_menu_node(node, caption, url, selected)
  120. # Standard children
  121. standard_children_list = "".html_safe.tap do |child_html|
  122. node.children.each do |child|
  123. child_html << render_menu_node(child, project) if allowed_node?(child, User.current, project)
  124. end
  125. end
  126. html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty?
  127. # Unattached children
  128. unattached_children_list = render_unattached_children_menu(node, project)
  129. html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank?
  130. html << '</li>'
  131. end
  132. return html.join("\n").html_safe
  133. end
  134. # Returns a list of unattached children menu items
  135. def render_unattached_children_menu(node, project)
  136. return nil unless node.child_menus
  137. "".html_safe.tap do |child_html|
  138. unattached_children = node.child_menus.call(project)
  139. # Tree nodes support #each so we need to do object detection
  140. if unattached_children.is_a? Array
  141. unattached_children.each do |child|
  142. child_html << content_tag(:li, render_unattached_menu_item(child, project)) if allowed_node?(child, User.current, project)
  143. end
  144. else
  145. raise MenuError, ":child_menus must be an array of MenuItems"
  146. end
  147. end
  148. end
  149. def render_single_menu_node(item, caption, url, selected)
  150. options = item.html_options(:selected => selected)
  151. # virtual nodes are only there for their children to be displayed in the menu
  152. # and should not do anything on click, except if otherwise defined elsewhere
  153. if url.blank?
  154. url = '#'
  155. options.reverse_merge!(:onclick => 'return false;')
  156. end
  157. link_to(h(caption), url, options)
  158. end
  159. def render_unattached_menu_item(menu_item, project)
  160. raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem
  161. if menu_item.allowed?(User.current, project)
  162. link_to(menu_item.caption, menu_item.url, menu_item.html_options)
  163. end
  164. end
  165. def menu_items_for(menu, project=nil)
  166. items = []
  167. Redmine::MenuManager.items(menu).root.children.each do |node|
  168. if node.allowed?(User.current, project)
  169. if block_given?
  170. yield node
  171. else
  172. items << node # TODO: not used?
  173. end
  174. end
  175. end
  176. return block_given? ? nil : items
  177. end
  178. def extract_node_details(node, project=nil)
  179. item = node
  180. url =
  181. case item.url
  182. when Hash
  183. project.nil? ? item.url : {item.param => project}.merge(item.url)
  184. when Symbol
  185. if project
  186. send(item.url, project)
  187. else
  188. send(item.url)
  189. end
  190. else
  191. item.url
  192. end
  193. caption = item.caption(project)
  194. return [caption, url, (current_menu_item == item.name)]
  195. end
  196. # See MenuItem#allowed?
  197. def allowed_node?(node, user, project)
  198. raise MenuError, ":child_menus must be an array of MenuItems" unless node.is_a? MenuItem
  199. node.allowed?(user, project)
  200. end
  201. end
  202. class << self
  203. def map(menu_name)
  204. @items ||= {}
  205. mapper = Mapper.new(menu_name.to_sym, @items)
  206. if block_given?
  207. yield mapper
  208. else
  209. mapper
  210. end
  211. end
  212. def items(menu_name)
  213. @items[menu_name.to_sym] || MenuNode.new(:root, {})
  214. end
  215. end
  216. class Mapper
  217. attr_reader :menu, :menu_items
  218. def initialize(menu, items)
  219. items[menu] ||= MenuNode.new(:root, {})
  220. @menu = menu
  221. @menu_items = items[menu]
  222. end
  223. # Adds an item at the end of the menu. Available options:
  224. # * param: the parameter name that is used for the project id (default is :id)
  225. # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
  226. # * caption that can be:
  227. # * a localized string Symbol
  228. # * a String
  229. # * a Proc that can take the project as argument
  230. # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
  231. # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
  232. # * children: a Proc that is called before rendering the item. The Proc should return an array of MenuItems, which will be added as children to this item.
  233. # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
  234. # * last: menu item will stay at the end (eg. :last => true)
  235. # * html_options: a hash of html options that are passed to link_to
  236. def push(name, url, options={})
  237. options = options.dup
  238. if options[:parent]
  239. subtree = self.find(options[:parent])
  240. if subtree
  241. target_root = subtree
  242. else
  243. target_root = @menu_items.root
  244. end
  245. else
  246. target_root = @menu_items.root
  247. end
  248. # menu item position
  249. if first = options.delete(:first)
  250. target_root.prepend(MenuItem.new(name, url, options))
  251. elsif before = options.delete(:before)
  252. if exists?(before)
  253. target_root.add_at(MenuItem.new(name, url, options), position_of(before))
  254. else
  255. target_root.add(MenuItem.new(name, url, options))
  256. end
  257. elsif after = options.delete(:after)
  258. if exists?(after)
  259. target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
  260. else
  261. target_root.add(MenuItem.new(name, url, options))
  262. end
  263. elsif options[:last] # don't delete, needs to be stored
  264. target_root.add_last(MenuItem.new(name, url, options))
  265. else
  266. target_root.add(MenuItem.new(name, url, options))
  267. end
  268. end
  269. # Removes a menu item
  270. def delete(name)
  271. if found = self.find(name)
  272. @menu_items.remove!(found)
  273. end
  274. end
  275. # Checks if a menu item exists
  276. def exists?(name)
  277. @menu_items.any? {|node| node.name == name}
  278. end
  279. def find(name)
  280. @menu_items.find {|node| node.name == name}
  281. end
  282. def position_of(name)
  283. @menu_items.each do |node|
  284. if node.name == name
  285. return node.position
  286. end
  287. end
  288. end
  289. end
  290. class MenuNode
  291. include Enumerable
  292. attr_accessor :parent
  293. attr_reader :last_items_count, :name
  294. def initialize(name, content = nil)
  295. @name = name
  296. @children = []
  297. @last_items_count = 0
  298. end
  299. def children
  300. if block_given?
  301. @children.each {|child| yield child}
  302. else
  303. @children
  304. end
  305. end
  306. # Returns the number of descendants + 1
  307. def size
  308. @children.inject(1) {|sum, node| sum + node.size}
  309. end
  310. def each &block
  311. yield self
  312. children { |child| child.each(&block) }
  313. end
  314. # Adds a child at first position
  315. def prepend(child)
  316. add_at(child, 0)
  317. end
  318. # Adds a child at given position
  319. def add_at(child, position)
  320. raise "Child already added" if find {|node| node.name == child.name}
  321. @children = @children.insert(position, child)
  322. child.parent = self
  323. child
  324. end
  325. # Adds a child as last child
  326. def add_last(child)
  327. add_at(child, -1)
  328. @last_items_count += 1
  329. child
  330. end
  331. # Adds a child
  332. def add(child)
  333. position = @children.size - @last_items_count
  334. add_at(child, position)
  335. end
  336. alias :<< :add
  337. # Removes a child
  338. def remove!(child)
  339. @children.delete(child)
  340. @last_items_count -= +1 if child && child.last
  341. child.parent = nil
  342. child
  343. end
  344. # Returns the position for this node in it's parent
  345. def position
  346. self.parent.children.index(self)
  347. end
  348. # Returns the root for this node
  349. def root
  350. root = self
  351. root = root.parent while root.parent
  352. root
  353. end
  354. end
  355. class MenuItem < MenuNode
  356. include Redmine::I18n
  357. attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last, :permission
  358. def initialize(name, url, options={})
  359. raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
  360. raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
  361. raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
  362. raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call)
  363. @name = name
  364. @url = url
  365. @condition = options[:if]
  366. @permission = options[:permission]
  367. @permission ||= false if options.key?(:permission)
  368. @param = options[:param] || :id
  369. @caption = options[:caption]
  370. @html_options = options[:html] || {}
  371. # Adds a unique class to each menu item based on its name
  372. @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
  373. @parent = options[:parent]
  374. @child_menus = options[:children]
  375. @last = options[:last] || false
  376. super @name.to_sym
  377. end
  378. def caption(project=nil)
  379. if @caption.is_a?(Proc)
  380. c = @caption.call(project).to_s
  381. c = @name.to_s.humanize if c.blank?
  382. c
  383. else
  384. if @caption.nil?
  385. l_or_humanize(name, :prefix => 'label_')
  386. else
  387. @caption.is_a?(Symbol) ? l(@caption) : @caption
  388. end
  389. end
  390. end
  391. def html_options(options={})
  392. if options[:selected]
  393. o = @html_options.dup
  394. o[:class] += ' selected'
  395. o
  396. else
  397. @html_options
  398. end
  399. end
  400. # Checks if a user is allowed to access the menu item by:
  401. #
  402. # * Checking the permission or the url target (project only)
  403. # * Checking the conditions of the item
  404. def allowed?(user, project)
  405. if url.blank?
  406. # this is a virtual node that is only there for its children to be diplayed in the menu
  407. # it is considered an allowed node if at least one of the children is allowed
  408. all_children = children
  409. all_children += child_menus.call(project) if child_menus
  410. return false unless all_children.detect{|child| child.allowed?(user, project) }
  411. elsif user && project
  412. if permission
  413. unless user.allowed_to?(permission, project)
  414. return false
  415. end
  416. elsif permission.nil? && url.is_a?(Hash)
  417. unless user.allowed_to?(url, project)
  418. return false
  419. end
  420. end
  421. end
  422. if condition && !condition.call(project)
  423. # Condition that doesn't pass
  424. return false
  425. end
  426. return true
  427. end
  428. end
  429. end
  430. end