class Manage::CmsPagesController < Manage::ApplicationController
include ActionController::Caching::Pages
self.page_cache_directory = "#{Rails.root}/public"
before_action :check_permissions
before_action :convert_invalid_chars_in_params
upload_status_for :receive_image
upload_status_for :receive_gallery
upload_status_for :add_to_gallery
cache_sweeper :cms_content_sweeper
def index
@page_levels = [ '' ].concat((params[:path] || session[:cms_pages_path] || '').split('/').reject { |l| l.blank? })
@page_levels << ''
@path = ''
@page = nil
end
def new
validate_user_access or return
@pg ||= CmsPage.new
edit_page
end
def list_pages
@page_level = params[:level].to_i
@parent = CmsPage.find_by_id(params[:parent_id])
if @page_level == 0
render :partial => 'list_page', :locals => { :list_page => CmsPage.find(1) } and return
else
if @parent
@pages = @parent.children
session[:cms_pages_path] = @parent.path
else
@pages = nil
end
end
render :partial => 'list_pages'
end
def edit_page
@pg = CmsPage.find_by_id(params[:id])
validate_user_access or return
@pg ||= CmsPage.new
@parent = @pg.parent || CmsPage.find_by_id(params[:parent_id])
if @parent
@pg.parent ||= @parent
@pg.template ||= @parent.template
end
@attrs = CmsPageObject.where(obj_type: 'attribute').pluck(:name).uniq.sort
@taglist = CmsPageTag.pluck(:name).uniq.sort
if params[:mode] == 'ajax_new' || params[:mode] == 'ajax_edit'
@pg.published_version = -1 if params[:mode] == 'ajax_new'
load_page_objects
load_template_options
render :partial => 'edit_page' and return
end
if request.post?
params[:pg] ||= {}
if params[:pg][:article_date_year]
params[:pg][:article_date] = Time.zone.parse("#{params[:pg].delete(:article_date_year)}-#{params[:pg].delete(:article_date_month)}-#{params[:pg].delete(:article_date_day)}")
end
if params[:pg][:article_end_date_year]
params[:pg][:article_end_date] = Time.zone.parse("#{params[:pg].delete(:article_end_date_year)}-#{params[:pg].delete(:article_end_date_month)}-#{params[:pg].delete(:article_end_date_day)}")
end
if params[:pg][:published_date_year]
params[:pg][:published_date] = Time.zone.parse("#{params[:pg].delete(:published_date_year)}-#{params[:pg].delete(:published_date_month)}-#{params[:pg].delete(:published_date_day)}")
end
if params[:pg][:expires]
date = Time.zone.parse("#{params[:pg].delete(:expiration_date_year)}-#{params[:pg].delete(:expiration_date_month)}-#{params[:pg].delete(:expiration_date_day)}")
params[:pg][:expiration_date] = date if params[:pg][:expires] == 'true'
end
@pg.assign_attributes(cms_page_params)
unless params[:use_article_date_range].to_i > 0
@pg.article_end_date = nil
end
@pg.updated_by ||= session[:user_id]
@pg.updated_by_username ||= session[:user_username]
@pg.published_version = 0 if @pg.respond_to?(:redirect_enabled) && @pg.redirect_enabled
if @pg.send(@pg.new_record? ? :save : :save_without_revision)
# now try to save tags
existing_tags = @pg.tags.map(&:name)
tags_to_delete = [] ; @pg.tags.each { |t| tags_to_delete << t }
params[:tags].split(',').map(&:strip).reject(&:blank?).each do |t|
if existing_tags.include?(t)
# still in use, don't delete
tags_to_delete = tags_to_delete.reject { |tag| tag.name == t }
else
# doesn't exist, create
@pg.tags.create(name: t)
end
end
tags_to_delete.each { |t| t.destroy }
# now try to save page objects (just attributes in this case)
objects_to_delete = @pg.objects.where("obj_type = 'attribute' or obj_type = 'option'").to_a
params[:page_objects].to_unsafe_h.each do |key, val|
next if val.blank?
if key =~ /^obj-(\w+?)-(.+?)$/
obj = @pg.objects.where(name: $2, obj_type: $1).first
obj ||= @pg.objects.build(name: $2, obj_type: $1)
obj.content = val
obj.save!
objects_to_delete.reject! { |obj| obj.name == $2 }
end
end if params[:page_objects]
objects_to_delete.each { |t| t.destroy }
case params[:return_to]
when 'preview'
render :update do |page|
page.redirect_to "#{@pg.path.blank? ? '' : '/' + @pg.path}/version/#{@pg.published_version > 0 ? @pg.published_version : @pg.version}"
end
else
flash[:notice] = 'Page saved.'
session[:cms_pages_path] = @pg.path
render :update do |page|
page.redirect_to action: 'index'
end
end
else
# save failed, display errors
logger.error "Save failed: #{CmsPage.without_revision { @pg.save }} #{@pg.errors.full_messages.join('; ')}"
render :update do |page|
page.replace_html 'save_errors', @pg.errors.full_messages.join('
')
page << "try { $('btn_next').disabled = false; } catch (e) {}"
page << "try { $('btn_finish').disabled = false; } catch (e) {}"
page << "try { $('btn_save').disabled = false; $('btn_save').value = 'Save'; } catch (e) {}"
end and return
end
end
end
def delete_page
@pg = CmsPage.find_by_id(params[:id])
if !@pg
flash[:error] = "Sorry, couldn't find the requested page."
elsif @pg.children.size > 0
flash[:error] = "This page contains other pages. Please delete those first if you are sure you want to delete this page."
elsif @pg.id == 1
flash[:error] = "You cannot delete the home page."
else
flash[:notice] = "Page deleted."
session[:cms_pages_path] = @pg.parent.path rescue nil
@pg.destroy
end
redirect_to action: 'index'
end
def select_page
@page_levels = [ '' ].concat((params[:path].blank? ? session[:cms_pages_path] : params[:path]).to_s.split('/').reject { |l| l.blank? })
@page_levels << ''
@path = ''
@page = nil
render :layout => false
end
def list_pages_select
@page_level = params[:level].to_i
@parent = CmsPage.find_by_id(params[:parent_id])
if @page_level == 0
render :partial => 'list_page_select', :locals => { :list_page_select => CmsPage.find(1) } and return
else
if @parent
@pages = @parent.children
session[:cms_pages_path] = @parent.path
else
@pages = nil
end
end
render :partial => 'list_pages_select'
end
def show_template_options
@pg = CmsPage.find_by_id(params[:id]) || CmsPage.new
@pg.cms_template_id = params[:template_id]
load_page_objects
load_template_options
render partial: 'template_options'
end
def page_attribute
render :nothing => true and return unless params[:name]
@page_objects = OpenStruct.new({ params[:name] => params[:value] })
render :partial => 'page_attribute', :locals => { :name => params[:name] }
end
def edit_page_content
@pg = CmsPage.find(params[:id])
validate_user_access or return
@page_objects = params[:page_objects] ? OpenStruct.new(params[:page_objects].to_unsafe_h) : OpenStruct.new
if request.get?
@pg.version = params[:version] if params[:version] && params[:version].to_i != @pg.version
@pg.objects.where(cms_page_version: @pg.version).each do |obj|
key = "obj-#{obj.obj_type.to_s}-#{obj.name}"
@page_objects[key] = obj.content.html_safe
end
# set "legacy" vars
@content_levels = @pg.path.split('/')
params[:section] = @content_levels.size < 1 ? '' : @content_levels.first
params[:subsection] = @content_levels[1] unless @content_levels.size < 3
if @content_levels.size == 1
params[:page] = 'index'
elsif @content_levels.size > 1
params[:page] = @content_levels.last
end
@page_title = @pg.title
@cms_head ||= ''
@taglist = CmsPageTag.pluck(:name).uniq.sort
@template_content = substitute_placeholders(@pg.template.content, @pg)
render layout: 'application'
elsif request.post?
CmsPage.transaction do
# need to revise this later if we implement deletion of page objects
old_objs = @pg.objects.where(cms_page_version: @pg.version).to_a
@pg.updated_by = session[:user_id]
@pg.updated_by_username = session[:user_username]
# if basic user, make sure published version is not set to 'latest'
if (UseCmsAccessLevels && !user_has_permission?(:manage_cms_full_access)) && @pg.published_version == 0
@pg.published_version = @pg.version
end
@pg.updated_on = Time.now.utc
@pg.save # create a new version for new page objects to reference
# do a little bit of classification... for now, just identify page lists
page_lists = []
@page_objects.each do |key,val|
key =~ /^obj-(\w+?)-(\w+?)-sources-tag-count$/
if $1 == 'page_list'
page_lists << "obj-#{$1}-#{$2}"
end
end
# run through page lists and do a little housekeeping
page_lists.each do |key|
# optimize source lists: tags
if @page_objects["#{key}-sources-tag-count"].to_i > 0
tags = []
@page_objects["#{key}-sources-tag-count"].to_i.times do |i|
logger.debug "Adding tag: #{@page_objects["#{key}-sources-tag#{i}"]}"
tags << @page_objects["#{key}-sources-tag#{i}"]
end
tags.reject! { |tag| tag.blank? }
@page_objects["#{key}-sources-tag-count"] = tags.size
tags.each_with_index do |tag, i|
@page_objects["#{key}-sources-tag#{i}"] = tag
end
end
# optimize source lists: folders
if @page_objects["#{key}-sources-folder-count"].to_i > 0
folders = []
@page_objects["#{key}-sources-folder-count"].to_i.times do |i|
logger.debug "Adding folder: #{@page_objects["#{key}-sources-folder#{i}"]}"
folders << @page_objects["#{key}-sources-folder#{i}"]
end
folders.reject! { |folder| folder.blank? }
@page_objects["#{key}-sources-folder-count"] = folders.size
folders.each_with_index do |folder, i|
@page_objects["#{key}-sources-folder#{i}"] = folder
end
end
# consolidate date picker fields
if @page_objects["#{key}-date-range-custom-start_year"]
@page_objects["#{key}-date-range-custom-start"] =
Time.utc(@page_objects.delete("#{key}-date-range-custom-start_year"),
@page_objects.delete("#{key}-date-range-custom-start_month"),
@page_objects.delete("#{key}-date-range-custom-start_day"))
end
if @page_objects["#{key}-date-range-custom-end_year"]
@page_objects["#{key}-date-range-custom-end"] =
Time.utc(@page_objects.delete("#{key}-date-range-custom-end_year"),
@page_objects.delete("#{key}-date-range-custom-end_month"),
@page_objects.delete("#{key}-date-range-custom-end_day"))
end
end
@page_objects.to_h.each do |key,val|
key =~ /^obj-(\w+?)-(.+?)$/
obj = @pg.objects.build(name: $2, obj_type: $1)
# do a little bit of "censorship" to fix up Word pastes and strange things from the editor
if val.is_a?(String)
# all meta and link tags
val.gsub!(/(<\/?)(meta|link)(.*?)>/m, '')
# the dreaded MsoNormal
val.gsub!(' class="MsoNormal"', '')
# remove all font-family/font-size css styles, as well as font tags
val.gsub!(/font-(?:family|size):.*?(;|")/, '\3')
val.gsub!(/(.*?)<\/font>/, '\1')
# strange conditional IE stuff
val.gsub!(//m, '')
# not even sure what these are supposed to be
val.gsub!(/(.*?)<\/xml>/m, '')
# images pointing to the local drive??
val.gsub!(/(.*?)<\/span>/m, '\1')
val.gsub!(' style="(?:;*)"', '')
val.gsub!('', '')
# we could try to catch all strange ms tags...
#val.gsub!(/(<\/?)(?:o|u1):p>/, '\1p>')
#val.gsub!(/(<\/?)(?:st1):(.*?)>/, '')
# but it's easier just remove all tags with colons in them
val.gsub!(/(<\/?)[\w\d]+:[\w\d]+(.*?)>/, '')
### other non-ms word specific stuff ###
# pirate styles not welcome
val.gsub!(/