2020-02-01 03:47:52 +00:00
module AnnouncementsHelper
2020-07-30 14:12:21 +00:00
def complementaryColor ( my_hex )
if my_hex [ 0 ] == '#'
my_hex = my_hex [ 1 .. - 1 ]
end
rgb = my_hex . split ( / / ) . each_slice ( my_hex . length / 3 ) . map { | v | v . join }
comp = rgb . map { | a | ( 255 - a . to_i ( 16 ) ) . to_s ( 16 ) . rjust ( 2 , '0' ) }
'#' + comp . join
end
def lighten_color ( my_hex , percent )
if my_hex [ 0 ] == '#'
my_hex = my_hex [ 1 .. - 1 ]
end
rgb = my_hex . split ( / / ) . each_slice ( my_hex . length / 3 ) . map { | v | v . join }
comp = rgb . collect do | a |
tmp = a . to_i ( 16 ) * ( 1 + percent / 100 . 0 )
tmp = 255 if tmp > 255
tmp = 0 if tmp < 0
tmp . to_i . to_s ( 16 ) . rjust ( 2 , '0' )
end
'#' + comp . join
end
2020-03-01 07:49:01 +00:00
def set_image_version_for_widget
subpart = OrbitHelper . get_current_widget
@image_version = 'thumb'
if subpart . methods . include? 'select_options' . to_sym
ModuleApp . all . select { | tmp | tmp . key . to_s == 'announcement' } . each do | modile_app |
@show_options = modile_app . show_options rescue nil
end
subpart . select_options . each do | select_option |
if ! ( @show_options . nil? ) && select_option . field_name == @show_options . keys . first . to_s
value = YAML . load ( select_option . value )
2020-04-25 04:35:30 +00:00
tmp = value [ :en ]
I18n . with_locale ( :en ) do
if tmp == t ( 'announcement.small_size' )
@image_version = 'thumb'
elsif tmp == t ( 'announcement.medium_size' )
@image_version = 'mobile'
elsif tmp == t ( 'announcement.orignal_size' )
@image_version = 'orignal'
end
2020-03-01 07:49:01 +00:00
end
end
end
end
end
2021-04-09 09:45:58 +00:00
def data_to_human_type ( a , set_tag_ids = nil )
2020-03-01 07:49:01 +00:00
statuses = a . statuses_with_classname . collect do | status |
{
" status " = > status [ " name " ] ,
" status-class " = > " status- #{ status [ 'classname' ] } "
}
end
files = a . bulletin_files . map { | file | { " file_url " = > file . file . url , " file_title " = > ( file . title . blank? ? File . basename ( file . file . path ) : file . title rescue '' ) } if file . enabled_for? ( locale ) } rescue [ ]
files . delete ( nil )
links = a . bulletin_links . map { | link | { " link_url " = > link . url , " link_title " = > ( link . title . blank? ? link . url : link . title ) } } rescue [ ]
author = User . find ( a . create_user_id ) . member_profile . name rescue " "
desc = a . image_description
desc = ( desc . nil? || desc == " " ? " announcement image " : desc )
2020-05-19 04:31:57 +00:00
link_to_show = ( a . is_external_link? ? a . external_link : OrbitHelper . widget_item_url ( a . to_param ) ) rescue " "
2020-03-01 07:49:01 +00:00
target = a . is_external_link ? " _blank " : " _self "
if @image_version == 'thumb'
image_url = a . image . thumb . url
elsif @image_version == 'mobile'
image_url = a . image . mobile . url
else
image_url = a . image . url
end
{
" bulletin_links " = > links ,
" bulletin_files " = > files ,
" title " = > a . title ,
" source-site " = > " " ,
" source-site-title " = > " " ,
" source-site-link " = > " " ,
" subtitle " = > a . subtitle ,
" statuses " = > statuses ,
" category " = > a . category . title ,
2021-04-09 09:45:58 +00:00
" tag_ids " = > ( set_tag_ids . nil? ? ( a . tag_ids . map { | id | id . to_s } . to_s . gsub ( '"' , " ' " ) rescue '[]' ) : set_tag_ids ) ,
2020-03-01 07:49:01 +00:00
" postdate " = > a . postdate ,
" author " = > author ,
" link_to_show " = > link_to_show ,
" target " = > target ,
" img_src " = > image_url || " /assets/announcement-default.jpg " ,
" img_description " = > desc
}
end
2020-04-05 14:38:26 +00:00
def get_feed_annc ( type , site_source , locale )
ma_key = 'announcement'
if type == " index "
2020-04-08 12:55:16 +00:00
categories = Array ( OrbitHelper . page_categories )
2020-04-05 14:38:26 +00:00
elsif type == " widget "
categories = Array ( OrbitHelper . widget_categories )
else
categories = [ ]
end
if categories . include? ( " all " )
feeds = SiteFeedAnnc . where ( :channel_key = > ma_key )
else
feeds = SiteFeedAnnc . where ( :channel_key = > ma_key , :merge_with_category . in = > categories )
end
if feeds . count > 0
temp_ids = [ ]
data = feeds . collect do | feed |
2020-04-10 02:13:23 +00:00
feed . all_contents_for_feed ( site_source , locale , type == 'widget' )
2020-04-05 14:38:26 +00:00
end . flatten . compact
else
data = [ ]
end
data
end
2020-02-01 03:47:52 +00:00
def get_feed_announcements ( type , site_source = nil )
locale = OrbitHelper . get_site_locale . to_s
2020-04-05 14:38:26 +00:00
if ! ( defined? SiteFeedAnnc ) . nil?
fans = get_feed_annc ( type , site_source , locale )
else
feed_anns = OrbitHelper . get_feed_for_module ( type )
fans = [ ]
feed_anns . each do | fa |
next if ! site_source . nil? && site_source != fa [ " source-site-title " ]
status = {
" status " = > " <a href=' #{ fa [ " source-site " ] } ' target='_blank' class='feed-source'> #{ fa [ " source-site-title " ] } </a> " ,
" status-class " = > " status-source "
}
2020-02-01 03:47:52 +00:00
2020-04-05 14:38:26 +00:00
files = fa [ " bulletin_files " ] . collect { | bf | { " file_url " = > bf [ " url " ] , " file_title " = > ( fa [ " title_translations " ] [ locale ] . blank? ? File . basename ( fa [ " url " ] ) : fa [ " title_translations " ] [ locale ] rescue '' ) } } rescue [ ]
links = fa [ " bulletin_links " ] . map { | link | { " link_url " = > link [ " url " ] , " link_title " = > ( link [ " title_translations " ] [ locale ] . blank? ? link [ " url " ] : link [ " title_translations " ] [ locale ] ) } } rescue [ ]
2020-02-01 03:47:52 +00:00
2020-04-05 14:38:26 +00:00
x = {
" bulletin_links " = > links ,
" bulletin_files " = > files ,
" title " = > fa [ " title_translations " ] [ locale ] ,
" subtitle " = > fa [ " subtitle_translations " ] [ locale ] ,
" statuses " = > [ status ] ,
" category " = > fa [ " category " ] ,
" postdate " = > fa [ " postdate " ] ,
" author " = > fa [ " author " ] ,
" source-site " = > " <a href=' #{ fa [ " source-site " ] } ' target='_blank' class='feed-source'> #{ fa [ " source-site-title " ] } </a> " ,
" source-site-title " = > fa [ " source-site-title " ] ,
" source-site-link " = > fa [ " source-site " ] ,
" link_to_show " = > OrbitHelper . url_to_show ( fa [ " params " ] ) ,
" target " = > " _self " ,
" img_src " = > fa [ " image " ] [ " thumb " ] || " /assets/announcement-default.jpg " ,
" img_description " = > fa [ " image_description_translations " ] [ locale ] ,
" more " = > t ( :more_plus ) ,
" view_count " = > " "
}
if ( ! x [ " title " ] . empty? rescue false )
fans << x
end
2020-02-01 03:47:52 +00:00
end
end
fans
end
2020-05-20 13:42:13 +00:00
def filter_by_keywords ( sorted , keywords , stime , etime )
kflag = keywords . blank?
sflag = stime . blank?
eflag = etime . blank?
stime = stime . to_s . split ( '/' )
stime = Time . zone . local ( * stime ) rescue nil
etime = etime . to_s . split ( '/' )
etime = Time . zone . local ( * etime ) rescue nil
if ! kflag || ! sflag || ! eflag
2020-03-02 10:09:55 +00:00
sorted . select { | anns |
2020-05-20 13:42:13 +00:00
if kflag
flag = true
2020-03-02 10:09:55 +00:00
else
2020-05-20 13:42:13 +00:00
if anns [ " source-site " ] . present?
title = Nokogiri :: HTML ( anns [ " title " ] . to_s ) . text
else
title = Nokogiri :: HTML ( anns . title . to_s ) . text
end
flag = title . include? ( keywords . to_s )
end
if sflag && ! eflag
flag = flag && ( anns . postdate < = etime )
elsif ! sflag && eflag
flag = flag && ( anns . postdate > = stime )
elsif ! sflag && ! eflag
flag = flag && ( anns . postdate > = stime ) && ( anns . postdate < = etime )
2020-03-02 10:09:55 +00:00
end
2020-05-20 13:42:13 +00:00
flag
2020-03-02 10:09:55 +00:00
}
else
sorted
end
2020-02-01 03:47:52 +00:00
end
2020-04-25 04:35:30 +00:00
def get_sorted_annc ( data_count = nil )
2020-02-01 03:47:52 +00:00
params = OrbitHelper . params
locale = OrbitHelper . get_site_locale . to_s
2020-03-01 07:49:01 +00:00
page_number = OrbitHelper . page_number . to_i
page_number = 1 if page_number == 0
2020-04-25 04:35:30 +00:00
page_data_count = data_count || OrbitHelper . page_data_count . to_i
2020-02-01 03:47:52 +00:00
feeds_anns = [ ]
2020-05-19 04:31:57 +00:00
page = Page . where ( url : params [ 'url' ] ) . first
if @type == " show_widget "
tags = @tags
categories = @categories
else
tags = page . tags
2020-12-16 09:33:52 +00:00
tags = params [ :tags ] if params [ :tags ] . present?
2021-04-08 03:46:02 +00:00
categories = params [ 'category' ] == 'all' ? ( page . categories || [ ] ) : ( Array ( params [ 'category' ] ) rescue ( page . categories || [ ] ) )
2020-05-19 04:31:57 +00:00
end
2020-03-01 07:49:01 +00:00
if ! params [ " source " ] . present?
2020-05-19 04:31:57 +00:00
if @type == " show_widget "
if params [ :uids ] . blank?
announcements = Bulletin . where ( :title . nin = > [ " " , nil ] , :is_preview . in = > [ false , nil ] )
2020-03-01 07:49:01 +00:00
. can_display_and_sorted . is_approved
. filter_by_categories ( categories , false ) . filter_by_tags ( tags ) . to_a
2020-05-19 04:31:57 +00:00
else
member_prfile = MemberProfile . any_in ( :uid = > params [ :uids ] )
user_ids = member_prfile . map { | m | m . user . id rescue nil } . select { | id | ! id . nil? }
announcements = Bulletin . where ( :title . nin = > [ " " , nil ] , :is_preview . in = > [ false , nil ] , :create_user_id . in = > user_ids )
. can_display_and_sorted . is_approved
. filter_by_categories ( categories , false ) . filter_by_tags ( tags ) . to_a
end
else
announcements = Bulletin . where ( :title . nin = > [ " " , nil ] , :is_preview . in = > [ false , nil ] )
. can_display_and_sorted . is_approved
. filter_by_categories ( categories , false ) . filter_by_tags ( tags ) . to_a
end
2020-03-17 02:32:51 +00:00
if ! ( defined? SiteFeed ) . nil?
2020-05-19 04:31:57 +00:00
if @type != " show_widget "
feeds_anns = get_feed_announcements ( " index " )
else
feeds_anns = [ ]
end
2020-02-01 03:47:52 +00:00
end
2020-03-01 07:49:01 +00:00
else
announcements = [ ]
2020-05-19 04:31:57 +00:00
if @type != " show_widget "
feeds_anns = get_feed_announcements ( " index " , params [ " source " ] )
else
feeds_anns = [ ]
end
2020-02-01 03:47:52 +00:00
end
if ! feeds_anns . blank?
2020-03-01 07:49:01 +00:00
if announcements . count != 0
2020-04-05 14:38:26 +00:00
top_anns = announcements . select { | v | v . is_top } + feeds_anns . select { | v | v [ 'is_top' ] }
rest_all_anns = feeds_anns . select { | v | v [ 'is_top' ] != true } + announcements . select { | v | ! v . is_top }
2020-03-17 01:26:34 +00:00
rest_anns = rest_all_anns . sort { | v1 , v2 | v2 [ " postdate " ] < = > v1 [ " postdate " ] }
2020-04-05 14:38:26 +00:00
all_sorted = top_anns . sort { | v1 , v2 | v2 [ " postdate " ] < = > v1 [ " postdate " ] } + rest_anns
2020-03-01 07:49:01 +00:00
else
2020-04-08 12:55:16 +00:00
all_sorted = feeds_anns . select { | v | v [ 'is_top' ] } . sort { | v1 , v2 | v2 [ " postdate " ] < = > v1 [ " postdate " ] } + feeds_anns . select { | v | v [ 'is_top' ] != true } . sort { | v1 , v2 | v2 [ " postdate " ] < = > v1 [ " postdate " ] }
2020-02-01 03:47:52 +00:00
end
2020-06-08 16:07:35 +00:00
all_filter = filter_by_keywords ( all_sorted , params [ :keywords ] , params [ :stime ] , params [ :etime ] )
2020-02-01 03:47:52 +00:00
else
2020-05-20 13:42:13 +00:00
all_filter = filter_by_keywords ( announcements , params [ :keywords ] , params [ :stime ] , params [ :etime ] )
2020-02-01 03:47:52 +00:00
end
2020-03-01 07:49:01 +00:00
if page_data_count != 0
sorted = all_filter [ ( page_number - 1 ) * page_data_count ... page_number * page_data_count ]
else
sorted = all_filter
2020-02-28 04:18:43 +00:00
end
2020-03-01 07:49:01 +00:00
annc_count = all_filter . count
total_pages = page_data_count == 0 ? 1 : ( annc_count . to_f / page_data_count ) . ceil
[ sorted , total_pages ]
2020-02-01 03:47:52 +00:00
end
2020-05-19 04:31:57 +00:00
def render_view_for_annc ( overridehtml = nil )
@key = Site . first . template
def render_link_to_edit ( html , url_to_edit )
if html . scan ( " {{link_to_edit}} " ) . length == 0
html = url_to_edit . blank? ? html : html + " <p class='admin-edit text-right'><a class='btn btn-primary' href=' #{ url_to_edit } '><i class='icon-edit'></i> #{ t ( :edit ) } </a></p> "
else
html = url_to_edit . blank? ? html . gsub ( " {{link_to_edit}} " , " " ) : html . gsub ( " {{link_to_edit}} " , " <p class='admin-edit text-right'><a class='btn btn-primary' href=' #{ url_to_edit } '><i class='icon-edit'></i> #{ t ( :edit ) } </a></p> " )
end
return html
end
def parsing_repeats_again ( elements , d , level )
newhtml = [ ]
oldhtml = [ ]
elements . each do | el |
html_to_render = " "
data_name = el . attr ( " data-list " )
wrap_elements = el . css ( " *[data-list][data-level=' #{ level } '] " )
if d [ data_name ]
d [ data_name ] . each_with_index do | item , i |
element = el . inner_html
if wrap_elements . count > 0
htmls = parsing_repeats_again ( wrap_elements , d [ data_name ] [ i ] , level + 1 )
htmls [ 0 ] . each_with_index do | html , i |
element = element . gsub ( html , htmls [ 1 ] [ i ] )
end
end
item . each do | key , value |
if ! value . kind_of? ( Array )
value = value . nil? ? " " : value
element = element . gsub ( " {{ #{ key } }} " , value . to_s . html_safe )
element = element . gsub ( " %7B%7B #{ key } %7D%7D " , value . to_s . html_safe )
element = render_link_to_edit ( element , value ) if key . eql? ( " url_to_edit " )
end
end
html_to_render = html_to_render + element
end
temp = el . to_s
oldhtml << temp
temp = temp . gsub ( el . inner_html , html_to_render )
newhtml << temp
end
end
[ oldhtml , newhtml ]
end
if @target_action == " index "
filename = overridehtml . nil? ? params [ :layout_type ] : overridehtml
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , 'announcement' , " #{ filename } .html.erb " )
if ! File . exists? f
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , 'announcement' , " index.html.erb " )
if ! File . exists? f
return " <div class='well'>Maybe the administrator has changed the theme, please select the index page design again from the page settings.</div> " . html_safe
end
end
file = File . open ( f )
doc = Nokogiri :: HTML ( file , nil , " UTF-8 " )
file . close
controller = AnnouncementsController . new
begin
data = @data # rescue nil
rescue Exception = > e
write_debug_file ( e , 'announcements' , @target_action ) if Site :: DEBUG
end
if ! data . nil?
wrap_elements = doc . css ( " *[data-list][data-level='0'] " )
htmls = parsing_repeats_again ( wrap_elements , data , 1 )
html = doc . to_s
htmls [ 0 ] . each_with_index do | h , i |
html = html . gsub ( h , htmls [ 1 ] [ i ] )
end
extras = data [ " extras " ] || { }
extras [ " page-title " ] = Page . find_by ( :page_id = > params [ :page_id ] ) . name rescue " " if ! extras [ " page-title " ]
extras . each do | key , value |
value = value . nil? ? " " : value
html = html . gsub ( " {{ #{ key } }} " , value . to_s . html_safe )
html = html . gsub ( " %7B%7B #{ key } %7D%7D " , value . to_s . html_safe )
end
total_pages = data [ 'total_pages' ] . to_i rescue 1
if total_pages > 1
html = html . gsub ( " {{pagination_goes_here}} " , create_pagination ( total_pages ) )
else
html = html . gsub ( " {{pagination_goes_here}} " , " " ) ;
end
html . html_safe
else
return " <div class='well'>No content to show.</div> " . html_safe
end
else
filename = overridehtml . nil? ? @target_action : overridehtml
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , 'announcement' , " #{ filename } .html.erb " )
if File . exists? f
file = File . open ( f )
doc = Nokogiri :: HTML ( file , nil , " UTF-8 " )
file . close
controller = AnnouncementsController . new
begin
data = @data # rescue nil
rescue Exception = > e
write_debug_file ( e , 'announcements' , @target_action ) if Site :: DEBUG
end
if data . nil?
return " <div class='well'> No content to show. </div> " . html_safe
end
if data . blank? || data . empty?
file = File . open ( " #{ Rails . root } /app/views/errors/404.html " )
doc = Nokogiri :: HTML ( file , nil , " UTF-8 " )
file . close
doc . to_html . html_safe
else
unless data [ 'impressionist' ] . blank?
Thread . new do
impression = data [ 'impressionist' ] . impressions . create
impression . user_id = request . session [ 'user_id' ]
impression . controller_name = 'announcements'
impression . action_name = @target_action
impression . ip_address = request . remote_ip
impression . session_hash = request . session . id
impression . request_hash = @impressionist_hash
impression . referrer = request . referrer
impression . save
end
data [ 'impressionist' ] . inc ( view_count : 1 )
data [ " data " ] [ " view_count " ] = data [ " impressionist " ] . view_count if data [ " data " ] . present?
end
wrap_elements = doc . css ( " *[data-list][data-level='0'] " )
if wrap_elements . count == 0
wrap_element_html = doc . to_s
el = wrap_element_html
data . each do | key , value |
next if key . eql? 'impressionist'
value = value . nil? ? " " : value
el = el . gsub ( " {{ #{ key } }} " , value . to_s . html_safe )
el = el . gsub ( " %7B%7B #{ key } %7D%7D " , value . to_s . html_safe )
end
el . html_safe
else
keys = data . keys
not_array_key = nil
data . keys . each do | key |
not_array_key = key if data [ " #{ key } " ] . kind_of? ( Hash )
end
htmls = parsing_repeats_again ( wrap_elements , data , 1 )
html = doc . to_s
htmls [ 0 ] . each_with_index do | h , i |
html = html . gsub ( h , htmls [ 1 ] [ i ] )
end
extras = data [ " #{ not_array_key } " ] || { }
extras . each do | key , value |
next if key . eql? 'impressionist'
value = value . nil? ? " " : value
html = html . gsub ( " {{ #{ key } }} " , value . to_s )
html = html . gsub ( " %7B%7B #{ key } %7D%7D " , value . to_s )
end
html = render_link_to_edit ( html , data [ " url_to_edit " ] ) if ! data [ " url_to_edit " ] . nil?
total_pages = data [ 'total_pages' ] . to_i rescue 1
2020-05-19 06:20:02 +00:00
if @show_page == " false "
2020-05-19 04:31:57 +00:00
html = html . gsub ( " {{pagination_goes_here}} " , " " )
else
if total_pages > 1
html = html . gsub ( " {{pagination_goes_here}} " , create_pagination ( total_pages ) )
else
html = html . gsub ( " {{pagination_goes_here}} " , " " )
end
end
html = Nokogiri :: HTML . parse ( html )
html . css ( '.i-annc__page-title' ) . remove
2020-05-19 06:10:12 +00:00
dates = html . css ( " *[date-format] " )
if ! dates . blank?
dates . each do | d |
2020-08-03 09:11:20 +00:00
begin
format = d . attributes [ " date-format " ] . value
date = DateTime . parse ( d . inner_text )
d . inner_html = d . inner_html . gsub ( d . inner_text . strip , " " + date . strftime ( format ) )
rescue
next
end
2020-05-19 06:10:12 +00:00
end
end
2021-02-21 04:31:22 +00:00
html . css ( " body " ) [ 0 ] . inner_html = html . css ( " body " ) [ 0 ] . inner_html . gsub ( " {{page-title}} " , " " )
2020-05-19 04:31:57 +00:00
html . css ( " body " ) . to_html . html_safe
end
end
else
return " <div class='well'>There is a problem with the design. We will try to fix it as soon as possible. Sorry for the inconvenience!! :(</div> " . html_safe
end
end
end
def get_layouts ( module_app )
layout_types = [ ]
@key = Site . first . template
f = File . join ( " #{ Rails . root } /app/templates/ #{ @key } /modules/ #{ module_app } /info.json " )
if File . exists? f
info = File . read ( f )
hash = JSON . parse ( info ) rescue { }
frontends = hash [ " frontend " ] || [ ]
frontends . each do | frontend |
frontend [ " thumbnail " ] = " /assets/ #{ module_app } /thumbs/ #{ frontend [ " thumbnail " ] } "
layout_types << frontend
end
end
if layout_types . empty?
Dir . glob ( " #{ Rails . root } /app/templates/ #{ @key } /modules/ #{ module_app } /* " ) . each do | w |
next if File . ftype ( w ) . eql? ( " directory " )
w = File . basename ( w , " .* " )
w = File . basename ( w , " .* " )
if w [ 0 , 1 ] != " _ " && w [ 0 , 1 ] != " s " && w != " info "
layout_types << w
end
end
end
layout_types
end
2020-02-01 03:47:52 +00:00
end