module Xforum class Category < ActiveRecord::Base require 'json' has_many :topics, dependent: :destroy has_many :people_lists, dependent: :destroy def self.add_category(params,user) params[:user.id]=user.id if params[:user_id] == 'me' user.forum_admin? ? id= Category.create(name:params[:new_item],user_id:user.id,suggestion:false).id : id='not happening' {id:id} end def self.remove_category(params,user) case(params[:state_action]) when 'del' then Forum.where(id:params[:id]).update_all(closed:true) when 'open' then Forum.where(id:params[:id]).update_all(closed:false) else # type code here end if user.forum_admin? {action:'nothing to do'} end def self.restrict_me(params,user) if user.forum_admin? category=Category.find_by(name:params[:category]) category.restricted= !category.restricted category.save invitation=PeopleList.find_or_create_by(category_id:category.id,list:'invitation') invitation.people= PeopleList.find_by(id:params[:user_list]).people invitation.save end end def self.get_suggestions(params,user) data= Category.where(suggestion:true).pluck(:name, :user_id,:id) data=ForumAssist.named_array(data, [:suggestion, :user,:id]) unless data.empty? data=ForumAssist.add_user_name(data.clone) unless data.empty? user.forum_admin? ? {data: data, list: 'forum-category-prososals-list'} : {response: 'nothing doing'} end def self.edit_category(params,user) Category.where(id: params[:id]).update_all(name:params[:newline]) if user.forum_admin? end def self.suggestion(params,user) #add new category or topic Category.create(suggestion:true,name:params[:suggestion],user_id:user.id) {response:'nothing to do'} end def self.suggestion_close(params,user) params[:result]=='Accept' ? (Category.where(id: params[:id]).update_all(suggestion: false) if user.forum_admin?) : Category.where(id: params[:id]).update_all(state: 'closed') if user.forum_admin? end def self.categories(params,user) if Category.first.nil? Category.create(name:'request new category',closed:false,suggestion:false) Category.create(name:'select category',closed:false,suggestion:false) end params[:hold_test]=='true' ? category_list= Category.grab({closed:false,suggestion:false},[:id,:name,:closed,:restricted],{}) : category_list= Category.grab('all',[:id,:name,:closed,:restricted],{}) category_list=ForumAssist.make_last(category_list.clone, {key: :id,ref:Category.where(name:'request new category').pluck(:id)[0]}) category_list=ForumAssist.make_first(category_list.clone,{key: :id,ref:Category.where(name:'select category').pluck(:id)[0]}) category_list=PeopleList.check_restrictions(category_list.clone,user,'categories') unless user.forum_admin? category_list=Translation.translate_list(category_list.clone,params[:language],'category') category_list.push({id:0, item:'admin',value: I18n.t('xForum.Admin_name')}) if user.forum_admin? {list_data:category_list,id:params[:list]} end def self.get_category_id(params) Category.where(name:params[:category]).pluck(:id)[0] end end end