module ApplicationHelper Time.zone = ActiveSupport::TimeZone[+8.hours] FLASH_NOTICE_KEYS = [:error, :notice, :warning] def current_path(param_hash) request.referer.include?('?') ? [request.referer,param_hash.to_param].join('&') : [request.referer,param_hash.to_param].join('?') end def delayed_impressionist(object) # Resque.enqueue_at(1.minute.from_now,DelayedImpressionist,object) end def check_user_role_enable(attribute_fields) @user.attribute_values.collect{|t| attribute_fields.include?(t.attribute_field) }.include?(true) rescue false end def show_attribute_value(value) if value.kind_of? Hash result = [] value.each{|t| result.push(t.last)} result.join "," else value end end def create_guest_user u = nil User.without_callback(:create, :before, :initialize_desktop) do u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(99)}@example.com") end u.admin = false u.skip_confirmation! u.save(:validate => false) u end # if user is logged in, return current_user, else return guest_user def current_or_guest_user if current_user if session[:guest_user_id] User.find(session[:guest_user_id]).destroy rescue nil session[:guest_user_id] = nil end current_user else guest_user end end # find guest_user object associated with the current session, # creating one as needed def guest_user User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id]) 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(custom_class=nil) case custom_class when nil link_to t('back'), get_go_back, :class => 'nav' else link_to t('back'), get_go_back, :class => custom_class end 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) (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.page_title ? page.page_title : page.title if page.root? && @site.title res << @site.title elsif @site.title && !@site.title_always_on res << @site.title + ' - ' + page_title else res << page_title end res << "\n" end def page_stylesheets(page, edit=nil) orbit_bar_bootstrap = asset_path 'orbit_bar_bootstrap' orbit_bar_bootstrap = orbit_bar_bootstrap.include?('css') ? orbit_bar_bootstrap : orbit_bar_bootstrap+'.css' stylesheets = '' stylesheets << "\n" stylesheets << "\n" if page.design.css_reset # stylesheets << "\n" # stylesheets << "\n" stylesheets << "\n" stylesheets << "\n" if page.design.css_default theme = page.design.themes.detect{ |d| d.id == page.theme_id } stylesheets << "\n" if theme stylesheets end def page_javascripts(page, edit=nil) javascripts = '' # 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 def dislpay_view_count(object) "#{t(:view_count)}: #{Impression.where(:impressionable_id=>object.id).count}" end def display_visitors(options={}) Impression.where(options).count end def display_visitors_today display_visitors(created_at: {'$gte' => Time.now.beginning_of_day, '$lte' => Time.now}) end def display_visitors_this_week display_visitors(created_at: {'$gte' => Time.now-7.days, '$lte' => Time.now}) end def display_visitors_this_month display_visitors(created_at: {'$gte' => Time.now-30.days, '$lte' => Time.now}) end def display_visitors_this_year display_visitors(created_at: {'$gte' => Time.now-365.days, '$lte' => Time.now}) end def get_month_traffic site = Site.first if site.month_traffic_cache.blank? or (site.month_traffic_cache['updated_at'] < (Time.now-1.day) rescue true) site.month_traffic_cache = {} site.month_traffic_cache['result'] = [] (1..30).each do |i| visits = Impression.where( created_at: { '$gte' => Time.now.beginning_of_day-i.days, '$lte' => Time.now.end_of_day-i.days} ).count site.month_traffic_cache['result'].push([ Time.now.beginning_of_day-i.days, visits]) end site.month_traffic_cache['updated_at'] = Time.now site.save end [:name=> t(:visitors_count),:data=>site.month_traffic_cache['result']] end # def get_today_traffic # result = [] # (0..30).each do |i| # the_day = i.day.ago # visits = Impression.where( created_at: {'$gte' => the_day.beginning_of_day, '$lte' => the_day.end_of_day}).count # # result.push([ the_day.to_time.to_i, visits]) # result.push([ the_day.strftime("%m %d"), visits]) # # result.push({'x'=> i, 'y'=> visits}) # end # result # end def display_date_time(object) object.strftime("%Y-%m-%d %H:%M") end def display_date(object) object.strftime("%Y-%m-%d") end def display_time(object) object.strftime("%H:%M") end def share_links(object, key) content_tag :div, :class => 'fb' do concat social_share_button_tag(object.title, :fb_url => generate_fb_url(object,key), :image => "http://#{request.env['HTTP_HOST']}#{object.image_url}") end end def generate_fb_url(object,key) "http://#{request.env['HTTP_HOST']}/share/#{object.class.to_s.underscore}/#{object.id}?key=#{key}" end def wrap_string_with(str,options={}) line_width = options[:line_width] || 12 wrap_mark = options[:wrap_mark] || "
" str.scan((/.{0,#{line_width}}/)).join(wrap_mark) end def show_avatar(user) image_tag(user.avatar.thumb.url) end # NTU link def get_link(site_number) "http://#{request.host}:2#{site_number}00" end def sortable(column, title = nil, options = {}) options[:remote] ||= true title ||= column.titleize css_class = column.to_s == params[:sort] ? "current #{params[:direction]}" : 'asc' direction = column.to_s == params[:sort] && params[:direction] == 'asc' ? 'desc' : 'asc' link_to title, params.merge({direction: direction, sort: column, sort_options: column}), {:class => "#{css_class} sortable", :remote => options[:remote]} end end