announcement-test/temp_file/app/helpers/orbit_backend_helper.rb

251 lines
9.1 KiB
Ruby
Raw Normal View History

require "uri"
require "net/http"
module OrbitBackendHelper
def self.included(base)
2020-03-31 05:55:11 +00:00
ActionView::Helpers::FormBuilder.send(:include,OrbitFormHelper)
end
def group_impression_by_day(field,day_limit,start_day=Time.now,format = 'day')
2020-03-31 05:55:11 +00:00
key_op = [['year','$year'],['month', '$month'], ['day', '$dayOfMonth']]
key_op = key_op.take(1 + key_op.find_index { |key, op| format == key })
project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten]
group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten]
pipeline = [
{"$match"=> {"created_at" => {"$gte" => (start_day.to_date-(day_limit-2).days rescue ''), "$lte" => start_day}}},
2020-03-31 05:55:11 +00:00
{"$project" => {field => 1}.merge(project_date_fields)},
{"$group" => {"_id" => group_id_fields,"count" => {"$sum" => 1}}},
{"$sort" => {"created_at"=>-1}}
2020-03-31 05:55:11 +00:00
]
2022-10-04 05:27:32 +00:00
tmp = Impression.collection.aggregate(pipeline).to_a
2020-03-31 05:55:11 +00:00
if tmp.count < day_limit
tmp1 = tmp
tmp = (0...day_limit).collect do |i|
d = start_day - i.days
d_year = d.year
d_month = d.month
d_day = d.day
count = tmp1.select do |v|
n_date = v['_id']
n_date['year']==d_year && n_date['month']==d_month && n_date['day']==d_day
end[0]['count'] rescue 0
{'_id'=>{'year'=>d_year,'month'=>d_month,'day'=>d_day},'count'=>count}
end
end
tmp
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") ? "<b class='icons-arrow-up-3'></b>" : "<b class='icons-arrow-down-4'></b>"
klass = field.eql?(:title) ? "span5" : "span2"
th_data = (sort=="preview" || !enable_sort) ? t(field.to_sym) : "<a href='?sort=#{sort}&order=#{order}'>#{t(field.to_sym)} #{active ? arrow : ""}</a>"
"<th class='#{klass} #{active ? "active" : ""}' style='#{center ? "text-align:center" : ""}'>#{th_data}</th>".html_safe
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
2020-03-03 12:15:17 +00:00
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
2020-03-03 12:15:17 +00:00
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
2020-03-03 12:15:17 +00:00
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
2020-03-03 13:27:28 +00:00
visitors_this_month
2020-03-03 12:15:17 +00:00
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
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'] = []
2020-03-31 05:55:11 +00:00
@result_of_thirty_day = group_impression_by_day(:created_at,30)
2020-03-03 12:15:17 +00:00
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?
return ( @user_authenticated_categories.include?(obj.category_id) rescue (create_user == current_user.id.to_s))
else
2022-10-04 05:27:32 +00:00
tmp = false
if @changed_module_app
if obj.class == Page
if obj.tmp_root_page_id
obj = Page.find(obj.tmp_root_page_id)
end
if obj.bind_model.present?
new_obj = obj.bind_model.constantize.where(:uid=>obj.bind_uid).first rescue nil
obj = new_obj if new_obj
end
end
tmp = @user_authenticated_categories.include?obj.category_id rescue (current_user.is_manager?(@changed_module_app) rescue false)
else
tmp = @user_authenticated_categories.include?obj.category_id rescue (current_user.is_manager?(@module_app) rescue false)
end
tmp
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 = {}
2022-10-04 05:27:32 +00:00
store_token = @site.store_token rescue nil
if !store_token.nil?
2022-10-04 05:27:32 +00:00
params_to_send = {'store_token' => @site.store_token}
uri = URI.parse(OrbitStore::URL)
http = Net::HTTP.new(uri.host,uri.port)
http.read_timeout =1 #seconds
request = Net::HTTP::Get.new("/site/permissions")
request.body = params_to_send.to_query
2020-03-16 08:39:51 +00:00
http.open_timeout = 1 #set read_timeout to 1 second to avoid web die caused by no response
http.ssl_timeout = 1
begin
response = http.request(request)
rescue
response = ActionDispatch::Response.new
response.body = {'success'=>true}.to_json
end
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"
2022-10-04 05:27:32 +00:00
@site.store_token = nil
@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