# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class NewsController < ApplicationController
  layout 'base_projects'# by young
  include ApplicationHelper
  before_filter :authorize1, :only => [:show]
  default_search_scope :news
  model_object News
  before_filter :find_model_object, :except => [:new, :create, :index]
  before_filter :find_project_from_association, :except => [:new, :create, :index]
  before_filter :find_project_by_project_id, :only => [:new, :create]
  before_filter :authorize, :except => [:index]
  before_filter :find_optional_project, :only => :index
  accept_rss_auth :index
  accept_api_auth :index

  helper :watchers
  helper :attachments
  helper :project_score

  def index
    case params[:format]
    when 'xml', 'json'
      @offset, @limit = api_offset_and_limit
    else
      @limit =  10
    end

    # modify by nwb
    if params[:course_id] && @course==nil
      @course = Course.find(params[:course_id])
    end
    if @project
      @page = params[:page] ? params[:page].to_i + 1 : 0
      scope = @project ? @project.news.visible : News.visible

      @news_count = scope.count
      #@news_pages = Paginator.new @news_count, @limit, params['page']
      #@offset ||= @news_pages.offset
      @newss = scope.all(:include => [:author, :project],
                         :order => "#{News.table_name}.created_on DESC",
                         :offset => @page * 10,
                         :limit => 10)

      respond_to do |format|
        format.html {
          @news = News.new # for adding news inline
                           # huang

          render :layout => false if request.xhr?
        }
        format.js
        format.api
        format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
      end
    elsif @course
      if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
        @order, @b_sort,@type = params[:order] || 1, params[:sort] || 1, params[:type] || 1

        #确定 sort_type 1升序 2 降序
        if @order.to_i == @type.to_i
          @b_sort = @b_sort.to_i == 1 ? 2 : 1
        else
          @b_sort = 2
        end

        sort_name = "updated_at"

        sort_type = @b_sort == 1 ? "asc" : "desc"

        #scope = News.find_by_sql("select a.*,b.updated_at from news a, course_activities b where a.course_id = 532 and a.course_id = b.course_id and b.course_act_id = a.id ) ")

        scope = @course ? @course.news.course_visible : News.course_visible
        news_arr = scope.map{|news| news.id}

        @page = params[:page] ? params[:page].to_i + 1 : 0
        news_page = 0 #@page *10
        @news_count = scope.count
        @is_new = params[:is_new]
        @q = params[:subject]
        if params[:subject].nil? || params[:subject].blank?
          scope_order = scope.reorder("#{News.table_name}.sticky DESC, #{News.table_name}.created_on #{sort_type}").offset(news_page).includes(:author,:course).all()
              #all(:include => [:author, :course],
                                  #:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC").limit(10).offset(news_page)
        else
          scope_order = scope.where("#{News.table_name}.title like '#{'%' << params[:subject].to_s << '%'}'").reorder("#{News.table_name}.sticky DESC, #{News.table_name}.#{sort_name} #{sort_type}").offset(news_page).includes(:author,:course).all()
          #.all(:include => [:author, :course],:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC")
        end

        scope_order.each do |topic|
          topic[:updated_at] = topic.course_acts.first.updated_at
          #topic[:updated_at] = CourseActivity.where("course_act_type='#{topic.class}' and course_act_id =#{topic.id}").first.updated_at
        end

        #根据 赞+回复数排序
        if @order.to_i == 2
          @type = 2
          scope_order.each do |topic|
            topic[:infocount] = get_praise_num(topic) + topic.comments.count
            if topic[:infocount] < 0
              topic[:infocount] = 0
            end
          end
          @b_sort == 1 ? scope_order = scope_order.sort{|x,y| x[:infocount]  <=> y[:infocount] } : scope_order = scope_order.sort{|x,y| y[:infocount]  <=> x[:infocount] }
          scope_order = sort_by_sticky scope_order
          scope_order = sortby_time_countcommon_hassticky scope_order,sort_name
        else
          @type = 1
          @b_sort == 1 ? scope_order = scope_order.sort{|x,y| x[:updated_at]  <=> y[:updated_at] } : scope_order = scope_order.sort{|x,y| y[:updated_at]  <=> x[:updated_at] }
          scope_order = sort_by_sticky scope_order
        end

        @newss = scope_order

        #分页
        @limit = 15
        @is_remote = true
        @atta_count = @newss.count
        @atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1
        @offset ||= @atta_pages.offset
        @newss = paginateHelper @newss,@limit

        #@newss = paginateHelper scope_order,10
        respond_to do |format|
          format.html {
            @news = News.new
            render :layout => 'base_courses'
          }
          format.js
          format.api
          format.atom { render_feed(@newss, :title => (@course ? @course.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
        end
      else
        render_403
      end
    end
   end

  def show
    # 更新news对应的forge_messages的消息viewed字段
    ids = @news.comments.map { |comment| comment.id }.join(",") unless @news.comments.nil?
    if @project
      # 更新新闻
      query_message_news = ForgeMessage.where("forge_message_id =? and user_id =? and viewed =?", @news.id, User.current.id, 0).first
      # 更新新闻的时候一并更新回复
      ForgeMessage.where("forge_message_id in (#{ids}) and forge_message_type =  'Comment' and user_id = #{User.current.id}").update_all(:viewed => true) unless ids.blank?
    else
      query_message_news = CourseMessage.where("course_message_id =? and user_id =? and viewed =?", @news.id, User.current.id, 0).first
      CourseMessage.where("course_message_id in (#{ids}) and course_message_type =  'Comment' and user_id = #{User.current.id}").update_all(:viewed => true) unless ids.blank?
    end
    query_message_news.update_attribute(:viewed, true) unless query_message_news.nil?
    # over
    cs = CoursesService.new
    result = cs.show_course_news params,User.current
    @news = result[:news]
    @comments =  result[:comments]
    @comment = Comment.new
    #@comments = @news.comments
    #@comments.reverse! if User.current.wants_comments_in_reverse_order?
    #modify by nwb
    if @news.course_id
      @course = Course.find(@news.course_id)
      if @course
        render :layout => 'base_courses'
      end
    elsif @news.org_subfield_id
      @org_subfield = OrgSubfield.find(@news.org_subfield_id)
      @organization = @org_subfield.organization
      render :layout => 'base_org'
    end
  end

  def new
    #modify by nwb
    if @project
      @news = News.new(:project => @project, :author => User.current)
      @news.safe_attributes = params[:news]
      @news.save_attachments(params[:attachments])
      if @news.save
        if params[:asset_id]
          ids = params[:asset_id].split(',')
          update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS
        end
        render_attachment_warning_if_needed(@news)
        #flash[:notice] = l(:notice_successful_create)
        redirect_to project_news_index_url(@project)
      else
        redirect_to project_news_index_url(@project)
        #layout_file =  'base_courses'
        #render :action => 'new', :layout => layout_file
      end
    elsif @course
      @news = News.new(:course => @course, :author => User.current)
      #render :layout => 'base_courses'
      @news.safe_attributes = params[:news]
      @news.save_attachments(params[:attachments])
      if @news.save
        if params[:asset_id]
          ids = params[:asset_id].split(',')
          update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS
        end
        render_attachment_warning_if_needed(@news)
        #flash[:notice] = l(:notice_successful_create)
        redirect_to course_news_index_url(@course)
      else
        redirect_to course_news_index_url(@course)
        #layout_file =  'base_courses'
        #render :action => 'new', :layout => layout_file
      end
    end
  end

  def create
    #modify by nwb
    if @project
      @news = News.new(:project => @project, :author => User.current)
      @news.safe_attributes = params[:news]
      @news.save_attachments(params[:attachments])
      if @news.save
        render_attachment_warning_if_needed(@news)
        flash[:notice] = l(:notice_successful_create)
        redirect_to project_news_index_url(@project)
      else
        layout_file = @project ? 'base_projects' : 'base_courses'
        render :action => 'new', :layout => layout_file
      end
    elsif @course
      @news = News.new(:course => @course, :author => User.current)
      @news.safe_attributes = params[:news]
      @news.save_attachments(params[:attachments])
      if @news.save
        if params[:asset_id]
          ids = params[:asset_id].split(',')
          update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS
        end
        # ������ض�̬�ļ�¼add start
        # teachers = searchTeacherAndAssistant(@course)
        # for teacher in teachers
        #   if(teacher.user_id != User.current.id)
        #     notify = ActivityNotify.new()
        #     notify.activity_container_id = @course.id
        #     notify.activity_container_type = 'Course'
        #     notify.activity_id = @news.id
        #     notify.activity_type = 'News'
        #     notify.notify_to = teacher.user_id
        #     notify.is_read = 0
        #     notify.save()
        #   end
        # end
        # ������ض�̬�ļ�¼add end
        render_attachment_warning_if_needed(@news)
        flash[:notice] = l(:notice_successful_create)
        redirect_to course_news_index_url(@course)
      else
        layout_file =  'base_courses'
        render :action => 'new', :layout => layout_file
      end
    end
  end

  def edit
    if @news.org_subfield_id
      @org_subfield = OrgSubfield.find(@news.org_subfield_id)
      @organization = @org_subfield.organization
    end
    if @course
      render :layout => "base_courses"
    elsif @org_subfield
      render :layout => 'base_org'
    end
  end

  def update
    @news.safe_attributes = params[:news]
    @news.save_attachments(params[:attachments])
    if @news.save
      render_attachment_warning_if_needed(@news)
      #flash[:notice] = l(:notice_successful_update)
      redirect_to news_url(@news)
    else
      #flash[:error] = l(:notice_successful_update)
      redirect_to news_url(@news)
    end
  end

  def destroy
    if @news.org_subfield_id
      @org_subfield = OrgSubfield.find(@news.org_subfield_id)
    end
    @news.destroy
    # modify by nwb
    if @project
      redirect_to project_news_index_url(@project)
    elsif @course
      redirect_to course_news_index_url(@course)
    elsif @org_subfield
      redirect_to organization_path(@org_subfield.organization, :org_subfield_id => @org_subfield.id)
    end

  end

  private

  def find_optional_project
    return true unless params[:project_id]
    @project = Project.find(params[:project_id])
    authorize
  rescue ActiveRecord::RecordNotFound
    render_404
  end
end
