/
shop.js
2231 lines (1695 loc) · 74.5 KB
/
shop.js
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.shop
* @static
*
* Creates a shop on the forum so that members can purchase items.
*/
monetary.shop = (function(){
return {
/**
* @property {String} required_monetary_version The minimum version required of the Monetary System.
*/
required_monetary_version: "0.9.0",
/**
* @property {String} VERSION Current version.
*/
VERSION: "{VER}",
/**
* @property {Object} settings Default settings for the shop, these get overwritten in setup.
* @property {Boolean} settings.enabled Module enabled or not.
* @property {Boolean} settings.icon_enabled If enable, an icon will show in the Yootil bar.
* @property {String} settings.base_image The base URL for the images.
* @property {Boolean} settings.show_total_bough If enabled, on profiles it will show total bought.
* @property {Boolean} settings.items_private Items can be private to the user if enabled (kinda pointless).
* @property {Number} settings.refund_percent The default refund percentage amount per item.
* @property {Boolean} settings.allow_removing If disable then items can not be removed.
* @property {Array} settings.no_members Users who are not allowed to use the shop.
* @property {Array} settings.no_groups Groups that can not use the shop.
* @property {Number} settings.image_width Image width for shop and profile.
* @property {Number} settings.image_height Image height for shop and profile.
* @property {Number} settings.image_percent Used when doing percentage scaling.
* @property {Boolean} settings.show_in_mini_profile If enable, items will show in mini profiles.
* @property {Number} settings.mini_image_width The width of images in the mini profile.
* @property {Number} settings.mini_image_height The height of images in the mini profile.
* @property {Number} settings.mini_image_percent Used when doing percentage scaling.
* @property {Number} settings.mini_limit_shown Can limit the amount of items that show in a mini profile.
* @property {Boolean} settings.show_quantity_drop_down If enable, the drop down will show on the shop page.
* @property {Object} settings.text Text replacements.
* @property {String} settings.text.monetary_shop Name of the shop.
* @property {String} settings.text.shop
* @property {String} settings.text.name
* @property {String} settings.text.description
* @property {String} settings.text.item
* @property {String} settings.text.basket
* @property {String} settings.text.search
* @property {String} settings.text.results
* @property {String} settings.text.purchase
* @property {String} settings.text.purchased
* @property {String} settings.text.price
* @property {String} settings.text.last_bought
* @property {String} settings.text.total_cost
* @property {String} settings.text.total_amount
* @property {String} settings.text.thank_you
* @property {String} settings.text.add_to_cart
* @property {String} settings.text.checkout
* @property {String} settings.text.remove
* @property {String} settings.text.quantity
* @property {String} settings.text.paid
* @property {String} settings.text.refund
* @property {String} settings.text.accept
* @property {Boolean} settings.welcome_message_enabled If enable, a welcome message will show on the shop page.
* @property {String} settings.welcome_message_title
* @property {String} settings.welcome_message_message
* @property {String} settings.random_image Default image used when it can't find the item image.
*/
settings: {
enabled: true,
icon_enabled: true,
base_image: "",
show_total_bought: true,
items_private: false,
refund_percent: 50,
allow_removing: false,
no_members: [],
no_groups: [],
image_width: 0,
image_height: 0,
image_percent: 0,
show_in_mini_profile: true,
mini_image_width: 0,
mini_image_height: 0,
mini_image_percent: 0,
mini_limit_shown: 0,
show_quantity_drop_down: false,
text: {
monetary_shop: "Monetary Shop",
shop: "Shop",
name: "Name",
description: "Description",
item: "Item",
basket: "Basket",
search: "Search",
results: "Results",
purchase: "Purchase",
purchased: "Purchased",
price: "Price",
last_bought: "Last Bought",
total_cost: "Total Cost",
total_amount: "Total Amount",
thank_you: "Thank You",
add_to_cart: "Add To Cart",
checkout: "Checkout",
remove: "Remove",
quantity: "Quantity",
paid: "Paid",
refund: "Refund",
accept: "Accept"
},
welcome_message_enabled: false,
welcome_message_title: "",
welcome_message_message: "",
random_image: null
},
/**
* @property {Object} images Plugin images.
*/
images: {},
/**
* @property {Object} plugin The ProBoards plugin.
*/
plugin: {},
/**
* @property {Array} items Holds all the items stored in the plugin.
*/
items: [],
/**
* @property {Array} categories Categories created by the use in the plugin admin area.
*/
categories: [],
/**
* @property {Object} category_lookup Stores all the categories for quick lookup. Key is cat id.
*/
category_lookup: {},
/**
* @property {Object} category_items Holds all the items in certain category tables.
*/
category_items: {},
/**
* @property {Array} cart Items the user has added to their cart.
*/
cart: [],
/**
* @property {Object} lookup Item lookup table.
*/
lookup: {},
/**
* @property {Object} user_data_table Holds all the data for each user here for lookup.
*/
user_data_table: {},
/**
* @property {String} KEY The key for this plugin.
*/
KEY: "pixeldepth_money_shop",
/**
* @property {Array} modules Sub modules are added here.
*/
modules: [],
/**
* @property {Object} random_id_pool Used for random items in the shop. Lookup for random items pool.
*/
random_id_pool: {},
/**
* This is called from the main class. Each module gets registered and a loop goes through and calls this.
*/
init: function(){
if(typeof yootil == "undefined"){
return;
}
this.check_monetary_version();
this.setup_user_data_table();
this.setup();
if(!this.settings.enabled){
monetary.show_default();
return;
}
if(yootil.user.logged_in() && this.settings.icon_enabled){
if(this.images.shop){
yootil.bar.add("/?monetaryshop", this.images.shop, this.settings.text.shop, "pdmsshop");
}
}
if(yootil.user.logged_in() && location.href.match(/\?monetaryshop$/i)){
if(this.can_use_shop()){
yootil.create.nav_branch(yootil.html_encode(location.href), this.settings.text.monetary_shop);
yootil.create.page("?monetaryshop", this.settings.text.monetary_shop);
this.build_shop_html();
} else {
yootil.create.nav_branch(yootil.html_encode(location.href), this.settings.text.monetary_shop + " - An Error Has Occurred");
yootil.create.page("?monetaryshop", this.settings.text.monetary_shop + " - An Error Has Occurred");
var html = "";
html += "<div class='monetary-shop-notice-icon'><img src='" + monetary.images.info + "' /></div>";
html += "<div class='monetary-shop-notice-content'>You do not have permission to access the " + this.settings.text.shop + ".</div>";
var container = yootil.create.container("An Error Has Occurred", html).show();
container.appendTo("#content");
}
} else if(yootil.location.profile_home()){
if(yootil.user.is_staff() || !this.settings.items_private || (this.settings.items_private && yootil.user.id() == yootil.page.member.id())){
this.create_shop_item_box();
}
}
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.modules.length){
for(var m = 0, ml = this.modules.length; m < ml; m ++){
if(this.modules[m].init){
this.modules[m].init();
}
}
}
},
/**
* Returns the curren version of the shop.
*
* @returns {String}
*/
version: function(){
return this.VERSION;
},
/**
* Checks the version of the Monetary System plugin is compatiable with this module.
*/
check_monetary_version: function(){
var versions = yootil.convert_versions(monetary.version(), this.required_monetary_version);
if(versions[0] < versions[1]){
var msg = "<p>The Monetary System - Shop requires at least " + yootil.html_encode(this.required_monetary_version) + " of the <a href='http://support.proboards.com/thread/429360/'>Monetary System</a> plugin to function correctly.</p>";
msg += "<p style='margin-top: 8px;'>For more information, please visit the the <a href='http://support.proboards.com'>ProBoards support forum</a>.</p>";
var container = yootil.create.container("Monetary System - Shop", msg).show();
$("div#content").prepend(container);
}
},
/**
* Handles overwriting default values. These come from the plugin settings.
* Lookup tables for categories, items etc are created here as well.
*/
setup: function(){
if(monetary.plugin){
this.plugin = pb.plugin.get("pixeldepth_monetary_shop");
var settings = this.plugin.settings;
this.images = this.plugin.images;
if(this.plugin && this.plugin.settings){
this.settings.enabled = (!! ~~ settings.shop_enabled)? true : false;
this.settings.icon_enabled = (!! ~~ settings.shop_icon_enabled)? true : false;
this.items = (settings.shop_items && settings.shop_items.length)? settings.shop_items : this.items;
this.settings.base_image = (settings.item_image_base && settings.item_image_base.length)? settings.item_image_base : this.settings.base_image;
this.settings.refund_percent = (settings.refund_percent && settings.refund_percent.length)? settings.refund_percent : this.settings.refund_amount;
this.settings.show_total_bought = (!! ~~ settings.show_total_bought)? true : false;
this.settings.items_private = (!! ~~ settings.items_private)? true : false;
this.settings.allow_removing = (!! ~~ settings.allow_removing)? true : false;
this.settings.no_members = settings.no_members || [];
this.settings.no_groups = settings.no_groups || [];
this.settings.text.monetary_shop = (settings.txt_monetary_shop && settings.txt_monetary_shop.length)? settings.txt_monetary_shop : this.settings.text.monetary_shop;
this.settings.text.shop = (settings.txt_shop && settings.txt_shop.length)? settings.txt_shop : this.settings.text.shop;
this.settings.text.name = (settings.txt_name && settings.txt_name.length)? settings.txt_name : this.settings.text.name;
this.settings.text.description = (settings.txt_description && settings.txt_description.length)? settings.txt_description : this.settings.text.description;
this.settings.text.item = (settings.txt_item && settings.txt_item.length)? settings.txt_item : this.settings.text.item;
this.settings.text.basket = (settings.txt_basket && settings.txt_basket.length)? settings.txt_basket : this.settings.text.basket;
this.settings.text.search = (settings.txt_search && settings.txt_search.length)? settings.txt_search : this.settings.text.search;
this.settings.text.results = (settings.txt_results && settings.txt_results.length)? settings.txt_results : this.settings.text.results;
this.settings.text.purchase = (settings.txt_purchase && settings.txt_purchase.length)? settings.txt_purchase : this.settings.text.purchase;
this.settings.text.purchased = (settings.txt_purchased && settings.txt_purchased.length)? settings.txt_purchased : this.settings.text.purchased;
this.settings.text.price = (settings.txt_price && settings.txt_price.length)? settings.txt_price : this.settings.text.price;
this.settings.text.last_bought = (settings.txt_last_bought && settings.txt_last_bought.length)? settings.txt_last_bought : this.settings.text.last_bought;
this.settings.text.total_cost = (settings.txt_total_cost && settings.txt_total_cost.length)? settings.txt_total_cost : this.settings.text.total_cost;
this.settings.text.total_amount = (settings.txt_total_amount && settings.txt_total_amount.length)? settings.txt_total_amount : this.settings.text.total_amount;
this.settings.text.thank_you = (settings.txt_thank_you && settings.txt_thank_you.length)? settings.txt_thank_you : this.settings.text.thank_you;
this.settings.text.add_to_cart = (settings.txt_add_to_cart && settings.txt_add_to_cart.length)? settings.txt_add_to_cart : this.settings.text.add_to_cart;
this.settings.text.checkout = (settings.txt_checkout && settings.txt_checkout.length)? settings.txt_checkout : this.settings.text.checkout;
this.settings.text.remove = (settings.txt_remove && settings.txt_remove.length)? settings.txt_remove : this.settings.text.remove;
this.settings.text.quantity = (settings.txt_quantity && settings.txt_quantity.length)? settings.txt_quantity : this.settings.text.quantity;
this.settings.text.paid = (settings.txt_paid && settings.txt_paid.length)? settings.txt_paid : this.settings.text.paid;
this.settings.text.refund = (settings.txt_refund && settings.txt_refund.length)? settings.txt_refund : this.settings.text.refund;
this.settings.text.accept = (settings.txt_accept && settings.txt_accept.length)? settings.txt_accept : this.settings.text.accept;
this.settings.welcome_message_enabled = (!! ~~ settings.show_message)? true : false;
this.settings.welcome_message_title = (settings.welcome_title && settings.welcome_title.length)? settings.welcome_title : this.settings.welcome_message_title;
this.settings.welcome_message_message = (settings.welcome_message && settings.welcome_message.length)? settings.welcome_message : this.settings.welcome_message_message;
this.settings.image_width = (settings.item_img_width && settings.item_img_width.length)? settings.item_img_width : 0;
this.settings.image_height = (settings.item_img_height && settings.item_img_height.length)? settings.item_img_height : 0;
this.settings.image_percent = (settings.img_size_percent && settings.img_size_percent.length)? settings.img_size_percent : 0;
this.settings.show_in_mini_profile = (!! ~~ settings.show_in_mini_profile)? true : false;
this.settings.mini_image_width = (settings.mini_img_width && settings.mini_img_width.length)? settings.mini_img_width : 0;
this.settings.mini_image_height = (settings.mini_img_height && settings.mini_img_height.length)? settings.mini_img_height : 0;
this.settings.mini_image_percent = (settings.mini_img_percent && settings.mini_img_percent.length)? settings.mini_img_percent : 0;
this.settings.mini_limit_shown = (!! ~~ settings.limit_shown)? (~~ settings.limit_shown) : 0;
this.settings.show_quantity_drop_down = (!! ~~ settings.show_quantity_select)? true : false;
var categories = settings.categories;
if(categories.length){
categories = categories.sort(function(a, b){
return (a["category_name"] > b["category_name"])? 1 : 0;
});
for(var c = 0; c < categories.length; c ++){
this.category_lookup[categories[c].category_id] = categories[c];
this.categories.push(categories[c].category_name);
}
this.categories.sort();
}
// Support for extra items
// Current we support 3 extra plugins, but it does allow for more
var extra_items_plugin_1 = proboards.plugin.get("monetary_shop_extra_items_1");
if(extra_items_plugin_1 && extra_items_plugin_1.settings){
var extra_1_settings = extra_items_plugin_1.settings;
if(extra_1_settings.shop_items && extra_1_settings.shop_items.length){
this.items = this.items.concat(extra_1_settings.shop_items);
}
}
var extra_items_plugin_2 = proboards.plugin.get("monetary_shop_extra_items_2");
if(extra_items_plugin_2 && extra_items_plugin_2.settings){
var extra_2_settings = extra_items_plugin_2.settings;
if(extra_2_settings.shop_items && extra_2_settings.shop_items.length){
this.items = this.items.concat(extra_2_settings.shop_items);
}
}
var extra_items_plugin_3 = proboards.plugin.get("monetary_shop_extra_items_3");
if(extra_items_plugin_3 && extra_items_plugin_3.settings){
var extra_3_settings = extra_items_plugin_3.settings;
if(extra_3_settings.shop_items && extra_3_settings.shop_items.length){
this.items = this.items.concat(extra_3_settings.shop_items);
}
}
for(var i = 0, l = this.items.length; i < l; i ++){
this.lookup[this.items[i].item_id] = this.items[i];
if(typeof this.items[i].item_max_quantity === "undefined"){
this.lookup[this.items[i].item_id].item_max_quantity = 0;
}
if(typeof this.items[i].item_show === "undefined" || !this.items[i].item_show.length){
this.lookup[this.items[i].item_id].item_show = 1;
}
if(this.category_lookup[this.items[i].item_category]){
if(this.items[i].item_show == 1){
if(!this.category_items[this.items[i].item_category]){
this.category_items[this.items[i].item_category] = [];
}
this.category_items[this.items[i].item_category].push(this.items[i]);
}
}
}
this.settings.random_image = this.images.hidden;
if(settings.random_items && settings.random_items.length){
this.prepare_random_items(settings.random_items);
}
//this.setup_specials();
}
}
},
// If there is no id pool, then we don't add the random item
// to the lookup table
/**
* Prepares random items.
* Random items can be based on a list of ids, or min and max price.
* Each random item that is valid gets added to the random pool lookup table that is used later at checkout.
*
* @param {Array} items Random items from the plugin.
*/
prepare_random_items: function(items){
for(var i = 0, l = items.length; i < l; i ++){
var id_pool = [];
if(items[i].item_ids.length){
var tmp_id_pool = items[i].item_ids.replace(/\s+/g, "").split(",");
// Loop through and make sure the item exists
for(var e = 0, el = tmp_id_pool.length; e < el; e ++){
if(this.lookup[tmp_id_pool[e]]){
id_pool.push(tmp_id_pool[e]);
}
}
}
if(items[i].minimum_price && items[i].maximum_price && items[i].minimum_price.length && items[i].maximum_price.length){
var min_price = parseFloat(items[i].minimum_price);
var max_price = parseFloat(items[i].maximum_price);
if(max_price < min_price){
max_price = min_price;
}
for(var key in this.lookup){
if(parseFloat(this.lookup[key].item_price) >= min_price && parseFloat(this.lookup[key].item_price) <= max_price){
id_pool.push(key);
}
}
}
if(items[i].use_cat_id && items[i].use_cat_id.length){
var the_items = this.category_items[items[i].use_cat_id];
if(the_items && the_items.length){
for(var ci = 0, cil = the_items.length; ci < cil; ci ++){
id_pool.push(the_items[ci].item_id);
}
}
}
if(id_pool.length){
this.random_id_pool["___" + i + "___"] = id_pool;
} else {
continue;
}
items[i].item_id = "___" + i + "___";
items[i].item_discount = 0;
items[i].item_show = 1;
items[i].item_tradable = 1;
items[i].item_refundable = 1;
this.lookup[items[i].item_id] = items[i];
if(typeof items[i].item_max_quantity === "undefined"){
this.lookup[items[i].item_id].item_max_quantity = 0;
}
if(typeof items[i].item_show === "undefined" || !items[i].item_show.length){
this.lookup[items[i].item_id].item_show = 1;
}
if(this.category_lookup[items[i].item_category]){
if(!this.category_items[items[i].item_category]){
this.category_items[items[i].item_category] = [];
}
this.category_items[items[i].item_category].push(items[i]);
}
}
},
/**
* Makes the id safer to use.
*
* @param {String} id
* @return {String}
*/
safe_id: function(id){
return id.toString().replace(/[^\d\.\_]+/g, "");
},
/**
* Checks to see if the user can use the shop. This check the no_members and no_groups arrays.
*
* @returns {Boolean}
*/
can_use_shop: function(){
if(this.settings.no_members.length){
if($.inArrayLoose(yootil.user.id(), this.settings.no_members) > -1){
return false;
}
}
if(this.settings.no_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_groups) > -1){
return false;
}
}
}
return true;
},
/**
* Gets the users data, if it doesn't exist then a new instance of Data is created and added to the lookup.
*
* @param {Number} user_id
* @returns {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;
},
/**
* 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){
this.user_data_table[key] = new this.Data(key, all_data[key]);
}
},
/**
* Coming soon...
* @ignore
*/
setup_specials: function(){
var colored_name = {
item_id: "s1",
item_name: "Colored Name",
item_description: "Have a colored name across the forum.",
item_price: 1500,
item_image: "",
item_category: 99,
item_refundable: 0,
item_tradable: 0
};
this.category_items[99] = [];
this.category_items[99].push(colored_name);
this.lookup["s1"] = colored_name;
},
/**
* Registers this module to the money class.
* @returns {Object}
*/
register: function(){
monetary.modules.push(this);
return this;
},
/**
* Checks to see if the user has any items.
*
* @returns {Boolean}
*/
has_items: function(){
var user_id = yootil.page.member.id() || yootil.user.id();
var items = this.data(user_id).get.items();
for(var key in items){
if(this.lookup[key]){
return true;
}
}
return false;
},
/**
* Checks what image settings are being used and return the inline style.
*
* @param {Boolean} mini Is it for the mini profile or profiles.
* @returns {String} inline style created.
*/
get_size_css: function(mini){
if(mini){
if(this.settings.mini_image_width > 0 && this.settings.mini_image_height > 0){
return 'style="max-height: ' + this.settings.mini_image_height + 'px; max-width: ' + this.settings.mini_image_width + 'px;"';
}
} else {
if(this.settings.image_width > 0 && this.settings.image_height > 0){
return ' style="max-height: ' + this.settings.image_height + 'px; max-width: ' + this.settings.image_width + 'px;"';
}
}
return "";
},
/**
* Gets the item image, otherwise uses the default image setup by the plugin.
*
* @param {Object} item The item.
* @returns {String}
*/
get_image_src: function(item){
var img = (item.item_image && item.item_image.length)? item.item_image : this.settings.random_image;
if(!img.match(/^http/i)){
img = this.settings.base_image + img;
}
return img;
},
/**
* Creates the box for the items on user profiles.
*/
create_shop_item_box: function(){
if(!this.has_items()){
return;
}
var box = yootil.create.profile_content_box();
var first_box = $("form.form_user_status .content-box:first");
var using_custom = false;
if($("#monetary_shop_items").length){
first_box = $("#monetary_shop_items");
using_custom = true;
}
if(first_box.length){
var items_html = "";
var user_id = yootil.page.member.id() || yootil.user.id();
var items = this.data(user_id).get.items();
var time_24 = (yootil.user.time_format() == "12hr")? false : true;
var img_size = this.get_size_css();
var disp = (!img_size.length && parseInt(this.settings.image_percent) > 0)? " style='display: none;'" : "";
for(var key in items){
var num = "";
if(items[key].q > 1 && this.settings.show_total_bought){
num = '<span class="shop_item">';
num += '<img' + disp + ' class="shop_item_x" src="' + this.images.x + '" />';
if(items[key].q >= 99){
num += '<img' + disp + ' class="shop_item_num" src="' + this.images["9"] + '" /><img class="shop_item_num shop_item_num_last" src="' + this.images["9"] + '" />';
} else {
var str = items[key].q.toString();
for(var s = 0; s < str.length; s ++){
var klass = (s > 0)? " shop_item_num_last" : "";
num += '<img' + disp + ' class="shop_item_num' + klass + '" src="' + this.images[str.substr(s, 1)] + '" />';
}
}
num += '</span>';
}
if(this.lookup[key]){
items_html += '<div data-shop-item-id="' + this.lookup[key].item_id + '" title="' + yootil.html_encode(this.lookup[key].item_name) + '" class="shop_items_list"><img src="' + this.get_image_src(this.lookup[key]) + '"' + img_size + disp + ' />' + num + '</div>';
}
}
if(using_custom){
first_box.addClass("monetary_shop_profile_box").show().html(items_html);
} else {
box.addClass("monetary_shop_profile_box");
box.html(items_html);
if(yootil.user.id() == yootil.page.member.id()){
box.insertAfter(first_box);
} else {
box.insertBefore(first_box);
}
}
if(parseInt(this.settings.image_percent) > 0){
var self = this;
$(".shop_items_list").each(function(){
$(this).find("img:first").bind("load", function(){
var width = this.width;
var height = this.height;
this.width = (width - (width * (parseInt(self.settings.image_percent) / 100)));
this.height = (height - (height * (parseInt(self.settings.image_percent) / 100)));
$(this).parent().find("img").fadeIn("slow");
});
});
}
this.bind_refundable_dialog();
/*if(yootil.user.id() == yootil.page.member.id()){
this.setup_reordering();
}*/
}
},
// Browsers reorder the elements if the keys look like numbers
// It's too later to be changing the type, and I don't really
// want to start storing indexes in the key to allow ordering.
// Disabled for now.
/*setup_reordering: function(){
var self = this;
var box = $(".monetary_shop_profile_box");
if(box.length){
box.sortable({
axis: "x",
cursor: "move",
items: " > div",
opacity: 0.5,
revert: true,
helper: "clone",
update: function(evt, ui){
var item_id = ui.item.attr("data-shop-item-id");
var items = self.data(yootil.user.id()).get.items();
var tmp_obj = {};
if(items[item_id]){
tmp_obj[item_id.toString()] = items[item_id];
for(var k in items){
if(k != item_id){
tmp_obj[k.toString()] = items[k];
}
}
console.log(tmp_obj);
}
}
});
}
},*/
/**
* Updates the item count (if enabled) of items when an action is performed (i.e refund).
*
* @param {String} item_id
* @param {Number} quantity
*/
update_item_count: function(item_id, quantity){
if(!this.settings.show_total_bought){
return;
}
var item = $(".shop_items_list[data-shop-item-id='" + this.safe_id(item_id) + "'] .shop_item");
if(item){
item.empty();
var num = "";
if(quantity > 1){
num += '<img class="shop_item_x" src="' + this.images.x + '" />';
if(quantity >= 99){
num += '<img class="shop_item_num" src="' + this.images["9"] + '" /><img class="shop_item_num shop_item_num_last" src="' + this.images["9"] + '" />';
} else {
var str = quantity.toString();
for(var s = 0; s < str.length; s ++){
var klass = (s > 0)? " shop_item_num_last" : "";
num += '<img class="shop_item_num' + klass + '" src="' + this.images[str.substr(s, 1)] + '" />';
}
}
}
item.html(num);
}
},
/**
* Builds the info dialog when clicking on an item on the profile page.
*
* @param {Object} item_elem The div the item sits in.
*/
build_item_info_dialog: function(item_elem){
var self = this;
var owner = yootil.page.member.id() || yootil.user.id();
var item_id = this.safe_id($(item_elem).attr("data-shop-item-id"));
var bought_item = self.data(owner).get.item(item_id);
var shop_item = self.lookup[item_id];
if(!shop_item){
return;
}
var msg = "";
msg += "<div>";
msg += "<div class='item_info_img'><img src='" + self.get_image_src(shop_item) + "' /></div>";
msg += "<div class='item_info_info'>";
msg += "<p><strong>" + self.settings.text.item + " " + self.settings.text.name + ":</strong> " + shop_item.item_name + "</p>";
msg += "<p><strong>" + self.settings.text.quantity + ":</strong> <span id='shop_item_quantity'>" + bought_item.q + "</span></p>";
msg += "<p><strong>" + self.settings.text.price + " " + self.settings.text.paid + ":</strong> " + monetary.settings.money_symbol + yootil.number_format(monetary.format(bought_item.p, true)) + "</p>";
msg += "<p><strong>" + self.settings.text.total_cost + ":</strong> <span id='shop_item_total_cost'>" + monetary.settings.money_symbol + yootil.number_format(monetary.format(bought_item.p * bought_item.q, true)) + "</span></p>";
msg += "<p class='item_info_desc'>" + pb.text.nl2br(shop_item.item_description) + "</p>";
msg += "</div>";
msg += "</div>";
var info_buttons = {
"Close": function(){
$(this).dialog("close");
}
};
if(shop_item.item_refundable == "1" && yootil.user.id() == owner){
var refund_buttons = {
"Close": function(){
$(this).dialog("close");
}
};
refund_buttons[self.settings.text.accept + " " + self.settings.text.refund] = this.build_refund_dialog(item_id);
info_buttons[self.settings.text.refund] = function(){
pb.window.dialog("monetaryshop-item-refund-dialog", {
modal: true,
height: 220,
width: 350,
title: self.settings.text.refund,
html: self.build_refund_msg(item_id),
resizable: false,
draggable: false,
buttons: refund_buttons
});
};
}
if(this.settings.allow_removing && yootil.user.is_staff()){
info_buttons["Remove"] = function(){
var elem = this;
pb.window.dialog("monetaryshop-item-remove-dialog", {
modal: true,
height: 160,
width: 350,
title: self.settings.text.remove,
html: self.build_remove_msg(item_id),
resizable: false,
draggable: false,
buttons: {
"Cancel": function(){
$(this).dialog("close");
},
"Remove": self.build_remove_dialog(item_id)
}
});
};
}
var refund_txt = "";
if(yootil.user.id() == owner){
refund_txt = (shop_item.item_refundable == "1")? "" : " (Not Refundable)";
}
pb.window.dialog("monetaryshop-item-info-dialog", {
modal: true,
height: 320,
width: 600,
title: shop_item.item_name + refund_txt,
html: msg,
resizable: false,
draggable: false,
buttons: info_buttons
});
},
/**
* Builds the remove message when removing items from the profile.
*
* @param {String} item_id The ID for the item that will get removed.
* @returns {Object} Message jQuery wrapped.
*/
build_remove_msg: function(item_id){
var self = this;
var item_counter = 0;
var remove_msg = "<strong>" + self.settings.text.quantity + " to " + self.settings.text.remove.toLowerCase() + ":</strong> ";
var bought_item = self.data(yootil.page.member.id()).get.item(item_id);
var one_remove = (parseFloat(bought_item.p) * (self.settings.refund_percent / 100)) * 1;
remove_msg += "<select id='shop_item_remove_quantity'>";
while(item_counter < bought_item.q){
item_counter ++;
remove_msg += "<option value='" + item_counter + "'> " + item_counter + " </option>";
}
remove_msg += "</select>";
remove_msg = $("<div>" + remove_msg + "</div>");
return remove_msg;
},
/**
* Builds the remove item dialog for items on the profile page.
*
* @param {String} item_id The item id for which item will be removed.
* @returns {Function}
*/