summaryrefslogtreecommitdiff
path: root/app/controllers/admin_controller.rb
blob: 84459973b26acccbf4166ad429d068b2c890b330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class AdminController < ApplicationController

  # Private

  before_action :login_required

  def index
    @drafts = Node.where("draft_id IS NOT NULL OR autosave_id IS NOT NULL")
      .limit(50).order("updated_at desc")

    @drafts_count = Node.where("draft_id IS NOT NULL OR autosave_id IS NOT NULL").count

    @recent_changes = Node.where(
      "updated_at < ? AND updated_at > ? AND parent_id IS NOT NULL",
      Time.now, Time.now - 14.days
    ).limit(50).order("updated_at desc")

    ordered_with_level = Node.root.self_and_descendants_ordered_with_level
    @sitemap_depth = {}
    ordered_with_level.each { |node, level| @sitemap_depth[node.id] = level }
    @sitemap = ordered_with_level.map(&:first).reject(&:update?)

    @mynodes = Node.joins(:pages)
          .where("pages.user_id = ? or pages.editor_id = ?", current_user, current_user)
          .order("updated_at desc")
          .distinct.first(50)
  end

  def conventions
    @node_kinds = CccConventions::NODE_KINDS
  end

  def search
    @results = Node.editor_search(params[:search_term])

    respond_to do |format|
      format.html do
        render :template => 'admin/search_results'
      end
      format.js do
        render( :json => @results.map do |node|
            if node
              { :id => node.id, :title => node.title, :unique_name => node.unique_name, :node_path => node_path(node) }
            end
          end
        )

      end
    end
  end

  def dashboard_search
    term = params[:search_term]

    if term.blank?
      render json: { tags: [], nodes: [] }
      return
    end

    render json: {
      tags: ActsAsTaggableOn::Tag.named_like(term).limit(5).map { |tag|
        { name: tag.name, tag_path: tags_nodes_path(tags: tag.name) }
      },
      nodes: Node.editor_search(term).limit(10).map { |node|
        { node_id: node.id, title: node.title, unique_name: node.unique_name, node_path: node_path(node) }
      }
    }
  end

  def menu_search
    if params[:search_term] == "Root"
      @results = [Node.root]
    else
      @results = Node.editor_search(params[:search_term])
    end

    respond_to do |format|
      format.html do
        render :partial => 'admin/menu_search_results'
      end

      format.js do
        render( :json => @results.map do |node|
          {:node_id => node.id, :title => node.title, :unique_name => node.unique_name, :node_path => node_path(node)}
          end
        )

      end
    end
  end
end