2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2012-05-05 12:56:53 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2012-05-05 12:56:53 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
|
2010-08-17 15:03:58 +00:00
|
|
|
class AutoCompletesController < ApplicationController
|
2016-07-14 07:27:31 +00:00
|
|
|
before_action :find_project
|
2011-08-30 13:03:05 +00:00
|
|
|
|
2010-08-17 15:03:58 +00:00
|
|
|
def issues
|
2019-02-18 14:56:19 +00:00
|
|
|
issues = []
|
2012-08-09 16:43:12 +00:00
|
|
|
q = (params[:q] || params[:term]).to_s.strip
|
2017-01-21 09:59:12 +00:00
|
|
|
status = params[:status].to_s
|
|
|
|
|
issue_id = params[:issue_id].to_s
|
2019-09-11 06:39:00 +00:00
|
|
|
|
2025-04-16 06:50:26 +00:00
|
|
|
scope = Issue.cross_project_scope(@project, params[:scope]).includes(:tracker).visible
|
2019-09-11 06:39:00 +00:00
|
|
|
scope = scope.open(status == 'o') if status.present?
|
|
|
|
|
scope = scope.where.not(:id => issue_id.to_i) if issue_id.present?
|
2012-08-09 16:43:12 +00:00
|
|
|
if q.present?
|
2019-09-11 08:08:38 +00:00
|
|
|
if q =~ /\A#?(\d+)\z/
|
|
|
|
|
issues << scope.find_by(:id => $1.to_i)
|
2012-08-09 16:43:12 +00:00
|
|
|
end
|
2019-02-18 14:56:19 +00:00
|
|
|
issues += scope.like(q).order(:id => :desc).limit(10).to_a
|
|
|
|
|
issues.compact!
|
2019-09-11 06:39:00 +00:00
|
|
|
else
|
|
|
|
|
issues += scope.order(:id => :desc).limit(10).to_a
|
2010-08-17 15:03:58 +00:00
|
|
|
end
|
2019-02-18 14:56:19 +00:00
|
|
|
|
|
|
|
|
render :json => format_issues_json(issues)
|
2010-08-17 15:03:58 +00:00
|
|
|
end
|
|
|
|
|
|
2021-02-25 04:07:37 +00:00
|
|
|
def wiki_pages
|
|
|
|
|
q = params[:q].to_s.strip
|
|
|
|
|
wiki = Wiki.find_by(project: @project)
|
|
|
|
|
if wiki.nil? || !User.current.allowed_to?(:view_wiki_pages, @project)
|
|
|
|
|
render json: []
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scope = wiki.pages.reorder(id: :desc)
|
|
|
|
|
wiki_pages =
|
|
|
|
|
if q.present?
|
|
|
|
|
scope.where("LOWER(#{WikiPage.table_name}.title) LIKE LOWER(?)", "%#{q}%").limit(10).to_a
|
|
|
|
|
else
|
|
|
|
|
scope.limit(10).to_a
|
|
|
|
|
end
|
|
|
|
|
render json: format_wiki_pages_json(wiki_pages)
|
|
|
|
|
end
|
|
|
|
|
|
2010-08-17 15:03:58 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_project
|
2012-08-09 16:58:36 +00:00
|
|
|
if params[:project_id].present?
|
|
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
|
end
|
2010-08-17 15:03:58 +00:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
|
|
|
|
end
|
2019-02-18 14:56:19 +00:00
|
|
|
|
|
|
|
|
def format_issues_json(issues)
|
2020-11-07 12:31:33 +00:00
|
|
|
issues.map do |issue|
|
2019-11-09 17:11:06 +00:00
|
|
|
{
|
|
|
|
|
'id' => issue.id,
|
2020-05-18 14:13:58 +00:00
|
|
|
'label' => "#{issue.tracker} ##{issue.id}: #{issue.subject.to_s.truncate(255)}",
|
2019-11-09 17:11:06 +00:00
|
|
|
'value' => issue.id
|
2019-02-18 14:56:19 +00:00
|
|
|
}
|
2020-11-07 12:31:33 +00:00
|
|
|
end
|
2019-02-18 14:56:19 +00:00
|
|
|
end
|
2021-02-25 04:07:37 +00:00
|
|
|
|
|
|
|
|
def format_wiki_pages_json(wiki_pages)
|
2021-02-27 08:15:08 +00:00
|
|
|
wiki_pages.map do |wiki_page|
|
2021-02-25 04:07:37 +00:00
|
|
|
{
|
|
|
|
|
'id' => wiki_page.id,
|
|
|
|
|
'label' => wiki_page.title.to_s.truncate(255),
|
|
|
|
|
'value' => wiki_page.title
|
|
|
|
|
}
|
2021-02-27 08:15:08 +00:00
|
|
|
end
|
2021-02-25 04:07:37 +00:00
|
|
|
end
|
2010-08-17 15:03:58 +00:00
|
|
|
end
|