-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathSubscriptions.php
More file actions
2227 lines (1956 loc) · 67 KB
/
Subscriptions.php
File metadata and controls
2227 lines (1956 loc) · 67 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
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2026 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 4
*/
declare(strict_types=1);
namespace SMF\Actions\Admin;
use SMF\ActionInterface;
use SMF\ActionTrait;
use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\ErrorHandler;
use SMF\IntegrationHook;
use SMF\ItemList;
use SMF\Lang;
use SMF\Menu;
use SMF\Parser;
use SMF\Sapi;
use SMF\SecurityToken;
use SMF\TaskRunner;
use SMF\Theme;
use SMF\Time;
use SMF\User;
use SMF\Utils;
/**
* Contains all the administration functions for paid subscriptions.
* (and some more than that :P)
*/
class Subscriptions implements ActionInterface
{
use ActionTrait;
/*******************
* Public properties
*******************/
/**
* @var string
*
* The requested sub-action.
* This should be set by the constructor.
*/
public string $subaction = 'view';
/**************************
* Public static properties
**************************/
/**
* @var array
*
* Available sub-actions.
*
* Format: 'sa' => array('method', 'required_permission')
*/
public static array $subactions = [
'view' => ['view', 'admin_forum'],
'viewsub' => ['viewUsers', 'admin_forum'],
'modify' => ['modify', 'admin_forum'],
'modifyuser' => ['modifyUser', 'admin_forum'],
'settings' => ['settings', 'admin_forum'],
];
/**
* @var array
*
* Data about all the subscriptions the admin has created.
*/
public static array $all = [];
/****************
* Public methods
****************/
/**
* Dispatcher to whichever sub-action method is necessary.
*/
public function execute(): void
{
Theme::loadTemplate('ManagePaid');
Utils::$context['page_title'] = Lang::getTxt('paid_subscriptions', file: 'Admin');
// Tabs for browsing the different subscription functions.
Menu::$loaded['admin']->tab_data = [
'title' => Lang::getTxt('paid_subscriptions', file: 'Admin'),
'help' => '',
'description' => Lang::getTxt('paid_subscriptions_desc', file: 'ManagePaid'),
];
if (!empty(Config::$modSettings['paid_enabled']) && !empty(Config::$modSettings['paid_currency_symbol'])) {
Menu::$loaded['admin']->tab_data['tabs'] = [
'view' => [
'description' => Lang::getTxt('paid_subs_view_desc', file: 'ManagePaid'),
],
'settings' => [
'description' => Lang::getTxt('paid_subs_settings_desc', file: 'ManagePaid'),
],
];
}
// Make sure you can do this.
User::$me->isAllowedTo(self::$subactions[$this->subaction][1]);
$call = \is_string(self::$subactions[$this->subaction][0]) && method_exists($this, self::$subactions[$this->subaction][0]) ? [$this, self::$subactions[$this->subaction][0]] : Utils::getCallable(self::$subactions[$this->subaction][0]);
if (!empty($call)) {
\call_user_func($call);
}
}
/**
* View a list of all the current subscriptions
* Requires the admin_forum permission.
* Accessed from ?action=admin;area=paidsubscribe;sa=view.
*/
public function view(): void
{
// Not made the settings yet?
if (empty(Config::$modSettings['paid_currency_symbol'])) {
ErrorHandler::fatalLang('paid_not_set_currency', false, ['url' => Config::$scripturl . '?action=admin;area=paidsubscribe;sa=settings']);
}
// Some basic stuff.
Utils::$context['page_title'] = Lang::getTxt('paid_subs_view', file: 'Admin');
$all = self::getSubs();
$listOptions = [
'id' => 'subscription_list',
'title' => Lang::getTxt('subscriptions', file: 'ManagePaid'),
'items_per_page' => Config::$modSettings['defaultMaxListItems'],
'base_href' => Config::$scripturl . '?action=admin;area=paidsubscribe;sa=view',
'get_items' => [
'function' => function ($start, $items_per_page) {
$subscriptions = [];
$counter = 0;
$start++;
foreach (self::$all as $data) {
if (++$counter < $start) {
continue;
}
if ($counter == $start + $items_per_page) {
break;
}
$subscriptions[] = $data;
}
return $subscriptions;
},
],
'get_count' => [
'function' => function () {
return \count(self::$all);
},
],
'no_items_label' => Lang::getTxt('paid_none_yet', file: 'ManagePaid'),
'columns' => [
'name' => [
'header' => [
'value' => Lang::getTxt('paid_name', file: 'ManagePaid'),
'style' => 'width: 35%;',
],
'data' => [
'function' => function ($rowData) {
return \sprintf('<a href="%1$s?action=admin;area=paidsubscribe;sa=viewsub;sid=%2$s">%3$s</a>', Config::$scripturl, $rowData['id'], $rowData['name']);
},
],
],
'cost' => [
'header' => [
'value' => Lang::getTxt('paid_cost', file: 'ManagePaid'),
],
'data' => [
'function' => function ($rowData) {
return $rowData['flexible'] ? '<em>' . Lang::getTxt('flexible', file: 'ManagePaid') . '</em>' : $rowData['cost'] . ' / ' . $rowData['length'];
},
],
],
'pending' => [
'header' => [
'value' => Lang::getTxt('paid_pending', file: 'ManagePaid'),
'style' => 'width: 18%;',
'class' => 'centercol',
],
'data' => [
'db_htmlsafe' => 'pending',
'class' => 'centercol',
],
],
'finished' => [
'header' => [
'value' => Lang::getTxt('paid_finished', file: 'ManagePaid'),
'class' => 'centercol',
],
'data' => [
'db_htmlsafe' => 'finished',
'class' => 'centercol',
],
],
'total' => [
'header' => [
'value' => Lang::getTxt('paid_active', file: 'ManagePaid'),
'class' => 'centercol',
],
'data' => [
'db_htmlsafe' => 'total',
'class' => 'centercol',
],
],
'is_active' => [
'header' => [
'value' => Lang::getTxt('paid_is_active', file: 'ManagePaid'),
'class' => 'centercol',
],
'data' => [
'function' => function ($rowData) {
return '<span style="color: ' . ($rowData['active'] ? 'green' : 'red') . '">' . Lang::getTxt($rowData['active'] ? 'yes' : 'no', file: 'General') . '</span>';
},
'class' => 'centercol',
],
],
'modify' => [
'data' => [
'function' => function ($rowData) {
return '<a href="' . Config::$scripturl . '?action=admin;area=paidsubscribe;sa=modify;sid=' . $rowData['id'] . '">' . Lang::getTxt('modify', file: 'General') . '</a>';
},
'class' => 'centercol',
],
],
'delete' => [
'data' => [
'function' => function ($rowData) {
return '<a href="' . Config::$scripturl . '?action=admin;area=paidsubscribe;sa=modify;delete;sid=' . $rowData['id'] . '">' . Lang::getTxt('delete', file: 'General') . '</a>';
},
'class' => 'centercol',
],
],
],
'form' => [
'href' => Config::$scripturl . '?action=admin;area=paidsubscribe;sa=modify',
],
'additional_rows' => [
[
'position' => 'above_table_headers',
'value' => '<input type="submit" name="add" value="' . Lang::getTxt('paid_add_subscription', file: 'ManagePaid') . '" class="button">',
],
[
'position' => 'below_table_data',
'value' => '<input type="submit" name="add" value="' . Lang::getTxt('paid_add_subscription', file: 'ManagePaid') . '" class="button">',
],
],
];
new ItemList($listOptions);
Utils::$context['sub_template'] = 'show_list';
Utils::$context['default_list'] = 'subscription_list';
}
/**
* View all the users subscribed to a particular subscription.
* Requires the admin_forum permission.
* Accessed from ?action=admin;area=paidsubscribe;sa=viewsub.
*
* Subscription ID is required, in the form of $_GET['sid'].
*/
public function viewUsers(): void
{
// Setup the template.
Utils::$context['page_title'] = Lang::getTxt('viewing_users_subscribed', file: 'ManagePaid');
// ID of the subscription.
Utils::$context['sub_id'] = (int) $_REQUEST['sid'];
// Load the subscription information.
$request = Db::$db->query(
'SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active
FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}',
[
'current_subscription' => Utils::$context['sub_id'],
],
);
// Something wrong?
if (Db::$db->num_rows($request) == 0) {
ErrorHandler::fatalLang('no_access', false);
}
$row = Db::$db->fetch_assoc($request);
Db::$db->free_result($request);
// Do the subscription context.
Utils::$context['subscription'] = [
'id' => $row['id_subscribe'],
'name' => $row['name'],
'desc' => $row['description'],
'active' => $row['active'],
];
// Are we searching for people?
$search_string = isset($_POST['ssearch']) && !empty($_POST['sub_search']) ? ' AND COALESCE(mem.real_name, {string:guest}) LIKE {string:search}' : '';
$search_vars = empty($_POST['sub_search']) ? [] : ['search' => '%' . $_POST['sub_search'] . '%', 'guest' => Lang::getTxt('guest', file: 'General')];
$listOptions = [
'id' => 'subscribed_users_list',
'title' => Lang::getTxt('view_users_subscribed', $row, file: 'ManagePaid'),
'items_per_page' => Config::$modSettings['defaultMaxListItems'],
'base_href' => Config::$scripturl . '?action=admin;area=paidsubscribe;sa=viewsub;sid=' . Utils::$context['sub_id'],
'default_sort_col' => 'name',
'get_items' => [
'function' => __CLASS__ . '::list_getSubscribedUsers',
'params' => [
Utils::$context['sub_id'],
$search_string,
$search_vars,
],
],
'get_count' => [
'function' => __CLASS__ . '::list_getSubscribedUserCount',
'params' => [
Utils::$context['sub_id'],
$search_string,
$search_vars,
],
],
'no_items_label' => Lang::getTxt('no_subscribers', file: 'ManagePaid'),
'columns' => [
'name' => [
'header' => [
'value' => Lang::getTxt('who_member', file: 'General'),
'style' => 'width: 20%;',
],
'data' => [
'function' => function ($rowData) {
return $rowData['id_member'] == 0 ? Lang::getTxt('guest', file: 'General') : '<a href="' . Config::$scripturl . '?action=profile;u=' . $rowData['id_member'] . '">' . $rowData['name'] . '</a>';
},
],
'sort' => [
'default' => 'name',
'reverse' => 'name DESC',
],
],
'status' => [
'header' => [
'value' => Lang::getTxt('paid_status', file: 'ManagePaid'),
'style' => 'width: 10%;',
],
'data' => [
'db_htmlsafe' => 'status_text',
],
'sort' => [
'default' => 'status',
'reverse' => 'status DESC',
],
],
'payments_pending' => [
'header' => [
'value' => Lang::getTxt('paid_payments_pending', file: 'ManagePaid'),
'style' => 'width: 15%;',
],
'data' => [
'db_htmlsafe' => 'pending',
],
'sort' => [
'default' => 'payments_pending',
'reverse' => 'payments_pending DESC',
],
],
'start_time' => [
'header' => [
'value' => Lang::getTxt('start_date', file: 'ManagePaid'),
'style' => 'width: 20%;',
],
'data' => [
'db_htmlsafe' => 'start_date',
'class' => 'smalltext',
],
'sort' => [
'default' => 'start_time',
'reverse' => 'start_time DESC',
],
],
'end_time' => [
'header' => [
'value' => Lang::getTxt('end_date', file: 'ManagePaid'),
'style' => 'width: 20%;',
],
'data' => [
'db_htmlsafe' => 'end_date',
'class' => 'smalltext',
],
'sort' => [
'default' => 'end_time',
'reverse' => 'end_time DESC',
],
],
'modify' => [
'header' => [
'style' => 'width: 10%;',
'class' => 'centercol',
],
'data' => [
'function' => function ($rowData) {
return '<a href="' . Config::$scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $rowData['id'] . '">' . Lang::getTxt('modify', file: 'General') . '</a>';
},
'class' => 'centercol',
],
],
'delete' => [
'header' => [
'style' => 'width: 4%;',
'class' => 'centercol',
],
'data' => [
'function' => function ($rowData) {
return '<input type="checkbox" name="delsub[' . $rowData['id'] . ']">';
},
'class' => 'centercol',
],
],
],
'form' => [
'href' => Config::$scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;sid=' . Utils::$context['sub_id'],
],
'additional_rows' => [
[
'position' => 'below_table_data',
'value' => '
<input type="submit" name="add" value="' . Lang::getTxt('add_subscriber', file: 'ManagePaid') . '" class="button">
<input type="submit" name="finished" value="' . Lang::getTxt('complete_selected', file: 'ManagePaid') . '" data-confirm="' . Lang::getTxt('complete_are_sure', file: 'ManagePaid') . '" class="button you_sure">
<input type="submit" name="delete" value="' . Lang::getTxt('delete_selected', file: 'ManagePaid') . '" data-confirm="' . Lang::getTxt('delete_are_sure', file: 'ManagePaid') . '" class="button you_sure">
',
],
[
'position' => 'top_of_list',
'value' => '
<div class="flow_auto">
<input type="submit" name="ssearch" value="' . Lang::getTxt('search_sub', file: 'ManagePaid') . '" class="button" style="margin-top: 3px;">
<input type="text" name="sub_search" value="" class="floatright">
</div>
',
],
],
];
new ItemList($listOptions);
Utils::$context['sub_template'] = 'show_list';
Utils::$context['default_list'] = 'subscribed_users_list';
}
/**
* Adding, editing and deleting subscriptions.
*
* Accessed from ?action=admin;area=paidsubscribe;sa=modify.
*/
public function modify(): void
{
Utils::$context['sub_id'] = (int) ($_REQUEST['sid'] ?? 0);
Utils::$context['action_type'] = Utils::$context['sub_id'] ? (isset($_REQUEST['delete']) ? 'delete' : 'edit') : 'add';
// Setup the template.
Utils::$context['sub_template'] = Utils::$context['action_type'] == 'delete' ? 'delete_subscription' : 'modify_subscription';
Utils::$context['page_title'] = Lang::getTxt('paid_' . Utils::$context['action_type'] . '_subscription', file: 'ManagePaid');
// Delete it?
if (isset($_POST['delete_confirm'], $_REQUEST['delete'])) {
User::$me->checkSession();
SecurityToken::validate('admin-pmsd');
// Before we delete the subscription we need to find out if anyone currently has said subscription.
$members = [];
$request = Db::$db->query(
'SELECT ls.id_member, ls.old_id_group, mem.id_group, mem.additional_groups
FROM {db_prefix}log_subscribed AS ls
INNER JOIN {db_prefix}members AS mem ON (ls.id_member = mem.id_member)
WHERE id_subscribe = {int:current_subscription}
AND status = {int:is_active}',
[
'current_subscription' => Utils::$context['sub_id'],
'is_active' => 1,
],
);
while ($row = Db::$db->fetch_assoc($request)) {
$id_member = array_shift($row);
$members[$id_member] = $row;
}
Db::$db->free_result($request);
// If there are any members with this subscription, we have to do some more work before we go any further.
if (!empty($members)) {
$id_group = 0;
$add_groups = '';
$request = Db::$db->query(
'SELECT id_group, add_groups
FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}',
[
'current_subscription' => Utils::$context['sub_id'],
],
);
if (Db::$db->num_rows($request)) {
list($id_group, $add_groups) = Db::$db->fetch_row($request);
}
Db::$db->free_result($request);
$changes = [];
// Is their group changing? This subscription may not have changed primary group.
if (!empty($id_group)) {
foreach ($members as $id_member => $member_data) {
// If their current primary group isn't what they had before the
// subscription, and their current group was granted by the sub,
// then remove it.
if ($member_data['old_id_group'] != $member_data['id_group'] && $member_data['id_group'] == $id_group) {
$changes[$id_member]['id_group'] = $member_data['old_id_group'];
}
}
}
// Did this subscription add secondary groups?
if (!empty($add_groups)) {
$add_groups = explode(',', $add_groups);
foreach ($members as $id_member => $member_data) {
// First let's get their groups sorted.
$current_groups = explode(',', $member_data['additional_groups']);
$new_groups = implode(',', array_diff($current_groups, $add_groups));
if ($new_groups != $member_data['additional_groups']) {
$changes[$id_member]['additional_groups'] = $new_groups;
}
}
}
// We're going through changes...
if (!empty($changes)) {
foreach ($changes as $id_member => $new_values) {
User::updateMemberData($id_member, $new_values);
}
}
}
// Delete the subscription
Db::$db->query(
'DELETE FROM {db_prefix}subscriptions
WHERE id_subscribe = {int:current_subscription}',
[
'current_subscription' => Utils::$context['sub_id'],
],
);
// And delete any subscriptions to it to clear the phantom data too.
Db::$db->query(
'DELETE FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}',
[
'current_subscription' => Utils::$context['sub_id'],
],
);
IntegrationHook::call('integrate_delete_subscription', [Utils::$context['sub_id']]);
Utils::redirectexit('action=admin;area=paidsubscribe;view');
}
// Saving?
if (isset($_POST['save'])) {
User::$me->checkSession();
// Some cleaning...
$isActive = isset($_POST['active']) ? 1 : 0;
$isRepeatable = isset($_POST['repeatable']) ? 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']) : '';
$_POST['prim_group'] = !empty($_POST['prim_group']) ? (int) $_POST['prim_group'] : 0;
// Cleanup text fields
$_POST['name'] = Utils::htmlspecialchars($_POST['name']);
$_POST['desc'] = Utils::htmlspecialchars($_POST['desc']);
$emailComplete = Utils::htmlspecialchars($emailComplete);
// Is this a fixed one?
if ($_POST['duration_type'] == 'fixed') {
$_POST['span_value'] = !empty($_POST['span_value']) && is_numeric($_POST['span_value']) ? ceil((int) $_POST['span_value']) : 0;
// There are sanity check limits on these things.
$limits = [
'D' => 90,
'W' => 52,
'M' => 24,
'Y' => 5,
];
if (empty($_POST['span_unit']) || empty($limits[$_POST['span_unit']]) || $_POST['span_value'] < 1) {
ErrorHandler::fatalLang('paid_invalid_duration', false);
}
if ($_POST['span_value'] > $limits[$_POST['span_unit']]) {
ErrorHandler::fatalLang('paid_invalid_duration_' . $_POST['span_unit'], false);
}
// Clean the span.
$span = $_POST['span_value'] . $_POST['span_unit'];
// Sort out the cost.
$cost = ['fixed' => \sprintf('%01.2f', strtr($_POST['cost'], ',', '.'))];
// There needs to be something.
if ($cost['fixed'] == '0.00') {
ErrorHandler::fatalLang('paid_no_cost_value');
}
}
// Flexible is harder but more fun ;)
else {
$span = 'F';
$cost = [
'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'], ',', '.')),
];
if ($cost['day'] == '0.00' && $cost['week'] == '0.00' && $cost['month'] == '0.00' && $cost['year'] == '0.00') {
ErrorHandler::fatalLang('paid_all_freq_blank');
}
}
$cost = Utils::jsonEncode($cost);
// Having now validated everything that might throw an error,
// let's also now deal with the token.
SecurityToken::validate('admin-pms');
// Yep, time to do additional groups.
$addgroups = [];
if (!empty($_POST['addgroup'])) {
foreach ($_POST['addgroup'] as $id => $dummy) {
$addgroups[] = (int) $id;
}
}
$addgroups = implode(',', $addgroups);
// Is it new?!
if (Utils::$context['action_type'] == 'add') {
$id_subscribe = Db::$db->insert(
'',
'{db_prefix}subscriptions',
[
'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',
],
[
[
$_POST['name'],
$_POST['desc'],
$isActive,
$span,
$cost,
$_POST['prim_group'],
$addgroups,
$isRepeatable,
$allowpartial,
$emailComplete,
$reminder,
],
],
['id_subscribe'],
Db::INSERT_RETURN_MODE_SINGLE,
);
}
// Otherwise must be editing.
else {
// Don't do groups if there are active members
$request = Db::$db->query(
'SELECT COUNT(*)
FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}
AND status = {int:is_active}',
[
'current_subscription' => Utils::$context['sub_id'],
'is_active' => 1,
],
);
list($disableGroups) = Db::$db->fetch_row($request);
Db::$db->free_result($request);
Db::$db->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}',
[
'is_active' => $isActive,
'id_group' => $_POST['prim_group'],
'repeatable' => $isRepeatable,
'allow_partial' => $allowpartial,
'reminder' => $reminder,
'current_subscription' => Utils::$context['sub_id'],
'name' => $_POST['name'],
'description' => $_POST['desc'],
'length' => $span,
'cost' => $cost,
'additional_groups' => !empty($addgroups) ? $addgroups : '',
'email_complete' => $emailComplete,
],
identifier: 'substring',
);
}
IntegrationHook::call('integrate_save_subscription', [(Utils::$context['action_type'] == 'add' ? $id_subscribe : Utils::$context['sub_id']), $_POST['name'], $_POST['desc'], $isActive, $span, $cost, $_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete, $reminder]);
Utils::redirectexit('action=admin;area=paidsubscribe;view');
}
// Defaults.
if (Utils::$context['action_type'] == 'add') {
Utils::$context['sub'] = [
'name' => '',
'desc' => '',
'cost' => [
'fixed' => 0,
],
'span' => [
'value' => '',
'unit' => 'D',
],
'prim_group' => 0,
'add_groups' => [],
'active' => 1,
'repeatable' => 1,
'allow_partial' => 0,
'duration' => 'fixed',
'email_complete' => '',
'reminder' => 0,
];
}
// Otherwise load up all the details.
else {
$request = Db::$db->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',
[
'current_subscription' => Utils::$context['sub_id'],
],
);
while ($row = Db::$db->fetch_assoc($request)) {
// Sort the date.
preg_match('~(\d*)(\w)~', $row['length'], $match);
if (isset($match[2])) {
$_POST['span_value'] = $match[1];
$span_unit = $match[2];
} else {
$_POST['span_value'] = 0;
$span_unit = 'D';
}
// Is this a flexible one?
$isFlexible = $row['length'] == 'F';
Utils::$context['sub'] = [
'name' => $row['name'],
'desc' => $row['description'],
'cost' => Utils::jsonDecode($row['cost'], true),
'span' => [
'value' => $_POST['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' => $isFlexible ? 'flexible' : 'fixed',
'email_complete' => $row['email_complete'],
'reminder' => $row['reminder'],
];
}
Db::$db->free_result($request);
// Does this have members who are active?
$request = Db::$db->query(
'SELECT COUNT(*)
FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}
AND status = {int:is_active}',
[
'current_subscription' => Utils::$context['sub_id'],
'is_active' => 1,
],
);
list(Utils::$context['disable_groups']) = Db::$db->fetch_row($request);
Db::$db->free_result($request);
}
// Load up all the groups.
Utils::$context['groups'] = [];
$request = Db::$db->query(
'SELECT id_group, group_name
FROM {db_prefix}membergroups
WHERE id_group != {int:moderator_group}
AND min_posts = {int:min_posts}',
[
'moderator_group' => 3,
'min_posts' => -1,
],
);
while ($row = Db::$db->fetch_assoc($request)) {
Utils::$context['groups'][$row['id_group']] = $row['group_name'];
}
Db::$db->free_result($request);
// This always happens.
SecurityToken::create(Utils::$context['action_type'] == 'delete' ? 'admin-pmsd' : 'admin-pms');
}
/**
* Edit or add a user subscription.
*
* Accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
*/
public function modifyUser(): void
{
self::getSubs();
Utils::$context['log_id'] = (int) ($_REQUEST['lid'] ?? 0);
Utils::$context['sub_id'] = (int) ($_REQUEST['sid'] ?? 0);
Utils::$context['action_type'] = Utils::$context['log_id'] ? 'edit' : 'add';
// Setup the template.
Utils::$context['sub_template'] = 'modify_user_subscription';
Utils::$context['page_title'] = Lang::getTxt(Utils::$context['action_type'] . '_subscriber', file: 'ManagePaid');
Theme::loadJavaScriptFile('paidsubs.js', ['defer' => true, 'minimize' => true], 'smf_paidsubs');
// If we haven't been passed the subscription ID get it.
if (Utils::$context['log_id'] && !Utils::$context['sub_id']) {
$request = Db::$db->query(
'SELECT id_subscribe
FROM {db_prefix}log_subscribed
WHERE id_sublog = {int:current_log_item}',
[
'current_log_item' => Utils::$context['log_id'],
],
);
if (Db::$db->num_rows($request) == 0) {
ErrorHandler::fatalLang('no_access', false);
}
list(Utils::$context['sub_id']) = Db::$db->fetch_row($request);
Db::$db->free_result($request);
}
if (!isset(self::$all[Utils::$context['sub_id']])) {
ErrorHandler::fatalLang('no_access', false);
}
Utils::$context['current_subscription'] = self::$all[Utils::$context['sub_id']];
// Searching?
if (isset($_POST['ssearch'])) {
$this->viewUsers();
return;
}
// Saving?
if (isset($_REQUEST['save_sub'])) {
User::$me->checkSession();
// Work out the dates...
$starttime = mktime(
(int) $_POST['hour'],
(int) $_POST['minute'],
0,
(int) $_POST['month'],
(int) $_POST['day'],
(int) $_POST['year'],
);
$endtime = mktime(
(int) $_POST['hourend'],
(int) $_POST['minuteend'],
0,
(int) $_POST['monthend'],
(int) $_POST['dayend'],
(int) $_POST['yearend'],
);
// Status.
$status = $_POST['status'];
// New one?
if (empty(Utils::$context['log_id'])) {
// Find the user...
$request = Db::$db->query(
'SELECT id_member, id_group
FROM {db_prefix}members
WHERE real_name = {string:name}
LIMIT 1',
[
'name' => $_POST['name'],
],
);
if (Db::$db->num_rows($request) == 0) {
ErrorHandler::fatalLang('error_member_not_found');
}
list($id_member, $id_group) = Db::$db->fetch_row($request);
$id_member = (int) $id_member;
$id_group = (int) $id_group;
Db::$db->free_result($request);
// Ensure the member doesn't already have a subscription!
$request = Db::$db->query(
'SELECT id_subscribe
FROM {db_prefix}log_subscribed
WHERE id_subscribe = {int:current_subscription}
AND id_member = {int:current_member}',
[
'current_subscription' => Utils::$context['sub_id'],
'current_member' => $id_member,
],
);
if (Db::$db->num_rows($request) != 0) {
ErrorHandler::fatalLang('member_already_subscribed');
}
Db::$db->free_result($request);
// Actually put the subscription in place.
if ($status == 1) {
self::add((int) Utils::$context['sub_id'], $id_member, 0, $starttime, $endtime);
} else {
Db::$db->insert(
'',
'{db_prefix}log_subscribed',
[
'id_subscribe' => 'int',
'id_member' => 'int',
'old_id_group' => 'int',
'start_time' => 'int',
'end_time' => 'int',
'status' => 'int',
'pending_details' => 'string-65534',
],
[
[
Utils::$context['sub_id'],
$id_member,
$id_group,
$starttime,
$endtime,
$status,
Utils::jsonEncode([]),
],
],
['id_sublog'],
);
}
}