2015-12-24 10:40:00 +08:00
class OrgSubfieldsController < ApplicationController
2016-01-14 10:33:15 +08:00
helper :attachments
2016-01-14 17:52:48 +08:00
include FilesHelper
2016-01-13 15:17:13 +08:00
layout 'base_org'
2015-12-24 10:40:00 +08:00
def create
2016-01-13 09:56:24 +08:00
if OrgSubfield . where ( " organization_id= #{ params [ :organization_id ] } and name=? " , params [ :name ] ) . count == 0
@res = true
@organization = Organization . find ( params [ :organization_id ] )
2016-03-09 18:06:28 +08:00
@subfield = OrgSubfield . create ( :name = > params [ :name ] , :organization_id = > params [ :organization_id ] , :priority = > @organization . org_subfields . order ( " priority " ) . last . priority + 1 )
2016-01-13 09:56:24 +08:00
if ! params [ :sub_dir ] . blank?
sql = " select subfield_subdomain_dirs.* from subfield_subdomain_dirs, org_subfields where subfield_subdomain_dirs.org_subfield_id = org_subfields.id " +
" and org_subfields.organization_id= #{ @organization . id } and subfield_subdomain_dirs.name=' #{ params [ :sub_dir ] } ' "
if SubfieldSubdomainDir . find_by_sql ( sql ) . count == 0
2016-03-22 13:51:19 +08:00
SubfieldSubdomainDir . create ( :org_subfield_id = > @subfield . id , :name = > params [ :sub_dir ] . downcase )
2016-01-13 09:56:24 +08:00
end
2016-01-13 09:34:33 +08:00
end
2016-03-09 18:06:28 +08:00
@subfield . update_attributes ( :field_type = > params [ :field_type ] )
2016-01-13 09:56:24 +08:00
else
@res = false
2016-01-13 09:34:33 +08:00
end
2015-12-24 10:40:00 +08:00
end
def show
2016-01-14 12:51:01 +08:00
@flag = params [ :flag ] || false
sort = " "
@sort = " "
@order = " "
@is_remote = false
2016-01-18 10:43:22 +08:00
if params [ :id ]
@organization = Organization . find ( params [ :id ] )
else
2016-02-23 12:52:04 +08:00
domain = Secdomain . where ( " subname=? " , request . subdomain ) . first
@organization = Organization . find ( domain . pid )
2016-01-18 10:43:22 +08:00
end
2016-03-07 10:29:37 +08:00
if @organization . is_public? or User . current . admin? or User . current . member_of_org? ( @organization )
@org_subfield = OrgSubfield . find_by_sql ( " select distinct org_subfields.* from org_subfields, " +
" subfield_subdomain_dirs where org_subfields.id = subfield_subdomain_dirs.org_subfield_id and " +
" org_subfields.organization_id= #{ @organization . id } and subfield_subdomain_dirs.name=' #{ params [ :sub_dir_name ] } ' " ) . first
if @org_subfield . field_type == 'Post'
2016-01-14 10:33:15 +08:00
@org_subfield_ids = @org_subfield . org_document_comments . map ( & :id ) << 0
@org_activities = OrgActivity . where ( " (org_act_type='OrgDocumentComment'and org_act_id in ( #{ @org_subfield_ids . join ( " , " ) } )) || (container_type='OrgSubfield' and container_id= #{ @org_subfield . id } ) " ) . order ( 'updated_at desc' ) . page ( params [ :page ] || 1 ) . per ( 10 )
#redirect_to organization_path(@organization, :org_subfield_id => @org_subfield.id)
2016-03-07 10:29:37 +08:00
else
if params [ :sort ]
params [ :sort ] . split ( " , " ) . each do | sort_type |
order_by = sort_type . split ( " : " )
case order_by [ 0 ]
when " filename "
attribute = " filename "
when " size "
attribute = " filesize "
when " attach_type "
attribute = " attachtype "
when " content_type "
attribute = " created_on "
when " field_file_dense "
attribute = " is_public "
when " downloads "
attribute = " downloads "
when " created_on "
attribute = " created_on "
when " quotes "
attribute = " quotes "
else
attribute = " created_on "
2016-01-14 10:33:15 +08:00
end
2016-03-07 10:29:37 +08:00
@sort = order_by [ 0 ]
@order = order_by [ 1 ]
if order_by . count == 1 && attribute
sort += " #{ Attachment . table_name } . #{ attribute } asc "
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
elsif order_by . count == 2 && order_by [ 1 ]
sort += " #{ Attachment . table_name } . #{ attribute } #{ order_by [ 1 ] } "
if sort_type != params [ :sort ] . split ( " , " ) . last
sort += " , "
end
2016-01-14 10:33:15 +08:00
end
2016-01-13 09:34:33 +08:00
end
2016-03-07 10:29:37 +08:00
else
sort = " #{ Attachment . table_name } .created_on desc "
2016-01-13 09:34:33 +08:00
end
2016-03-07 10:29:37 +08:00
@container_type = 2
@containers = [ OrgSubfield . includes ( :attachments ) . reorder ( sort ) . find ( @org_subfield . id ) ]
@organization = Organization . find ( @containers . first . organization_id )
show_attachments @containers
@tag_list = attachment_tag_list @all_attachments
2016-01-13 09:34:33 +08:00
end
2016-03-07 10:29:37 +08:00
@page = params [ :page ] || 1
else
render_403
2015-12-24 10:40:00 +08:00
end
2016-01-13 15:17:13 +08:00
#render :layout => 'base_org'
2015-12-24 10:40:00 +08:00
end
2016-01-13 15:17:13 +08:00
2015-12-24 10:40:00 +08:00
def destroy
@subfield = OrgSubfield . find ( params [ :id ] )
@organization = Organization . find ( @subfield . organization_id )
@subfield . destroy
end
def update
@subfield = OrgSubfield . find ( params [ :id ] )
@organization = Organization . find ( @subfield . organization_id )
@subfield . update_attributes ( :name = > params [ :name ] )
end
2016-01-13 15:17:13 +08:00
def update_sub_dir
@org_subfield = OrgSubfield . find ( params [ :id ] )
sql = " select subfield_subdomain_dirs.* from subfield_subdomain_dirs, org_subfields where subfield_subdomain_dirs.org_subfield_id = org_subfields.id " +
" and org_subfields.organization_id= #{ @org_subfield . organization . id } and subfield_subdomain_dirs.name=' #{ params [ :sub_dir_name ] } ' "
if SubfieldSubdomainDir . find_by_sql ( sql ) . count == 0
if @org_subfield . subfield_subdomain_dir
@sub_dir = @org_subfield . subfield_subdomain_dir
2016-01-27 15:29:15 +08:00
@sub_dir = SubfieldSubdomainDir . update ( @sub_dir . id , :name = > params [ :sub_dir_name ] )
#@sub_dir.update_attribute(:name, params[:sub_dir_name])
2016-01-13 15:17:13 +08:00
else
@sub_dir = SubfieldSubdomainDir . create ( :org_subfield_id = > @org_subfield . id , :name = > params [ :sub_dir_name ] )
end
@exist = false
else
@exist = true
end
end
2016-03-09 18:06:28 +08:00
def update_priority
@org_subfield = OrgSubfield . find ( params [ :id ] )
@org_subfield . update_attribute ( :priority , params [ :priority ] . to_i )
@organization = @org_subfield . organization
end
2016-01-13 15:17:13 +08:00
def show_attachments obj
@attachments = [ ]
obj . each do | container |
@attachments += container . attachments
end
@all_attachments = User . current . admin? ? @attachments : visable_attachemnts ( @attachments )
@limit = 10
@feedback_count = @all_attachments . count
@feedback_pages = Paginator . new @feedback_count , @limit , params [ 'page' ]
@offset || = @feedback_pages . offset
#@curse_attachments_all = @all_attachments[@offset, @limit]
@obj_attachments = paginateHelper @all_attachments , 10
end
#获取指定资源列表的TAG的集合以及每个TAG的数量, 降序排序
def attachment_tag_list attachments
tag_list = Hash . new
attachments . each do | attachment |
attachment . tag_list . map { | tag | tag_list . has_key? ( tag ) ? tag_list [ tag ] = tag_list [ tag ] + 1 : tag_list [ tag ] = 1 }
end
tag_list . sort { | a , b | b [ 1 ] < = > a [ 1 ] }
end
2015-12-24 10:40:00 +08:00
end