-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmoney.js
More file actions
1630 lines (1253 loc) · 53.9 KB
/
money.js
File metadata and controls
1630 lines (1253 loc) · 53.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @class monetary
* @static
*
* Main class that handles setup, init of sub modules, post event bindings and display of money across the forum.
*/
var money = {
/**
* Holds the latest version of this plugin.
* @property {String} VERSION
*/
VERSION: "{VER}",
/**
* This is the ProBoards plugin key we are using.
* @property {String} KEY
*/
KEY: "pixeldepth_money",
/**
* This is the min required Yootil version that is needed.
* @property {String} required_yootil_version
*/
required_yootil_version: "1.0.0",
/**
* This holds a reference to the plugin object returned by ProBoards.
* @property {Object} plugin
*/
plugin: null,
/**
* Route gets cached here, as it gets wrote over by some AJAX responses.
* We shouldn't need it, as Yootil does caching as well.
*
* @property {String} route
*/
route: null,
/**
* Reference to ProBoards page params.
* @property {Object} params
*/
params: null,
/**
* Reference to the images object from ProBoards for this plugin.
* @property {Object} images
*/
images: null,
/**
* Used in the sync class to prevent IE from syncing the master (caller).
* @property {Boolean} trigger_caller
*/
trigger_caller: false,
/**
* Holds all default settings for this class, these can be overwritten in setup.
* @property {Object} settings
* @property {String} settings.money_text This is what the money is called.
* @property {String} settings.money_symbol This holds the money symbol.
* @property {String} settings.money_separator The separator between money_text and the money value.
* @property {Boolean} settings.decimal_money Do we want to use decimals or not.
* @property {Boolean} settings.show_in_mini_profile Should money show in the mini profiles.
* @property {Boolean} settings.show_money_text_mini Should we show the money text in mini profiles.
* @property {Boolean} settings.show_money_symbol_mini Should we show the money symbol in mini profiles.
* @property {Boolean} settings.show_in_profile Should we show the money on profile pages.
* @property {Boolean} settings.show_money_text_profile Should we show the money text on profiles.
* @property {Boolean} settings.show_money_symbol_profile Should we show the money symbol on profiles.
* @property {Boolean} settings.show_in_members_list Should we show the money on the members list page.
* @property {Boolean} settings.show_money_symbol_members Should we show the money symbol on the members list page.
* @property {Boolean} settings.member_list_text This is the text for the column on the members page.
* @property {Boolean} settings.show_bank_balance_members Should we show the bank balance on the members page.
* @property {Boolean} settings.staff_edit_money Can staff edit money on the profile page.
* @property {Boolean} settings.show_edit_money_image Should we show the edit image on profile page.
* @property {Array} settings.who_can_edit_money Holds an array of users who can edit money on the profile page.
* @property {Object} settings.posting Holds settings to do with posting.
* @property {Boolean} settings.posting.earn_from_quick_reply Can users earn money from the quick reply.
* @property {Boolean} settings.posting.earn_from_quick_reply.amounts Holds the amounts users can earn from posting.
* @property {Number} settings.posting.earn_from_quick_reply.amounts.per_thread Amount for a new thread.
* @property {Number} settings.posting.earn_from_quick_reply.amounts.per_poll Amount for adding a poll to a thread.
* @property {Number} settings.posting.earn_from_quick_reply.amounts.per_reply Amount for when replying to a thread.
* @property {Number} settings.posting.earn_from_quick_reply.amounts.per_quick_reply Amount for replying while using the quick reply.
* @property {Object} settings.posting.earn_from_quick_reply.amounts.categories Can override amounts per category.
* @property {Object} settings.posting.earn_from_quick_reply.amounts.boards Can override amounts per board.
* @property {Object} settings.notification Settings for notifications that users get when their money is edited.
* @property {Boolean} settings.notification.show Should the notification show for users.
* @property {Boolean} settings.notification.show_edited If enabled, it will show the user who made the edit to their money.
* @property {Array} settings.no_earn_members Members who are not allowed to earn money.
* @property {Array} settings.no_earn_groups Groups who are not allowed to earn money.
* @property {Array} settings.no_earn_categories Categories where money can not be earned.
* @property {Array} settings.no_earn_boards Boards where money can not be earned.
* @property {Array} settings.no_earn_threads Threads where money can not be earned.
* @property {Object} settings.text Holds text values that can be changed in the plugin admin area.
* @property {String} settings.text.wallet The wallet.
* @property {String} settings.text.money_column This is for the members list page.
* @property {String} settings.text.bank_column This is for the members list page.
*/
settings: {
check_for_update: true,
check_how_often: 2,
money_text: "Money",
money_symbol: "£",
money_separator: ":",
decimal_money: true,
show_in_mini_profile: true,
show_money_text_mini: true,
show_money_symbol_mini: true,
show_in_profile: true,
show_money_text_profile: true,
show_money_symbol_profile: true,
show_in_members_list: true,
show_money_symbol_members: true,
member_list_text: "Money",
show_bank_balance_members: false,
staff_edit_money: true,
show_edit_money_image: true,
who_can_edit_money: [],
posting: {
earn_from_quick_reply: false,
amounts: {
per_thread: 10,
per_poll: 5,
per_reply: 5,
per_quick_reply: 5,
categories: {},
boards: {}
}
},
notification: {
show: false,
show_edited: true
},
no_earn_members: [],
no_earn_groups: [],
no_earn_categories: [],
no_earn_boards: [],
no_earn_threads: [],
text: {
wallet: "Wallet",
money_column: "Money",
bank_column: "Bank Balance"
}
},
/**
* Updated when posting to see if it's a new thread to work out money amount.
* @property {Boolean} is_new_thread
*/
is_new_thread: false,
/**
* Are we editing a post? We don't want to update money if editing, so we check.
* @property {Boolean} is_editing
*/
is_editing: false,
/**
* If money has been processed, we update here so we don't process again (this is old).
* @property {Boolean} processed
*/
processed: false,
/**
* We update this if quick reply is being used, this was before key events were added.
* @property {Boolean} using_quick_reply
*/
using_quick_reply: false,
/**
* This can be used to prevent money being earnt on the page.
* @property {Boolean} can_earn_money
*/
can_earn_money: true,
/**
* Used to show the default display of the forum.
* @property {Boolean} can_show_default
*/
can_show_default: true,
/**
* Modules are registered and placed in here and init later.
* @property {Array} modules
*/
modules: [],
/**
* A lookup table for user data objects on the page, always check here first before making a new Data instance.
* @property {Object} user_data_table
*/
user_data_table: {},
/**
* Instance of our notification class
* @property {Object} notify
*/
notify: null,
event: {},
/**
* Starts the magic.
* Various this happening here. We do Yootil checks, setup user lookup table, and other checks.
*/
init: function(){
$.support.cors = true;
if(!this.check_yootil()){
return;
}
this.setup_user_data_table();
this.setup();
if(yootil.user.logged_in()){
this.check_for_notifications();
this.look_for_wallet();
this.can_earn_money = this.can_earn();
if(yootil.location.posting() || (yootil.location.thread() && this.settings.posting.earn_from_quick_reply)){
if(this.can_earn_money && this.can_earn_in_cat_board()){
this.bind_events();
}
}
}
if(this.modules.length){
for(var m = 0, ml = this.modules.length; m < ml; m ++){
if(this.modules[m].init){
this.modules[m].init();
}
}
}
var location_check = (yootil.location.search_results() || yootil.location.message_thread() || yootil.location.thread() || yootil.location.recent_posts());
if(this.settings.show_in_mini_profile && location_check){
this.show_in_mini_profile();
yootil.ajax.after_search(this.show_in_mini_profile, this);
}
if(this.settings.show_in_profile && yootil.location.profile_home() && this.params && this.params.user_id != "undefined"){
this.show_in_profile();
}
if(this.settings.show_in_members_list && yootil.location.members()){
this.show_in_members_list();
yootil.ajax.after_search(this.show_in_members_list, this);
}
},
/**
* Uses yootils notification class.
*
* monetary.create_notification("message", 1);
*
* @param {String} msg The message for the notification.
* @param {Number} user_id The user id who is receiving this notification.
*/
create_notification: function(msg, user_id){
if(!msg || !user_id){
return;
}
if(typeof this.notify != "undefined"){
this.notify.create(msg, user_id);
}
},
/**
* Used to correct incorrect dates that were being saved.
* @deprecated
* @param {Number} the_date The date to be corrected.
* @return {Object} Date object.
*/
correct_date: function(the_date){
if(the_date.toString().indexOf(".") != -1){
return new Date(the_date * 1000);
}
return new Date(the_date);
},
/**
* This sets up the lookup table for all users on the current page. Each entry is an instance of Data. Always
* look here before creating your own instance, as multiple instances would not be good.
*
* It is recommended that if you do create an instance of Data to update the lookup table (key being user id).
*/
setup_user_data_table: function(){
var all_data = proboards.plugin.keys.data[this.KEY];
for(var key in all_data){
var data = this.check_data(all_data[key]);
this.user_data_table[key] = new this.Data(key, data);
}
},
/**
* Refreshed the user data lookup table.
*/
refresh_user_data_table: function(){
this.setup_user_data_table();
},
/**
* @property {Array} months An array of months used throughout the plugin and it's modules.
*/
months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"],
/**
* @property {Array} days An array of days used throughout the plugin and it's modules.
*/
days: ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"],
/**
* Checks a number and returns the correct suffix to be used with it.
*
* monetary.get_suffix(3); // "rd"
*
* @param {Number} n The number to be checked.
* @return {String}
*/
get_suffix: function(n){
var j = (n % 10);
if(j == 1 && n != 11){
return "st";
}
if(j == 2 && n != 12){
return "nd";
}
if(j == 3 && n != 13) {
return "rd";
}
return "th";
},
/**
* Gets current version of the plugin.
*
* @return {String}
*/
version: function(){
return this.VERSION;
},
/**
* Checks the data type to make sure it's correct. The reason for this is because ProBoards
* never used to JSON stringify values, so we check to make sure it's not double stringified.
*
* @param {String} data The key data.
* @return {Object}
*/
check_data: function(data){
if(typeof data == "string" && yootil.is_json(data)){
data = JSON.parse(data);
}
return data;
},
/**
* Looks for any element with the wallet class and updates the html of it.
*/
look_for_wallet: function(){
var wallet = $(".money_wallet_amount");
if(wallet.length){
wallet.html(this.settings.text.wallet + this.settings.money_separator + this.settings.money_symbol + yootil.html_encode(this.data(yootil.user.id()).get.money(true)));
}
},
/**
* Yootil is needed, so we check for it, and also check that we are using the needed version.
*
* @return {Boolean}
*/
check_yootil: function(){
if(proboards.data && proboards.data("user") && proboards.data("user").id == 1){
var body = "";
var title = "";
if(typeof yootil == "undefined"){
title = "<div class=\"title-bar\"><h2>Monetary System - Yootil Not Found</h2></div>";
body = "<p>You do not have the <a href='http://support.proboards.com/thread/429360/'>Yootil</a> plugin installed.</p>";
body += "<p>Without the <a href='http://support.proboards.com/thread/429360/'>Yootil</a> plugin, the <a href='http://support.proboards.com/thread/429762/'>Monetary System</a> will not work.</p>";
} else {
var versions = yootil.convert_versions(yootil.version(), this.required_yootil_version);
if(versions[0] < versions[1]){
title = "<div class=\"title-bar\"><h2>Monetary System - Yootil Needs Updating</h2></div>";
body += "<p>The <a href='http://support.proboards.com/thread/429762/'>Monetary System</a> requires at least " + yootil.html_encode(this.required_yootil_version) + " of the <a href='http://support.proboards.com/thread/429360/'>Yootil</a> plugin.</p>";
}
}
if(title.length){
var msg = "<div class='monetary-notification-content'>";
msg += body;
msg += "<p style='margin-top: 8px;'>For more information, please visit the <a href='http://support.proboards.com/thread/429762/#plugindownload'>Monetary System</a> forum topic on the <a href='http://support.proboards.com'>ProBoards forum</a>.</p>";
msg += "</div>";
var notification = "<div class=\"container monetary-yootil-notification\">";
notification += title;
notification += "<div class=\"content pad-all\">" + msg + "</div></div>";
$("div#content").prepend(notification);
return false;
}
}
return true;
},
/**
* Shows the forum default display by showing the content div and all children. This is generally used
* in the modules where the setting for the module is disabled.
*/
show_default: function(){
if(this.can_show_default){
$("#content > *").show();
}
},
/**
* Formats the money value into a whole or float number depending on settings.
*
* monetary.format(33, true); // "33.00"
*
* @param {String} str Money value to be formatted.
* @param {Boolean} string If a string is needed back, pass in true.
* @return {Mixed}
*/
format: function(str, string){
var str = parseFloat(str);
if(money.settings.decimal_money){
str = str.toFixed(2);
if(isNaN(str)){
if(string){
str = "0.00";
} else {
str = 0.00;
}
}
} else {
str = parseInt(Math.ceil(str));
if(isNaN(str)){
if(string){
str = "0";
} else {
str = 0;
}
}
}
if(string){
str = str.toString();
}
return str;
},
/**
* This is deprecated, please see the data method and Data class.
*
* @deprecated
*/
get: function(format, bank){
return this.data(yootil.user.id()).get[((bank)? "bank" : "money")](format);
},
/**
* This is deprecated, please see the data method and Data class.
*
* @deprecated
*/
subtract: function(value, bank, no_update, opts, sync){
this.data(yootil.user.id()).decrease[((bank)? "bank" : "money")](value, no_update, opts, sync);
},
/**
* This is deprecated, please see the data method and Data class.
*
* @deprecated
*/
add: function(value, bank, no_update, opts, sync){
this.data(yootil.user.id()).increase[((bank)? "bank" : "money")](value, no_update, opts, sync);
},
/**
* Ddisables earning when posting.
*/
disable_earning: function(){
this.can_earn_money = false;
},
/**
* Enable earning when posting (earning is enabled by default).
*/
enable_earning: function(){
this.can_earn_money = true;
},
/**
* This is used to get the instance of the users Data class from the lookup table. Please see the Data
* class to see methods available.
*
* monetary.data(yootil.user.id()).get.money(); // Returns users money
*
* monetary.data(yootil.user.id()).increase.money(100); // Adds 100 to users money
*
* @param {Number} user_id The user id of the users data you want.
* @return {Object}
*/
data: function(user_id){
var user_data = this.user_data_table[((user_id)? user_id : yootil.user.id())];
if(!user_data){
user_data = new this.Data(user_id);
this.user_data_table[user_id] = user_data;
}
return user_data;
},
/**
* Checks to see if the user can earn money.
*
* @return {Boolean}
*/
can_earn: function(){
if(this.settings.no_earn_members && this.settings.no_earn_members.length){
if($.inArrayLoose(yootil.user.id(), this.settings.no_earn_members) > -1){
return false;
}
}
if(this.settings.no_earn_groups && this.settings.no_earn_groups.length){
var user_groups = yootil.user.group_ids();
for(var g = 0, l = user_groups.length; g < l; g ++){
if($.inArrayLoose(user_groups[g], this.settings.no_earn_groups) > -1){
return false;
}
}
}
return true;
},
/**
* Cleans the money value to make sure it is safe and valid.
*
* @return {String}
*/
clean_money: function(money){
return money.toString().replace(/[^\d\.]/g, "");
},
/**
* Checks to see if money can be earned in categories and boards
*
* @return {Boolean}
*/
can_earn_in_cat_board: function(){
if(this.settings.no_earn_categories && this.settings.no_earn_categories.length){
var cat_id = parseInt(yootil.page.category.id());
if(cat_id){
if($.inArrayLoose(cat_id, this.settings.no_earn_categories) > -1){
return false;
}
}
}
if(this.settings.no_earn_boards && this.settings.no_earn_boards.length){
var board_id = parseInt(yootil.page.board.id());
if(board_id && $.inArrayLoose(board_id, this.settings.no_earn_boards) > -1){
return false;
}
}
return true;
},
/**
* Clears the autosave for posts. This was a bug reported, and this seems to fix it.
*/
clear_auto_save: function(){
proboards.autosave.clear();
},
/**
* Handles binding events to earn money when posting.
*/
bind_events: function(){
// Check if in thread or posting, then check if thread is disabled
// from earning
if((yootil.location.thread() || yootil.location.posting()) && this.settings.no_earn_threads.length){
var thread_id = parseInt(yootil.page.thread.id());
if(thread_id){
for(var i = 0, l = this.settings.no_earn_threads.length; i < l; i ++){
if(this.settings.no_earn_threads[i].thread_id == thread_id){
return false;
}
}
}
}
if(yootil.location.posting_thread()){
this.is_new_thread = true;
}
if(yootil.location.editing()){
this.is_editing = true;
}
var self = this;
var the_form;
var hook;
if(yootil.location.posting()){
the_form = yootil.form.post();
hook = (this.is_new_thread)? "thread_new" : "post_new";
} else if(yootil.location.thread()){
the_form = yootil.form.post_quick_reply();
this.using_quick_reply = true;
hook = "post_quick_reply";
}
if(the_form.length){
the_form.bind("submit", function(event){
if(!self.processed){
if(self.is_new_thread || self.is_editing){
var poll_input = $(this).find("input[name=has_poll]");
self.is_poll = (poll_input && poll_input.val() == 1)? true : false;
}
if(hook){
self.apply_posting_money(hook);
self.clear_auto_save();
}
}
});
}
},
/**
* Handles applying money for posting, wages, rank up etc. Also overrides posting amounts if there are
* category or board settings.
*
* @param {String} event The key event we are hooking.
*/
apply_posting_money: function(event){
if(!this.can_earn_money){
return false;
}
/**
* Triggers before applying money to account when they are posting.
*
* $(monetary.event).on("before_apply_posting_money", function(event){
* console.log("do something");
* });
*
* @event before_apply_posting_money
*/
$(monetary.event).trigger("before_apply_posting_money");
var money_to_add = 0.00;
var category_id = yootil.page.category.id();
var board_id = yootil.page.board.id();
if(board_id && this.settings.posting.amounts.boards[board_id]){
var amounts = this.settings.posting.amounts.boards[board_id];
this.settings.posting.amounts.per_quick_reply = amounts.per_quick_reply;
this.settings.posting.amounts.per_reply = amounts.per_reply;
this.settings.posting.amounts.per_poll = amounts.per_poll;
this.settings.posting.amounts.per_thread = amounts.per_thread;
} else if(category_id && this.settings.posting.amounts.categories[category_id]){
var amounts = this.settings.posting.amounts.categories[category_id];
this.settings.posting.amounts.per_quick_reply = amounts.per_quick_reply;
this.settings.posting.amounts.per_reply = amounts.per_reply;
this.settings.posting.amounts.per_poll = amounts.per_poll;
this.settings.posting.amounts.per_thread = amounts.per_thread;
}
if(!this.is_editing && !this.is_new_thread){
if(this.using_quick_reply){
money_to_add += parseFloat(this.format(this.settings.posting.amounts.per_quick_reply));
} else {
money_to_add += parseFloat(this.format(this.settings.posting.amounts.per_reply));
}
}
if(this.is_poll){
money_to_add += parseFloat(this.format(this.settings.posting.amounts.per_poll));
}
if(this.is_new_thread){
money_to_add += parseFloat(this.format(this.settings.posting.amounts.per_thread));
}
if(!this.processed){
this.processed = true;
var interest_applied = this.bank.apply_interest();
var wages_paid = this.wages.pay();
var rank_up_paid = this.rank_up.pay();
if(money_to_add > 0 || interest_applied || wages_paid || rank_up_paid){
this.data(yootil.user.id()).increase.money(money_to_add, true);
yootil.key.set_on(this.KEY, this.data(yootil.user.id()).get.data(), yootil.user.id(), event);
}
}
/**
* Triggers after applying money to account when they are posting.
*
* $(monetary.event).on("after_apply_posting_money", function(event){
* console.log("do something");
* });
*
* @event after_apply_posting_money
*/
$(monetary.event).trigger("after_apply_posting_money");
},
/**
* Handles basic setup for settings.
*/
setup: function(){
this.route = (proboards.data("route") && proboards.data("route").name)? proboards.data("route").name.toLowerCase() : "";
this.params = (this.route && proboards.data("route").params)? proboards.data("route").params : "";
this.plugin = pb.plugin.get("pixeldepth_monetary");
if(this.plugin && this.plugin.settings){
this.images = this.plugin.images;
var settings = this.plugin.settings;
this.settings.show_in_mini_profile = (!! ~~ settings.show_in_mini_profile)? true : false;
this.settings.show_money_text_mini = (!! ~~ settings.show_money_text_mini)? true : false;
this.settings.show_money_symbol_mini = (!! ~~ settings.show_money_symbol_mini)? true : false;
this.settings.show_in_profile = (!! ~~ settings.show_in_profile)? true : false;
this.settings.show_money_text_profile = (!! ~~ settings.show_money_text_profile)? true : false;
this.settings.show_money_symbol_profile = (!! ~~ settings.show_money_symbol_profile)? true : false;
this.settings.show_in_members_list = (!! ~~ settings.show_in_members_list)? true : false;
this.settings.show_money_symbol_members = (!! ~~ settings.show_money_symbol_members)? true : false;
this.settings.show_bank_balance_members = (!! ~~ settings.show_bank_balance_members)? true : false;
this.settings.text.money_column = (settings.member_list_text && settings.member_list_text.length)? settings.member_list_text : ((settings.money_text && settings.money_text.length)? settings.money_text : this.settings.text.money_column);
this.settings.text.bank_column = (settings.bank_column_text && settings.bank_column_text.length)? settings.bank_column_text : this.settings.text.bank_column;
this.settings.staff_edit_money = (!! ~~ settings.staff_edit_money)? true : false;
this.settings.show_edit_money_image = (!! ~~ settings.show_edit_money_image)? true : false;
if(settings.edit_money_image && settings.edit_money_image.length){
this.images.edit_money = settings.edit_money_image;
}
if(settings.who_can_edit_money && settings.who_can_edit_money.length){
this.settings.who_can_edit_money = settings.who_can_edit_money;
}
this.settings.check_for_update = (!! ~~ settings.check_for_update)? true : false;
this.settings.check_how_often = (settings.check_how_often && settings.check_how_often.length)? settings.check_how_often : 2;
this.bank.settings.enabled = (!! ~~ settings.bank_enabled)? true : false;
this.settings.money_text = settings.money_text;
this.settings.money_symbol = settings.money_symbol;
this.settings.money_separator = (settings.separator && settings.separator.length)? settings.separator : this.settings.money_separator;
this.settings.money_separator += (!! ~~ settings.separator_space)? " " : "";
if(settings.money_symbol_image && settings.money_symbol_image.length){
this.settings.money_symbol = "<img class='money-sym-img' src='" + settings.money_symbol_image + "' />";
}
this.settings.decimal_money = (!! ~~ settings.decimal_money)? true : false;
this.settings.posting.earn_from_quick_reply = (parseInt(settings.earn_from_quick_reply) || this.settings.posting.earn_from_quick_reply);
this.settings.posting.amounts.per_thread = this.format(settings.money_per_thread);
this.settings.posting.amounts.per_poll = this.format(settings.money_per_poll);
this.settings.posting.amounts.per_reply = this.format(settings.money_per_reply);
this.settings.posting.amounts.per_quick_reply = this.format(settings.money_per_quick_reply);
if(settings.categories_earn_amounts && settings.categories_earn_amounts.length){
for(var c = 0, cl = settings.categories_earn_amounts.length; c < cl; c ++){
var cat_earn_amounts = settings.categories_earn_amounts[c];
var cat_amounts = {
per_thread: this.format(cat_earn_amounts.money_per_thread),
per_poll: this.format(cat_earn_amounts.money_per_poll),
per_reply: this.format(cat_earn_amounts.money_per_reply),
per_quick_reply: this.format(cat_earn_amounts.money_per_quick_reply)
};
for(var ci = 0, cil = cat_earn_amounts.category.length; ci < cil; ci ++){
this.settings.posting.amounts.categories[cat_earn_amounts.category[ci]] = cat_amounts;
}
}
}
if(settings.boards_earn_amounts && settings.boards_earn_amounts.length){
for(var b = 0, bl = settings.boards_earn_amounts.length; b < bl; b ++){
var board_earn_amounts = settings.boards_earn_amounts[b];
var board_amounts = {
per_thread: this.format(board_earn_amounts.money_per_thread),
per_poll: this.format(board_earn_amounts.money_per_poll),
per_reply: this.format(board_earn_amounts.money_per_reply),
per_quick_reply: this.format(board_earn_amounts.money_per_quick_reply)
};
for(var bi = 0, bil = board_earn_amounts.board.length; bi < bil; bi ++){
this.settings.posting.amounts.boards[board_earn_amounts.board[bi]] = board_amounts;
}
}
}
this.settings.no_earn_members = settings.no_earn_members;
this.settings.no_earn_groups = settings.no_earn_groups;
this.settings.no_earn_categories = settings.no_earn_categories;
this.settings.no_earn_boards = settings.no_earn_boards;
this.settings.no_earn_threads = settings.no_earn_threads;
this.settings.text.wallet = (settings.wallet_text && settings.wallet_text.length)? settings.wallet_text : this.settings.text.wallet;
this.settings.notification.show = (!! ~~ settings.n_show_notification)? true : false;
this.settings.notification.show_edited = (!! ~~ settings.n_show_edited_by)? true : false;
}
},
/**
* Checks if the user is allowed to edit another users money. If main admin, they can edit their own.
*
* @return {Boolean}
*/
is_allowed_to_edit_money: function(){
if(!this.settings.staff_edit_money || !yootil.user.logged_in() || !yootil.user.is_staff()){
return false;
}
if(this.settings.who_can_edit_money.length){
if($.inArrayLoose(yootil.user.id(), this.settings.who_can_edit_money) > -1 || yootil.user.id() == 1){
return true;
}
return false;
} else if(yootil.user.id() == 1){
return true;
}
return false;
},
/**
* Binds a dialog to the money and bank values on a users profile so staff with permissions can edit those values easily.
*
* @param {String} element HTML element we are binding too (not really needed though).
* @param {Number} user_id The users id we are binding to for editing.
* @param {Boolean} bank Is this a bank bind or wallet?
* @param {String} update_selector Selector for the element we need to update after editing money.
* @param {String} edit_image The edit image next to the money value.
* @return {Object} HTML elment jQuery wrapped/
*/
bind_edit_dialog: function(element, user_id, bank, update_selector, edit_image){
var self = this;
var bank_edit = (bank)? true : false;
var bank_str = (bank_edit)? "bank_" : "";
var title = (bank_edit)? (this.bank.settings.text.bank + " Balance") : this.settings.money_text;
var old_money = self.data(user_id).get[((bank_edit)? "bank" : "money")]();
element = $(element);
if(self.settings.staff_edit_money && yootil.key.write(self.KEY, user_id) && yootil.user.is_staff() && (yootil.user.id() != user_id || yootil.user.id() == 1) && this.is_allowed_to_edit_money()){
var edit_html = "";
edit_html += "<p>" + this.settings.money_symbol + ": <input type='text' style='width: 100px' name='edit_" + bank_str + "money' /> <button id='set_" + bank_str + "money'>Set</button> <button id='reset_" + bank_str + "money'>Reset</button></p>";
edit_html += "<p style='margin-top: 10px;'>" + this.settings.money_symbol + ": <input type='text' style='width: 100px' name='edit_specific_" + bank_str + "money' value='" + this.format(0, true) + "' /> <button id='add_specific_" + bank_str + "money'>Add</button> <button id='remove_specific_" + bank_str + "money'>Remove</button></p>";
edit_html = $("<span />").html(edit_html);
element.click(function(event){
pb.window.dialog("edit_money", {
title: ("Edit " + title),
modal: true,
height: 180,
width: 300,
resizable: false,
draggable: false,
html: edit_html,
open: function(){
var key = (bank_edit)? "bank" : "money";
/**
* Triggers when opening the edit money dialog box on the profile.
*
* $(monetary.event).on("on_open_edit_money", function(event, data){
* console.log(data.type);
* });