class Panel::News::Widget::NewsBulletinsController < OrbitWidgetController
# caches_page :home_banner

  def initialize
    super
    @app_title = 'news'
  end
  
  # GET /news_bulletins
  # GET /news_bulletins.xml
  
  def index
    @title = params[:part_title]
    date_now = Time.now
    if !params[:category_id].blank?
      @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(:news_bulletin_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(5)
      @current_category = NewsBulletinCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @tag = NewsTag.find(params[:tag_id]) rescue nil
      @tag = NewsTag.where(key: params[:tag_id])[0] unless @tag
      @news_bulletins = @tag.news_bulletins.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(5) rescue nil
    else
      @news_bulletins = NewsBulletin.all.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(5)
    end
    
  end
  
  def news_bulletins_and_web_links
    @tags = NewsTag.all
    @selected_tag = NewsTag.find(params[:id]) rescue @tags[0]
    @news_bulletins = @selected_tag.get_visible_news_bulletins(:postdate).available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links(:created_at).page(params[:page]).per(5) rescue nil
  end
  
  def reload_news_bulletins
    @selected_tag = NewsTag.find(params[:tag_id])
    @news_bulletins = @selected_tag.get_visible_news_bulletins(:postdate).available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil
  end
  
  def reload_web_links
    @selected_tag = NewsTag.find(params[:tag_id])
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links(:created_at).page(params[:page]).per(5) rescue nil
  end

  def news_bulletins_side_bar
    @tags_and_class = NewsTag.sorted_for_cloud
    get_categorys
  end

  def news_bulletins_search_block

  end

  def home_banner
    if !params[:category_id].blank?
      @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.where(:news_bulletin_category_id => params[:category_id]).widget_datas.limit(9)
    elsif !params[:tag_id].blank?
      @news_bulletins = NewsTag.find(params[:tag_id]).news_bulletins.available_for_lang(I18n.locale).can_display.widget_datas.limit(9) rescue nil
    else
      @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.widget_datas.limit(9)
    end
    get_categorys

  end
  
  
  protected
  
  def get_categorys
    @news_bulletin_categorys = NewsBulletinCategory.all
  end
  
end
