skinny_forum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/Gemfile +14 -0
  4. data/Gemfile.lock +92 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +29 -0
  7. data/README.rdoc +3 -0
  8. data/Rakefile +34 -0
  9. data/app/assets/images/.keep +0 -0
  10. data/app/assets/images/skinny_forum/.keep +0 -0
  11. data/app/assets/images/skinny_forum/internet.png +0 -0
  12. data/app/assets/images/skinny_forum/myphoto.jpg +0 -0
  13. data/app/assets/images/skinny_forum/silver_selector.png +0 -0
  14. data/app/assets/javascripts/skinnyforum/application.js +22 -0
  15. data/app/assets/javascripts/skinnyforum/forum_admin.js.coffee +433 -0
  16. data/app/assets/javascripts/skinnyforum/forum_basic.js.coffee +269 -0
  17. data/app/assets/javascripts/skinnyforum/forum_translator.js.coffee +113 -0
  18. data/app/assets/javascripts/skinnyforum/forum_user.js.coffee +394 -0
  19. data/app/assets/javascripts/skinnyforum/global_java.js +141 -0
  20. data/app/assets/stylesheets/skinnyforum/application.css.scss +25 -0
  21. data/app/assets/stylesheets/skinnyforum/custom.css.scss +289 -0
  22. data/app/controllers/skinnyforum/application_controller.rb +24 -0
  23. data/app/controllers/skinnyforum/forums_controller.rb +130 -0
  24. data/app/helpers/skinnyforum/application_helper.rb +4 -0
  25. data/app/mailers/.keep +0 -0
  26. data/app/mailers/skinnyforum/user_mailer.rb +13 -0
  27. data/app/models/.keep +0 -0
  28. data/app/models/skinnyforum/forum.rb +251 -0
  29. data/app/models/skinnyforum/forum_category.rb +70 -0
  30. data/app/models/skinnyforum/forum_hyperlink.rb +50 -0
  31. data/app/models/skinnyforum/forum_language.rb +86 -0
  32. data/app/models/skinnyforum/forum_people_list.rb +183 -0
  33. data/app/models/skinnyforum/forum_photo.rb +62 -0
  34. data/app/models/skinnyforum/forum_topic.rb +137 -0
  35. data/app/models/skinnyforum/forum_translation.rb +109 -0
  36. data/app/views/layouts/skinnyforum/application.html.erb +14 -0
  37. data/app/views/skinnyforum/forums/_dummy.html.erb +0 -0
  38. data/app/views/skinnyforum/forums/_forum.html.erb +107 -0
  39. data/app/views/skinnyforum/forums/_forum_admin.html.erb +133 -0
  40. data/app/views/skinnyforum/forums/newforum_photo.js.erb +1 -0
  41. data/app/views/skinnyforum/user_mailer/note_to_forum_users.html.erb +32 -0
  42. data/app_data/forum_base.csv +8 -0
  43. data/app_data/forum_help.csv +24 -0
  44. data/app_data/reset cmds +13 -0
  45. data/config/locales/skinnyforum/forum_de.yml +88 -0
  46. data/config/locales/skinnyforum/forum_en.yml +88 -0
  47. data/config/locales/skinnyforum/forum_es.yml +88 -0
  48. data/config/locales/skinnyforum/forum_fr.yml +88 -0
  49. data/config/routes.rb +2 -0
  50. data/db/migrate/20130100_add_users.rb +11 -0
  51. data/db/migrate/20140601_add_forums.rb +164 -0
  52. data/lib/skinny_forum.rb +4 -0
  53. data/lib/skinny_forum/engine.rb +5 -0
  54. data/lib/skinny_forum/forum_shared_code.rb +94 -0
  55. data/lib/skinny_forum/version.rb +3 -0
  56. data/lib/tasks/forum_tasks.rake +52 -0
  57. data/lib/tasks/skinny_forum_tasks.rake +4 -0
  58. data/skinny_forum.gemspec +28 -0
  59. data/test/dummy/README.rdoc +28 -0
  60. data/test/dummy/Rakefile +6 -0
  61. data/test/dummy/app/assets/images/.keep +0 -0
  62. data/test/dummy/app/assets/javascripts/application.js +13 -0
  63. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  64. data/test/dummy/app/controllers/application_controller.rb +5 -0
  65. data/test/dummy/app/controllers/concerns/.keep +0 -0
  66. data/test/dummy/app/helpers/application_helper.rb +2 -0
  67. data/test/dummy/app/mailers/.keep +0 -0
  68. data/test/dummy/app/models/.keep +0 -0
  69. data/test/dummy/app/models/concerns/.keep +0 -0
  70. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  71. data/test/dummy/bin/bundle +3 -0
  72. data/test/dummy/bin/rails +4 -0
  73. data/test/dummy/bin/rake +4 -0
  74. data/test/dummy/config.ru +4 -0
  75. data/test/dummy/config/application.rb +23 -0
  76. data/test/dummy/config/boot.rb +5 -0
  77. data/test/dummy/config/database.yml +25 -0
  78. data/test/dummy/config/environment.rb +5 -0
  79. data/test/dummy/config/environments/development.rb +37 -0
  80. data/test/dummy/config/environments/production.rb +83 -0
  81. data/test/dummy/config/environments/test.rb +39 -0
  82. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  83. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  84. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  85. data/test/dummy/config/initializers/inflections.rb +16 -0
  86. data/test/dummy/config/initializers/mime_types.rb +4 -0
  87. data/test/dummy/config/initializers/session_store.rb +3 -0
  88. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  89. data/test/dummy/config/locales/en.yml +23 -0
  90. data/test/dummy/config/routes.rb +4 -0
  91. data/test/dummy/config/secrets.yml +22 -0
  92. data/test/dummy/lib/assets/.keep +0 -0
  93. data/test/dummy/public/404.html +67 -0
  94. data/test/dummy/public/422.html +67 -0
  95. data/test/dummy/public/500.html +66 -0
  96. data/test/dummy/public/favicon.ico +0 -0
  97. data/test/integration/navigation_test.rb +10 -0
  98. data/test/skinny_forum_test.rb +7 -0
  99. data/test/test_helper.rb +15 -0
  100. metadata +234 -0
@@ -0,0 +1,50 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forum_hyperlinks
4
+ #
5
+ # id :integer not null, primary key
6
+ # forum_id :integer
7
+ # link_address :string(255)
8
+ # link_text :string(255)
9
+ #
10
+
11
+ class ForumHyperlink < ActiveRecord::Base
12
+ include ForumSharedCode
13
+ require 'json'
14
+
15
+ belongs_to :forum
16
+ has_many :forum_translations, dependent: :destroy
17
+
18
+ def self.add_one(params)
19
+ the_link=ForumHyperlink.where(link_address:params[:link_address],forum_id:params[:forum_id]).first_or_create{ |link|
20
+ link.link_address= params[:link_address]
21
+ link.link_text=params[:link_text]
22
+ link.forum_id= params[:forum_id]
23
+ }
24
+ ForumHyperlink.where(link_address:params[:link_address],forum_id:params[:forum_id]).update_all(link_text:params[:link_text])
25
+ unless params[:link_translation].nil? || params[:link_translation] == ''
26
+ ForumTranslation.where(forum_hyperlink_id:the_link.id,forum_language_id:ForumLanguage.get_me(params)).first_or_create{|link|
27
+ link.forum_hyperlink_id=the_link.id
28
+ link.forum_language_id=ForumLanguage.get_me(params)
29
+ link.content= params[:link_translation]
30
+ }
31
+ ForumTranslation.where(forum_hyperlink_id:the_link.id,forum_language_id:ForumLanguage.get_me(params)).update_all(content:params[:link_translation])
32
+ end
33
+ {todo:'nothing'}
34
+ end
35
+
36
+ def self.add_set(params,forum_id)
37
+ (0...params[:link_ref].to_i).each { |i|
38
+ data=params['link-'+i.to_s]
39
+ data[:forum_id]=forum_id
40
+ ForumHyperlink.add_one(data)
41
+ }
42
+ end
43
+
44
+ def self.get_links(forum_id)
45
+ alist=ForumHyperlink.where(forum_id:forum_id).pluck(:link_address,:link_text,:id)
46
+ unless alist.empty?
47
+ ForumAssist.named_array(alist,[:link_address,:link_text,:id],d2=true)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,86 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forum_languages
4
+ #
5
+ # id :integer not null, primary key
6
+ # user_id :integer
7
+ # code :string(5)
8
+ # name :string(50)
9
+ # local_name :string(50)
10
+ # region :string(50)
11
+ # suggestion :boolean
12
+ # state :string(50) default("suggesiton")
13
+ # active :boolean default(FALSE)
14
+ # created_at :datetime
15
+ # updated_at :datetime
16
+ #
17
+
18
+ class ForumLanguage < ActiveRecord::Base
19
+ include ForumSharedCode
20
+ require 'json'
21
+
22
+ has_many :forum_translations, dependent: :destroy
23
+
24
+ def self.toggle_language(params,user_id) #this has to be an all new language
25
+ state= ForumLanguage.where(name:params[:language]).pluck(:active)[0]
26
+ ForumLanguage.where(name:params[:language]).update_all(active:!state) if User.forum_admin?(user_id)
27
+ {action:'nothing to do'}
28
+ end
29
+
30
+ def self.add_language(params,user_id)
31
+ if User.forum_admin?(user_id)
32
+ ForumLanguage.create(code:params[:code],name:params[:name],local_name:params[:local])
33
+ end
34
+ end
35
+
36
+ def self.languages(params)
37
+ ForumLanguage.init_language_set if ForumLanguage.first.nil?
38
+ params[:active] == 'true' ?
39
+ alist=ForumLanguage.where(active:true).order(:code).pluck(:id,:name,:code,:local_name,:active) :
40
+ alist=ForumLanguage.where(active:false).order(:code).pluck(:id,:name,:code,:local_name,:active)
41
+ unless alist.empty?
42
+ alist=ForumAssist.named_array(alist,[:id,:item,:value,:local_name,:state],d2=true)
43
+ alist=ForumTranslation.translate_list(alist,'','language')
44
+ end
45
+ alist.unshift({value: I18n.t('forum.select_language'),item: I18n.t('forum.select_language') ,state:'open',id:0})
46
+ {list_data:alist,id:params[:list]}
47
+ end
48
+
49
+ def self.get_suggestions(params,user_id)
50
+ if User.forum_admin?(user_id)
51
+ data= ForumLanguage.where(suggestion:true).pluck(:content, :user_id, :id)
52
+ data=ForumAssist.named_array(data, [:suggestion, :user,:id ], d2=true) unless data.empty?
53
+ data=Forum.add_user_name(data.clone) unless data.empty?
54
+ {data:data,list:'topic-prososals-list'}
55
+ else
56
+ {reply:'nothing doing'}
57
+ end
58
+ end
59
+
60
+ def self.suggestion(params,user_id) #add new category or topic
61
+ ForumLanguage.create(suggestion:true,content:params[:suggestion],user_id:user_id,hold:true)
62
+ {response:'nothing to do'}
63
+ end
64
+
65
+ def self.suggestion_close(params,user_id)
66
+ ForumLanguage.where(id:params[:id]).update_all(state:'closed') if User.forum_admin?(user_id)
67
+ end
68
+
69
+ def self.get_me(params)
70
+ params[:language] = params[:language][0] if params[:language].is_a?(Array)
71
+ code=params[:language].split('-')[0].strip
72
+ ForumLanguage.where(code:code).pluck(:id)[0]
73
+ end
74
+
75
+ def self.init_language_set
76
+
77
+ language_table = %w(af.Afrikaans.Afrikaans ar.Arabic.العربية be.Belarusian.беларускі bg.Bulgarian.български ca.Catalan.Català cs.Czech.čeština da.Danish.dansk de.German.dansk el.Greek.ελληνικά en.English.English es.Spanish.español et.Estonian.eesti eu.Basque.Euskal fa.Farsi.فارسی fi.Finnish.suomalainen fo.Faroese.føroyskt fr.French.français ga.Irish.Gaeilge gd.Gaelic.gaélico he.Hebrew.עברית hi.Hindi.हिंदी hr.Croatian.hrvatski hu.Hungarian.magyar id.Indonesian.Indonesia is.Icelandic.Icelandic it.Italian.italiano ja.Japanese.日本の ji.Yiddish.ייִדיש ko.Korean.한국의 lt.Lithuanian.Lietuvos lv.Latvian.Latvijas mk.Macedonian.македонски ms.Malaysian.Malaysia mt.Maltese.Malti nl.Dutch.Nederlands no.Norwegian.norsk pl.Polish.polski pt.Portuguese.português ro.Romanian.român ru.Russian.русский sk.Slovak.slovenský sl.Slovenian.slovenščina sq.Albanian.shqiptar sr.Serbian.српски sv.Swedish.Svenska th.Thai.ภาษาไทย tr.Turkish.Türk uk.Ukrainian.Український ur.Urdu.اردو vi.Vietnamese.Việt zh.Chinese.中國的 zu.Zulu.Zulu)
78
+
79
+ language_table.each {|entry|
80
+ set=ForumAssist.named_array(entry.split('.'),[:code,:language,:local],d2=false)
81
+ ForumLanguage.create(code:set[:code], name:set[:language],local_name:set[:local],active:false )
82
+ }
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,183 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forum_people_lists
4
+ #
5
+ # id :integer not null, primary key
6
+ # owner_id :integer
7
+ # name :string(20)
8
+ # people :binary default("--- []\n")
9
+ # list :string(20)
10
+ # created_at :datetime
11
+ # updated_at :datetime
12
+ #
13
+
14
+ class ForumPeopleList < ActiveRecord::Base
15
+ include ForumSharedCode
16
+ require 'json'
17
+
18
+ belongs_to :forum_topic
19
+ belongs_to :forum_category
20
+ belongs_to :forum
21
+
22
+ serialize :people
23
+
24
+ def self.get_people_list(user_id, list)
25
+ people = []
26
+ people.push(user_id)
27
+ unless list.nil?
28
+ list.gsub(',', ';')
29
+ new_list=list.split(';')
30
+ email_list=[]
31
+ user_list=[]
32
+ new_list.each { |entry|
33
+ if entry.include?('@')
34
+ email_list.push(entry)
35
+ else
36
+ user_list.push(entry)
37
+ end
38
+ }
39
+ user_list.each { |entry|
40
+ id=User.where(forum_post_name: entry).pluck(:id)[0]
41
+ people.push id unless id.nil?
42
+ }
43
+ email_list.each { |entry|
44
+ id=User.where(email: entry).pluck(:id)[0]
45
+ people.push id unless id.nil?
46
+ }
47
+ end
48
+ people
49
+ end
50
+
51
+ def self.manage_list(params, user_ids)
52
+ case params[:forum_action]
53
+ when 'create distribution'
54
+ ForumPeopleList.create(name: params[:new_list_name], people: user_ids.to_json, list: 'distribution list')
55
+ when 'add to distribution'
56
+ alist=user_ids+JSON.load(ForumPeopleList.where(id: params[:distribution_list]).pluck(:people)[0])
57
+ alist.uniq!
58
+ ForumPeopleList.where(id: params[:distribution_list]).update_all(people: alist.to_json)
59
+ when 'remove from distro'
60
+ alist=JSON.load(ForumPeopleList.where(id: params[:distribution_list]).pluck(:people)[0])-user_ids
61
+ ForumPeopleList.where(id: params[:distribution_list]).update_all(people: alist.to_json)
62
+ else
63
+ end
64
+ end
65
+
66
+ def self.get_forum_people_list(params)
67
+ which=ForumPeopleList.get_which_owner(params)
68
+ which.store(:list,params[:list])
69
+ people= JSON.load(ForumPeopleList.where(which).pluck(:people)[0])
70
+ if people.nil?
71
+ ForumPeopleList.create(owner_id: id, list: alist)
72
+ []
73
+ else
74
+ people
75
+ end
76
+ end
77
+
78
+ def self.get_which_owner(params)
79
+ if !params[:topic].nil?
80
+ {forum_topic_id:params[:topic]}
81
+ elsif !params[:forum].nil?
82
+ {forum_id:params[:topic]}
83
+ elsif !params[:category].nil?
84
+ {forum_category_id:params[:topic]}
85
+ else
86
+ {}
87
+ end
88
+ end
89
+
90
+ def self.get_forum_emails(params, user_id)
91
+ User.forum_admin?(user_id) ? alist=User.all.pluck(:email, :id) : alist= []
92
+ alist.unshift([I18n.t('forum.select_none'), 'select none', 0])
93
+ {list_data: ForumAssist.named_array(alist, [:value, :item], d2=true), name: 'forum-user-email'}
94
+ end
95
+
96
+ def self.get_forum_users(params, user_id)
97
+ User.forum_admin?(user_id) ? alist = User.all.pluck(:forum_post_name, :id) : alist =[]
98
+ alist.unshift([I18n.t('forum.select_none'), 'select none', 0])
99
+ {list_data: ForumAssist.named_array(alist, [:value, :item], d2=true), name: 'forum-user-tag'}
100
+ end
101
+
102
+ def self.check_restrictions(alist, user_id)
103
+ blist=alist.clone
104
+ blist.each { |record|
105
+ alist.delete(record) if Forum.check_user_invite(record[:id], user_id) if record[:restricted]
106
+ }
107
+ alist
108
+ end
109
+
110
+ def self.get_forum_distribution_lists(user_id)
111
+ User.forum_admin?(user_id) ? alist=ForumPeopleList.where(list: 'distribution list').pluck(:name, :list, :id) : alist=[]
112
+ alist.unshift([I18n.t('forum.select_list'), 'select list', 0])
113
+ {list_data: ForumAssist.named_array(alist, [:value, :item, :id], d2=true), id: 'forum-distribution-lists'}
114
+ end
115
+
116
+ def self.is_a_moderator?(user_id)
117
+ the_lists=ForumPeopleList.where(list: 'moderator').pluck(:people)
118
+ moderators=[]
119
+ the_lists.each { |a_list|
120
+ moderators+= a_list
121
+ moderators.uniq!
122
+ }
123
+ moderators.include?(user_id)
124
+ end
125
+
126
+ def self.make_invitation(params, forum_id, user_id)
127
+ invitees=ForumPeopleList.get_people_list(user_id, params[:invitees])
128
+ peoplelist=ForumPeopleList.where(list: 'invitation', owner_id: forum_id).first_or_create { |invitation|
129
+ invitation.owner_id=forum_id
130
+ invitation.list='invitaton'
131
+ }
132
+ peoplelist.invite_these_guys(invitees)
133
+ end
134
+
135
+ def self.add_to_list(params, users)
136
+ if params[:name].nil?
137
+ a_list=ForumPeopleList.where(list: params[:list], owner_id: params[:owner]).first_or_create { |new|
138
+ new.list=params[:list]
139
+ new.owner_id=params[:owner]
140
+ }
141
+ else
142
+ a_list=ForumPeopleList.where(list: 'distribution list', name: params[:list]).first_or_create { |new|
143
+ new.list = 'distribution list'
144
+ new.name = params[:list]
145
+ }
146
+ end
147
+ a_list.invite_these_guys(users)
148
+ end
149
+
150
+ def self.check_people(params) # owner_id,user_id,list)
151
+ which=ForumPeopleList.get_which_owner(params)
152
+ which.store(:list,params[:llist])
153
+ people=ForumPeopleList.where(which).pluck(:people)[0]
154
+ (people.nil? || people.empty?) ? false : people.include?(params[:user_id].to_s)
155
+ end
156
+
157
+ def self.add_to_invitation(params, id, user_id)
158
+ peoplelist = ForumPeopleList.find_people_list(params, id)
159
+ peoplelist.add_these_guys(ForumPeopleList.get_people_list(user_id, params[:invitees])) unless peoplelist.nil?
160
+ end
161
+
162
+ def self.find_people_list(params, forum_id)
163
+ which=ForumPeopleList.get_which_owner(params)
164
+ which.store(:list,'invitation')
165
+ list = ForumPeopleList.find_by(which)
166
+ if list.nil?
167
+ parent=Forum.where(id: forum_id).pluck(:parent)[0]
168
+ if parent == 0
169
+ topic=Forum.where(id:forum_id).pluck(:forum_topic_id)
170
+ ForumPeopleList.find_by(forum_topic_id:topic,list:params[:list])
171
+ else
172
+ ForumPeopleList.find_people_list(params, parent) unless parent.nil?
173
+ end
174
+ end
175
+ end
176
+
177
+ def invite_these_guys(invitees)
178
+ invitees += JSON.load(self.people) unless JSON.load(self.people).nil?
179
+ invitees.uniq!
180
+ self.people = invitees.to_json
181
+ self.save
182
+ end
183
+ end
@@ -0,0 +1,62 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forum_photos
4
+ #
5
+ # id :integer not null, primary key
6
+ # forum_id :integer
7
+ # user_id :integer
8
+ # name :string(50)
9
+ # resized :boolean default(FALSE)
10
+ # key :string(255)
11
+ # created_at :datetime
12
+ # updated_at :datetime
13
+ #
14
+
15
+ class ForumPhoto < ActiveRecord::Base
16
+ include ForumSharedCode
17
+ require 'json'
18
+
19
+ belongs_to :user
20
+ belongs_to :forum
21
+
22
+ def self.get_photos(forum_id)
23
+ photos=ForumPhoto.where(forum_id:forum_id).pluck(:key)
24
+ urls=[]
25
+ photos.each {|photo|
26
+ urls.push(ForumAssist.get_url(photo) )
27
+ }
28
+ urls
29
+ end
30
+
31
+ def self.get_photo(params)
32
+ params[:id].nil? ? which = {tag:params[:tag]} : which = {id:params[:id]}
33
+ data=ForumAssist.named_array(Forum.where(which).pluck(:id,:parent)[0],[:id,:parent],d2=false)
34
+ urls=ForumPhoto.get_photos(data[:id])
35
+ {tag:params[:tag],edit:params[:edit],urls:urls,id:data[:id],parent:data[:parent],count:params[:count].to_i+1}
36
+ end
37
+
38
+ def self.upload(params,user_id)
39
+ temp=JSON.load(params[:forum_photo][:data])
40
+ pforward=Hash[temp.map{|k,v| [k.to_sym,v]}] #make the true/false strings again
41
+ pforward[:needs] ? pforward[:needs_photo] ='true' : pforward[:needs_photo] ='false'
42
+ pforward[:already_has_photo] ? pforward[:already_has_photo] ='true' : pforward[:already_has_photo] ='false'
43
+ pforward[:edit] ? pforward[:edit] ='true' : pforward[:edit] ='false'
44
+ pforward[:translating] ? pforward[:translating] ='true' : pforward[:translating] ='false'
45
+ comment_info=Forum.add_comment(pforward,user_id)
46
+ k=params[:forum_photo][:file]
47
+ Spawnling.new {
48
+ (0...k.size).each {|i|
49
+ name=k[i].original_filename.downcase
50
+ ext=name.split('.').last
51
+ if %w(jpg gif png).include?(ext)
52
+ folder='doll/forum_photos'
53
+ save_name=File.join(folder,name)
54
+ ForumAssist.save_image(k[i].tempfile.path, save_name)
55
+ ForumPhoto.create(user_id:user_id,forum_id:comment_info[:id],name:name,key:save_name)
56
+ end
57
+ }
58
+ } unless k.nil?
59
+ Rails.logger.debug 'saved job, now going back'
60
+ end
61
+
62
+ end
@@ -0,0 +1,137 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: forum_topics
4
+ #
5
+ # id :integer not null, primary key
6
+ # forum_category_id :integer
7
+ # user_id :integer
8
+ # name :string(50)
9
+ # state :string(20)
10
+ # closed :boolean default(FALSE)
11
+ # suggestion :boolean
12
+ # votes :binary default("--- []\n")
13
+ # vote :boolean default(FALSE)
14
+ # restricted :boolean default(FALSE)
15
+ # created_at :datetime
16
+ # updated_at :datetime
17
+ #
18
+
19
+ class ForumTopic < ActiveRecord::Base
20
+ include ForumSharedCode
21
+ require 'json'
22
+ belongs_to :forum_category
23
+ has_many :forums, dependent: :destroy
24
+ has_many :forum_people_lists, dependent: :destroy
25
+ serialize :votes
26
+
27
+ def self.topics(params,user_id)
28
+ if ForumTopic.first.nil?
29
+ ForumTopic.create(name:'select topic')
30
+ ForumTopic.create(name:'request new topic')
31
+ end
32
+ unless params[:category] == 'select category'
33
+ category_id=ForumCategory.where(name:params[:category]).pluck(:id)[0]
34
+ params[:hold_test]=='true' ?
35
+ topics=ForumAssist.named_array(ForumTopic.where(forum_category_id:category_id,closed:false).pluck(:id,:name,:state,:restricted,:closed), [:id,:item,:state,:restricted,:closed],d2=true) :
36
+ topics=ForumAssist.named_array(ForumTopic.where(forum_category_id:category_id).pluck(:id,:name,:state,:restricted,:closed), [:id,:item,:state,:restricted,:closed],d2=true)
37
+ topics=[] if topics[0][:id].nil?
38
+ topics=ForumPeopleList.check_restrictions(topics,user_id) if params[:restricted]=='true'
39
+ a=ForumAssist.named_array(ForumTopic.where(name:'select topic').pluck(:id,:name,:state,:restricted,:closed)[0], [:id,:item,:state,:restricted,:closed],d2=false)
40
+ b=ForumAssist.named_array(ForumTopic.where(name:'request new topic').pluck(:id,:name,:state,:restricted,:closed)[0], [:id,:item,:state,:restricted,:closed],d2=false)
41
+ topics.unshift(a)
42
+ topics.push(b)
43
+ {list_data:ForumTranslation.translate_list(topics,params[:language],'topic'),id:params[:list]}
44
+ end
45
+ end
46
+
47
+ def self.voting_booth?(params)
48
+ topic=ForumTopic.find_by(name:params[:topic])
49
+ topic.vote=!topic.vote
50
+ topic.save
51
+ {vote:topic.vote,votes:'0'}
52
+ end
53
+
54
+ def self.add_topic_monitor(params,user_ids)
55
+ topic=ForumTopic.where(name:params[:topic]).pluck(:id)[0]
56
+ ForumPeopleList.add_to_list({list:'monitor',owner:topic},user_ids)
57
+ end
58
+
59
+ def self.get_suggestion(params,user_id)
60
+ if User.forum_admin?(user_id)
61
+ data=ForumTopic.where(suggestion:true).pluck(:content, :user_id, :forum_category_id,:id,:owner_name)
62
+ data=ForumAssist.named_array(data, [:suggestion, :user, :category, :id, :owner_name], d2=true) unless data.empty?
63
+ data=ForumAssist.add_user_name(data.clone) unless data.empty?
64
+ {data:data,list:'topic-prososals-list'}
65
+ else
66
+ {response:'nothing doing'}
67
+ end
68
+ end
69
+
70
+ def self.suggestion(params,user_id) #add new category or topic
71
+ ForumTopic.create(suggestion:true,content:params[:suggestion],user_id:user_id)
72
+ {response:'nothing to do'}
73
+ end
74
+
75
+ def self.suggestion_close(params,user_id)
76
+ ForumTopic.where(id:params[:id]).update_all(state:'closed') if User.forum_admin?(user_id)
77
+ {response:'nothing to do'}
78
+ end
79
+
80
+ def self.count_votes(votes)
81
+ count=votes.length
82
+ count2=votes.delete_if{|element| element=='-1'
83
+ }.length
84
+ 2*count2-count
85
+ end
86
+
87
+ def self.cast_vote(topic,user_id,vote)
88
+ topic_record=ForumTopic.find_by(name:topic)
89
+ voters=ForumPeopleList.get_forum_people_list({topic:topic_record.id,list:'voters'})
90
+ votes=JSON.load(topic_record.votes)
91
+ voters.empty? ? index=nil : index=voters.index(user_id)
92
+ if vote == '1' || vote == '-1'
93
+ if index.nil?
94
+ voters.push(user_id)
95
+ votes.push(vote)
96
+ else
97
+ votes[index]=vote
98
+ end
99
+ end
100
+ ForumPeopleList.where(owner_id:topic_record.id,list:'voters').update_all(people:voters.to_json)
101
+ topic_record.votes=votes.to_json
102
+ topic_record.save
103
+ votes.empty? ? 0 : ForumTopic.count_votes(votes.clone)
104
+ end
105
+
106
+ def self.subscribe(topic,user_id) #if they're in the list unsubscribe else subscribe
107
+ id=ForumTopic.find_by(name:topic).pluck(:id)[0]
108
+ subscribers=Json.load(ForumPeopleList.where(id:id,list:'subscribers').pluck(people)[0])
109
+ index= subscribers.index(user_id.to_s) unless subscribers.empty?
110
+ index.nil? ? subscribers.push(user_id.to_s) : subscribers.delete_at(index)
111
+ ForumPeopleList.where(id:id,list:'subscribers').update_all(people:subscribers.to_json)[0]
112
+ end
113
+
114
+ def self.add_topic(params,user_id)
115
+ params[:user_id]=user_id if params[:user_id] == 'me'
116
+ category=ForumCategory.where(name:params[:category]).pluck(:id)[0]
117
+ User.forum_admin?(user_id) ?
118
+ id=ForumTopic.create(name:params[:topic],user_id:params[:user_id],forum_category_id:category).id :
119
+ id='not happening'
120
+ {id:id}
121
+ end
122
+
123
+ def self.edit_topic(params,user_id)
124
+ Forum.where(id: params[:id]).update_all(name:params[:newline]) if User.forum_admin?(user_id)
125
+ end
126
+
127
+ def self.remove_topic(params,user_id)
128
+ case(params[:state_action])
129
+ when 'del' then Forum.where(id:params[:id]).update_all(closed:true)
130
+ when 'open' then Forum.where(id:params[:id]).update_all(closed:false)
131
+ else
132
+ # type code here
133
+ end if User.forum_admin?(user_id)
134
+ {action:'nothing to do'}
135
+ end
136
+
137
+ end