Class: Alchemy::Admin::PagesController

Inherits:
BaseController show all
Includes:
CurrentLanguage, OnPageLayout::CallbacksRunner
Defined in:
app/controllers/alchemy/admin/pages_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#leave

Methods included from Modules

included, #module_definition_for, register_module

Methods included from Alchemy::AbilityHelper

#current_ability

Methods included from ConfigurationMethods

#configuration, #multi_language?, #multi_site?, #prefix_locale?

Instance Method Details

#configureObject

Set page configuration like page names, meta tags and states.


93
94
95
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 93

def configure
  @page_layouts = PageLayout.layouts_with_own_for_select(@page.page_layout, @current_language.id, @page.layoutpage?)
end

#copy_language_treeObject


185
186
187
188
189
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 185

def copy_language_tree
  language_root_to_copy_from.copy_children_to(copy_of_language_root)
  flash[:notice] = Alchemy.t(:language_pages_copied)
  redirect_to admin_pages_path
end

#createObject


65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 65

def create
  @page = paste_from_clipboard || Page.new(page_params)
  if @page.save
    flash[:notice] = Alchemy.t("Page created", name: @page.name)
    do_redirect_to(redirect_path_after_create_page)
  else
    new
    render :new
  end
end

#destroyObject

Fetches page via before filter, destroys it and redirects to page tree


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 117

def destroy
  if @page.destroy
    flash[:notice] = Alchemy.t("Page deleted", name: @page.name)

    # Remove page from clipboard
    clipboard = get_clipboard("pages")
    clipboard.delete_if { |item| item["id"] == @page.id.to_s }
  end

  respond_to do |format|
    format.js do
      @redirect_url = if @page.layoutpage?
          alchemy.admin_layoutpages_path
        else
          alchemy.admin_pages_path
        end

      render :redirect
    end
  end
end

#editObject

Edit the content of the page and all its elements and contents.

Locks the page to current user to prevent other users from editing it meanwhile.


80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 80

def edit
  # fetching page via before filter
  if page_is_locked?
    flash[:warning] = Alchemy.t("This page is locked", name: @page.locker_name)
    redirect_to admin_pages_path
  elsif page_needs_lock?
    @page.lock_to!(current_alchemy_user)
  end
  @preview_url = Alchemy::Admin::PREVIEW_URL.url_for(@page)
  @layoutpage = @page.layoutpage?
end

#flushObject


212
213
214
215
216
217
218
219
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 212

def flush
  @current_language.pages.flushables.update_all(published_at: Time.current)
  # We need to ensure, that also all layoutpages get the +published_at+ timestamp set,
  # but not set to public true, because the cache_key for an element is +published_at+
  # and we don't want the layout pages to be present in +Page.published+ scope.
  @current_language.pages.flushable_layoutpages.update_all(published_at: Time.current)
  respond_to { |format| format.js }
end

#foldObject


145
146
147
148
149
150
151
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 145

def fold
  # @page is fetched via before filter
  @page.fold!(current_alchemy_user.id, !@page.folded?(current_alchemy_user.id))
  respond_to do |format|
    format.js
  end
end

#indexObject


30
31
32
33
34
35
36
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 30

def index
  if !@page_root
    @language = @current_language
    @languages_with_page_tree = Language.on_current_site.with_root_page
    @page_layouts = PageLayout.layouts_for_select(@language.id)
  end
end

#infoObject


54
55
56
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 54

def info
  render layout: !request.xhr?
end

139
140
141
142
143
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 139

def link
  @attachments = Attachment.all.collect { |f|
    [f.name, download_attachment_path(id: f.id, name: f.slug)]
  }
end

#newObject


58
59
60
61
62
63
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 58

def new
  @page ||= Page.new(layoutpage: params[:layoutpage] == "true", parent_id: params[:parent_id])
  @page_layouts = PageLayout.layouts_for_select(@current_language.id, @page.layoutpage?)
  @clipboard = get_clipboard("pages")
  @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, @page.layoutpage?)
end

#orderObject

Receives a JSON object representing a language tree to be ordered and updates all pages in that language structure to their correct indexes


197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 197

def order
  neworder = JSON.parse(params[:set])
  tree = create_tree(neworder, @page_root)

  Alchemy::Page.transaction do
    tree.each do |key, node|
      dbitem = Page.find(key)
      dbitem.update_node!(node)
    end
  end

  flash[:notice] = Alchemy.t("Pages order saved")
  do_redirect_to admin_pages_path
end

#publishObject

Sets the page public and updates the published_at attribute that is used as cache_key


178
179
180
181
182
183
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 178

def publish
  # fetching page via before filter
  @page.publish!
  flash[:notice] = Alchemy.t(:page_published, name: @page.name)
  redirect_back(fallback_location: admin_pages_path)
end

#showObject

Used by page preview iframe in Page#edit view.


46
47
48
49
50
51
52
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 46

def show
  @preview_mode = true
  Page.current_preview = @page
  # Setting the locale to pages language, so the page content has it's correct translations.
  ::I18n.locale = @page.language.locale
  render(layout: Alchemy::Config.get(:admin_page_preview_layout) || "application")
end

#sortObject


191
192
193
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 191

def sort
  @sorting = true
end

#treeObject

Returns all pages as a tree from the root given by the id parameter


40
41
42
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 40

def tree
  render json: serialized_page_tree
end

#unlockObject

Leaves the page editing mode and unlocks the page for other users


154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 154

def unlock
  # fetching page via before filter
  @page.unlock!
  flash[:notice] = Alchemy.t(:unlocked_page, name: @page.name)
  @pages_locked_by_user = Page.from_current_site.locked_by(current_alchemy_user)
  respond_to do |format|
    format.js
    format.html {
      redirect_to params[:redirect_to].blank? ? admin_pages_path : params[:redirect_to]
    }
  end
end

#updateObject

Updates page

  • fetches page via before filter


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 101

def update
  # stores old page_layout value, because unfurtunally rails @page.changes does not work here.
  @old_page_layout = @page.page_layout
  if @page.update(page_params)
    @notice = Alchemy.t("Page saved", name: @page.name)
    @while_page_edit = request.referer.include?("edit")

    unless @while_page_edit
      @tree = serialized_page_tree
    end
  else
    configure
  end
end

#visitObject


167
168
169
170
171
172
173
174
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 167

def visit
  @page.unlock!
  redirect_to show_page_url(
    urlname: @page.urlname,
    locale: prefix_locale? ? @page.language_code : nil,
    host: @page.site.host == "*" ? request.host : @page.site.host,
  )
end