Class: EventsController

Inherits:
ApplicationController show all
Defined in:
frontend/app/controllers/events_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Instance Method Details

#createObject



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
# File 'frontend/app/controllers/events_controller.rb', line 77

def create
  handle_crud(:instance => :event,
              :model => JSONModel(:event),
              :on_invalid => ->() {
                if params.has_key?(:redirect_action)
                  @redirect_action = params[:redirect_action]
                end
                render :action => :new
              },
              :on_valid => ->(id) {
                flash[:success] = t("event._frontend.messages.created")
                if params.has_key?(:plus_one)
                  # This is confusing because redirect_record is used as the resource/etc to which this event is
                  # linked to, and should indeed redirect to that record in the case of the Save button being pressed.
                  # (Or to the edit page of the new event if Create->Event from the main menu was used.)
                  # However, if the +1 button is used, we are redirecting to a new event, but still want to
                  # keep the link to that resource, hence the hijacking of redirect_record for that purpose, but
                  # not actually redirecting to it.
                  if params.has_key?(:redirect_record)
                    return redirect_to(
                      :controller => :events,
                      :action => :new,
                      :record_uri => params[:redirect_record],
                      :record_type => JSONModel.parse_reference(params[:redirect_record])[:type])
                  else
                    # If +1 was used from a New Event created via Create->Event in the menu, we will not automatically
                    # link the next event to any record, even if there were linked events added to it. This is because
                    # the intent to link events to a single previously existing resource was not expressed by coming
                    # here from an Add Event button on a resource, and the user could have linked any number of
                    # resources (or none) on this fresh standalone event.
                    return redirect_to(:controller => :events, :action => :new)
                  end
                end

                # we parse the reference as a simple sanity check here...
                if params.has_key?(:redirect_record) && JSONModel.parse_reference(params[:redirect_record])
                  if params[:redirect_action].blank?
                    redirect_action = :show
                  else
                    redirect_action = params[:redirect_action].intern
                  end

                  redirect_to(:controller => :resolver,
                              :action => (redirect_action == :edit) ? :resolve_edit : :resolve_readonly,
                              :uri => params[:redirect_record])
                else
                  redirect_to :controller => :events, :action => :edit, :id => id
                end
              })
end

#current_recordObject



32
33
34
# File 'frontend/app/controllers/events_controller.rb', line 32

def current_record
  @event
end

#defaultsObject



148
149
150
151
152
153
154
155
156
# File 'frontend/app/controllers/events_controller.rb', line 148

def defaults
  defaults = DefaultValues.get 'event'

  values = defaults ? defaults.form_values : {}

  @event = JSONModel(:event).new(values)._always_valid!

  render "defaults"
end

#deleteObject



140
141
142
143
144
145
# File 'frontend/app/controllers/events_controller.rb', line 140

def delete
  event = JSONModel(:event).find(params[:id])
  event.delete
  flash[:success] = t("event._frontend.messages.deleted")
  redirect_to(:controller => :events, :action => :index, :deleted_uri => event.uri)
end

#editObject



69
70
71
72
73
74
75
# File 'frontend/app/controllers/events_controller.rb', line 69

def edit
  @event = JSONModel(:event).find(params[:id], find_opts)

  if @event.suppressed
    redirect_to(:controller => :events, :action => :show, :id => params[:id])
  end
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'frontend/app/controllers/events_controller.rb', line 11

def index
  respond_to do |format|
    format.html {
      @search_data = Search.for_type(session[:repo_id], "event", params_for_backend_search.merge({"facet[]" => SearchResultData.EVENT_FACETS}))
    }
    format.csv {
      search_params = params_for_backend_search.merge({"facet[]" => SearchResultData.EVENT_FACETS})
      search_params["type[]"] = "event"

      # ANW-1635: when outputting to CSV, use linked_record_titles instead of linked_records since that's where the linked record data is available in the solr schema
      if search_params["fields[]"].include?("linked_records")
        search_params["fields[]"].delete("linked_records")
        search_params["fields[]"].push("linked_record_titles")
      end

      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(search_params), "#{t('event._plural').downcase}." )
    }
  end
end

#newObject



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
# File 'frontend/app/controllers/events_controller.rb', line 42

def new
  @event = JSONModel(:event).new._always_valid!
  @event.linked_agents = [{}]
  @event.linked_records = [{}]

  if user_prefs['default_values']
    defaults = DefaultValues.get 'event'

    @event.update(defaults.values) if defaults
  end


  if params.has_key?(:event_type)
    @event.event_type = params[:event_type]
  end

  if params.has_key?(:record_uri)
    record = JSONModel(params[:record_type]).find_by_uri(params[:record_uri])
    @event.linked_records = []
    @event.linked_records << {'ref' => record.uri, '_resolved' => record.to_hash, 'role' => 'source'}

    if request.referrer.end_with?("/edit")
      @redirect_action = 'edit'
    end
  end
end

#showObject



36
37
38
39
40
# File 'frontend/app/controllers/events_controller.rb', line 36

def show
  @event = JSONModel(:event).find(params[:id], find_opts)

  flash.now[:info] = t("event._frontend.messages.suppressed_info") if @event.suppressed
end

#updateObject



128
129
130
131
132
133
134
135
136
137
# File 'frontend/app/controllers/events_controller.rb', line 128

def update
  handle_crud(:instance => :event,
              :model => JSONModel(:event),
              :obj => JSONModel(:event).find(params[:id]),
              :on_invalid => ->() { render :action => :edit },
              :on_valid => ->(id) {
                flash[:success] = t("event._frontend.messages.updated")
                redirect_to :controller => :events, :action => :edit, :id => id
              })
end

#update_defaultsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'frontend/app/controllers/events_controller.rb', line 158

def update_defaults
  begin
    DefaultValues.from_hash({
                              "record_type" => "event",
                              "lock_version" => params[:event].delete('lock_version'),
                              "defaults" => cleanup_params_for_schema(
                                                                      params[:event],
                                                                      JSONModel(:event).schema)
                            }).save

    flash[:success] = t("default_values.messages.defaults_updated")
    redirect_to :controller => :events, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :events, :action => :defaults
  end
end