Class: ManifestationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/manifestations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /manifestations POST /manifestations.json



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'app/controllers/manifestations_controller.rb', line 384

def create
  @manifestation = Manifestation.new(manifestation_params)
  parent = Manifestation.find_by(id: @manifestation.parent_id)
  unless @manifestation.original_title?
    @manifestation.original_title = @manifestation.attachment_file_name
  end

  respond_to do |format|
    if @manifestation.save
      Manifestation.transaction do
        set_creators

        if parent
          parent.derived_manifestations << @manifestation
          parent.index
          @manifestation.index
        end

        Sunspot.commit
      end

      format.html { redirect_to @manifestation, notice: t('controller.successfully_created', model: t('activerecord.models.manifestation')) }
      format.json { render json: @manifestation, status: :created, location: @manifestation }
    else
      prepare_options
      format.html { render action: "new" }
      format.json { render json: @manifestation.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /manifestations/1 DELETE /manifestations/1.json



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'app/controllers/manifestations_controller.rb', line 436

def destroy
  # workaround
  @manifestation.identifiers.destroy_all
  @manifestation.creators.destroy_all
  @manifestation.contributors.destroy_all
  @manifestation.publishers.destroy_all
  @manifestation.bookmarks.destroy_all if defined?(EnjuBookmark)
  @manifestation.reload
  @manifestation.destroy

  respond_to do |format|
    format.html { redirect_to manifestations_url, notice: t('controller.successfully_deleted', model: t('activerecord.models.manifestation')) }
    format.json { head :no_content }
  end
end

#editObject

GET /manifestations/1/edit



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'app/controllers/manifestations_controller.rb', line 368

def edit
  unless current_user.has_role?('Librarian')
    unless params[:mode] == 'tag_edit'
      access_denied; return
    end
  end
  if defined?(EnjuBookmark)
    if params[:mode] == 'tag_edit'
      @bookmark = current_user.bookmarks.where(manifestation_id: @manifestation.id).first if @manifestation rescue nil
      render partial: 'manifestations/tag_edit', locals: {manifestation: @manifestation}
    end
  end
end

#indexObject

GET /manifestations GET /manifestations.json



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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/controllers/manifestations_controller.rb', line 18

def index
  mode = params[:mode]
  if mode == 'add'
    unless current_user.try(:has_role?, 'Librarian')
      access_denied; return
    end
  end

  @seconds = Benchmark.realtime do
    set_reservable if defined?(EnjuCirculation)

    sort, @count = {}, {}
    query = ""

    if request.format.text?
      per_page = 65534
    end

    if params[:api] == 'openurl'
      openurl = Openurl.new(params)
      @manifestations = openurl.search
      query = openurl.query_text
      sort = set_search_result_order(params[:sort_by], params[:order])
    else
      query = make_query(params[:query], params)
      sort = set_search_result_order(params[:sort_by], params[:order])
    end

    # 絞り込みを行わない状態のクエリ
    @query = query.dup
    query = query.gsub(' ', ' ')

    includes = [:series_statements]
    includes << :classifications if defined?(EnjuSubject)
    includes << :bookmarks if defined?(EnjuBookmark)
    search = Manifestation.search(include: includes)
    case @reservable
    when 'true'
      reservable = true
    when 'false'
      reservable = false
    else
      reservable = nil
    end

    agent = get_index_agent
    @index_agent = agent
    manifestation = @manifestation if @manifestation
    series_statement = @series_statement if @series_statement
    parent = @parent = Manifestation.where(id: params[:parent_id]).first if params[:parent_id].present?

    if defined?(EnjuSubject)
      subject = @subject if @subject
    end

    unless mode == 'add'
      search.build do
        with(:creator_ids).equal_to agent[:creator].id if agent[:creator]
        with(:contributor_ids).equal_to agent[:contributor].id if agent[:contributor]
        with(:publisher_ids).equal_to agent[:publisher].id if agent[:publisher]
        with(:series_statement_ids).equal_to series_statement.id if series_statement
        with(:parent_ids).equal_to parent.id if parent
      end
    end

    search.build do
      fulltext query if query.present?
      order_by sort[:sort_by], sort[:order]
      if defined?(EnjuSubject)
        with(:subject_ids).equal_to subject.id if subject
      end
      unless parent
        if params[:serial].to_s.downcase == "true"
          with(:series_master).equal_to true unless parent
          with(:serial).equal_to true
          #if series_statement.serial?
          #  if mode != 'add'
          #    order_by :volume_number, sort[:order]
          #    order_by :issue_number, sort[:order]
          #    order_by :serial_number, sort[:order]
          #  end
          #else
          #  with(:serial).equal_to false
          #end
        else
          if mode != 'add'
            with(:resource_master).equal_to true
          end
        end
      end
      facet :reservable if defined?(EnjuCirculation)
    end
    search = make_internal_query(search)
    search.data_accessor_for(Manifestation).select = [
      :id,
      :original_title,
      :title_transcription,
      :required_role_id,
      :carrier_type_id,
      :access_address,
      :volume_number_string,
      :issue_number_string,
      :serial_number_string,
      :date_of_publication,
      :pub_date,
      :language_id,
      :created_at,
      :updated_at,
      :volume_number_string,
      :volume_number,
      :issue_number_string,
      :issue_number,
      :serial_number,
      :edition_string,
      :edition,
      :serial,
      :statement_of_responsibility
    ] if params[:format] == 'html' or params[:format].nil?
    all_result = search.execute
    @count[:query_result] = all_result.total
    @reservable_facet = all_result.facet(:reservable).rows if defined?(EnjuCirculation)
    max_number_of_results = @library_group.max_number_of_results
    if max_number_of_results == 0
      @max_number_of_results = @count[:query_result]
    else
      @max_number_of_results = max_number_of_results
    end

    if params[:format] == 'html' or params[:format].nil?
      @search_query = Digest::SHA1.hexdigest(Marshal.dump(search.query.to_params).force_encoding('UTF-8'))
      if flash[:search_query] == @search_query
        flash.keep(:search_query)
      else
        if @series_statement
          flash.keep(:search_query)
        else
          flash[:search_query] = @search_query
          @manifestation_ids = search.build do
            paginate page: 1, per_page: @max_number_of_results
          end.execute.raw_results.collect(&:primary_key).map{|id| id.to_i}
        end
      end

      if defined?(EnjuBookmark)
        if params[:view] == 'tag_cloud'
          unless @manifestation_ids
            @manifestation_ids = search.build do
              paginate page: 1, per_page: @max_number_of_results
            end.execute.raw_results.collect(&:primary_key).map{|id| id.to_i}
          end
          #bookmark_ids = Bookmark.where(manifestation_id: flash[:manifestation_ids]).limit(1000).pluck(:id)
          bookmark_ids = Bookmark.where(manifestation_id: @manifestation_ids).limit(1000).pluck(:id)
          @tags = Tag.bookmarked(bookmark_ids)
          render partial: 'manifestations/tag_cloud'
          return
        end
      end
    end

    page ||= params[:page] || 1
    if params[:per_page].to_i > 0
      per_page = params[:per_page].to_i
    else
      per_page = Manifestation.default_per_page
    end

    pub_dates = parse_pub_date(params)
    pub_date_range = {}

    if pub_dates[:from] == '*'
      pub_date_range[:from] = 0
    else
      pub_date_range[:from] = Time.zone.parse(pub_dates[:from]).year
    end
    if pub_dates[:until] == '*'
      pub_date_range[:until] = 10000
    else
      pub_date_range[:until] = Time.zone.parse(pub_dates[:until]).year
    end

    if params[:pub_year_range_interval]
      pub_year_range_interval = params[:pub_year_range_interval].to_i
    else
      pub_year_range_interval = @library_group.pub_year_facet_range_interval || 10
    end

    search.build do
      facet :reservable if defined?(EnjuCirculation)
      facet :carrier_type
      facet :library
      facet :language
      facet :pub_year, range: pub_date_range[:from]..pub_date_range[:until], range_interval: pub_year_range_interval
      facet :subject_ids if defined?(EnjuSubject)
      paginate page: page.to_i, per_page: per_page
    end

    search_result = search.execute
    if @count[:query_result] > @max_number_of_results
      max_count = @max_number_of_results
    else
      max_count = @count[:query_result]
    end
    @manifestations = Kaminari.paginate_array(
      search_result.results, total_count: max_count
    ).page(page).per(per_page)

    if params[:format].blank? or params[:format] == 'html'
      @carrier_type_facet = search_result.facet(:carrier_type).rows
      @language_facet = search_result.facet(:language).rows
      @library_facet = search_result.facet(:library).rows
      @pub_year_facet = search_result.facet(:pub_year).rows.reverse
    end

    @search_engines = SearchEngine.order(:position)

    if defined?(EnjuBookmark)
      # TODO: 検索結果が少ない場合にも表示させる
      if @manifestation_ids.blank?
        if query.respond_to?(:suggest_tags)
          @suggested_tag = query.suggest_tags.first
        end
      end
    end

    if defined?(EnjuSearchLog)
      if current_user.try(:save_search_history)
        current_user.save_history(query, @manifestations.offset_value + 1, @count[:query_result], params[:format])
      end
    end
  end

  respond_to do |format|
    format.html
    format.html.phone
    format.xml  { render xml: @manifestations }
    format.rss  { render layout: false }
    format.text  { render layout: false }
    format.rdf  { render layout: false }
    format.atom
    format.mods
    format.json
    format.js
  end
end

#newObject

GET /manifestations/new GET /manifestations/new.json



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/controllers/manifestations_controller.rb', line 344

def new
  @manifestation = Manifestation.new
  @manifestation.language = Language.find_by(iso_639_1: @locale)
  @parent = Manifestation.find_by(id: params[:parent_id]) if params[:parent_id].present?
  if @parent
    @manifestation.parent_id = @parent.id
    [ :original_title, :title_transcription,
      :serial, :title_alternative, :statement_of_responsibility, :publication_place,
      :height, :width, :depth, :price, :access_address, :language, :frequency, :required_role,
    ].each do |attribute|
      @manifestation.send("#{attribute}=", @parent.send(attribute))
    end
    [ :creators, :contributors, :publishers, :classifications, :subjects ].each do |attribute|
      @manifestation.send(attribute).build(@parent.send(attribute).collect(&:attributes))
    end
  end

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @manifestation }
  end
end

#showObject

GET /manifestations/1 GET /manifestations/1.json



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'app/controllers/manifestations_controller.rb', line 265

def show
  if @version
    @manifestation = @manifestation.versions.find(@version).item if @version
  end

  case params[:mode]
  when 'send_email'
    if user_signed_in?
      Notifier.manifestation_info(current_user.id, @manifestation.id).deliver_later
      flash[:notice] = t('page.sent_email')
      redirect_to @manifestation
      return
    else
      access_denied; return
    end
  end

  return if render_mode(params[:mode])

  flash.keep(:search_query)

  if @manifestation.series_master?
    flash.keep(:notice) if flash[:notice]
    flash[:manifestation_id] = @manifestation.id
    redirect_to manifestations_url(parent_id: @manifestation.id)
    return
  end

  if defined?(EnjuCirculation)
    @reserved_count = Reserve.waiting.where(manifestation_id: @manifestation.id, checked_out_at: nil).count
    @reserve = current_user.reserves.where(manifestation_id: @manifestation.id).first if user_signed_in?
  end

  if defined?(EnjuQuestion)
    @questions = @manifestation.questions(user: current_user, page: params[:question_page])
  end

  if @manifestation.attachment.path
    if ENV['ENJU_STORAGE'] == 's3'
      data = Faraday.get(@manifestation.attachment.expiring_url).body.force_encoding('UTF-8')
    else
      file = @manifestation.attachment.path
    end
  end

  respond_to do |format|
    format.html # show.html.erb
    format.html.phone
    format.xml  {
      case params[:mode]
      when 'related'
        render template: 'manifestations/related'
      else
        render xml: @manifestation
      end
    }
    format.rdf
    format.mods
    format.json
    format.text
    format.js { render template: 'manifestations/show.js' }
    format.download {
      if @manifestation.attachment.path
        if ENV['ENJU_STORAGE'] == 's3'
          send_data data, filename: File.basename(@manifestation.attachment_file_name), type: 'application/octet-stream'
        else
          if File.exist?(file) && File.file?(file)
            send_file file, filename: File.basename(@manifestation.attachment_file_name), type: 'application/octet-stream'
          end
        end
      else
        render template: 'page/404', status: 404
      end
    }
  end
end

#updateObject

PUT /manifestations/1 PUT /manifestations/1.json



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'app/controllers/manifestations_controller.rb', line 417

def update
  @manifestation.assign_attributes(manifestation_params)

  respond_to do |format|
    if @manifestation.save
      set_creators
      
      format.html { redirect_to @manifestation, notice: t('controller.successfully_updated', model: t('activerecord.models.manifestation')) }
      format.json { head :no_content }
    else
      prepare_options
      format.html { render action: "edit" }
      format.json { render json: @manifestation.errors, status: :unprocessable_entity }
    end
  end
end