Class: LatoCms::PagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lato_cms/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#clone_component_actionObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/lato_cms/pages_controller.rb', line 128

def clone_component_action
  @page = query_pages.find(params[:id])
  source = @page.translations.find_by(id: params[:source_page_id])
  template_component_id = params[:template_component_id].to_s

  respond_to do |format|
    if source && clone_component_fields(source, @page, template_component_id)
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.component_cloned') }
      format.json { render json: { message: t('lato_cms.component_cloned') } }
    else
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_clone_failed') }
      format.json { render json: { error: t('lato_cms.component_clone_failed') }, status: :unprocessable_entity }
    end
  end
end

#createObject



26
27
28
# File 'app/controllers/lato_cms/pages_controller.rb', line 26

def create
  @page = LatoCms::Page.new(locale: LatoCms.config.locales.first.to_s)
end

#create_actionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/lato_cms/pages_controller.rb', line 30

def create_action
  @page = LatoCms::Page.new(create_params.merge(lato_spaces_group_id: @session.get(:spaces_group_id)))

  respond_to do |format|
    if @page.save
      format.html { redirect_to lato_cms.pages_path, notice: t('lato_cms.page_created') }
      format.json { render json: @page }
    else
      format.html { render :create, status: :unprocessable_entity }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end

#destroy_actionObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/lato_cms/pages_controller.rb', line 178

def destroy_action
  @page = query_pages.find(params[:id])

  respond_to do |format|
    if @page.destroy
      format.html { redirect_to lato_cms.pages_path, notice: t('lato_cms.page_deleted') }
      format.json { render json: { message: t('lato_cms.page_deleted') } }
    else
      format.html { redirect_to lato_cms.pages_path, alert: t('lato_cms.page_delete_failed') }
      format.json { render json: { error: t('lato_cms.page_delete_failed') }, status: :unprocessable_entity }
    end
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/lato_cms/pages_controller.rb', line 5

def index
  pages = query_pages
  pages = pages.where(locale: params[:locale]) if params[:locale].present?

  @pages = lato_index_collection(
    pages,
    columns: %i[title permalink locale actions],
    sortable_columns: %i[title permalink locale],
    searchable_columns: %i[title permalink],
    default_sort_by: 'title|ASC',
    pagination: 20
  )
end


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/lato_cms/pages_controller.rb', line 148

def link_translation_action
  @page = query_pages.find(params[:id])
  other_page = query_pages.find(params[:translation_page_id])

  respond_to do |format|
    if @page.link_translation(other_page)
      format.html { redirect_to lato_cms.pages_translations_path(@page), notice: t('lato_cms.translation_linked') }
      format.json { render json: @page }
    else
      format.html { redirect_to lato_cms.pages_translations_path(@page), alert: t('lato_cms.translation_link_failed') }
      format.json { render json: { error: t('lato_cms.translation_link_failed') }, status: :unprocessable_entity }
    end
  end
end

#save_fields_actionObject



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
# File 'app/controllers/lato_cms/pages_controller.rb', line 62

def save_fields_action
  @page = query_pages.find(params[:id])

  template_component_id = params[:template_component_id]
  component_id = params[:component_id]
  fields_data = params[:fields] || {}

  component = LatoCms::TemplateManager.find_component(component_id)
  errors = []

  unless @page.component_effectively_enabled?(template_component_id)
    respond_to do |format|
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_disabled_cannot_save') }
      format.json { render json: { error: t('lato_cms.component_disabled_cannot_save') }, status: :unprocessable_entity }
    end
    return
  end

  template_component = @page.template_components.find { |tc| tc[:template_component_id] == template_component_id.to_s }

  if template_component&.dig(:repeater)
    save_repeater_fields(template_component, component, params[:repeater_items] || {}, params[:repeater_order] || [], errors)
  else
    fields_data.each do |field_id, field_data|
      save_field(component, template_component_id, component_id, field_id, field_id, field_data, errors)
    end
  end

  respond_to do |format|
    if errors.empty?
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.fields_saved') }
      format.json { render json: { message: t('lato_cms.fields_saved'), fields: @page.fields.reload.map(&:as_json) } }
    else
      error_messages = errors.map { |e| "#{e[:field_id]}: #{e[:errors].join(', ')}" }.join('; ')
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: error_messages }
      format.json { render json: { errors: errors }, status: :unprocessable_entity }
    end
  end
end

#showObject



19
20
21
22
23
24
# File 'app/controllers/lato_cms/pages_controller.rb', line 19

def show
  @page = query_pages.find(params[:id])
  @page.fields.load
  @template = @page.template
  @template_components = @page.template_components
end

#toggle_component_actionObject



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 'app/controllers/lato_cms/pages_controller.rb', line 102

def toggle_component_action
  @page = query_pages.find(params[:id])
  template_component_id = params[:template_component_id].to_s
  enabled = ActiveModel::Type::Boolean.new.cast(params[:enabled])

  if @page.component_required?(template_component_id)
    respond_to do |format|
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: t('lato_cms.component_required_cannot_disable') }
      format.json { render json: { error: t('lato_cms.component_required_cannot_disable') }, status: :unprocessable_entity }
    end
    return
  end

  @page.set_component_enabled(template_component_id, enabled)

  respond_to do |format|
    if @page.save
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.component_state_updated') }
      format.json { render json: { message: t('lato_cms.component_state_updated') } }
    else
      format.html { redirect_to lato_cms.pages_show_path(@page), alert: @page.errors.full_messages.to_sentence }
      format.json { render json: { errors: @page.errors.full_messages }, status: :unprocessable_entity }
    end
  end
end

#translationsObject



144
145
146
# File 'app/controllers/lato_cms/pages_controller.rb', line 144

def translations
  @page = query_pages.find(params[:id])
end


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/lato_cms/pages_controller.rb', line 163

def unlink_translation_action
  @page = query_pages.find(params[:id])
  other_page = query_pages.find(params[:translation_page_id])

  respond_to do |format|
    if @page.unlink_translation(other_page)
      format.html { redirect_to lato_cms.pages_translations_path(@page), notice: t('lato_cms.translation_unlinked') }
      format.json { render json: @page }
    else
      format.html { redirect_to lato_cms.pages_translations_path(@page), alert: t('lato_cms.translation_unlink_failed') }
      format.json { render json: { error: t('lato_cms.translation_unlink_failed') }, status: :unprocessable_entity }
    end
  end
end

#updateObject



44
45
46
# File 'app/controllers/lato_cms/pages_controller.rb', line 44

def update
  @page = query_pages.find(params[:id])
end

#update_actionObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/lato_cms/pages_controller.rb', line 48

def update_action
  @page = query_pages.find(params[:id])

  respond_to do |format|
    if @page.update(update_params)
      format.html { redirect_to lato_cms.pages_show_path(@page), notice: t('lato_cms.page_updated') }
      format.json { render json: @page }
    else
      format.html { render :update, status: :unprocessable_entity }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end