Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

4.3 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# 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.
#
# 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.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module Projects
  class Menu < Submenu
    include Rails.application.routes.url_helpers

    attr_reader :controller_path, :params, :current_user

    def initialize(params:, controller_path:, current_user:)
      @params = params
      @controller_path = controller_path
      @current_user = current_user

      super(view_type:, project:, params:)
    end

    def menu_items
      [
        OpenProject::Menu::MenuGroup.new(header: nil, children: main_static_filters),
        OpenProject::Menu::MenuGroup.new(header: I18n.t(:"projects.lists.my_lists"), children: my_filters),
        OpenProject::Menu::MenuGroup.new(header: I18n.t(:"projects.lists.shared"), children: shared_filters),
        OpenProject::Menu::MenuGroup.new(header: I18n.t(:"activerecord.attributes.project.status_code"),
                                         children: status_static_filters)
      ]
    end

    def selected?(query_params) # rubocop:disable Metrics/AbcSize
      case controller_path
      when "projects"
        case params[:query_id]
        when nil
          query_params[:query_id].to_s == ProjectQueries::Static::DEFAULT
        when /\A\d+\z/
          query_params[:query_id].to_s == params[:query_id]
        else
          query_params[:query_id].to_s == params[:query_id] unless modification_params?
        end
      when "projects/queries"
        query_params[:query_id].to_s == params[:id]
      end
    end

    def favorited?(query_params)
      query_params[:query_id].in?(favorited_ids)
    end

    def query_path(query_params)
      projects_path(query_params)
    end

    private

    def main_static_filters
      static_filters [
        ProjectQueries::Static::ACTIVE,
        current_user.logged? ? ProjectQueries::Static::MY : nil,
        current_user.logged? ? ProjectQueries::Static::FAVORITED : nil,
        current_user.admin? ? ProjectQueries::Static::ARCHIVED : nil
      ].compact
    end

    def status_static_filters
      static_filters [
        ProjectQueries::Static::ON_TRACK,
        ProjectQueries::Static::OFF_TRACK,
        ProjectQueries::Static::AT_RISK
      ]
    end

    def static_filters(ids)
      ids.map do |id|
        menu_item(title: ::ProjectQueries::Static.query(id).name, query_params: { query_id: id })
      end
    end

    def my_filters
      persisted_filters
        .select { |query| !query.public? && query.user == current_user }
        .map { |query| menu_item(title: query.name, query_params: { query_id: query.id }) }
    end

    def shared_filters
      persisted_filters
        .select { |query| query.public? || query.user != current_user }
        .map { |query| menu_item(title: query.name, query_params: { query_id: query.id }) }
    end

    def persisted_filters
      @persisted_filters ||= ::ProjectQuery
        .visible(current_user)
        .with_favorited_by_user(current_user)
        .order(favorited: :desc, name: :asc)
    end

    def favorited_ids
      @favorited_ids ||= persisted_filters.select(&:favorited).to_set(&:id)
    end

    def modification_params?
      params.values_at(:filters, :columns, :sortBy).any?
    end
  end
end