Method: ItemsController#index

Defined in:
app/controllers/items_controller.rb

#indexObject

GET /items GET /items.json



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/items_controller.rb', line 17

def index
  query = params[:query].to_s.strip
  per_page = Item.default_per_page
  @count = {}
  if user_signed_in?
    if current_user.has_role?('Librarian')
      if params[:format] == 'txt'
        per_page = 65534
      elsif params[:mode] == 'barcode'
        per_page = 40
      end
    end
  end

  if defined?(InventoryFile)
    if @inventory_file
      if user_signed_in?
        if current_user.has_role?('Librarian')
          case params[:inventory]
          when 'not_in_catalog'
            mode = 'not_in_catalog'
          else
            mode = 'not_on_shelf'
          end
          order = 'items.id'
          @items = Item.inventory_items(@inventory_file, mode).order(order).page(params[:page]).per(per_page)
        else
          access_denied
          return
        end
      else
        redirect_to new_user_session_url
        return
      end
    end
  end

  unless @inventory_file
    search = Sunspot.new_search(Item)
    set_role_query(current_user, search)

    @query = query.dup
    unless query.blank?
      search.build do
        fulltext query
      end
    end

    agent = @agent
    manifestation = @manifestation
    shelf = @shelf
    unless params[:mode] == 'add'
      search.build do
        with(:agent_ids).equal_to agent.id if agent
        with(:manifestation_id).equal_to manifestation.id if manifestation
        with(:shelf_id).equal_to shelf.id if shelf
        with(:circulation_status).equal_to params[:circulation_status] if params[:circulation_status].present?
        facet :circulation_status if defined?(EnjuCirculation)
      end
    end

    search.build do
      order_by(:created_at, :desc)
    end

    role = current_user.try(:role) || Role.default_role
    search.build do
      with(:required_role_id).less_than_or_equal_to role.id
    end

    if params[:acquired_from].present?
      begin
        acquired_from = Time.zone.parse(params[:acquired_from]).beginning_of_day
        @acquired_from = acquired_from.strftime('%Y-%m-%d')
      rescue ArgumentError
      rescue NoMethodError
      end
    end
    if params[:acquired_until].present?
      begin
        acquired_until = @acquired_until = Time.zone.parse(params[:acquired_until]).end_of_day
        @acquired_until = acquired_until.strftime('%Y-%m-%d')
      rescue ArgumentError
      rescue NoMethodError
      end
    end
    search.build do
      with(:acquired_at).greater_than_or_equal_to acquired_from.beginning_of_day if acquired_from
      with(:acquired_at).less_than acquired_until.tomorrow.beginning_of_day if acquired_until
    end

    page = params[:page] || 1
    search.query.paginate(page.to_i, per_page)
    result = search.execute
    @circulation_status_facet = result.facet(:circulation_status).rows if defined?(EnjuCirculation)
    @items = result.results
    @count[:total] = @items.total_entries
  end

  if defined?(EnjuBarcode)
    if params[:mode] == 'barcode'
      render action: 'barcode', layout: false
      return
    end
  end

  flash[:page_info] = { page: page, query: query }

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @items }
    format.txt  { render layout: false }
    format.atom
  end
end