Repository navigation
Expand file tree
/
Copy pathProcessNativeForum.module.php
More file actions
2408 lines (2172 loc) · 125 KB
/
Copy pathProcessNativeForum.module.php
File metadata and controls
2408 lines (2172 loc) · 125 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 namespace ProcessWire;
class ProcessNativeForum extends Process implements Module {
const PAGE_NAME = 'native-forum';
public static function getModuleInfo() {
return [
'title' => 'Native Forum Admin',
'version' => 100,
'summary' => 'Admin for Native Forum: dashboard, reports, groups & categories, topics, messages, members, settings and export/import.',
'author' => 'Gregor Rojko (Pyxios)',
'href' => 'https://processwire.com/modules/native-forum/',
'icon' => 'comments',
'requires' => ['ProcessWire>=3.0.200', 'NativeForum'],
'permissions' => ['native-forum'],
'permission' => 'native-forum',
'singular' => true,
'nav' => [
['url' => '', 'label' => 'Dashboard', 'icon' => 'dashboard'],
['url' => 'reports', 'label' => 'Reports', 'icon' => 'flag'],
['url' => 'groups', 'label' => 'Groups/Categories'],
['url' => 'topics', 'label' => 'Topics'],
['url' => 'messages', 'label' => 'Messages'],
['url' => 'members', 'label' => 'Members'],
['url' => 'settings', 'label' => 'Settings'],
['url' => 'transfer', 'label' => 'Export / Import', 'icon' => 'exchange'],
],
];
}
public function ___install() {
$pages = $this->wire('pages');
$permissions = $this->wire('permissions');
$config = $this->wire('config');
$modules = $this->wire('modules');
// Ensure Native Forum is installed (will create fields/templates/pages)
$modules->get('NativeForum');
// Ensure permission exists (do NOT modify any roles automatically)
$perm = $permissions->get('native-forum');
if(!$perm || !$perm->id) {
$perm = new Permission();
$perm->name = 'native-forum';
$perm->title = 'Access Native Forum admin';
$permissions->save($perm);
}
// Create Admin page (Process page) – or take over the one of the former SimpleForum admin
$page = $pages->get('template=admin, name=' . self::PAGE_NAME . ', include=all');
if(!$page->id) $page = $pages->get('template=admin, name=simple-forum, include=all');
if($page->id) {
$page->of(false);
$page->name = self::PAGE_NAME;
$page->title = 'Native Forum';
$page->process = $this;
$page->save();
} else {
$page = new Page();
$page->template = 'admin';
$page->parent = $pages->get($config->adminRootPageID);
$page->title = 'Native Forum';
$page->name = self::PAGE_NAME;
$page->process = $this;
$page->save();
}
// Copy frontend template files + CSS from module/template-files to /site/templates
$this->copyFrontendFiles(true);
}
public function ___uninstall() {
$pages = $this->wire('pages');
$permissions = $this->wire('permissions');
// Delete admin page
$page = $pages->get('template=admin, name=' . self::PAGE_NAME);
if($page->id) {
$this->message("Deleted admin page: {$page->path}");
$page->delete();
}
// Remove permission (non-destructive in terms of data)
$perm = $permissions->get('native-forum');
if($perm && $perm->id) {
$permissions->delete($perm);
$this->message('Deleted permission: native-forum');
}
}
public function upgrade($fromVersion, $toVersion) {
// On module upgrade, refresh frontend templates/CSS
$this->copyFrontendFiles(true);
}
protected function copyFrontendFiles(bool $overwrite = false) {
$config = $this->wire('config');
$sourcepath = dirname(__FILE__) . '/template-files/';
$dest = $config->paths->templates;
$files = [
'forum-root.php',
'forum-layout.php',
'forum-category.php',
'forum-topic.php',
'forum-reply.php',
'forum-messages-root.php',
'forum-message.php',
'forum-profile.php',
'forum-login.php',
'forum-register.php',
'native-forum.css',
'native-forum-bootstrap.css',
];
foreach($files as $file) {
if(!is_file($sourcepath . $file)) continue;
if(is_file($dest . $file) && !$overwrite) continue; // do not overwrite user's files unless forced
copy($sourcepath . $file, $dest . $file);
}
}
/**
* Return shared admin CSS for NativeForum admin pages.
* Returns the <style> tag only on the first call per request to avoid duplication.
*/
protected function sfAdminCss(): string {
static $emitted = false;
if($emitted) return '';
$emitted = true;
$css = <<<'CSS'
:root{--sfa-accent:var(--pw-main-color,#e83561);--sfa-surface:rgba(127,127,127,.05);--sfa-surface-2:rgba(127,127,127,.10);--sfa-border:rgba(127,127,127,.25);--sfa-radius:10px;--sfa-ok:#2fb344;--sfa-warn:#f59f00;--sfa-danger:#e03131;--sfa-info:#1c7ed6}
.nativeforum-admin{max-width:none!important;width:100%;color:inherit}
.nativeforum-admin *{box-sizing:border-box}
.nativeforum-admin .sf-muted,.nativeforum-admin .sfa-muted,.nativeforum-admin small{opacity:.72}
.nativeforum-admin h2{margin:0 0 4px;font-size:1.45em;font-weight:600}
.nativeforum-admin h3{margin:0 0 10px;font-size:1.05em;font-weight:600;border:0;padding:0}
.nativeforum-admin h4{margin:0 0 8px;font-size:.78em;font-weight:700;letter-spacing:.07em;text-transform:uppercase;opacity:.7}
.sfa-head{display:flex;align-items:flex-end;justify-content:space-between;gap:16px;flex-wrap:wrap;margin:0 0 18px}
.sfa-head p{margin:2px 0 0;opacity:.72}
.sfa-head .sfa-actions{display:flex;gap:8px;flex-wrap:wrap}
.sfa-tiles{display:grid;grid-template-columns:repeat(auto-fill,minmax(170px,1fr));gap:12px;margin:0 0 18px}
.sfa-tile{display:flex;align-items:center;gap:12px;padding:14px 16px;border:1px solid var(--sfa-border);border-radius:var(--sfa-radius);background:var(--sfa-surface);color:inherit!important;text-decoration:none!important;transition:border-color .12s,transform .12s}
a.sfa-tile:hover{border-color:var(--sfa-accent);transform:translateY(-1px)}
.sfa-tile .fa{font-size:20px;width:40px;height:40px;border-radius:50%;display:flex;align-items:center;justify-content:center;background:var(--sfa-surface-2);color:var(--sfa-accent)}
.sfa-tile b{display:block;font-size:1.5em;line-height:1.1;font-weight:700}
.sfa-tile span{font-size:.82em;opacity:.72}
.sfa-tile.is-alert{border-color:var(--sfa-danger)}
.sfa-tile.is-alert .fa{background:var(--sfa-danger);color:#fff}
.sfa-grid{display:grid;gap:16px;grid-template-columns:repeat(auto-fit,minmax(420px,1fr));align-items:start;margin:0 0 16px}
.sfa-grid-3{grid-template-columns:repeat(auto-fit,minmax(300px,1fr))}
.sfa-card,.sfa-panel{background:var(--sfa-surface);border:1px solid var(--sfa-border);border-radius:var(--sfa-radius);padding:16px 18px;margin:0 0 16px;min-width:0}
.sfa-grid>.sfa-card,.sfa-grid>.sfa-panel{margin:0}
.sfa-panel-h{display:flex;align-items:center;justify-content:space-between;gap:10px;margin:-2px 0 12px}
.sfa-panel-h h3{margin:0;display:flex;align-items:center;gap:8px}
.sfa-panel-h h3 .fa{color:var(--sfa-accent)}
.sfa-panel-h a{font-size:.88em}
.sfa-flex{display:grid;gap:16px;grid-template-columns:repeat(auto-fit,minmax(380px,1fr));margin:0 0 16px}
.sf-sortable li.sf-sort-placeholder{visibility:visible!important;border:2px dashed var(--sfa-accent);background:transparent;min-height:40px}
.sfa-col{min-width:0}
.sfa-flex>.sfa-col.sfa-card{margin:0}
.nativeforum-admin table.AdminDataTable,.nativeforum-admin table.sfa-table{width:100%;border-collapse:separate;border-spacing:0;margin:0;background:transparent;border:1px solid var(--sfa-border);border-radius:8px;overflow:hidden}
.nativeforum-admin table.AdminDataTable th,.nativeforum-admin table.sfa-table th{padding:9px 12px;background:var(--sfa-surface-2);border-bottom:1px solid var(--sfa-border);text-align:left;font-weight:700;font-size:.72em;text-transform:uppercase;letter-spacing:.06em;opacity:.85;white-space:nowrap}
.nativeforum-admin table.AdminDataTable td,.nativeforum-admin table.sfa-table td{padding:9px 12px;border-bottom:1px solid var(--sfa-border);text-align:left;vertical-align:middle}
.nativeforum-admin table.AdminDataTable tbody tr:last-child td,.nativeforum-admin table.sfa-table tbody tr:last-child td{border-bottom:0}
.nativeforum-admin table.AdminDataTable tbody tr:nth-child(even){background:transparent}
.nativeforum-admin table.AdminDataTable tbody tr:hover,.nativeforum-admin table.sfa-table tbody tr:hover{background:var(--sfa-surface)}
.sfa-table td.sfa-nowrap,.sfa-nowrap{white-space:nowrap}
.sfa-badge{display:inline-flex;align-items:center;gap:4px;padding:2px 8px;border-radius:999px;font-size:.75em;font-weight:700;background:var(--sfa-surface-2);white-space:nowrap;line-height:1.6}
.sfa-badge.is-red{background:var(--sfa-danger);color:#fff}.sfa-badge.is-green{background:var(--sfa-ok);color:#fff}.sfa-badge.is-amber{background:var(--sfa-warn);color:#111}.sfa-badge.is-blue{background:var(--sfa-info);color:#fff}.sfa-badge.is-accent{background:var(--sfa-accent);color:#fff}
.sfa-btn{display:inline-flex;align-items:center;gap:6px;padding:6px 11px;border-radius:7px;border:1px solid var(--sfa-border);background:var(--sfa-surface-2);color:inherit!important;font:inherit;font-size:.86em;font-weight:600;line-height:1.3;cursor:pointer;text-decoration:none!important;white-space:nowrap}
.sfa-btn:hover{border-color:var(--sfa-accent)}
.sfa-btn.is-primary{background:var(--sfa-accent);border-color:var(--sfa-accent);color:#fff!important}
.sfa-btn.is-danger:hover{border-color:var(--sfa-danger);color:var(--sfa-danger)!important}
.sfa-btn.is-ok:hover{border-color:var(--sfa-ok);color:var(--sfa-ok)!important}
.sfa-btns{display:flex;gap:6px;flex-wrap:wrap}
.sfa-toolbar{display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin:0 0 14px;padding:10px 12px;background:var(--sfa-surface);border:1px solid var(--sfa-border);border-radius:var(--sfa-radius)}
.sfa-toolbar input[type=search],.sfa-toolbar select{min-width:220px}
.sfa-tabs{display:flex;gap:6px;flex-wrap:wrap;margin:0 0 14px}
.sfa-tabs a{padding:6px 12px;border-radius:999px;border:1px solid var(--sfa-border);text-decoration:none!important;color:inherit!important;font-size:.88em;font-weight:600}
.sfa-tabs a.is-active{background:var(--sfa-accent);border-color:var(--sfa-accent);color:#fff!important}
.sfa-empty{padding:22px;text-align:center;opacity:.75;border:1px dashed var(--sfa-border);border-radius:var(--sfa-radius);margin:0}
.sfa-avatar{width:32px;height:32px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;background:var(--sfa-surface-2);font-weight:700;font-size:.85em;flex:0 0 auto;overflow:hidden}
.sfa-avatar img{width:100%;height:100%;object-fit:cover}
.sfa-user{display:flex;align-items:center;gap:10px}
.sfa-excerpt{opacity:.8;font-size:.9em;margin-top:3px;max-width:60ch;overflow:hidden;text-overflow:ellipsis;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical}
.sfa-quick{list-style:none;margin:0;padding:0;display:grid;gap:6px}
.sfa-quick a{display:flex;align-items:center;gap:10px;padding:8px 10px;border-radius:8px;border:1px solid var(--sfa-border);text-decoration:none!important;color:inherit!important}
.sfa-quick a:hover{border-color:var(--sfa-accent)}
.sfa-quick .fa{color:var(--sfa-accent);width:18px;text-align:center}
/* sortable lists (groups, categories, topics) */
.sf-sortable{list-style:none;margin:0;padding:8px;min-height:56px;border:1px dashed var(--sfa-border);border-radius:var(--sfa-radius);background:transparent}
.sf-sortable li{list-style:none;margin:0 0 6px;padding:9px 12px;border:1px solid var(--sfa-border);border-radius:8px;background:var(--sfa-surface);display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap}
.sf-sortable li:last-child{margin-bottom:0}
.sf-sortable li.ui-sortable-helper{box-shadow:0 10px 24px rgba(0,0,0,.25);border-color:var(--sfa-accent)}
.sf-sortable li.ui-sortable-placeholder{visibility:visible!important;border:2px dashed var(--sfa-accent);background:transparent}
.sf-sortable li.sf-drop-hint{opacity:.65;border-style:dashed;background:transparent;justify-content:center;font-size:.9em}
.sf-title{display:flex;align-items:center;gap:10px;min-width:0;flex:1 1 220px}
.sf-title strong{font-weight:600}
.sf-drag{cursor:grab;opacity:.55;width:22px;height:22px;display:inline-flex;align-items:center;justify-content:center;border-radius:5px;flex:0 0 auto}
.sf-drag:hover{opacity:1;background:var(--sfa-surface-2)}
.sf-drag:active{cursor:grabbing}
.sf-actions{display:flex;align-items:center;gap:6px;flex-wrap:wrap;justify-content:flex-end}
.sf-actions label{display:inline-flex!important;align-items:center;gap:5px;margin:0!important;padding:3px 9px;border:1px solid var(--sfa-border);border-radius:999px;font-size:.78em;font-weight:600;cursor:pointer;user-select:none;white-space:nowrap}
.sf-actions label:has(input:checked){background:var(--sfa-accent);border-color:var(--sfa-accent);color:#fff}
.sf-actions label input{margin:0!important;accent-color:#fff}
.sf-actions select{max-width:240px;font-size:.88em}
.sf-actions .pw-button-small,.sf-actions .sf-edit,.sf-actions .sf-edit-link{padding:4px 10px!important;font-size:.82em!important;border-radius:7px!important;margin:0!important}
.sfa-catgrid{display:grid;gap:14px;grid-template-columns:repeat(auto-fill,minmax(330px,1fr))}
.sfa-catgrid h4 .sfa-badge{margin-left:6px}
/* modal */
.sf-admin-modal-backdrop{position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:10000;display:none}
.sf-admin-modal{position:fixed;inset:0;display:none;z-index:10001;align-items:center;justify-content:center;padding:20px}
.sf-admin-modal .box{background:var(--sfa-modal-bg,#fff);color:inherit;border-radius:12px;max-width:820px;width:100%;max-height:88vh;overflow:auto;border:1px solid var(--sfa-border);box-shadow:0 20px 60px rgba(0,0,0,.35)}
.sf-admin-modal .head{display:flex;align-items:center;justify-content:space-between;padding:12px 16px;border-bottom:1px solid var(--sfa-border)}
.sf-admin-modal .body{padding:16px}
/* forms inside admin pages */
.nativeforum-admin form p{margin:0 0 12px}
.nativeforum-admin form label{font-weight:600;font-size:.92em}
.nativeforum-admin input.pw-input-text,.nativeforum-admin select.pw-input,.nativeforum-admin .sfa-card input[type=text],.nativeforum-admin .sfa-card select{display:block;width:100%;margin-top:5px;padding:8px 10px;border:1px solid var(--sfa-border);border-radius:7px;background:rgba(255,255,255,.04);color:inherit;font:inherit;font-weight:400;min-height:38px}
.nativeforum-admin form label input[type=checkbox]{margin-right:6px;accent-color:var(--sfa-accent)}
.nativeforum-admin .sf-actions select.pw-input{display:inline-block;width:auto;margin:0;min-height:30px;padding:4px 8px}
.sfa-grid-msg{grid-template-columns:minmax(300px,1fr) minmax(420px,2fr)}
.nativeforum-admin .sfa-panel .InputfieldContent select,.nativeforum-admin .sfa-panel select.uk-select{width:100%;max-width:100%}
.nativeforum-admin select,.nativeforum-admin input[type=text],.nativeforum-admin input[type=search]{max-width:100%}
/* native dropdown lists follow the admin theme (Konkat dark: no white-on-white options) */
.nativeforum-admin select,.sf-admin-modal select{color-scheme:var(--sfa-scheme,normal)}
.nativeforum-admin select option,.nativeforum-admin select optgroup,.sf-admin-modal select option,.sf-admin-modal select optgroup{background-color:var(--sfa-modal-bg,#fff);color:var(--sfa-fg,#1f2328)}
@media (max-width:980px){.sfa-grid-msg{grid-template-columns:1fr}}
@media (max-width:640px){
.sfa-grid,.sfa-flex,.sfa-grid-msg,.sfa-catgrid{grid-template-columns:1fr}
.sfa-tiles{grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}
.sfa-head{flex-wrap:wrap}.sfa-head .sfa-actions{width:100%;justify-content:flex-start;flex-wrap:wrap}
.nativeforum-admin table.sfa-table,.nativeforum-admin table.sfa-table tbody,.nativeforum-admin table.sfa-table tr,.nativeforum-admin table.sfa-table td{display:block;width:100%;box-sizing:border-box}
.nativeforum-admin table.sfa-table thead{display:none}
.nativeforum-admin table.sfa-table tr{display:flex!important;flex-wrap:wrap;align-items:center;gap:4px 2px;padding:10px 4px;border-bottom:1px solid var(--sfa-border)}
.nativeforum-admin table.sfa-table td{width:auto!important}.nativeforum-admin table.sfa-table td:first-child{flex:1 1 100%}
.nativeforum-admin table.sfa-table tbody tr:last-child{border-bottom:0}
.nativeforum-admin table.sfa-table td{border:0!important;padding:3px 10px!important;white-space:normal}
.nativeforum-admin table.sfa-table td:empty{display:none}
.nativeforum-admin table.sfa-table td.sfa-nowrap{font-size:.88em;opacity:.85}
.nativeforum-admin table.sfa-table .sfa-btns{flex-wrap:wrap;justify-content:flex-start}
.sfa-panel,.sfa-card{padding-left:12px!important;padding-right:12px!important}
.sf-sortable li{flex-wrap:wrap}.sf-actions{flex-wrap:wrap;justify-content:flex-start!important}
.sfa-edit-topic .sfa-pager .sf-muted{flex-basis:100%;margin:6px 0 0}
}
.sfa-title-link{color:inherit!important;text-decoration:none!important}.sfa-title-link:hover strong{color:var(--sfa-accent)}
.sf-actions .sf-edit-link{display:inline-flex;align-items:center;gap:5px;text-decoration:none!important}
.sfa-edit-topic .sfa-pager{display:flex;flex-wrap:wrap;gap:6px;align-items:center}.sfa-edit-topic .sfa-pager .sf-muted{margin-left:8px;font-size:.85em}
.sfa-edit-topic .sfa-reply-fs>.InputfieldHeader{font-weight:600}
.sfa-edit-topic .sfa-reply-fs .InputfieldContent>.description,.sfa-edit-topic .sfa-reply-fs>.InputfieldContent>p.description{opacity:.75}
.sfa-edit-topic .sfa-danger-check label,.sfa-edit-topic .sfa-danger-check .pw-no-select{color:var(--sfa-danger)}
.sfa-edit-topic .InputfieldMarkup .sfa-btn{margin-right:6px}
CSS;
return '<script>window.sfaReload=function(){try{sessionStorage.setItem("sfa-y:"+location.pathname+location.search,String(window.scrollY));}catch(e){}location.reload();};(function(){function r(){try{var k="sfa-y:"+location.pathname+location.search,y=sessionStorage.getItem(k);if(y!==null){sessionStorage.removeItem(k);window.scrollTo(0,+y);setTimeout(function(){window.scrollTo(0,+y);},300);}}catch(e){}}if(document.readyState!=="loading")r();else document.addEventListener("DOMContentLoaded",r);})();(function(){function bg(e){while(e){var c=getComputedStyle(e).backgroundColor;if(c&&c!=="transparent"&&!/rgba\\(.*,\\s*0\\)$/.test(c))return c;e=e.parentElement;}return "";}function set(){var c=bg(document.getElementById("pw-content-body")||document.querySelector("#pw-content,main")||document.body);if(c){var r=document.documentElement.style;r.setProperty("--sfa-modal-bg",c);var m=c.match(/[\\d.]+/g)||[255,255,255];var l=(0.299*m[0]+0.587*m[1]+0.114*m[2])/255;r.setProperty("--sfa-scheme",l<0.5?"dark":"light");r.setProperty("--sfa-fg",l<0.5?"#e9ecef":"#1f2328");}}if(document.readyState!=="loading")set();else document.addEventListener("DOMContentLoaded",set);})();</script><style>' . $css . '</style>';
}
/** Page header with icon, description and optional action buttons. */
protected function sfaHead(string $title, string $icon, string $desc = '', string $actions = ''): string {
return '<div class="sfa-head"><div><h2><i class="fa fa-fw fa-' . $icon . '"></i> ' . $this->sanitizer->entities($title) . '</h2>'
. ($desc !== '' ? '<p>' . $desc . '</p>' : '') . '</div>' . ($actions !== '' ? '<div class="sfa-actions">' . $actions . '</div>' : '') . '</div>';
}
/************************************************************
* NAV: Dashboard (default execute)
************************************************************/
public function execute() {
$modules = $this->wire('modules');
/** @var NativeForum $forum */
$forum = $modules->get('NativeForum');
$pages = $this->wire('pages');
$config = $this->wire('config');
$users = $this->wire('users');
$san = $this->sanitizer;
$admin = $this->page->url;
// demo content: add / start empty / remove
$input = $this->wire('input');
if($input->post('sfa_demo') && method_exists($forum, 'addDemoContent')) {
$this->wire('session')->CSRF->validate();
try {
$act = (string) $input->post('sfa_demo');
if($act === 'add') { $r = $forum->addDemoContent(); $this->message('Demo content added: ' . count($r['pages'] ?? []) . ' pages and ' . count($r['users'] ?? []) . ' demo members. Have a look at the forum!'); }
elseif($act === 'skip') { $forum->skipDemoContent(); $this->message('OK – starting with an empty forum. Create your first group and category under Groups/Categories.'); }
elseif($act === 'remove') { $n = $forum->removeDemoContent(); $this->message("Demo content removed ($n items)."); }
} catch(\Throwable $e) { $this->error($e->getMessage()); }
$this->wire('session')->redirect($this->page->url);
}
$forumRoot = $forum->forumRoot();
$messagesRoot = $forum->messagesRoot();
$rootId = ($forumRoot && $forumRoot->id) ? (int) $forumRoot->id : 0;
$msgRootId = ($messagesRoot && $messagesRoot->id) ? (int) $messagesRoot->id : 0;
$nCats = $rootId ? (int) $pages->count("template=forum-category, has_parent=$rootId, include=all") : 0;
$nTopics = $rootId ? (int) $pages->count("template=forum-topic, has_parent=$rootId, include=all") : 0;
$nReplies = $rootId ? (int) $pages->count("template=forum-reply, has_parent=$rootId, include=all") : 0;
$nMembers = (int) $users->count('id>0, name!=guest, include=all, check_access=0');
$nUnread = $msgRootId ? (int) $pages->count("template=forum-message, has_parent=$msgRootId, forum_is_read=0, include=all") : 0;
$nReports = method_exists($forum, 'openReportCount') ? $forum->openReportCount() : 0;
$day = time() - 86400;
$nToday = $rootId ? (int) $pages->count("template=forum-topic|forum-reply, has_parent=$rootId, created>$day, include=all") : 0;
$out = $this->sfAdminCss() . '<div class="nativeforum-admin">';
$out .= $this->sfaHead('Dashboard', 'dashboard', 'Overview of your forum.',
($forumRoot && $forumRoot->id ? '<a class="sfa-btn" href="' . $forumRoot->url . '" target="_blank"><i class="fa fa-external-link"></i> View forum</a>' : '')
. '<a class="sfa-btn" href="' . $admin . 'settings/"><i class="fa fa-cog"></i> Settings</a>');
// Welcome: offer demo content once; afterwards a small "remove demo" bar while it exists
if(method_exists($forum, 'demoContentInfo')) {
$tok = $this->wire('session')->CSRF->renderInput();
$demo = $forum->demoContentInfo();
$btn = function($act, $icon, $label, $cls = '') use ($tok) {
return '<form method="post" style="display:inline">' . $tok . '<button type="submit" name="sfa_demo" value="' . $act . '" class="sfa-btn' . ($cls ? ' ' . $cls : '') . '"' . ($act === 'remove' ? ' onclick="return confirm(\'Remove the demo groups, topics, replies and demo members?\')"' : '') . '><i class="fa fa-' . $icon . '"></i> ' . $label . '</button></form> ';
};
if($demo) {
$out .= '<div class="sfa-panel sfa-demo-bar" style="display:flex;gap:12px;align-items:center;justify-content:space-between;flex-wrap:wrap">'
. '<div><i class="fa fa-magic"></i> <strong>Demo content is on your forum</strong> <span class="sf-muted">– ' . count($demo['pages'] ?? []) . ' pages, ' . count($demo['users'] ?? []) . ' demo members. Edit it or remove it when you are ready.</span></div>'
. '<div>' . $btn('remove', 'trash', 'Remove demo content', 'is-danger') . '</div></div>';
} elseif(!$forum->demoChoiceMade() && !$nTopics) {
$out .= '<div class="sfa-panel sfa-welcome" style="border-left:4px solid var(--sfa-accent);">'
. '<h3 style="margin:0 0 6px"><i class="fa fa-hand-peace-o"></i> Welcome to Native Forum!</h3>'
. '<p style="margin:0 0 12px">Want to see how a living forum looks? Add a little demo content: a <em>Community</em> group with 3 categories, '
. '4 topics (incl. pinned <em>Forum rules</em>), a few replies and 2 demo members. You can edit it – or remove it with one click.</p>'
. $btn('add', 'magic', 'Add demo content', 'is-primary') . $btn('skip', 'square-o', 'Start with an empty forum')
. '</div>';
}
}
// KPI tiles
$tile = function($icon, $n, $label, $url = '', $alert = false) {
$tag = $url ? 'a href="' . $url . '"' : 'div';
$end = $url ? 'a' : 'div';
return '<' . $tag . ' class="sfa-tile' . ($alert ? ' is-alert' : '') . '"><i class="fa fa-' . $icon . '"></i><div><b>' . number_format($n, 0, ',', '.') . '</b><span>' . $label . '</span></div></' . $end . '>';
};
$out .= '<div class="sfa-tiles">'
. $tile('flag', $nReports, 'Open reports', $admin . 'reports/', $nReports > 0)
. $tile('comments', $nTopics, 'Topics', $admin . 'topics/')
. $tile('comment-o', $nReplies, 'Replies')
. $tile('bolt', $nToday, 'Posts in 24 h')
. $tile('folder-o', $nCats, 'Categories', $admin . 'groups/')
. $tile('users', $nMembers, 'Members', $admin . 'members/')
. $tile('envelope-o', $nUnread, 'Unread messages', $admin . 'messages/')
. '</div>';
// Reports block
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-flag"></i> Reports to review</h3><a href="' . $admin . 'reports/">All reports →</a></div>';
$out .= $this->sfaReportsTable($forum, 'open', 5, true);
$out .= '</div>';
$out .= '<div class="sfa-grid">';
// Latest posts
$latest = $rootId ? $pages->find("template=forum-topic|forum-reply, has_parent=$rootId, include=all, sort=-created, limit=8") : new PageArray();
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-comments"></i> Latest posts</h3><a href="' . $admin . 'topics/">Topics →</a></div>';
if(!$latest->count()) {
$out .= '<p class="sfa-empty">No posts yet.</p>';
} else {
$out .= '<table class="sfa-table"><thead><tr><th>Post</th><th>Author</th><th>Date</th></tr></thead><tbody>';
foreach($latest as $p) {
$t = $p->template->name === 'forum-topic' ? $p : $p->parent;
$a = $p->getUnformatted('forum_author'); if($a instanceof PageArray) $a = $a->first();
$url = $p->template->name === 'forum-reply' && method_exists($forum, 'replyUrl') ? $forum->replyUrl($p) : $p->url;
$out .= '<tr><td>' . ($p->template->name === 'forum-topic' ? '<span class="sfa-badge is-blue">topic</span> ' : '<span class="sfa-badge">reply</span> ')
. '<a href="' . $url . '" target="_blank"><strong>' . $san->entities($t ? $t->title : $p->title) . '</strong></a>'
. ($p->isUnpublished() ? ' <span class="sfa-badge is-amber">unpublished</span>' : '')
. '<div class="sfa-excerpt">' . $san->entities(mb_strimwidth(trim(preg_replace('/\s+/u', ' ', strip_tags((string) $p->getUnformatted('forum_body')))), 0, 120, '…')) . '</div></td>'
. '<td>' . $this->sfaUserCell($a instanceof User ? $a : null) . '</td><td class="sfa-nowrap">' . date('Y-m-d H:i', $p->created)
. ($t && $t->id ? ' <a class="sfa-btn" style="margin-left:6px" href="' . $admin . 'edit-topic/?id=' . (int) $t->id . ($p->template->name === 'forum-reply' ? '&reply=' . (int) $p->id : '') . '" title="Edit"><i class="fa fa-pencil"></i></a>' : '') . '</td></tr>';
}
$out .= '</tbody></table>';
}
$out .= '</div>';
// Right column: quick links + newest members + messages
$out .= '<div>';
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-link"></i> Quick links</h3></div><ul class="sfa-quick">';
if($forumRoot && $forumRoot->id) $out .= '<li><a href="' . $forumRoot->url . '" target="_blank"><i class="fa fa-external-link"></i> Open the forum</a></li>';
$out .= '<li><a href="' . $admin . 'groups/"><i class="fa fa-sitemap"></i> Groups & categories</a></li>';
$out .= '<li><a href="' . $admin . 'settings/"><i class="fa fa-cog"></i> Forum settings</a></li>';
$out .= '<li><a href="' . $config->urls->admin . 'setup/logs/view/nativeforum-spam/"><i class="fa fa-shield"></i> Blocked spam (log)</a></li>';
if($forumRoot && $forumRoot->id) $out .= '<li><a href="' . $config->urls->admin . 'page/edit/?id=' . $forumRoot->id . '"><i class="fa fa-pencil"></i> Edit forum root page</a></li>';
$out .= '</ul></div>';
$newest = $users->find('id>0, name!=guest, include=all, check_access=0, sort=-created, limit=6');
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-user-plus"></i> Newest members</h3><a href="' . $admin . 'members/">All members →</a></div>';
$out .= '<table class="sfa-table"><tbody>';
foreach($newest as $u) $out .= '<tr><td>' . $this->sfaUserCell($u, true) . '</td><td class="sfa-nowrap" style="text-align:right">' . date('Y-m-d', (int) $u->created) . '</td></tr>';
$out .= '</tbody></table></div>';
$out .= '</div>'; // right column
$out .= '</div>'; // grid
// Categories overview
if($rootId) {
$cats = $pages->find("template=forum-category, has_parent=$rootId, include=all, sort=parent, sort=sort");
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-folder-o"></i> Categories</h3><a href="' . $admin . 'groups/">Manage →</a></div>';
if(!$cats->count()) {
$out .= '<p class="sfa-empty">No categories yet.</p>';
} else {
$out .= '<table class="sfa-table"><thead><tr><th>Category</th><th>Group</th><th>Topics</th><th>Last topic</th><th></th></tr></thead><tbody>';
foreach($cats as $cat) {
$n = (int) $pages->count('template=forum-topic, include=all, parent_id=' . (int) $cat->id);
$last = $pages->findOne('template=forum-topic, include=all, parent_id=' . (int) $cat->id . ', sort=-created');
$flags = '';
if($cat->hasField('forum_is_vip') && (int) $cat->getUnformatted('forum_is_vip')) $flags .= ' <span class="sfa-badge is-amber">VIP</span>';
if($cat->isHidden()) $flags .= ' <span class="sfa-badge">hidden</span>';
if($cat->isUnpublished()) $flags .= ' <span class="sfa-badge is-red">unpublished</span>';
$out .= '<tr><td><a href="' . $cat->url . '" target="_blank"><strong>' . $san->entities($cat->title) . '</strong></a>' . $flags . '</td>'
. '<td>' . ($cat->parent->template->name === 'forum-group' ? $san->entities($cat->parent->title) : '<span class="sf-muted">—</span>') . '</td>'
. '<td>' . $n . '</td>'
. '<td>' . ($last && $last->id ? '<a href="' . $last->url . '" target="_blank">' . $san->entities($last->title) . '</a> <small>' . date('Y-m-d', $last->created) . '</small>' : '<span class="sf-muted">—</span>') . '</td>'
. '<td><a class="sfa-btn" href="' . $admin . 'topics/?cat=' . (int) $cat->id . '" title="Manage topics"><i class="fa fa-list"></i></a></td></tr>';
}
$out .= '</tbody></table>';
}
$out .= '</div>';
}
$out .= '</div>';
return $out;
}
/************************************************************
* NAV: Reports (moderation queue)
************************************************************/
/************************************************************
* EXPORT / IMPORT (move a whole forum to another site)
************************************************************/
public function ___executeTransfer() {
$forum = $this->wire('modules')->get('NativeForum');
$input = $this->wire('input');
$session = $this->wire('session');
$user = $this->wire('user');
$san = $this->sanitizer;
if(!$user->isSuperuser()) return '<p>Only superusers can export or import the forum.</p>';
$self = $this->page->url . 'transfer/';
// export: stream the zip
if($input->post('sfa_export')) {
$session->CSRF->validate();
try {
$path = $forum->transfer()->export([
'settings' => (bool) $input->post('ex_settings'),
'content' => (bool) $input->post('ex_content'),
'members' => (bool) $input->post('ex_members'),
'files' => (bool) $input->post('ex_files'),
'passwords' => (bool) $input->post('ex_passwords'),
'passphrase' => (string) $input->post('ex_passphrase'),
]);
$this->wire('log')->save('native-forum', 'Export created: ' . basename($path) . ' (' . round(filesize($path) / 1048576, 2) . ' MB)');
wireSendFile($path, ['forceDownload' => true, 'exit' => true, 'downloadFilename' => 'native-forum-' . preg_replace('~[^a-z0-9.-]+~i', '-', (string) $this->wire('config')->httpHost) . '-' . date('Y-m-d') . '.zip']);
} catch(\Throwable $e) { $this->error('Export failed: ' . $e->getMessage()); $session->redirect($self); }
}
// import
if($input->post('sfa_import')) {
$session->CSRF->validate();
$f = $_FILES['import_zip'] ?? null;
if(!$f || (int) ($f['error'] ?? 4) !== UPLOAD_ERR_OK || !is_uploaded_file((string) $f['tmp_name'])) {
$err = (int) ($f['error'] ?? 4);
$this->error($err === UPLOAD_ERR_INI_SIZE || $err === UPLOAD_ERR_FORM_SIZE
? 'The file is larger than this server accepts (upload_max_filesize = ' . ini_get('upload_max_filesize') . ', post_max_size = ' . ini_get('post_max_size') . ').'
: 'Please choose the export .zip file.');
$session->redirect($self);
}
if((string) $input->post('im_confirm') !== 'IMPORT') { $this->error('Type IMPORT to confirm.'); $session->redirect($self); }
try {
$r = $forum->transfer()->import((string) $f['tmp_name'], [
'settings' => (bool) $input->post('im_settings'),
'content' => (bool) $input->post('im_content'),
'members' => (bool) $input->post('im_members'),
'createUsers' => (bool) $input->post('im_create_users'),
'mode' => $input->post('im_mode') === 'replace' ? 'replace' : 'merge',
'passphrase' => (string) $input->post('im_passphrase'),
]);
$this->message(sprintf('Import done: %d posts/pages, %d files, %d settings, members %d matched + %d created (%d keep their password), %d reports%s%s.',
$r['pages'], $r['files'], $r['settings'], $r['users_matched'], $r['users_created'], $r['passwords'] ?? 0, $r['reports'],
$r['deleted'] ? ", {$r['deleted']} old sections removed first" : '', $r['skipped'] ? ", {$r['skipped']} skipped" : ''));
foreach(array_slice($r['warnings'], 0, 10) as $w) $this->warning($w);
$this->wire('log')->save('native-forum', 'Import: ' . json_encode(array_diff_key($r, ['warnings' => 1])));
} catch(\Throwable $e) { $this->error('Import failed: ' . $e->getMessage()); }
$session->redirect($self);
}
$this->headline('Export / Import');
$this->browserTitle('Export / Import');
$csrf = $session->CSRF->renderInput();
$maxUp = ini_get('upload_max_filesize');
$chk = function($name, $label, $hint = '', $on = true) {
return '<label class="sfa-check"><input type="checkbox" name="' . $name . '" value="1"' . ($on ? ' checked' : '') . '> <span><strong>' . $label . '</strong>' . ($hint !== '' ? '<small>' . $hint . '</small>' : '') . '</span></label>';
};
$out = $this->sfAdminCss() . '<div class="nativeforum-admin sfa-transfer">';
$out .= $this->sfaHead('Export / Import', 'exchange', 'Move a whole forum to another site: download one .zip here, install Native Forum on the other site and import the file there.');
$out .= '<style>.sfa-transfer .sfa-check{display:flex;gap:10px;align-items:flex-start;margin:0 0 10px;cursor:pointer}.sfa-transfer .sfa-check input{margin-top:4px}.sfa-transfer .sfa-check small{display:block;opacity:.7;font-weight:400}'
. '.sfa-transfer .sfa-radio{display:block;margin:0 0 6px}.sfa-transfer .sfa-field{margin:12px 0}.sfa-transfer input[type=file],.sfa-transfer input[type=text]{max-width:100%}'
. '.sfa-transfer .sfa-note{font-size:.88em;opacity:.8;margin:10px 0 0}</style>';
$out .= '<div class="sfa-grid">';
// export panel
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-download"></i> Export</h3></div>'
. '<form method="post" action="' . $self . '">' . $csrf
. $chk('ex_settings', 'Settings', 'All Native Forum settings, design preset, Design Studio, texts.')
. $chk('ex_content', 'Forum content', 'Groups, categories, topics, replies, private messages, reports – with dates, authors, likes and votes.')
. $chk('ex_members', 'Members', 'User name, e-mail, forum roles and forum profile (nickname, signature, rank, followed topics, read markers). Passwords only with the option below.')
. $chk('ex_files', 'Uploaded images and files', 'Pictures and attachments in posts and messages, avatars.')
. $chk('ex_passwords', 'Passwords (encrypted)', 'Members keep their password on the new site. Only the secure password hashes are exported – never readable passwords – and they are encrypted (AES-256) with the transfer password below. At a member\'s first login on the new site the password is checked once and saved with the new site\'s own hashing.', false)
. '<div class="sfa-field sfa-pass" style="margin:-4px 0 12px 28px"><label>Transfer password (min. 8 characters, you need it for the import): '
. '<input type="password" name="ex_passphrase" autocomplete="new-password" style="width:220px"></label></div>'
. '<button type="submit" name="sfa_export" value="1" class="ui-button ui-state-default"><i class="fa fa-download"></i> Download export (.zip)</button>'
. '<p class="sfa-note">Big forums with many images can take a while.</p></form></div>';
// import panel
$out .= '<div class="sfa-panel"><div class="sfa-panel-h"><h3><i class="fa fa-upload"></i> Import</h3></div>'
. '<form method="post" action="' . $self . '" enctype="multipart/form-data">' . $csrf
. '<div class="sfa-field"><input type="file" name="import_zip" accept=".zip,application/zip" required> <span class="sf-muted">max ' . $san->entities($maxUp) . '</span></div>'
. $chk('im_settings', 'Settings', 'Replace this site\'s forum settings with the ones from the file (page tree and root pages of this site are kept).')
. $chk('im_content', 'Forum content')
. '<div style="margin:-4px 0 10px 28px">'
. '<label class="sfa-radio"><input type="radio" name="im_mode" value="merge" checked> Add to the existing forum</label>'
. '<label class="sfa-radio"><input type="radio" name="im_mode" value="replace"> Replace: delete this forum\'s groups, categories, topics and messages first</label></div>'
. $chk('im_members', 'Members', 'Members are matched by user name, then e-mail.')
. $chk('im_create_users', 'Create missing members', 'They get the forum-member role. With exported passwords they log in with their old password, otherwise they use “Forgot password”.')
. '<div class="sfa-field" style="margin:-4px 0 12px 28px"><label>Transfer password (only if the export contains passwords): '
. '<input type="password" name="im_passphrase" autocomplete="off" style="width:220px"></label></div>'
. '<div class="sfa-field"><label>Type <strong>IMPORT</strong> to confirm: <input type="text" name="im_confirm" autocomplete="off" style="width:120px"></label></div>'
. '<button type="submit" name="sfa_import" value="1" class="ui-button ui-state-default"><i class="fa fa-upload"></i> Import</button>'
. '<p class="sfa-note">Tip: make a database backup before importing. Posts get new page IDs; links and pictures in the text are updated.</p></form></div>';
$out .= '</div></div>';
$out .= '<script>(function(){var c=document.querySelector("input[name=ex_passwords]"),f=document.querySelector(".sfa-pass");if(!c||!f)return;function s(){f.style.display=c.checked?"":"none";}c.addEventListener("change",s);s();})();</script>';
return $out;
}
public function ___executeReports() {
$forum = $this->wire('modules')->get('NativeForum');
$input = $this->wire('input');
$session = $this->wire('session');
if($input->post('sfa_report_action')) {
$session->CSRF->validate();
try {
$msg = $forum->handleReport((int) $input->post('post_id'), (string) $input->post('sfa_report_action'), $this->wire('user'));
if($msg) $this->message($msg);
} catch(\Throwable $e) { $this->error($e->getMessage()); }
$back = (string) $input->post('back');
$session->redirect($back && strpos($back, '/') === 0 ? $back : $this->page->url . 'reports/');
}
$status = (string) $input->get('status');
if(!in_array($status, ['open', 'closed', 'all'], true)) $status = 'open';
$open = $forum->openReportCount();
$out = $this->sfAdminCss() . '<div class="nativeforum-admin">';
$out .= $this->sfaHead('Reports', 'flag', 'Posts that members reported. Look at the post, then hide it, lock the topic, delete it, or dismiss the report.');
$out .= '<div class="sfa-tabs">'
. '<a href="?status=open"' . ($status === 'open' ? ' class="is-active"' : '') . '>Open' . ($open ? ' (' . $open . ')' : '') . '</a>'
. '<a href="?status=closed"' . ($status === 'closed' ? ' class="is-active"' : '') . '>Handled</a>'
. '<a href="?status=all"' . ($status === 'all' ? ' class="is-active"' : '') . '>All</a></div>';
$out .= '<div class="sfa-panel">' . $this->sfaReportsTable($forum, $status, 100, false) . '</div>';
$out .= '</div>';
return $out;
}
/** Reports table (dashboard + reports page). */
protected function sfaReportsTable($forum, string $status, int $limit, bool $compact): string {
if(!method_exists($forum, 'reportsByPost')) return '<p class="sfa-empty">Reports need the latest Native Forum.</p>';
$rows = $forum->reportsByPost($status, $limit);
if(!$rows) return '<p class="sfa-empty"><i class="fa fa-check"></i> ' . ($status === 'open' ? 'Nothing to review.' : 'No reports.') . '</p>';
$san = $this->sanitizer;
$pages = $this->wire('pages');
$reasons = $forum->reportReasons();
$back = $this->wire('input')->url(true);
$token = $this->wire('session')->CSRF->renderInput();
$out = '<table class="sfa-table"><thead><tr><th>Reported post</th><th>Why</th>' . ($compact ? '' : '<th>Reported by</th>') . '<th>When</th><th>Actions</th></tr></thead><tbody>';
foreach($rows as $pid => $r) {
$p = $pages->get((int) $pid);
$exists = $p && $p->id;
$topic = $exists ? ($p->template->name === 'forum-topic' ? $p : $p->parent) : null;
$author = null;
if($exists) { $a = $p->getUnformatted('forum_author'); if($a instanceof PageArray) $a = $a->first(); if($a instanceof User) $author = $a; }
$hidden = $exists && ($p->isUnpublished() || ($p->hasField('forum_is_deleted') && (int) $p->getUnformatted('forum_is_deleted')) || $p->isTrash());
$locked = $topic && $topic->id && $topic->hasField('forum_is_locked') && (int) $topic->getUnformatted('forum_is_locked');
$url = $exists ? ($p->template->name === 'forum-reply' && method_exists($forum, 'replyUrl') ? $forum->replyUrl($p) : $p->url) : '';
$why = '';
foreach($r['reasons'] as $k => $n) $why .= '<span class="sfa-badge' . ($k === 'spam' || $k === 'illegal' ? ' is-red' : ($k === 'offensive' ? ' is-amber' : '')) . '">' . $san->entities($reasons[$k] ?? $k) . ($n > 1 ? ' ×' . $n : '') . '</span> ';
$comments = '';
$who = [];
foreach($r['items'] as $it) {
$ru = $this->wire('users')->get((int) $it['reporter_id']);
$who[] = $ru && $ru->id ? $san->entities($forum->displayName($ru)) : 'guest';
if(trim((string) $it['comment']) !== '') $comments .= '<div class="sfa-excerpt">“' . $san->entities($it['comment']) . '”</div>';
}
$out .= '<tr><td>';
if($exists) {
$out .= '<a href="' . $url . '" target="_blank"><strong>' . $san->entities($topic ? $topic->title : $p->title) . '</strong></a> '
. ($p->template->name === 'forum-topic' ? '<span class="sfa-badge is-blue">topic</span>' : '<span class="sfa-badge">reply</span>')
. ($hidden ? ' <span class="sfa-badge is-amber">hidden</span>' : '') . ($locked ? ' <span class="sfa-badge">locked</span>' : '')
. '<div class="sfa-excerpt">' . $san->entities(mb_strimwidth(trim(preg_replace('/\s+/u', ' ', strip_tags((string) $p->getUnformatted('forum_body')))), 0, 160, '…')) . '</div>'
. '<div style="margin-top:6px">' . $this->sfaUserCell($author) . '</div>';
} else {
$out .= '<span class="sf-muted">Post #' . (int) $pid . ' no longer exists</span>';
}
$out .= '</td><td>' . $why . $comments . '</td>';
if(!$compact) $out .= '<td>' . implode(', ', array_unique($who)) . '</td>';
$out .= '<td class="sfa-nowrap">' . date('Y-m-d H:i', (int) $r['last']) . ($r['count'] > 1 ? '<br><small>' . (int) $r['count'] . ' reports</small>' : '')
. ($r['status'] !== 'open' ? '<br><span class="sfa-badge is-green">' . $san->entities($r['status']) . ($r['action'] ? ': ' . $san->entities($r['action']) : '') . '</span>' : '') . '</td>';
$btn = function($action, $icon, $label, $cls = '', $confirm = '') use ($pid, $token, $back) {
return '<form method="post" action="' . $this->page->url . 'reports/" style="display:inline">' . $token
. '<input type="hidden" name="post_id" value="' . (int) $pid . '"><input type="hidden" name="back" value="' . htmlspecialchars($back, ENT_QUOTES) . '">'
. '<button class="sfa-btn ' . $cls . '" type="submit" name="sfa_report_action" value="' . $action . '" title="' . $label . '"' . ($confirm ? ' onclick="return confirm(\'' . $confirm . '\')"' : '') . '><i class="fa fa-' . $icon . '"></i> ' . $label . '</button></form>';
};
$out .= '<td><div class="sfa-btns">';
if($exists) {
$out .= '<a class="sfa-btn" href="' . $url . '" target="_blank" title="Open the post"><i class="fa fa-eye"></i> View</a>';
$out .= $hidden ? $btn('restore', 'undo', 'Restore', 'is-ok') : $btn('hide', 'eye-slash', 'Hide', 'is-danger');
if($topic && $topic->id && $topic->hasField('forum_is_locked')) $out .= $locked ? $btn('unlock', 'unlock', 'Unlock') : $btn('lock', 'lock', 'Lock topic');
if($topic && $topic->id) $out .= '<a class="sfa-btn" href="' . $this->page->url . 'edit-topic/?id=' . (int) $topic->id . ($p->template->name === 'forum-reply' ? '&reply=' . (int) $p->id : '') . '" title="Edit the text"><i class="fa fa-pencil"></i> Edit</a>';
$out .= $btn('delete', 'trash', 'Delete', 'is-danger', 'Move this post to the trash?');
}
if($r['status'] === 'open') {
$out .= $btn('resolve', 'check', 'Resolved', 'is-ok') . $btn('dismiss', 'times', 'Dismiss');
}
$out .= '</div></td></tr>';
}
return $out . '</tbody></table>';
}
/**
* Handle AJAX drag/drop actions for sorting/moving groups/categories/topics in admin.
* Returns true if handled (response already output + exited).
*/
protected function sfHandleAjax() {
$input = $this->wire('input');
if(!$input->post->sf_action) return false;
$session = $this->wire('session');
$pages = $this->wire('pages');
// CSRF
try { $session->CSRF->validate(); } catch(\Throwable $e) {
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok' => false, 'error' => 'CSRF']);
exit;
}
$action = (string) $input->post->sf_action;
// Helpers
$saveOrder = function(Page $parent, array $ids, string $expectedTpl) use ($pages) {
$i = 0;
foreach($ids as $id) {
$id = (int) $id;
if(!$id) continue;
$p = $pages->get($id);
if(!$p || !$p->id) continue;
if($expectedTpl && $p->template && $p->template->name !== $expectedTpl) continue;
$p->of(false);
if($parent && $parent->id && $p->parent->id !== $parent->id) {
$p->parent = $parent;
}
$p->sort = $i++;
$p->save();
$p->of(true);
}
};
$ok = true; $msg = '';
$html = null;
if($action === 'sort_groups') {
$rootId = (int) $input->post->root;
$root = $pages->get($rootId);
$order = $input->post->order;
if(!$root || !$root->id) { $ok=false; $msg='Missing root'; }
else {
$ids = is_array($order) ? $order : [];
$saveOrder($root, $ids, 'forum-group');
}
} elseif($action === 'sort_categories') {
$parentId = (int) $input->post->parent;
$parent = $pages->get($parentId);
$order = $input->post->order;
if(!$parent || !$parent->id) { $ok=false; $msg='Missing parent'; }
else {
$ids = is_array($order) ? $order : [];
$saveOrder($parent, $ids, 'forum-category');
}
} elseif($action === 'move_category') {
$id = (int) $input->post->id;
$parentId = (int) $input->post->parent;
$p = $pages->get($id);
$parent = $pages->get($parentId);
if(!$p || !$p->id || $p->template->name !== 'forum-category') { $ok=false; $msg='Bad category'; }
elseif(!$parent || !$parent->id || !in_array($parent->template->name, ['forum-group','forum-root'], true)) { $ok=false; $msg='Bad parent'; }
else {
$p->of(false);
$p->parent = $parent;
$p->sort = 9999;
$p->save();
$p->of(true);
}
} elseif($action === 'sort_topics') {
$parentId = (int) $input->post->parent;
$parent = $pages->get($parentId);
$order = $input->post->order;
if(!$parent || !$parent->id) { $ok=false; $msg='Missing category'; }
else {
$ids = is_array($order) ? $order : [];
$saveOrder($parent, $ids, 'forum-topic');
}
} elseif($action === 'move_topic') {
$id = (int) $input->post->id;
$parentId = (int) $input->post->parent;
$p = $pages->get($id);
$parent = $pages->get($parentId);
if(!$p || !$p->id || $p->template->name !== 'forum-topic') { $ok=false; $msg='Bad topic'; }
elseif(!$parent || !$parent->id || $parent->template->name !== 'forum-category') { $ok=false; $msg='Bad category'; }
else {
$p->of(false);
$p->parent = $parent;
$p->sort = 9999;
$p->save();
$p->of(true);
}
} elseif($action === 'toggle_field') {
$type = (string) $input->post->type;
$id = (int) $input->post->id;
$field = (string) $input->post->field;
$value = (int) $input->post->value;
$p = $pages->get($id);
if(!$p || !$p->id) { $ok = false; $msg = 'Not found'; }
else {
// Validate expected template by type
$expected = [
'group' => 'forum-group',
'category' => 'forum-category',
'topic' => 'forum-topic',
];
if(!isset($expected[$type]) || $p->template->name !== $expected[$type]) {
$ok = false; $msg = 'Bad type';
} else {
$p->of(false);
// Page status toggles
if($field === 'status_hidden') {
if($value) $p->addStatus(Page::statusHidden);
else $p->removeStatus(Page::statusHidden);
} elseif($field === 'status_unpublished') {
if($value) $p->addStatus(Page::statusUnpublished);
else $p->removeStatus(Page::statusUnpublished);
} else {
// Field toggles (whitelist)
$allowed = [
'group' => ['forum_group_is_vip', 'forum_group_is_pinned'],
'category' => ['forum_is_vip'],
'topic' => ['forum_is_pinned', 'forum_is_locked'],
];
if(!in_array($field, $allowed[$type] ?? [], true) || !$p->hasField($field)) {
$ok = false; $msg = 'Not allowed';
} else {
$p->set($field, $value ? 1 : 0);
}
}
if($ok) $p->save();
$p->of(true);
}
}
} elseif($action === 'load_edit_form') {
$type = (string) $input->post->type;
$id = (int) $input->post->id;
$p = $pages->get($id);
if(!$p || !$p->id) { $ok=false; $msg='Not found'; }
else {
$html = $this->sfAdminEditFormHtml($type, $p);
if(!$html) { $ok=false; $msg='Bad type'; }
}
} elseif($action === 'save_edit_form') {
$type = (string) $input->post->type;
$id = (int) $input->post->id;
$p = $pages->get($id);
if(!$p || !$p->id) { $ok=false; $msg='Not found'; }
else {
$ok = $this->sfAdminSaveFromPost($type, $p, $input->post);
if(!$ok) $msg = 'Save failed';
}
} else {
$ok = false;
$msg = 'Unknown action';
}
header('Content-Type: application/json; charset=utf-8');
$payload = ['ok' => $ok, 'message' => $msg];
if($html !== null) $payload['html'] = $html;
echo json_encode($payload);
exit;
}
/**
* Render category list row with quick toggles + edit button (admin).
*/
protected function sfAdminCategoryRow(Page $c): string {
$isVip = $c->hasField('forum_is_vip') && (int)$c->get('forum_is_vip') === 1;
$isHidden = $c->isHidden();
$isUnpub = $c->isUnpublished();
$topicsCount = $c->children("template=forum-topic")->count();
$out = '';
$out .= '<li data-id="' . (int)$c->id . '">';
$out .= '<div class="sf-title"><span class="sf-drag" title="Drag to reorder"><i class="fa fa-bars"></i></span><strong>' . $this->sanitizer->entities($c->title) . '</strong> <span class="sfa-badge" title="Topics">' . (int)$topicsCount . ' <i class="fa fa-comments-o"></i></span></div>';
$out .= '<div class="sf-actions">';
$out .= '<label title="VIP category"><input class="sf-qt" type="checkbox" data-type="category" data-id="' . (int)$c->id . '" data-field="forum_is_vip"' . ($isVip ? ' checked' : '') . '>VIP</label>';
$out .= '<label title="Hidden (page status)"><input class="sf-qt" type="checkbox" data-type="category" data-id="' . (int)$c->id . '" data-field="status_hidden"' . ($isHidden ? ' checked' : '') . '>Hidden</label>';
$out .= '<label title="Unpublished (page status)"><input class="sf-qt" type="checkbox" data-type="category" data-id="' . (int)$c->id . '" data-field="status_unpublished"' . ($isUnpub ? ' checked' : '') . '>Unpub</label>';
$out .= '<button type="button" class="pw-button pw-button-small sf-edit" data-type="category" data-id="' . (int)$c->id . '"><i class="fa fa-pencil"></i> Edit</button>';
$out .= '</div></li>';
return $out;
}
/**
* Build custom edit form HTML (returned for modal via AJAX).
* This intentionally avoids ProcessWire page editor, and only exposes relevant forum fields.
*/
protected function sfAdminEditFormHtml(string $type, Page $p): string {
$modules = $this->wire('modules');
$pages = $this->wire('pages');
$session = $this->wire('session');
$expected = [
'group' => 'forum-group',
'category' => 'forum-category',
'topic' => 'forum-topic',
];
if(!isset($expected[$type]) || $p->template->name !== $expected[$type]) return '';
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->addClass('sf-admin-edit-form');
$form->attr('method', 'post');
$form->attr('action', $this->page->url); // AJAX posts back to same Process URL
/** @var InputfieldMarkup $m */
$m = $modules->get('InputfieldHidden');
$m->attr('name', $session->CSRF->getTokenName());
$m->attr('value', $session->CSRF->getTokenValue());
$form->add($m);
// Hidden context
$ctx = $modules->get('InputfieldHidden');
$ctx->name = 'type';
$ctx->value = $type;
$form->add($ctx);
$ctx2 = $modules->get('InputfieldHidden');
$ctx2->name = 'id';
$ctx2->value = (int)$p->id;
$form->add($ctx2);
// Title
/** @var InputfieldText $fTitle */
$fTitle = $modules->get('InputfieldText');
$fTitle->label = 'Title';
$fTitle->name = 'title';
$fTitle->value = $p->title;
$fTitle->required = true;
$form->add($fTitle);
// Status
/** @var InputfieldCheckboxes $fStatus */
$fStatus = $modules->get('InputfieldCheckboxes');
$fStatus->label = 'Visibility';
$fStatus->name = 'status_flags';
$fStatus->addOption('hidden', 'Hidden');
$fStatus->addOption('unpublished', 'Unpublished');
$fStatus->value = [];
if($p->isHidden()) $fStatus->value[] = 'hidden';
if($p->isUnpublished()) $fStatus->value[] = 'unpublished';
$form->add($fStatus);
if($type === 'group') {
/** @var InputfieldCheckbox $vip */
$vip = $modules->get('InputfieldCheckbox');
$vip->label = 'VIP group';
$vip->name = 'forum_group_is_vip';
$vip->attr('checked', ($p->hasField('forum_group_is_vip') && (int)$p->get('forum_group_is_vip') === 1) ? 'checked' : '');
$form->add($vip);
/** @var InputfieldCheckbox $pin */
$pin = $modules->get('InputfieldCheckbox');
$pin->label = 'Pinned group';
$pin->name = 'forum_group_is_pinned';
$pin->attr('checked', ($p->hasField('forum_group_is_pinned') && (int)$p->get('forum_group_is_pinned') === 1) ? 'checked' : '');
$form->add($pin);
}
if($type === 'category') {
// Summary
if($p->hasField('field_forum_summary_category')) {
/** @var InputfieldText $sum */
$sum = $modules->get('InputfieldText');
$sum->label = 'Summary';
$sum->name = 'field_forum_summary_category';
$sum->value = (string) $p->get('field_forum_summary_category');
$sum->attr('maxlength', 255);
$form->add($sum);
}
// VIP
if($p->hasField('forum_is_vip')) {
/** @var InputfieldCheckbox $vipc */
$vipc = $modules->get('InputfieldCheckbox');
$vipc->label = 'VIP category';
$vipc->name = 'forum_is_vip';
$vipc->attr('checked', ((int)$p->get('forum_is_vip') === 1) ? 'checked' : '');
$form->add($vipc);
}
// Read/Write roles (optional)
$roles = $pages->find("template=role, include=all, sort=name");
if($p->hasField('forum_read_roles')) {
/** @var InputfieldSelect $rr */
$rr = $modules->get('InputfieldSelect');
$rr->label = 'Allowed read roles (optional)';
$rr->name = 'forum_read_roles';
$rr->attr('multiple', 'multiple');
foreach($roles as $r) $rr->addOption($r->id, $r->name);
$rr->value = $p->getUnformatted('forum_read_roles') ? $p->getUnformatted('forum_read_roles')->explode('id') : [];
$form->add($rr);
}
if($p->hasField('forum_write_roles')) {
/** @var InputfieldSelect $wr */
$wr = $modules->get('InputfieldSelect');
$wr->label = 'Allowed write roles (optional)';
$wr->name = 'forum_write_roles';
$wr->attr('multiple', 'multiple');
foreach($roles as $r) $wr->addOption($r->id, $r->name);
$wr->value = $p->getUnformatted('forum_write_roles') ? $p->getUnformatted('forum_write_roles')->explode('id') : [];
$form->add($wr);
}
}
if($type === 'topic') {
// Summary
if($p->hasField('field_forum_summary_topic')) {
/** @var InputfieldText $sumt */
$sumt = $modules->get('InputfieldText');
$sumt->label = 'Summary';
$sumt->name = 'field_forum_summary_topic';
$sumt->value = (string) $p->get('field_forum_summary_topic');
$sumt->attr('maxlength', 255);
$form->add($sumt);
}
// Pinned / Locked
if($p->hasField('forum_is_pinned')) {
$pin = $modules->get('InputfieldCheckbox');
$pin->label = 'Pinned topic';
$pin->name = 'forum_is_pinned';
$pin->attr('checked', ((int)$p->get('forum_is_pinned') === 1) ? 'checked' : '');
$form->add($pin);
}
if($p->hasField('forum_is_locked')) {
$lock = $modules->get('InputfieldCheckbox');
$lock->label = 'Locked topic';
$lock->name = 'forum_is_locked';
$lock->attr('checked', ((int)$p->get('forum_is_locked') === 1) ? 'checked' : '');
$form->add($lock);
}
// Move topic
$forumRoot = $this->wire('modules')->get('NativeForum')->forumRoot();
$rootId = ($forumRoot && $forumRoot->id) ? (int) $forumRoot->id : 0;
$cats = $rootId ? $pages->find("template=forum-category, has_parent=$rootId, include=all, sort=title") : new PageArray();
$move = $modules->get('InputfieldSelect');
$move->label = 'Move to category';
$move->name = 'move_parent';
foreach($cats as $c) {
$label = $c->title;
if($c->parent && $c->parent->template->name === 'forum-group') $label = $c->parent->title . ' / ' . $label;
$move->addOption($c->id, $label);
}
$move->value = $p->parent ? $p->parent->id : 0;
$form->add($move);
}
/** @var InputfieldSubmit $save */
$save = $modules->get('InputfieldSubmit');
$save->name = 'save';
$save->value = 'Save';
$save->icon = 'check';
$form->add($save);