orbit-basic/app/controllers/admin/links_controller.rb

92 lines
2.2 KiB
Ruby
Raw Normal View History

class Admin::LinksController < Admin::ItemsController
def new
@item = Link.new
2012-02-17 06:54:11 +00:00
@item.parent = Page.find(params[:parent_id]) rescue nil
render layout: false
end
def edit
@item = Link.find(params[:id])
render layout: false
end
def create
@item = Link.new(params[:link])
if @item.save(params[:link])
success = true
else
success = check_valid_url
end
if success
2012-09-12 11:12:50 +00:00
flash.now[:notice] = t('create.success.link')
2011-05-13 01:08:42 +00:00
respond_to do |format|
format.js { render 'admin/items/reload_items' }
2011-05-13 01:08:42 +00:00
end
else
2012-09-12 11:12:50 +00:00
flash.now[:error] = t('create.error.link')
2011-05-13 01:08:42 +00:00
render :action => "new"
end
end
def update
@item = Link.find(params[:id])
2013-10-23 10:28:09 +00:00
unless @item.update_attributes(params[:link])
@error = error_messages_for(@item)
end
2013-10-23 10:28:09 +00:00
end
def get_link_setting_json
begin
link = Link.find(params[:id])
m = {}
m["public"] = link.is_published ? 1 : 0
m["link"] = @site_valid_locales.inject([]) do |result, locale|
result << [locale, (link.enabled_for && link.enabled_for.include?(locale)) ? 1 : 0, (link.menu_enabled_for && link.menu_enabled_for[locale]) ? 1 :0]
2011-05-13 01:08:42 +00:00
end
2013-10-23 10:28:09 +00:00
render json: JSON.pretty_generate(m)
rescue
render :json => {error: ''}, status: 422
end
end
protected
def check_valid_url
if @item.errors.include?(:url) && !@item.errors.added?(:url, :blank) && @item.errors.added?(:url, :invalid)
begin
url = @item.url
url.gsub!('http://', '').slice!(/^\//)
path = Rails.application.routes.recognize_path(url)
if path.has_key?(:page_name)
if Page.where(path: path[:page_name]).first
new_url = "#{request.base_url}/#{url}"
else
success = false
end
else
new_url = "#{request.base_url}/#{url}"
end
if @item.errors.count == 1
@item.url = new_url
if @item.save
success = true
else
success = false
end
else
@item.url = new_url
success = false
end unless success == false
rescue
success = false
end
else
success = false
end
success
end
2011-05-13 01:59:29 +00:00
end