//  [ruby]
  def save
    Log.add_info(request, params.inspect)

    return unless request.post?

    date = Date.parse(params[:date])

    unless params[:id].blank?
      begin
        schedule = Schedule.find(params[:id])
      rescue => evar
        Log.add_error(request, evar)
        flash[:notice] = 'ERROR:' + t('msg.already_deleted', :name => Schedule.model_name.human)
        redirect_to(:action => 'day', :date => date.strftime(Schedule::SYS_DATE_FORM))
        return
      end

      unless schedule.check_user_auth(@login_user, 'w', true)

        Log.add_check(request, '[Schedule.check_user_auth]'+request.to_s)

        if @login_user.nil?
          check_login
        else
          redirect_to(:controller => 'frames', :action => 'http_error', :id => '401')
        end
        return
      end
    end

    if params[:users].blank? \
        and params[:groups].blank? \
        and params[:teams].blank? \
        and (params[:schedule][:scope] != Schedule::SCOPE_ALL)

      nearest_day = schedule.get_nearest_day(date)
      schedule.destroy unless schedule.nil?
    else
      [:users, :groups, :teams, :items].each do |attr|
        SqlHelper.validate_token([params[attr]])
        if params[attr].blank?
          params[:schedule][attr] = nil
        else
          params[:schedule][attr] = '|' + params[attr].join('|') + '|'
        end
      end

      equipment_ids = (params[:equipment] || [])
      equipment_ids.delete('')

      if equipment_ids.empty?
        params[:schedule][:equipment] = nil
      else
        equipment_ids.each do |equipment_id|
          begin
            equipment = Equipment.find(equipment_id)
          rescue => evar
            equipment = nil
          end
          if equipment.nil? or !equipment.is_accessible_by(@login_user)
            flash[:notice] = 'ERROR:' + t('msg.need_auth_to_access') + t('cap.suffix') + Equipment.get_name(equipment_id)
            redirect_to(:action => 'day', :date => params[:date])
            return
          end
        end
        params[:schedule][:equipment] = '|' + equipment_ids.join('|') + '|'
      end

      if params[:is_repeat] == '1'

        if params[:repeat_rules].blank?
          params[:schedule][:repeat_rule] = nil
        else
          params[:schedule][:repeat_rule] = '|' + params[:repeat_rules].join('|') + '|'
        end

        if params[:excepts].blank?
          params[:schedule][:except] = nil
        else
          excepts = params[:excepts]
          excepts.sort!
          excepts.reverse!
          params[:schedule][:except] = '|' + excepts.join('|') + '|'
        end
      else
        params[:schedule][:repeat_rule] = nil
        params[:schedule][:repeat_start] = nil
        params[:schedule][:repeat_end] = nil
        params[:schedule][:except] = nil
      end

      params[:schedule][:end] = SchedulesHelper.regularize(params[:schedule][:end])

      check_schedule = Schedule.new(params.require(:schedule).permit(Schedule::PERMIT_BASE))
      nearest_day = check_schedule.get_nearest_day(date)
      if nearest_day.nil?
        check_schedule.id = params[:id].to_i unless params[:id].nil? or params[:id].empty?
        flash[:notice] = 'ERROR:' + t('schedule.no_day_in_rule')
        if params[:fwd_controller].blank?
          self.index
        else
          prms = ApplicationHelper.get_fwd_params(params)
          prms.delete('id')
          prms.delete('schedule')
          prms[:controller] = params[:fwd_controller]
          prms[:action] = params[:fwd_action]
          redirect_to(prms)
        end
      # redirect_to(:action => 'day', :date => params[:date])
        return
      end

      created = false
      if schedule.nil? or params[:repeat_update_target] == 'each'
        # Create
        params[:schedule][:created_by] = @login_user.id
        params[:schedule][:created_at] = Time.now
        schedule = Schedule.new(params.require(:schedule).permit(Schedule::PERMIT_BASE))
        schedule.save!
        created = true
      else
        # Update
        params[:schedule][:updated_by] = @login_user.id
        params[:schedule][:updated_at] = Time.now
        schedule.update_attributes(params.require(:schedule).permit(Schedule::PERMIT_BASE))
      end

      if params[:repeat_update_target] == 'each'
        # Update original repeated schedule
        org_schedule = Schedule.find(params[:id])
        attrs = ActionController::Parameters.new()
        attrs[:updated_by] = @login_user.id
        attrs[:updated_at] = Time.now
        excepts = org_schedule.get_excepts_a
        excepts << params[:date]
        excepts.sort!
        excepts.reverse!
        attrs[:except] = '|' + excepts.join('|') + '|'
        org_schedule.update_attributes(attrs.permit(Schedule::PERMIT_BASE))
      end

      # prms = {:show_id => schedule.id}
    end

    if created
      flash[:notice] = t('msg.register_success')
    else
      flash[:notice] = t('msg.update_success')
    end

    params[:date] = nearest_day.strftime(Schedule::SYS_DATE_FORM)

    if params[:fwd_controller].blank?
      self.index
      self.show unless self.performed?
    else
      prms = ApplicationHelper.get_fwd_params(params)
      prms.delete('id')
      prms.delete('schedule')
      prms[:controller] = params[:fwd_controller]
      prms[:action] = params[:fwd_action]
      redirect_to(prms)
    end

  rescue => evar
    Log.add_error(request, evar)

    date = Date.parse(params[:date])
    redirect_to(:action => 'day', :date => date.strftime(Schedule::SYS_DATE_FORM))
  end