require "uri" require "net/http" module OrbitBackendHelper def self.included(base) ActionView::Helpers::FormBuilder.send(:include, Orbit::FormBuilder) end def thead(field,center=false,enable_sort=true) sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s active = params[:sort].eql? sort order = active ? (["asc", "desc"]-[params[:order]]).first : "asc" arrow = (order.eql? "desc") ? "" : "" klass = field.eql?(:title) ? "span5" : "span2" th_data = (sort=="preview" || !enable_sort) ? t(field.to_sym) : "#{t(field.to_sym)} #{active ? arrow : ""}" "#{th_data}".html_safe end def datetime_picker(object_name, method, options = {}) options[:icon_time] ||= 'icons-clock' options[:icon_date] ||= 'icons-calendar' options[:icon_clear] ||= 'icons-cross-3' options[:input_class] ||= 'input-large' options[:new_record] = true if options[:new_record].nil? options[:value] ||= options[:object].send(method) if options[:object] && options[:object][method] case options[:picker_type] when 'date' content_tag :div, :id => options[:id], :class => options[:class] do date_picker(object_name, method, options) end when 'time' content_tag :div, :id => options[:id], :class => options[:class] do time_picker(object_name, method, options) end when 'separated' options[:label] ||= I18n.t('datetime_picker.separated.label') content_tag :div, :id => options[:id], :class => "separated_picker #{options[:class]}" do concat label_tag options[:label] unless options[:no_label] concat hidden_field(object_name, method, :value => options[:value]) concat separated_picker(object_name, method, options) end when 'simple' content_tag :div, :id => options[:id], :class => options[:class] do simple_picker(object_name, method, options) end else content_tag :div, :id => options[:id], :class => options[:class] do default_picker(object_name, method, options) end end end def default_picker(object_name, method, options) custom = {} custom[:format] = options[:format] || 'yyyy/MM/dd hh:mm' custom[:value] = format_value(options[:value], custom[:format]) if options[:value] && !options[:new_record] custom[:value] = "" if options[:new_record] custom[:picker_class] = 'default_picker' custom[:label] = options[:label] || I18n.t('datetime_picker.default.label') custom[:placeholder] = options[:placeholder] || I18n.t('datetime_picker.default.placeholder') picker(object_name, method, options.merge(custom)) end def picker(object_name, method, options) content_tag :div, :class => "#{options[:picker_class]} input-append", :style => "#{(options[:picker_class].eql?('time_picker') && options[:value].blank? && options[:separated]) ? 'pointer-events:none' : nil}" do concat label_tag options[:label] unless options[:no_label] concat text_field object_name, method, :placeholder => options[:placeholder], :class => options[:input_class], 'data-format' => options[:format], :value => options[:value], :data => options[:data] concat (content_tag :span, :class => 'add-on clearDate' do content_tag :i, nil, :class => options[:icon_clear] end) concat (content_tag :span, :class => 'add-on iconbtn' do content_tag :i, nil, 'data-time-icon' => options[:icon_time], 'data-date-icon' => options[:icon_date] end) end end def format_value(value, format = 'yyyy-MM-dd hh:mm') value.strftime(format.gsub('yyyy', '%Y').gsub('MM', '%m').gsub('dd', '%d').gsub('hh', '%H').gsub('mm', '%M')) rescue "" end def add_attribute(partial, f, attribute) new_object = f.object.send(attribute).build rescue nil 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 is_filter_active?(field, value) params[:filters][field].include?(value.to_s) ? "active" : "" rescue "" end def select_category(f, module_app) @user_authorized_categories = module_app.categories.enabled.authorized(current_user) if @user_authorized_categories.nil? render :partial => '/admin/categories/select_form', :locals => {:f=> f, :module_app=>module_app, :categories=> @user_authorized_categories } end def copy_to_all_language_button(language_tab, language_area) render :partial => '/shared/copy_to_all_language_button', :locals => {:language_area => language_area, :language_tab => language_tab} end def select_tags(f, module_app) render :partial => '/admin/tags/tag_form', :locals => {:f=> f, :module_app=>module_app, :tags=>module_app.tags } end def render_filter(fields, search_dom_id=nil, quick_new=false) render :partial => "shared/filter", :locals =>{:fields => fields, :search_dom_id=>search_dom_id, :quick_new=>quick_new} end def display_visitors(options={}) Impression.where(options).count end def display_visitors_today if (defined? @result_of_thirty_day).nil? display_visitors(created_at: {'$gte' => Time.now.beginning_of_day}) else @result_of_thirty_day[0]['count'] end end def display_visitors_this_week if (defined? @result_of_thirty_day).nil? display_visitors(created_at: {'$gte' => Time.now.beginning_of_week}) else @result_of_thirty_day[0..((Time.now-Time.now.beginning_of_week)/86400).floor].map{|v| v['count']}.reduce{|v,x| v+x} end end def display_visitors_this_month if (defined? @result_of_thirty_day).nil? visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do display_visitors(created_at: {'$gte' => Time.now.beginning_of_month}) end else @result_of_thirty_day[0..((Time.now-Time.now.beginning_of_month)/86400).floor].map{|v| v['count']}.reduce{|v,x| v+x} end visitors_this_month end def display_visitors_this_year visitors_this_year = Rails.cache.fetch("visitors_this_year", expires_in: 1.day) do display_visitors(created_at: {'$gte' => Time.now.beginning_of_year}) end visitors_this_year 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'] = [] @result_of_thirty_day = Impression.group_by(:created_at,30) trafic_result = @result_of_thirty_day.map do |v| date = v['_id'].values [Date.new(date[0],date[1],date[2]).to_time,v['count']] end #site.month_traffic_cache['result'] = trafic_result #site.month_traffic_cache['updated_at'] = Time.now #site.save #end #[:name=> t(:visitors_count),:data=>site.month_traffic_cache['result']] [:name=> t(:visitors_count),:data=>trafic_result] end def can_edit_or_delete?(obj) create_user = obj.create_user_id.to_s rescue nil if @user_authenticated_categories.first == "all" return true elsif obj.class == Page && current_user.is_manager?(ModuleApp.where(:key=>'page_content').first) return true elsif @current_user_is_sub_manager && !create_user.nil? create_user == current_user.id.to_s else @user_authenticated_categories.include?obj.category_id rescue (current_user.is_manager?(@module_app) rescue false) end end def user_authenticated_categories @user_authenticated_categories end def is_user_sub_manager? @current_user_is_sub_manager end def has_access? #@user_has_privileges comes from orbit_member_controller.. used just in members @user_has_privileges end def check_store_permissions #checks with the store if it has proper access and rights to access store store_permission = {} store_token = current_site.store_token rescue nil if !store_token.nil? params_to_send = {'store_token' => current_site.store_token} uri = URI.parse(OrbitStore::URL) http = Net::HTTP.new(uri.host,uri.port) http.read_timeout = 25 #seconds request = Net::HTTP::Get.new("/site/permissions") request.body = params_to_send.to_query response = http.request(request) rescue nil if response.nil? data = {} data["message"] = "Could not connect to the store." data["error"] = "CONNECTION_REFUSED" data["success"] = false else data = JSON.parse(response.body) rescue nil end if data.nil? data = {} data["message"] = "Could not connect to the store." data["error"] = "JSON_ERROR" data["success"] = false end if !data["success"] case data["error"] when "INVALID_SITE_TOKEN" current_site.store_token = nil current_site.save end store_permission["permission_granted"] = false store_permission["error"] = data["error"] store_permission["message"] = data["message"] else store_permission["permission_granted"] = true end else store_permission["permission_granted"] = false store_permission["error"] = "SITE_NOT_REGISTERED" store_permission["message"] = "Site not registered." end store_permission end def render_401 render "errors/401" end def render_403 render "errors/403" end def need_access_right render_401 if !has_access? end def get_referer_url referer_path = Rails.application.routes.recognize_path(request.referer) if referer_path[:controller]!="pages" or (referer_path[:controller]==params[:controller] and referer_path[:action]!="index") referer_url = '/'+params[:controller] else referer_url = request.referer end referer_url end end module Orbit::FormBuilder def datetime_picker(method, options = {}) @template.datetime_picker(@object_name, method, objectify_options(options)) end end