module Xforum class Forum < ActiveRecord::Base require 'json' belongs_to :topic has_many :translations, dependent: :destroy has_many :photos, dependent: :destroy has_many :people_lists, dependent: :destroy has_many :hyperlinks, dependent: :destroy USER_BLOCK_SIZE= 300 def self.do_admin_action(params,user) params[:users1]=[] if params[:users1].nil? params[:users2]=[] if params[:users2].nil? user_ids=(params[:users1] + params[:users2]) user_ids.uniq! (0...user_ids.size).each{|i| user_ids[i]=user_ids[i].to_i} user_ids.delete(0) case params[:forum_action] when 'suspend posting' users_ids.each { |user_id| Xforum.user_class.where(id: user_id).update_all(forum_posting_priviages: false) } when 'suspend all' users_ids.each { |user_id| Xforum.user_class.where(id: user_id).update_all(forum_reading_priviages: false) } users_ids.each { |user_id| Xforum.user_class.where(id: user_id).update_all(forum_posting_priviages: false) } when 'send email to' UserMailer.note_to_forum_users(params, user_ids).deliver when 'create distribution', 'add to distribution', 'remove from distro' PeopleList.manage_list(params, user_ids) when 'add topic monitor' Topic.add_topic_monitor(params, user_ids) when 'restrict topic' Topic.restrict_me(params, user) when 'restrict category' Category.restrict_me(params, user) else # type code here end if user.forum_admin? {action:'nothing to do'} end def self.senate(params,user) if params[:topic]== 'select topic' {to_do:'nothing'} else topic_id=Topic.where(name:params[:topic]).pluck(:id)[0] index=Forum.clear_user_topic(user.id,topic_id) the_list=[] plucking= [:id, :parent, :content, :user_id,:owner_name,:state,:restricted,:tag,:has_photo] plucking_names= [:id, :parent, :content,:owner,:owner_name,:comment_state,:restricted,:tag,:has_photo,:needs_translation,:empty_list] #we're going to append empty list to the reply moderator=PeopleList.check_people({topic:topic_id,user_id:user.id,list:'moderator'}) || user.forum_admin? if params[:review]=='true' && moderator test=Forum.where(topic_id:topic_id,state:'new').pluck(:id) unless test.empty? item1=ForumAssist.named_array(Translation.get_localized_forum(Language.get_me(params),{lookup:{topic_id:topic_id},plucking:plucking,size:0}),plucking_names) item1.store(:moderator,true) item1.store(:review,true) new_list= ForumAssist.named_array( Translation.get_localized_forum(Language.get_me(params),{lookup:{topic_id:topic_id,state:'new'},ordering:([:topic_id]),plucking:plucking,size:'all'}),plucking_names ) new_list.unshift(item1) the_list=new_list end else senate_list=ForumAssist.named_array(Translation.get_localized_forum(Language.get_me(params),{lookup:{topic_id:topic_id,hold:false},ordering:[:parent,:created_at],plucking:plucking,size:'all'}), plucking_names ) if senate_list.nil? || senate_list.empty? [] else voting=Topic.grab({id:topic_id},[:vote,:votes],{})[0] voting[:vote] ? votes=Topic.count_votes(JSON.load(voting[:votes])) : votes=0 parent=Forum.look_for_key(senate_list,:parent,0) new_list=[] subscribed=PeopleList.check_people({topic:topic_id,user_id:user.id,list:'subscribers'}) parent=set_new_senate_entry(senate_list,new_list,parent,user.id, {moderator:moderator,vote:voting[:vote],votes:votes,subscribed:subscribed,admin:user.forum_admin?}) unless parent.nil? Forum.is_child_there?(senate_list,new_list,0,parent,user.id) #push first child new_list.delete_if {|record| test=PeopleList.check_people_list({topic:record[:id], user_id:user.id,list:'invitation'}) if (record[:restricted] && !record[:owner]) if test.nil? case record[:state] when 'reject' then test= false when 'new' then test= !(ENV['XFORUM_NEW_RULE']=='no') else test=true end end !test } the_list=new_list end end {the_list:the_list,index:index} end end def self.clear_user_topic(user_id,topic_id) comments=Forum.grab({topic_id:topic_id},[:id,:tag],{order:[:tag]}) tags=[] test_comments=comments.clone test_comments.each{ |comment| if (comment[:tag].to_i) < USER_BLOCK_SIZE*user_id && comment[:tag].to_i <= USER_BLOCK_SIZE*(user_id+1) comments.delete_at(comments.index(comment)) else tags.push((comment[:tag].to_i)%USER_BLOCK_SIZE ) comment[:tag]= tags.last end } if tags.empty? index=0 elsif tags.index(USER_BLOCK_SIZE*1/3).nil? && tags.index(USER_BLOCK_SIZE*2/3).nil? index=USER_BLOCK_SIZE*1/3 elsif tags.index(USER_BLOCK_SIZE*2/3).nil? index= USER_BLOCK_SIZE*2/3 (0...tags.size).each {|i| Forum.where(id:comments[i][:id]).update_all(tag:-1) if tags[i] < USER_BLOCK_SIZE*1/3 } elsif tags.index(USER_BLOCK_SIZE*0/3).nil? index=0 (0...tags.size).each {|i| Forum.where(id:comments[i][:id]).update_all(tag:-1) if tags[i] >= USER_BLOCK_SIZE*1/3 && tags[i]< USER_BLOCK_SIZE*2/3 } else tags.index(USER_BLOCK_SIZE*1/3).nil? index=USER_BLOCK_SIZE*1/3 (0...tags.size).each {|i| Forum.where(id:comments[i][:id]).update_all(tag:-1) if tags[i] >= USER_BLOCK_SIZE*2/3 } end index+user_id*USER_BLOCK_SIZE end def self.set_new_senate_entry(senate_list,new_list,index,user_id,options) senate_list[index].store(:photos,Photo.get_photos(senate_list[index][:id])) if senate_list[index][:has_photo] senate_list[index].store(:hyperlinks,Hyperlink.get_links(senate_list[index][:id])) senate_list[index].store(:indent,Forum.get_indent(senate_list[index][:id],0) ) senate_list[index][:owner]= senate_list[index][:owner]==user_id senate_list[index].store(:moderator,options[:moderator]) unless options[:moderator].nil? senate_list[index].store(:vote,options[:vote]) unless options[:vote].nil? senate_list[index].store(:votes,options[:votes]) unless options[:votes].nil? senate_list[index].store(:admin_lists,options[:admin_lists]) unless options[:admin_lists].nil? senate_list[index].store(:subscribed,options[:subscribed]) unless options[:subscribed].nil? new_list.push(senate_list[index]) id=senate_list[index][:id] senate_list.delete_at(index) id end def self.get_indent(id,index) parent=Forum.where(id:id).pluck(:parent)[0] index=Forum.get_indent(parent,index+1) unless parent==0 || parent.nil? index end def self.look_for_key(list,key,test) index_out=nil (list.size-1).downto(0).each { |index| index_out=index if list[index][key]==test } unless list.empty? index_out end def self.add_comment(params,user) topic_id=Topic.where(name:params[:topic]).pluck(:id)[0] restricted=params[:restricted] == 'true' unless params[:comment].nil? || params[:comment].empty? || !user.forum_posting_privileges? has_photo=params[:already_has_photo] == 'true' || params[:needs_photo] == 'true' params[:id]=Forum.where(user_id:user.id,tag:params[:tag].to_s).pluck(:id)[0] if params[:id].nil? if params[:edit]=='true' && (user.id==Forum.where(id:params[:id]).pluck(:user_id)[0] || user.forum_admin?) forum_id=params[:id] Forum.where(id: params[:id]).update_all(content: params[:comment],has_photo:has_photo, parent: params[:parent],restricted:restricted) else owner_name=Xforum.user_class.where(id:user.id).pluck(:forum_post_name)[0] forum_id= Forum.create(parent:params[:parent],topic_id:topic_id,tag:params[:tag], has_photo:has_photo, user_id:user.id,content:params[:comment],restricted:restricted,owner_name:owner_name).id Translation.create(forum_id:forum_id,user_id:user.id,topic_id:topic_id, language_id:Language.get_language_locale(params), content:params[:translation],suggestion:false,state:'new') end Translation.add_one(params,topic_id,user.id,forum_id) if params[:translating] && user.forum_translating_privileges? PeopleList.make_invitation(params,forum_id,user) if params[:restricted]=='true' PeopleList.add_to_invitation(params,forum_id,user) unless params[:invitees].nil? Hyperlink.add_set(params,forum_id) unless params[:link_ref].nil? end {parent:params[:parent],id:forum_id,topic_id:topic_id,tag:params[:tag],edit:params[:edit]=='true'} end def self.get_forum_actions(params,user) alist= [] alist.push(['select action',I18n.t('xForum.select_action')]) if user.forum_admin? alist.push(['suspend posting',I18n.t('xForum.suspend_posting')]) alist.push(['suspend all',I18n.t('xForum.suspend_all')]) alist.push(['send email to',I18n.t('xForum.send_email_to')]) alist.push(['create distribution',I18n.t('xForum.create_distribution')]) alist.push(['add to distribution',I18n.t('xForum.add_to_distribution') ]) alist.push(['add topic monitor',I18n.t('xForum.Add_Topic_Monitor') ]) end {list_data:ForumAssist.named_array(alist,[:item,:value]),id:'forum-actions'} end def self.change_state(params,user) Forum.where(id:params[:id]).update_all(state: params[:function],resolution:params[:resolution]) if PeopleList.check_people({topic:params[:id], user_id:user.id,list:'moderator'}) || user.forum_admin? {action:'nothing to do'} end def self.set_the_rest(params,user) topic_id=Topic.where(name:params[:topic]).pluck(:id)[0] Forum.where(topic_id:topic_id, state: 'new').update_all(state: params[:state]) if PeopleList.check_people({topic:topic_id,user_id:user.id,list:'moderator'}) || user.forum_admin? {action:'nothing to do'} end def self.is_child_there?(senate_list,new_list, parent,me,user_id) child=Forum.look_for_key(senate_list,:parent,me) unless child.nil? child=Forum.set_new_senate_entry(senate_list, new_list, child, user_id, {}) is_child_there?(senate_list, new_list, me, child, user_id) unless senate_list.empty? end if me!=parent && me!=0 Forum.is_child_there?(senate_list, new_list,parent ,parent, user_id) unless senate_list.empty? end end end end