forked from saurabh/orbit4-5
138 lines
5.4 KiB
Ruby
138 lines
5.4 KiB
Ruby
module OrbitBackendHelper
|
|
|
|
def self.included(base)
|
|
ActionView::Helpers::FormBuilder.send(:include, Orbit::FormBuilder)
|
|
end
|
|
|
|
def thead(field)
|
|
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") ? t(field.to_sym) : "<a href='?sort=#{sort}&order=#{order}''>#{t(field.to_sym)} #{active ? arrow : ""}</a>"
|
|
|
|
"<th class='#{klass} #{active ? "active" : ""}'>#{th_data}</th>".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[: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
|
|
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]
|
|
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]
|
|
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)
|
|
render :partial => '/admin/categories/select_form', :locals => {:f=> f, :module_app=>module_app, :categories=>module_app.categories.enabled }
|
|
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)
|
|
render :partial => "shared/filter", :locals =>{:fields => fields}
|
|
end
|
|
|
|
def display_visitors(options={})
|
|
Impression.where(options).distinct(:request_hash).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
|
|
result = []
|
|
(0..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}
|
|
).distinct(:request_hash).count
|
|
result.push([ Time.now.beginning_of_day-i.days, visits])
|
|
end
|
|
|
|
[:name=> t(:visitors_count),:data=>result]
|
|
end
|
|
|
|
end
|
|
|
|
module Orbit::FormBuilder
|
|
def datetime_picker(method, options = {})
|
|
@template.datetime_picker(@object_name, method, objectify_options(options))
|
|
end
|
|
end |