module ApplicationHelper FLASH_NOTICE_KEYS = [:error, :notice, :warning] def colorize_in_use_locale(locale) @site_in_use_locales.include?(locale)? 'green' : 'red' end def flash_messages return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)} formatted_messages = messages.map do |type| content_tag :div, :class => type.to_s do message_for_item(flash[type], flash["#{type}_item".to_sym]) end end raw(formatted_messages.join) end def link_back link_to t('back'), get_go_back, :class => 'nav' end # Clean the link back def get_go_back begin if request.url.include?('locale=') session[:last_page] else session[:last_page] = remove_locale(request.referer) end rescue eval(params[:controller].split('/').join('_') << '_url') end end # Remove the locale but keep all the other params def remove_locale(url) target = url.split('?') vars = target[1].split('&') rescue [] vars.delete_if {|var| var.include? 'locale=' } if vars.size > 0 target[0].to_s + '?' + vars.join('&') else target[0].to_s end end def add_locale(url, locale) if url.include?('?') url + "&locale=#{locale}" else url + "?locale=#{locale}" end end def message_for_item(message, item = nil) if item.is_a?(Array) message % link_to(*item) else message % item end end def add_attribute(partial, f, attribute) new_object = f.object.send(attribute).build fields = f.fields_for(attribute, new_object, :child_index => "new_#{attribute}") do |f| render :partial => partial, :object => new_object, :locals => {:f => f} end end def active_for_ob_auths_object(object_class,field = :object_auth_id) unless active_for_action("object_auths_new_interface","setting").nil? ob_auth = ObjectAuth.find params[field] ob_auth.obj_authable_type == object_class.to_s ? 'active' : nil end end def active_for_ob_auth(ob_auth_title,field = :object_auth_id) unless active_for_action("module_apps_new_interface","setting").nil? oa_auth = ObjectAuth.find params[field] oa_auth.title == ob_auth_title ? 'active' : nil end end def active_for_app_auth(app_title ='', opt={:controller_name => 'module_apps_new_interface',:action_name=>'setting',:field => :module_app_id}) unless active_for_action(opt[:controller_name],opt[:action_name]).nil? app = ModuleApp.find params[opt[:field]] app.title == app_title ? 'active' : nil else nil end end def active_for_controllers(*controller_names) (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? 'active' : nil end def visible_for_controllers(*controller_names) puts controller_names (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? '' : 'hide' end def active_for_action(controller_name, action_name) ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil end def page_metas(page) tmp_meta = {} metas = '' @site.site_metas.each do |meta| name, content = meta.get_name_content tmp_meta.merge!(name => content) end rescue nil page.page_metas.each do |meta| name, content = meta.get_name_content tmp_meta.merge!(name => content) end rescue nil tmp_meta.each_pair{|name, content| metas << "\n" } if !tmp_meta.blank? metas end def page_title(page) res = "" page_title = page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale] if page.root? && @site.title res << @site.title[I18n.locale] elsif @site.title && @site.title_always_on res << @site.title[I18n.locale] + ' - ' + page_title else res << page_title end res << "\n" end def page_stylesheets(page, edit=nil) stylesheets = '' unless edit stylesheets << "\n" stylesheets << "\n" stylesheets << "\n" end stylesheets << "\n" if page.design.reset_css stylesheets << "\n" stylesheets << "\n" if page.design.default_css theme = page.design.themes.detect{ |d| d.id == page.theme_id } stylesheets << "\n" if theme stylesheets end def page_javascripts(page, edit=nil) javascripts = '' unless edit javascripts << "\n" javascripts << "\n" javascripts << "\n" javascripts << "\n" javascripts << "\n" javascripts << "\n" javascripts << "\n" end javascripts << "\n" javascripts << "\n" javascripts << "\n" page.design.javascripts.each do |js| # javascripts << "" end javascripts end def active_when_current_locale_eq(locale) locale.to_sym == I18n.locale ? 'active in': '' end def at_least_module_manager is_manager? || is_admin? end end