aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/stop_areas_controller.rb
blob: 734152c649cf8aed11e5d2024fa69de35104eea8 (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
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
class StopAreasController < ChouetteController
  include ApplicationHelper
  include Activatable

  defaults :resource_class => Chouette::StopArea

  belongs_to :stop_area_referential
  # do
  #   belongs_to :line, :parent_class => Chouette::Line, :optional => true, :polymorphic => true
  #   belongs_to :network, :parent_class => Chouette::Network, :optional => true, :polymorphic => true
  #   belongs_to :connection_link, :parent_class => Chouette::ConnectionLink, :optional => true, :polymorphic => true
  # end

  respond_to :html, :kml, :xml, :json
  respond_to :js, :only => :index

  def autocomplete
    scope = stop_area_referential.stop_areas.where(deleted_at: nil)
    args  = [].tap{|arg| 4.times{arg << "%#{params[:q]}%"}}
    @stop_areas = scope.where("unaccent(name) ILIKE unaccent(?) OR unaccent(city_name) ILIKE unaccent(?) OR registration_number ILIKE ? OR objectid ILIKE ?", *args).limit(50)
    @stop_areas
  end

  def select_parent
    @stop_area = stop_area
    @parent = stop_area.parent
  end

  def add_children
    authorize stop_area
    @stop_area = stop_area
    @children = stop_area.children
  end

  def add_routing_lines
    @stop_area = stop_area
    @lines = stop_area.routing_lines
  end

  def add_routing_stops
    @stop_area = stop_area
  end

  def access_links
    @stop_area = stop_area
    @generic_access_links = stop_area.generic_access_link_matrix
    @detail_access_links = stop_area.detail_access_link_matrix
  end

  def index
    request.format.kml? ? @per_page = nil : @per_page = 12
    @zip_codes = stop_area_referential.stop_areas.where("zip_code is NOT null").distinct.pluck(:zip_code)

    index! do |format|
      format.html {
        # binding.pry
        if collection.out_of_bounds?
          redirect_to params.merge(:page => 1)
        end

        @stop_areas = StopAreaDecorator.decorate(@stop_areas)
      }
    end
  end

  def new
    authorize resource_class
    new!
  end

  def create
    authorize resource_class
    create!
  end

  def show
    show! do |format|
      unless stop_area.position or params[:default] or params[:routing]
        format.kml {
          render :nothing => true, :status => :not_found
        }

      end

      @stop_area = @stop_area.decorate
    end
  end

  def edit
    authorize stop_area
    super
  end

  def destroy
    authorize stop_area
    super
  end

  def update
    authorize stop_area
    update!
  end

  def default_geometry
    count = stop_area_referential.stop_areas.without_geometry.default_geometry!
    flash[:notice] = I18n.translate("stop_areas.default_geometry_success", :count => count)
    redirect_to stop_area_referential_stop_areas_path(@stop_area_referential)
  end

  def zip_codes
    respond_to do |format|
      format.json { render :json => referential.stop_areas.collect(&:zip_code).compact.uniq.to_json }
    end
  end

  protected

  alias_method :stop_area, :resource
  alias_method :stop_area_referential, :parent

  def collection
    scope = parent.present? ? parent.stop_areas : referential.stop_areas
    scope = ransack_status(scope)
    @q = scope.search(params[:q])

    if sort_column && sort_direction
      @stop_areas ||=
        begin
          if sort_column == "area_type"
            sorted_area_type_labels = Chouette::AreaType.options(:all, I18n.locale).sort.transpose.last
            sorted_area_type_labels = sorted_area_type_labels.reverse if sort_direction != 'asc'
            order_by = ["CASE"]
            sorted_area_type_labels.each_with_index do |area_type, index|
              order_by << "WHEN area_type='#{area_type}' THEN #{index}"
            end
            order_by << "END"
            stop_areas = @q.result.order(order_by.join(" "))
          else
            stop_areas = @q.result.order(sort_column + ' ' + sort_direction)
          end
          stop_areas = stop_areas.paginate(:page => params[:page], :per_page => @per_page) if @per_page.present?
          stop_areas
        end
    else
      @stop_areas ||=
        begin
          stop_areas = @q.result.order(:name)
          stop_areas = stop_areas.paginate(:page => params[:page], :per_page => @per_page) if @per_page.present?
          stop_areas
        end
    end
  end

  def begin_of_association_chain
    current_organisation
  end

  private

  def sort_column
    if parent.present?
      parent.stop_areas.column_names.include?(params[:sort]) ? params[:sort] : 'name'
    else
      referential.stop_areas.column_names.include?(params[:sort]) ? params[:sort] : 'name'
    end
  end
  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  alias_method :current_referential, :stop_area_referential
  helper_method :current_referential

  def stop_area_params
    fields = [
      :area_type,
      :children_ids,
      :city_name,
      :comment,
      :coordinates,
      :country_code,
      :fare_code,
      :int_user_needs,
      :latitude,
      :lift_availability,
      :long_lat_type,
      :longitude,
      :mobility_restricted_suitability,
      :name,
      :nearest_topic_name,
      :object_version,
      :objectid,
      :parent_id,
      :registration_number,
      :routing_line_ids,
      :routing_stop_ids,
      :stairs_availability,
      :street_name,
      :time_zone,
      :url,
      :waiting_time,
      :zip_code,
      :kind,
      :status,
      localized_names: Chouette::StopArea::AVAILABLE_LOCALIZATIONS
    ] + permitted_custom_fields_params(Chouette::StopArea.custom_fields(stop_area_referential.workgroup))
    params.require(:stop_area).permit(fields)
  end

   # Fake ransack filter
  def ransack_status scope
    return scope unless params[:q].try(:[], :status)
    return scope if params[:q][:status].values.uniq.length == 1

    @status = {
      in_creation: params[:q][:status]['in_creation'] == 'true',
      confirmed: params[:q][:status]['confirmed'] == 'true',
      deactivated: params[:q][:status]['deactivated'] == 'true',
    }

    scope = Chouette::StopArea.where(
      "confirmed_at #{(@status[:confirmed] || @status[:deactivated]) ? "IS NOT NULL" : "IS NULL"}
      AND deleted_at #{@status[:deactivated] ? "IS NOT NULL" : "IS NULL"}"
      )

    params[:q].delete :status
    scope
  end
end