socialforge/app/controllers/organizations_controller.rb

253 lines
8.7 KiB
Ruby
Raw Normal View History

2015-11-05 17:57:07 +08:00
# encoding: utf-8
class OrganizationsController < ApplicationController
2015-11-14 14:31:52 +08:00
helper :sort
include SortHelper
helper :custom_fields
include CustomFieldsHelper
include AvatarHelper
include WordsHelper
include GitlabHelper
include UserScoreHelper
include PollHelper
helper :user_score
helper :journals
# added by liuping 关注
helper :watchers
helper :activities
### added by william
include ActsAsTaggableOn::TagsHelper
# fq
helper :words
helper :project_score
helper :issues
include UsersHelper
2015-11-12 20:57:41 +08:00
before_filter :find_organization, :only => [:show, :members]
2015-11-03 17:21:32 +08:00
layout 'base_org'
2015-11-05 17:57:07 +08:00
def index
end
def new
@organization = Organization.new
render :layout => 'new_base'
end
def create
@organization = Organization.new
@organization.name = params[:organization][:name]
@organization.description = params[:organization][:description]
@organization.is_public = params[:organization][:is_public]
@organization.creator_id = User.current.id
2015-11-05 17:57:07 +08:00
member = OrgMember.new(:user_id => User.current.id)
@organization.org_members << member
2015-11-03 17:21:32 +08:00
if @organization.save
2015-11-05 17:57:07 +08:00
OrgMemberRole.create(:org_member_id => member.id, :role_id => 11)
2015-11-03 17:21:32 +08:00
redirect_to organization_path(@organization)
end
end
def show
2015-11-12 20:57:41 +08:00
if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization)
2015-11-12 17:52:47 +08:00
@organization = Organization.find(params[:id])
2015-11-14 15:19:49 +08:00
project_ids = @organization.projects.map(&:id) << 0
2015-11-18 16:05:08 +08:00
course_ids = @organization.courses.map(&:id) << 0
course_types = "('Message','News','HomeworkCommon','Poll','Course')"
case params[:type]
when nil
2015-11-18 16:05:08 +08:00
@org_activities = OrgActivity.where("(container_id =? and container_type =?) " +
"or (container_type ='Project' and org_act_type in ('Issue','Message','ProjectCreateInfo') and container_id in (#{project_ids.join(',')})) "+
"or (container_type ='Course' and org_act_type in #{course_types} and container_id in (#{course_ids.join(',')}))",
@organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10)
when 'project_issue'
@org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Issue' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
when 'project_message'
@org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Message' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
when 'org'
@org_activities = OrgActivity.where("container_id =? and container_type =?",@organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10)
when 'course_homework'
@org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'HomeworkCommon' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
when 'course_news'
@org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'News' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
when 'course_message'
@org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Message' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
when 'course_poll'
@org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Poll' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10)
end
2015-11-14 14:31:52 +08:00
@page = params[:page]
respond_to do |format|
format.html
format.js
end
2015-11-12 17:52:47 +08:00
else
render_403
end
2015-11-03 17:21:32 +08:00
end
def update
2015-11-05 17:57:07 +08:00
@organization = Organization.find(params[:id])
@organization.name = params[:organization][:name]
@organization.description = params[:organization][:description]
@organization.domain = params[:organization][:domain]
@organization.is_public = params[:organization][:is_public] == 'on' ? 1 : 0
#@organization.name = params[:organization][:name]
@organization.save
respond_to do |format|
format.html { redirect_to setting_organization_path(@organization)}
end
end
def check_uniq
@check = false;
2015-11-12 20:57:41 +08:00
@org_name = params[:org_name].strip
2015-11-05 17:57:07 +08:00
@config_page = params[:config_page]
sameName = @config_page ? Organization.where('name = ? and id != ?',params[:org_name],params[:org_id].to_i).count == 0 : Organization.where('name = ?',params[:org_name]).count == 0
if sameName == true
@check = true
end
respond_to do |format|
format.js
end
end
def find_organization
@organization = Organization.find(params[:id])
end
def setting
2015-11-12 17:52:47 +08:00
@organization = Organization.find(params[:id])
if User.current.admin? || User.current.admin_of_org?(@organization)
else
render_403
end
2015-11-05 17:57:07 +08:00
end
def clear_org_avatar_temp
2015-11-03 17:21:32 +08:00
end
2015-11-12 09:32:00 +08:00
def set_homepage
2015-11-13 15:02:12 +08:00
@org = Organization.find(params[:id])
@org.home_id = params[:home_id]
@org.save
# respond_to do |format|
# format.html {redirect_to organization_path(org)}
# end
2015-11-12 09:32:00 +08:00
end
def autocomplete_search
@project = Project.find(params[:project_id])
#@flag = params[:flag] || false
respond_to do |format|
format.js
end
end
2015-11-12 20:57:41 +08:00
def members
if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization)
@members = OrgMember.where("organization_id =?", @organization.id)
else
render_403
end
2015-11-12 20:57:41 +08:00
end
2015-11-14 10:45:49 +08:00
def more_org_projects
@organization = Organization.find params[:id]
@page = params[:page]
@org_projects = @organization.projects.reorder('created_at').uniq.page((params[:page].to_i || 1) +1).per(5)
2015-11-18 16:05:08 +08:00
respond_to do |format|
format.js
end
end
def more_org_courses
@organization = Organization.find(params[:id])
@page = params[:page]
@org_courses = @organization.courses.reorder('created_at').uniq.page((params[:page].to_i || 1) + 1 ).per(5)
2015-11-14 10:45:49 +08:00
respond_to do |format|
format.js
end
end
2015-11-26 16:29:50 +08:00
def join_course_menu
@organization = Organization.find(params[:id])
respond_to do |format|
format.js
end
end
def search_courses
@organization = Organization.find(params[:id])
condition = '%%'
if !params[:name].nil?
condition = "%#{params[:name].strip}%".gsub(" ","")
end
sql = "select courses.* from courses inner join members on courses.id = members.course_id where members.user_id = #{User.current.id} and courses.name like '#{condition}'"+
"and courses.id not in (select distinct org_courses.course_id from org_courses where org_courses.organization_id = #{@organization.id})"
#user_courses = Course.find_by_sql(sql)
@courses = Course.find_by_sql(sql)
# @added_course_ids = @organization.courses.map(&:id)
# @courses = []
# user_courses.each do |course|
# if !@added_course_ids.include?(course.id)
# @courses << course
# end
# end
2015-11-26 16:29:50 +08:00
end
def join_courses
@organization = Organization.find(params[:id])
course_ids = params[:courseNames]
course_ids.each do |id|
OrgCourse.create(:organization_id => @organization.id, :course_id => id.to_i, :created_at => Time.now)
end
respond_to do |format|
format.js
end
end
def join_project_menu
@organization = Organization.find(params[:id])
respond_to do |format|
format.js
end
end
def search_projects
@organization = Organization.find(params[:id])
condition = '%%'
if !params[:name].nil?
condition = "%#{params[:name].strip}%".gsub(" ","")
end
sql = "select projects.* from projects inner join members on projects.id = members.project_id where members.user_id = #{User.current.id} and projects.status != 9 and projects.name like '#{condition}'" +
" and projects.id not in (select org_projects.project_id from org_projects where organization_id = #{@organization.id})"
#user_projects = Course.find_by_sql(sql)
@projects = Course.find_by_sql(sql)
# @added_course_ids = @organization.projects.map(&:id)
# @projects = []
# user_projects.each do |project|
# if !@added_course_ids.include?(project.id)
# @projects << project
# end
# end
end
def join_projects
@organization = Organization.find(params[:id])
project_ids = params[:projectNames]
project_ids.each do |id|
OrgProject.create(:organization_id => @organization.id, :project_id => id.to_i, :created_at => Time.now)
end
respond_to do |format|
format.js
end
end
end