class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  
  def index
  
	@part = PagePart.find(params[:part_id])
  
	if @part.widget_data_count
		@page_num = @part.widget_data_count
	else
		@page_num = 4
	end
  
	if @part.widget_field
		@widget_fields = @part.widget_field
	else
		@widget_fields = []
	end
	
    @title = params[:part_title]
	
    @widget_style = @part.widget_style
	
	@category_id = @part.category
	
    date_now = Time.now
    if !@category_id.blank?
      @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:category_id => @category_id).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page] ).per(@page_num)
      @current_category = BulletinCategory.from_id(@category_id) rescue nil
    elsif !params[:tag_id].blank?
      @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => params[:tag_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page] ).per(@page_num)
    else
      @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page] ).per(@page_num)
    end

    @categories =  @module_app.categories.enabled
    
  end
  
  def bulletins_list_and_pic
  
  	if params[:widget_data_count]
  		@page_num = params[:widget_data_count];
  	else
  		@page_num = 4;
  	end
	
    @title = params[:part_title]
    date_now = Time.now
    if !params[:category_id].blank?
      @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(@page_num )
      @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => params[:tag_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page] ).per(@page_num)
    else
      @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(@page_num )
    end

    @categories =  @module_app.categories.enabled
    
  end
  
  def bulletins_and_web_links
  
    @part = PagePart.find(params[:part_id]) if !params[:part_id].blank?
    
    @title = @part.title_translations[I18n.locale.to_s]
    
    if !@part.blank? and @part.widget_data_count
      @page_num = @part.widget_data_count
    else
      @page_num = 5
    end

    date_now = Time.now

    if !params[:tag_id].blank?
      @tags = Tag.any_in(:_id => params[:tag_id]).asc(:created_at)
    elsif params[:tag_id].blank? and !@part.tag.blank?
      @tags = Tag.any_in(:_id => @part.tag).asc(:created_at)
    else
      @ModuleApp_b = ModuleApp.first(:conditions => {:key=>'announcement'})
      @tags = Tag.where(:module_tag_id => @ModuleApp_b.id).asc(:created_at)
    end

    @selected_tag = Tag.find(params[:id]).first rescue @tags[0]

    @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => @selected_tag.id.to_s, :is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, :postdate).page(params[:page]).per(@page_num) rescue nil

    if @part.widget_style == 'bulletins_and_web_links'
      @ModuleApp_w = ModuleApp.first(:conditions => {:key=>'web_resource'})
      @link_selected_tag = Tag.first(:conditions => {:name => @selected_tag.name, :module_tag_id => @ModuleApp_w.id})
      @web_links = WebLink.where(:tagged_ids => @link_selected_tag.id.to_s, :is_hidden => false).desc(:is_top,:created_at).available_for_lang(I18n.locale).page(params[:page]).per(@page_num) rescue nil
    end
    
  end
  
  def reload_bulletins
  
    @part = PagePart.find(params[:part_id]) if !params[:part_id].blank?
    
    @title = @part.title_translations[I18n.locale.to_s]
    
    if !@part.blank? and @part.widget_data_count
      @page_num = @part.widget_data_count
    else
      @page_num = 5
    end
    
    date_now = Time.now

    @selected_tag = Tag.find(params[:tag_id]).first
    @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => @selected_tag.id.to_s, :is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, :postdate).page(params[:page]).per(@page_num) rescue nil
  end
  
  def reload_web_links
  
    @part = PagePart.find(params[:part_id]) if !params[:part_id].blank?
    
    if !@part.blank? and @part.widget_data_count
      @page_num = @part.widget_data_count
    else
      @page_num = 5
    end
    
    date_now = Time.now

    @selected_tag = Tag.find(params[:tag_id]).first
    @ModuleApp = ModuleApp.first(:conditions => {:key=>'web_resource'})
    @link_selected_tag = Tag.first(:conditions => {:name => @selected_tag.name, :module_tag_id => @ModuleApp.id})
    @web_links = WebLink.where(:tagged_ids => @link_selected_tag.id.to_s, :is_hidden => false).desc(:is_top,:created_at).available_for_lang(I18n.locale).page(params[:page]).per(@page_num) rescue nil
  end

  def bulletins_side_bar
    @tags_and_class = @module_app.sorted_tags_for_cloud
    @categories =  @module_app.categories.enabled
  end
  
  def bulletins_search_block
    @part = PagePart.find params[:part_id]
  end
  
end
