-
Notifications
You must be signed in to change notification settings - Fork 11
/
ManagePaid.php
1847 lines (1663 loc) · 55.4 KB
/
ManagePaid.php
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
<?php
/**
* Configuration of payment gateways for paid subscriptions.
*
* Wedge (http://wedge.org)
* Copyright © 2010 René-Gilles Deberdt, wedge.org
* Portions are © 2011 Simple Machines.
* License: http://wedge.org/license/
*/
if (!defined('WEDGE'))
die('Hacking attempt...');
/* This file contains all the screens that control settings for topics and
posts.
void ManagePaidSubscriptions()
- the main entrance point for the 'Paid Subscription' screen.
- accessed from ?action=admin;area=paidsubscribe.
- calls the right function based on the given sub-action.
- defaults to sub-action 'view'.
- requires admin_forum permission for admin based actions.
void ModifySubscriptionSettings()
- set any setting related to paid subscriptions.
- requires the moderate_forum permission
- accessed from ?action=admin;area=paidsubscribe;sa=settings.
void ViewSubscriptions()
- view a list of all the current subscriptions
- requires the admin_forum permission
- accessed from ?action=admin;area=paidsubscribe;sa=view.
void ModifySubscription()
- edit a subscription.
- accessed from ?action=admin;area=paidsubscribe;sa=modify.
void ViewSubscribedUsers()
- view a list of all the users who currently have a subscription.
- requires the admin_forum permission.
- subscription ID is required, in the form of $_GET['sid'].
- accessed from ?action=admin;area=paidsubscribe;sa=viewsub.
int list_getSubscribedUserCount()
// !!
array list_getSubscribedUsers()
// !!
void ModifyUserSubscription()
- edit a users subscription.
- accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
void reapplySubscriptions(array users)
- reapplies all subscription rules for each of the users.
void addSubscription(int id_subscribe, int id_member)
- add/extend a subscription for a member.
void removeSubscription(int id_subscribe, int id_member)
- remove a subscription from a user.
array loadPaymentGateways()
- checks the app folder for any files fitting the format of a payment gateway.
- loads each file to check it's valid.
- includes each file and returns the function name and whether it should work with this version of Wedge.
*/
function ManagePaidSubscriptions()
{
global $context, $txt, $settings;
// Load the required language and template.
loadLanguage('ManagePaid');
loadTemplate('ManagePaid');
$subActions = array(
'modify' => array('ModifySubscription', 'admin_forum'),
'modifyuser' => array('ModifyUserSubscription', 'admin_forum'),
'settings' => array('ModifySubscriptionSettings', 'admin_forum'),
'view' => array('ViewSubscriptions', 'admin_forum'),
'viewsub' => array('ViewSubscribedUsers', 'admin_forum'),
);
// Default the sub-action to 'view subscriptions', but only if they have already set things up..
$_REQUEST['sa'] = isset($_REQUEST['sa'], $subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (!empty($settings['paid_enabled']) && !empty($settings['paid_currency_symbol']) ? 'view' : 'settings');
// Make sure you can do this.
isAllowedTo($subActions[$_REQUEST['sa']][1]);
$context['page_title'] = $txt['paid_subscriptions'];
// Tabs for browsing the different subscription functions.
$context[$context['admin_menu_name']]['tab_data'] = array(
'title' => $txt['paid_subscriptions'],
'description' => $txt['paid_subscriptions_desc'],
'tabs' => array(
'view' => array(
'description' => $txt['paid_subs_view_desc'],
),
'settings' => array(
'description' => $txt['paid_subs_settings_desc'],
),
),
);
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']][0]();
}
// Modify which payment methods are to be used.
function ModifySubscriptionSettings($return_config = false)
{
global $context, $txt, $settings;
// If the currency is set to something different then we need to set it to other for this to work and set it back shortly.
$settings['paid_currency'] = !empty($settings['paid_currency_code']) ? $settings['paid_currency_code'] : '';
if (!empty($settings['paid_currency_code']) && !in_array($settings['paid_currency_code'], array('usd', 'eur', 'gbp')))
$settings['paid_currency'] = 'other';
// These are all the default settings.
$config_vars = array(
array('check', 'paid_enabled', 'subtext' => $txt['paid_enabled_desc']),
);
if (!empty($settings['paid_enabled']))
{
$config_vars = array_merge($config_vars, array(
array('select', 'paid_email', array(0 => $txt['paid_email_no'], 1 => $txt['paid_email_error'], 2 => $txt['paid_email_all']), 'subtext' => $txt['paid_email_desc']),
array('text', 'paid_email_to', 'subtext' => $txt['paid_email_to_desc'], 'size' => 60),
'',
'dummy_currency' => array('select', 'paid_currency', array('usd' => $txt['usd'], 'eur' => $txt['eur'], 'gbp' => $txt['gbp'], 'other' => $txt['other']), 'javascript' => ' onchange="toggleOther();"'),
array('text', 'paid_currency_code', 'subtext' => $txt['paid_currency_code_desc'], 'size' => 5, 'force_div_id' => 'custom_currency_code_div'),
array('text', 'paid_currency_symbol', 'subtext' => $txt['paid_currency_symbol_desc'], 'size' => 8, 'force_div_id' => 'custom_currency_symbol_div'),
array('check', 'paidsubs_test', 'subtext' => $txt['paidsubs_test_desc'], 'onclick' => 'return !$(\'#paidsubs_test\').prop(\'checked\') || ask(' . JavaScriptEscape($txt['paidsubs_test_confirm']) . ', e);'),
));
// Now load all the other gateway settings.
$gateways = loadPaymentGateways();
foreach ($gateways as $gateway)
{
$gatewayClass = new $gateway['display_class']();
$setting_data = $gatewayClass->getGatewaySettings();
if (!empty($setting_data))
{
$config_vars[] = array('title', $gatewayClass->title, 'text_label' => (isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title));
$config_vars = array_merge($config_vars, $setting_data);
}
}
}
// Just searching?
if ($return_config)
return $config_vars;
// Get the settings template fired up.
loadSource('ManageServer');
// Some important context stuff
$context['page_title'] = $txt['settings'];
wetem::load('show_settings');
$context['settings_message'] = str_replace('{board_url}', ROOT, $txt['paid_note']);
$context[$context['admin_menu_name']]['current_subsection'] = 'settings';
// Get the final touches in place.
$context['post_url'] = '<URL>?action=admin;area=paidsubscribe;save;sa=settings';
$context['settings_title'] = $txt['settings'];
// We want javascript for our currency options.
add_js('
function toggleOther()
{
var otherOn = $("#paid_currency").val() == "other";
$("#custom_currency_code_div").toggle(otherOn);
$("#custom_currency_symbol_div").toggle(otherOn);
$("#custom_currency_code_div_dd").toggle(otherOn);
$("#custom_currency_symbol_div_dd").toggle(otherOn);
}
toggleOther();');
// Saving the settings?
if (isset($_GET['save']))
{
checkSession();
// If we've gone from enabled to disabled or back, fix the scheduled task.
$new_value = !empty($_POST['paid_enabled']) ? 1 : 0;
$old_value = !empty($settings['paid_enabled']) ? 1 : 0;
if ($new_value != $old_value)
{
wesql::query('
UPDATE {db_prefix}scheduled_tasks
SET disabled = {int:disabled}
WHERE task = {literal:paid_subscriptions}',
array(
'disabled' => $new_value ? 0 : 1,
)
);
// Should we calculate next trigger?
if ($new_value)
{
loadSource('ScheduledTasks');
CalculateNextTrigger('paid_subscriptions');
}
}
if ($new_value)
{
// Sort out the currency stuff.
if (!empty($_POST['paid_currency']) && $_POST['paid_currency'] != 'other')
{
$_POST['paid_currency_code'] = $_POST['paid_currency'];
$_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
}
unset($config_vars['dummy_currency']);
}
saveDBSettings($config_vars);
redirectexit('action=admin;area=paidsubscribe;sa=settings');
}
// Prepare the settings...
prepareDBSettingContext($config_vars);
}
// Are we looking at viewing the subscriptions?
function ViewSubscriptions()
{
global $context, $txt, $settings;
// Not made the settings yet?
if (empty($settings['paid_currency_symbol']))
fatal_lang_error('paid_not_set_currency', false, '<URL>?action=admin;area=paidsubscribe;sa=settings');
// Some basic stuff.
$context['page_title'] = $txt['paid_subs_view'];
loadSubscriptions();
$listOptions = array(
'id' => 'subscription_list',
'items_per_page' => 20,
'base_href' => '<URL>?action=admin;area=paidsubscribe;sa=view',
'get_items' => array(
'function' => function () {
global $context;
return $context['subscriptions'];
},
),
'get_count' => array(
'function' => function () {
global $context;
return count($context['subscriptions']);
},
),
'no_items_label' => $txt['paid_none_yet'],
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['paid_name'],
'style' => 'text-align: left; width: 35%;',
),
'data' => array(
'function' => function ($rowData) {
return sprintf('<a href="<URL>?action=admin;area=paidsubscribe;sa=viewsub;sid=%1$s">%2$s</a>', $rowData['id'], $rowData['name']);
},
),
),
'cost' => array(
'header' => array(
'value' => $txt['paid_cost'],
'style' => 'text-align: left;',
),
'data' => array(
'function' => function ($rowData) {
global $txt;
return $rowData['flexible'] ? '<em>' . $txt['flexible'] . '</em>' : $rowData['cost'] . ' / ' . $rowData['length'];
},
),
),
'pending' => array(
'header' => array(
'value' => $txt['paid_pending'],
'style' => 'width: 18%;',
),
'data' => array(
'db_htmlsafe' => 'pending',
'style' => 'text-align: center;',
),
),
'finished' => array(
'header' => array(
'value' => $txt['paid_finished'],
),
'data' => array(
'db_htmlsafe' => 'finished',
'style' => 'text-align: center;',
),
),
'total' => array(
'header' => array(
'value' => $txt['paid_active'],
),
'data' => array(
'db_htmlsafe' => 'total',
'style' => 'text-align: center;',
),
),
'is_active' => array(
'header' => array(
'value' => $txt['paid_is_active'],
),
'data' => array(
'function' => function ($rowData) {
global $txt;
return '<span style="color: ' . ($rowData['active'] ? 'green' : 'red') . '">' . ($rowData['active'] ? $txt['yes'] : $txt['no']) . '</span>';
},
'style' => 'text-align: center;',
),
),
'modify' => array(
'data' => array(
'function' => function ($rowData) {
global $txt;
return '<a href="<URL>?action=admin;area=paidsubscribe;sa=modify;sid=' . $rowData['id'] . '">' . $txt['modify'] . '</a>';
},
'style' => 'text-align: center;',
),
),
'delete' => array(
'data' => array(
'function' => function ($rowData) {
global $txt;
return '<a href="<URL>?action=admin;area=paidsubscribe;sa=modify;delete;sid=' . $rowData['id'] . '">' . $txt['delete'] . '</a>';
},
'style' => 'text-align: center;',
),
),
),
'form' => array(
'href' => '<URL>?action=admin;area=paidsubscribe;sa=modify',
),
'additional_rows' => array(
array(
'position' => 'below_table_data',
'value' => '
<input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="new">
',
'style' => 'text-align: right;',
),
),
);
loadSource('Subs-List');
createList($listOptions);
wetem::load('show_list');
$context['default_list'] = 'subscription_list';
}
// Adding, editing and deleting subscriptions.
function ModifySubscription()
{
global $context, $txt;
$context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
$context['action_type'] = $context['sub_id'] ? (isset($_REQUEST['delete']) ? 'delete' : 'edit') : 'add';
// Setup the template.
wetem::load($context['action_type'] == 'delete' ? 'delete_subscription' : 'modify_subscription');
$context['page_title'] = $txt['paid_' . $context['action_type'] . '_subscription'];
// Delete it?
if (isset($_POST['delete_confirm'], $_REQUEST['delete']))
{
checkSession();
wesql::query('
DELETE FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}',
array(
'current_subscription' => $context['sub_id'],
)
);
wesql::query('
DELETE FROM {db_prefix}subscriptions_groups
WHERE id_subscribe = {int:current_subscription}',
array(
'current_subscription' => $context['sub_id'],
)
);
redirectexit('action=admin;area=paidsubscribe;view');
}
// Saving?
if (isset($_POST['save']))
{
checkSession();
// Some cleaning...
if (empty($_POST['duration_type']))
$_POST['duration_type'] = 'fixed';
$isActive = isset($_POST['active']) ? 1 : 0;
$isRepeatable = isset($_POST['repeatable']) && $_POST['duration_type'] != 'lifetime' ? 1 : 0;
$allowpartial = isset($_POST['allow_partial']) ? 1 : 0;
$reminder = isset($_POST['reminder']) ? (int) $_POST['reminder'] : 0;
$emailComplete = strlen($_POST['emailcomplete']) > 10 ? trim($_POST['emailcomplete']) : '';
// Is this a fixed one? Lifetime's kind of fixed too :P
if ($_POST['duration_type'] == 'fixed')
{
// There needs to be something.
if (empty($_POST['fixed_cost']) || ($_POST['duration_type'] == 'fixed' && empty($_POST['span_value'])))
fatal_lang_error('paid_no_cost_value');
// Clean the span.
$span = $_POST['span_value'] . $_POST['span_unit'];
// Sort out the cost.
$cost = array('fixed' => sprintf('%01.2f', strtr($_POST['fixed_cost'], ',', '.')));
}
// Lifetime?
elseif ($_POST['duration_type'] == 'lifetime')
{
// There needs to be something.
if (empty($_POST['life_cost']))
fatal_lang_error('paid_no_cost_value');
// Clean the span.
$span = 'LT';
// Sort out the cost.
$cost = array('fixed' => sprintf('%01.2f', strtr($_POST['life_cost'], ',', '.')));
}
// Flexible is harder but more fun ;)
else
{
$span = 'F';
if (empty($_POST['cost_day']) && empty($_POST['cost_week']) && empty($_POST['cost_month']) && empty($_POST['cost_year']))
fatal_lang_error('paid_all_freq_blank');
$cost = array(
'day' => sprintf('%01.2f', strtr($_POST['cost_day'], ',', '.')),
'week' => sprintf('%01.2f', strtr($_POST['cost_week'], ',', '.')),
'month' => sprintf('%01.2f', strtr($_POST['cost_month'], ',', '.')),
'year' => sprintf('%01.2f', strtr($_POST['cost_year'], ',', '.')),
);
}
$cost = serialize($cost);
// Yep, time to do additional groups.
$addgroups = array();
if (!empty($_POST['addgroup']))
foreach ($_POST['addgroup'] as $id => $dummy)
$addgroups[] = (int) $id;
$addgroups = implode(',', $addgroups);
// Is it new?!
if ($context['action_type'] == 'add')
{
wesql::insert('',
'{db_prefix}subscriptions',
array(
'name' => 'string-60', 'description' => 'string-255', 'active' => 'int', 'length' => 'string-4', 'cost' => 'string',
'id_group' => 'int', 'add_groups' => 'string-40', 'repeatable' => 'int', 'allow_partial' => 'int', 'email_complete' => 'string',
'reminder' => 'int',
),
array(
$_POST['name'], $_POST['desc'], $isActive, $span, $cost,
$_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete,
$reminder,
)
);
$context['sub_id'] = wesql::insert_id();
}
// Otherwise must be editing.
else
{
// Don't do groups if there are active members
$request = wesql::query('
SELECT COUNT(*)
FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}
AND status = {int:is_active}',
array(
'current_subscription' => $context['sub_id'],
'is_active' => 1,
)
);
list ($disableGroups) = wesql::fetch_row($request);
wesql::free_result($request);
wesql::query('
UPDATE {db_prefix}subscriptions
SET name = SUBSTRING({string:name}, 1, 60), description = SUBSTRING({string:description}, 1, 255), active = {int:is_active},
length = SUBSTRING({string:length}, 1, 4), cost = {string:cost}' . ($disableGroups ? '' : ', id_group = {int:id_group},
add_groups = {string:additional_groups}') . ', repeatable = {int:repeatable}, allow_partial = {int:allow_partial},
email_complete = {string:email_complete}, reminder = {int:reminder}
WHERE id_subscribe = {int:current_subscription}',
array(
'is_active' => $isActive,
'id_group' => !empty($_POST['prim_group']) ? $_POST['prim_group'] : 0,
'repeatable' => $isRepeatable,
'allow_partial' => $allowpartial,
'reminder' => $reminder,
'current_subscription' => $context['sub_id'],
'name' => $_POST['name'],
'description' => $_POST['desc'],
'length' => $span,
'cost' => $cost,
'additional_groups' => !empty($addgroups) ? $addgroups : '',
'email_complete' => $emailComplete,
)
);
}
// Let's also sort out the allowed-to-subscribe groups while we're at it. Firstly get rid of the old subscriptions.
wesql::query('
DELETE FROM {db_prefix}subscriptions_groups
WHERE id_subscribe = {int:current_subscription}',
array(
'current_subscription' => $context['sub_id'],
)
);
// Now add all the groups in.
if (!empty($context['sub_id']))
{
$allowed_groups = array();
if (!empty($_POST['allowed_groups']))
foreach ($_POST['allowed_groups'] as $id => $dummy)
$allowed_groups[] = array($context['sub_id'], (int) $id);
if (!empty($allowed_groups))
wesql::insert('',
'{db_prefix}subscriptions_groups',
array(
'id_subscribe' => 'int', 'id_group' => 'int',
),
$allowed_groups
);
}
redirectexit('action=admin;area=paidsubscribe;view');
}
// Defaults.
if ($context['action_type'] == 'add')
{
$context['sub'] = array(
'name' => '',
'desc' => '',
'cost' => array(
'fixed' => 0,
),
'span' => array(
'value' => '',
'unit' => 'D',
),
'prim_group' => 0,
'add_groups' => array(),
'active' => 1,
'repeatable' => 1,
'allow_partial' => 0,
'duration' => 'fixed',
'email_complete' => '',
'reminder' => 0,
'allowed_groups' => array(),
);
}
// Otherwise load up all the details.
else
{
$request = wesql::query('
SELECT name, description, cost, length, id_group, add_groups, active, repeatable, allow_partial, email_complete, reminder
FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}
LIMIT 1',
array(
'current_subscription' => $context['sub_id'],
)
);
while ($row = wesql::fetch_assoc($request))
{
// Sort the date.
preg_match('~(\d*)(\w)~', $row['length'], $match);
if (isset($match[2]))
{
$span_value = $match[1];
$span_unit = $match[2];
}
else
{
$span_value = 0;
$span_unit = 'D';
}
$context['sub'] = array(
'name' => $row['name'],
'desc' => $row['description'],
'cost' => @unserialize($row['cost']),
'span' => array(
'value' => $span_value,
'unit' => $span_unit,
),
'prim_group' => $row['id_group'],
'add_groups' => explode(',', $row['add_groups']),
'active' => $row['active'],
'repeatable' => $row['repeatable'],
'allow_partial' => $row['allow_partial'],
'duration' => $row['length'] == 'F' ? 'flexible' : ($row['length'] == 'LT' ? 'lifetime' : 'fixed'),
'email_complete' => htmlspecialchars($row['email_complete']),
'reminder' => $row['reminder'],
'allowed_groups' => array(),
);
}
wesql::free_result($request);
// Load the groups allowed to access this group already.
$request = wesql::query('
SELECT id_group
FROM {db_prefix}subscriptions_groups
WHERE id_subscribe = {int:current_subscription}',
array(
'current_subscription' => $context['sub_id'],
)
);
while ($row = wesql::fetch_row($request))
$context['sub']['allowed_groups'][] = $row[0];
wesql::free_result($request);
// Does this have members who are active?
$request = wesql::query('
SELECT COUNT(*)
FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}
AND status = {int:is_active}',
array(
'current_subscription' => $context['sub_id'],
'is_active' => 1,
)
);
list ($context['disable_groups']) = wesql::fetch_row($request);
wesql::free_result($request);
}
// Load up all the groups.
$request = wesql::query('
SELECT id_group, group_name
FROM {db_prefix}membergroups
WHERE id_group != {int:moderator_group}
AND min_posts = {int:min_posts}',
array(
'moderator_group' => 3,
'min_posts' => -1,
)
);
$context['groups'] = array();
while ($row = wesql::fetch_assoc($request))
$context['groups'][$row['id_group']] = $row['group_name'];
wesql::free_result($request);
}
// View all the users subscribed to a particular subscription!
function ViewSubscribedUsers()
{
global $context, $txt;
// Setup the template.
$context['page_title'] = $txt['viewing_users_subscribed'];
// ID of the subscription.
$context['sub_id'] = (int) $_REQUEST['sid'];
// Load the subscription information.
$request = wesql::query('
SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active
FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}',
array(
'current_subscription' => $context['sub_id'],
)
);
// Something wrong?
if (wesql::num_rows($request) == 0)
fatal_lang_error('no_access', false);
// Do the subscription context.
$row = wesql::fetch_assoc($request);
$context['subscription'] = array(
'id' => $row['id_subscribe'],
'name' => $row['name'],
'desc' => $row['description'],
'active' => $row['active'],
);
wesql::free_result($request);
// Are we searching for people?
$search_string = isset($_POST['ssearch']) && !empty($_POST['sub_search']) ? ' AND IFNULL(mem.real_name, {string:guest}) LIKE {string:search}' : '';
$search_vars = empty($_POST['sub_search']) ? array() : array('search' => '%' . $_POST['sub_search'] . '%', 'guest' => $txt['guest']);
$listOptions = array(
'id' => 'subscribed_users_list',
'items_per_page' => 20,
'base_href' => '<URL>?action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id'],
'default_sort_col' => 'name',
'get_items' => array(
'function' => 'list_getSubscribedUsers',
'params' => array(
$context['sub_id'],
$search_string,
$search_vars,
),
),
'get_count' => array(
'function' => 'list_getSubscribedUserCount',
'params' => array(
$context['sub_id'],
$search_string,
$search_vars,
),
),
'no_items_label' => $txt['no_subscribers'],
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['who_member'],
'style' => 'text-align: left; width: 20%;',
),
'data' => array(
'function' => function ($rowData) {
global $txt;
return $rowData['id_member'] == 0 ? $txt['guest'] : '<a href="<URL>?action=profile;u=' . $rowData['id_member'] . '">' . $rowData['name'] . '</a>';
},
),
'sort' => array(
'default' => 'name',
'reverse' => 'name DESC',
),
),
'status' => array(
'header' => array(
'value' => $txt['paid_status'],
'style' => 'text-align: left; width: 10%;',
),
'data' => array(
'db_htmlsafe' => 'status_text',
),
'sort' => array(
'default' => 'status',
'reverse' => 'status DESC',
),
),
'payments_pending' => array(
'header' => array(
'value' => $txt['paid_payments_pending'],
'style' => 'text-align: left; width: 10%;',
),
'data' => array(
'db_htmlsafe' => 'pending',
),
'sort' => array(
'default' => 'payments_pending',
'reverse' => 'payments_pending DESC',
),
),
'start_time' => array(
'header' => array(
'value' => $txt['start_date'],
'style' => 'text-align: left; width: 20%;',
),
'data' => array(
'db_htmlsafe' => 'start_date',
'class' => 'smalltext',
),
'sort' => array(
'default' => 'start_time',
'reverse' => 'start_time DESC',
),
),
'end_time' => array(
'header' => array(
'value' => $txt['end_date'],
'style' => 'text-align: left; width: 20%;',
),
'data' => array(
'db_htmlsafe' => 'end_date',
'class' => 'smalltext',
),
'sort' => array(
'default' => 'end_time',
'reverse' => 'end_time DESC',
),
),
'modify' => array(
'header' => array(
'style' => 'width: 10%;',
),
'data' => array(
'function' => function ($rowData) {
global $txt;
return '<a href="<URL>?action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $rowData['id'] . '">' . $txt['modify'] . '</a>';
},
'style' => 'text-align: center;',
),
),
'delete' => array(
'header' => array(
'style' => 'width: 4%;',
),
'data' => array(
'function' => function ($rowData) {
return '<input type="checkbox" name="delsub[' . $rowData['id'] . ']">';
},
'style' => 'text-align: center;',
),
),
),
'form' => array(
'href' => '<URL>?action=admin;area=paidsubscribe;sa=modifyuser;sid=' . $context['sub_id'],
),
'additional_rows' => array(
array(
'position' => 'below_table_data',
'value' => '
<div class="floatleft">
<input type="submit" name="add" value="' . $txt['add_subscriber'] . '" class="new">
</div>
<div class="floatright">
<input type="submit" name="finished" value="' . $txt['complete_selected'] . '" onclick="return ask(' . JavaScriptEscape($txt['complete_are_sure']) . ', e);" class="submit">
<input type="submit" name="delete" value="' . $txt['delete_selected'] . '" onclick="return ask(' . JavaScriptEscape($txt['delete_are_sure']) . ', e);" class="delete">
</div>
',
),
array(
'position' => 'top_of_list',
'value' => '
<we:title>
' . sprintf($txt['view_users_subscribed'], $row['name']) . '
</we:title>
<div class="floatright">
<input type="search" name="sub_search" value="">
<input type="submit" name="ssearch" value="' . $txt['search_sub'] . '">
</div>
',
),
),
);
loadSource('Subs-List');
createList($listOptions);
wetem::load('show_list');
$context['default_list'] = 'subscribed_users_list';
}
// Returns how many people are subscribed to a paid subscription.
function list_getSubscribedUserCount($id_sub, $search_string, $search_vars = array())
{
// Get the total amount of users.
$request = wesql::query('
SELECT COUNT(*) AS total_subs
FROM {db_prefix}log_subscribed AS ls
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_pending_payments})',
array_merge($search_vars, array(
'current_subscription' => $id_sub,
'no_end_time' => 0,
'no_pending_payments' => 0,
))
);
list ($memberCount) = wesql::fetch_row($request);
wesql::free_result($request);
return $memberCount;
}
function list_getSubscribedUsers($start, $items_per_page, $sort, $id_sub, $search_string, $search_vars = array())
{
global $txt;
$request = wesql::query('
SELECT ls.id_sublog, IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, {string:guest}) AS name, ls.start_time, ls.end_time,
ls.status, ls.payments_pending, s.length
FROM {db_prefix}log_subscribed AS ls
INNER JOIN {db_prefix}subscriptions AS s ON (ls.id_subscribe = s.id_subscribe)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_payments_pending})
ORDER BY ' . $sort . '
LIMIT ' . $start . ', ' . $items_per_page,
array_merge($search_vars, array(
'current_subscription' => $id_sub,
'no_end_time' => 0,
'no_payments_pending' => 0,
'guest' => $txt['guest'],
))
);
$subscribers = array();
while ($row = wesql::fetch_assoc($request))
$subscribers[] = array(
'id' => $row['id_sublog'],
'id_member' => $row['id_member'],
'name' => $row['name'],
'start_date' => timeformat($row['start_time'], false),
'end_date' => $row['length'] == 'LT' ? $txt['paid_mod_span_lifetime_expires'] : ($row['end_time'] == 0 ? $txt['not_applicable'] : timeformat($row['end_time'], false)),
'pending' => $row['payments_pending'],
'status' => $row['status'],
'status_text' => $row['status'] == 0 ? ($row['payments_pending'] == 0 ? $txt['paid_finished'] : $txt['paid_pending']) : $txt['paid_active'],
);
wesql::free_result($request);
return $subscribers;
}
// Edit or add a user subscription.
function ModifyUserSubscription()
{
global $context, $txt, $settings;
loadSubscriptions();
$context['log_id'] = isset($_REQUEST['lid']) ? (int) $_REQUEST['lid'] : 0;
$context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
$context['action_type'] = $context['log_id'] ? 'edit' : 'add';
// Setup the template.
wetem::load('modify_user_subscription');
$context['page_title'] = $txt[$context['action_type'] . '_subscriber'];
// If we haven't been passed the subscription ID get it.
if ($context['log_id'] && !$context['sub_id'])
{
$request = wesql::query('
SELECT id_subscribe
FROM {db_prefix}log_subscribed
WHERE id_sublog = {int:current_log_item}',
array(
'current_log_item' => $context['log_id'],
)
);
if (wesql::num_rows($request) == 0)
fatal_lang_error('no_access', false);
list ($context['sub_id']) = wesql::fetch_row($request);
wesql::free_result($request);
}
if (!isset($context['subscriptions'][$context['sub_id']]))
fatal_lang_error('no_access', false);
$context['current_subscription'] = $context['subscriptions'][$context['sub_id']];
// Searching?
if (isset($_POST['ssearch']))
return ViewSubscribedUsers();
// Saving?
elseif (isset($_REQUEST['save_sub']))
{
checkSession();
if (!empty($_POST['year']) && !empty($_POST['yearend']))
{
// Work out the dates...
$starttime = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
$endtime = mktime($_POST['hourend'], $_POST['minuteend'], 0, $_POST['monthend'], $_POST['dayend'], $_POST['yearend']);
}
else
{
$starttime = time();
$endtime = 0;
}
// Status.
$status = $_POST['status'];
// New one?
if (empty($context['log_id']))
{
// Find the user...
$request = wesql::query('
SELECT id_member, id_group
FROM {db_prefix}members
WHERE real_name = {string:name}
LIMIT 1',
array(
'name' => $_POST['name'],
)
);
if (wesql::num_rows($request) == 0)
fatal_lang_error('error_member_not_found');