projects_helper.rb 6.04 KB
Newer Older
1 2
# encoding: utf-8
#
3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9
#
# 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.
10
#
11 12 13 14
# 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.
15
#
16 17 18 19 20
# 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 ProjectsHelper
jplang's avatar
jplang committed
21
  def project_settings_tabs
jplang's avatar
jplang committed
22
    tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_project},
jplang's avatar
jplang committed
23
            {:name => 'members', :action => :manage_members, :partial => 'projects/settings/members', :label => :label_member_plural},
24
            {:name => 'issues', :action => :edit_project, :module => :issue_tracking, :partial => 'projects/settings/issues', :label => :label_issue_tracking},
25 26
            {:name => 'versions', :action => :manage_versions, :partial => 'projects/settings/versions', :label => :label_version_plural,
              :url => {:tab => 'versions', :version_status => params[:version_status], :version_name => params[:version_name]}},
jplang's avatar
jplang committed
27
            {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural},
jplang's avatar
jplang committed
28
            {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
29
            {:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural},
30
            {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :label_time_tracking}
jplang's avatar
jplang committed
31
            ]
32 33 34
    tabs.
      select {|tab| User.current.allowed_to?(tab[:action], @project)}.
      select {|tab| tab[:module].nil? || @project.module_enabled?(tab[:module])}
jplang's avatar
jplang committed
35
  end
36

37
  def parent_project_select_tag(project)
38 39 40 41 42 43
    selected = project.parent
    # retrieve the requested parent project
    parent_id = (params[:project] && params[:project][:parent_id]) || params[:parent_id]
    if parent_id
      selected = (parent_id.blank? ? nil : Project.find(parent_id))
    end
44

45
    options = ''
46
    options << "<option value=''>&nbsp;</option>" if project.allowed_parents.include?(nil)
47
    options << project_tree_options_for_select(project.allowed_parents.compact, :selected => selected)
48
    content_tag('select', options.html_safe, :name => 'project[parent_id]', :id => 'project_parent_id')
49
  end
50

51
  def render_project_action_links
52
    links = "".html_safe
jplang's avatar
jplang committed
53 54 55
    if User.current.allowed_to?(:add_project, nil, :global => true)
      links << link_to(l(:label_project_new), new_project_path, :class => 'icon icon-add')
    end
56
    links
57 58
  end

59
  # Renders the projects index
60
  def render_project_hierarchy(projects)
61
    render_project_nested_lists(projects) do |project|
62
      s = link_to_project(project, {}, :class => "#{project.css_classes} #{User.current.member_of?(project) ? 'icon icon-fav my-project' : nil}")
63 64
      if project.description.present?
        s << content_tag('div', textilizable(project.short_description, :project => project), :class => 'wiki description')
65
      end
66
      s
67 68
    end
  end
69 70 71 72 73

  # Returns a set of options for a select field, grouped by project.
  def version_options_for_select(versions, selected=nil)
    grouped = Hash.new {|h,k| h[k] = []}
    versions.each do |version|
74
      grouped[version.project.name] << [version.name, version.id]
75
    end
76

77
    selected = selected.is_a?(Version) ? selected.id : selected
78
    if grouped.keys.size > 1
79
      grouped_options_for_select(grouped, selected)
80
    else
81
      options_for_select((grouped.values.first || []), selected)
82 83 84
    end
  end

85 86 87 88 89 90 91 92
  def project_default_version_options(project)
    versions = project.shared_versions.open.to_a
    if project.default_version && !versions.include?(project.default_version)
      versions << project.default_version
    end
    version_options_for_select(versions, project.default_version)
  end

93 94 95 96 97
  def project_default_assigned_to_options(project)
    assignable_users = (project.assignable_users.to_a + [project.default_assigned_to]).uniq.compact
    principals_options_for_select(assignable_users, project.default_assigned_to)
  end

98 99 100 101
  def format_version_sharing(sharing)
    sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
    l("label_version_sharing_#{sharing}")
  end
102

103 104 105 106 107 108 109 110 111 112 113 114 115
  def render_boards_tree(boards, parent=nil, level=0, &block)
    selection = boards.select {|b| b.parent == parent}
    return '' if selection.empty?

    s = ''.html_safe
    selection.each do |board|
      node = capture(board, level, &block)
      node << render_boards_tree(boards, board, level+1, &block)
      s << content_tag('div', node)
    end
    content_tag('div', s, :class => 'sort-level')
  end

116 117 118 119 120 121 122 123 124 125 126 127 128
  def render_api_includes(project, api)
    api.array :trackers do
      project.trackers.each do |tracker|
        api.tracker(:id => tracker.id, :name => tracker.name)
      end
    end if include_in_api_response?('trackers')

    api.array :issue_categories do
      project.issue_categories.each do |category|
        api.issue_category(:id => category.id, :name => category.name)
      end
    end if include_in_api_response?('issue_categories')

129 130 131 132 133 134
    api.array :time_entry_activities do
      project.activities.each do |activity|
        api.time_entry_activity(:id => activity.id, :name => activity.name)
      end
    end if include_in_api_response?('time_entry_activities')

135 136 137 138 139 140
    api.array :enabled_modules do
      project.enabled_modules.each do |enabled_module|
        api.enabled_module(:id => enabled_module.id, :name => enabled_module.name)
      end
    end if include_in_api_response?('enabled_modules')
  end
jplang's avatar
jplang committed
141
end