@@ -100,11 +100,12 @@ public static function save_referral(&$regOptions, &$theme_vars, &$knownInts)
100
100
* Updates a member ref_count if set as the referral
101
101
*
102
102
* @param array $regOptions The register options
103
+ * @param int $memberID The id of the newly registered member
103
104
* @return void
104
105
*/
105
- public static function update_count ($ regOptions )
106
+ public static function update_count ($ regOptions, $ memberID )
106
107
{
107
- global $ smcFunc ;
108
+ global $ smcFunc, $ modSettings ;
108
109
109
110
// Do a checking just in case something is broken
110
111
if (!empty ($ regOptions ['register_vars ' ]['referral ' ]))
@@ -118,8 +119,9 @@ public static function update_count($regOptions)
118
119
]
119
120
);
120
121
121
- // Are we doing anything else with this?
122
- call_integration_hook ('integrate_mod_simplereferrals ' , [$ regOptions ['register_vars ' ]['referral ' ]]);
122
+ // Send an alert?
123
+ if (!empty ($ modSettings ['SimpleReferrals_send_alerts ' ]))
124
+ self ::sendAlert ($ regOptions ['register_vars ' ], $ memberID );
123
125
}
124
126
125
127
// If we still have the referral in the session, remove it
@@ -169,7 +171,7 @@ public static function register()
169
171
}
170
172
171
173
// Do we allow for selection? If not, don't display anything if there's no referral
172
- if (empty ($ modSettings ['SimpleReferrals_allow_select ' ]) || !empty (self ::$ _member_id ))
174
+ if (empty ($ modSettings ['SimpleReferrals_allow_select ' ]) || ( !empty ($ modSettings [ ' SimpleReferrals_allow_select ' ]) && ! empty ( self ::$ _member_data ) ))
173
175
{
174
176
// Add fake custom field
175
177
$ context ['custom_fields ' ][] = [
@@ -304,6 +306,38 @@ public static function admin(&$areas)
304
306
$ areas ['maintenance ' ]['areas ' ]['maintain ' ]['subsections ' ]['referrals ' ] = [$ txt ['maintain_referrals ' ], 'admin_forum ' ];
305
307
}
306
308
309
+ /**
310
+ * SimpleReferrals::subaction()
311
+ *
312
+ * Append the subaction
313
+ *
314
+ * @param array $subActions mod settings subactions
315
+ * @return void
316
+ */
317
+ public static function mod_settings (&$ subActions )
318
+ {
319
+ $ subActions ['referrals ' ] = 'SimpleReferrals::settings ' ;
320
+ }
321
+
322
+ /**
323
+ * SimpleReferrals::admin_area()
324
+ *
325
+ * Insert the button in the menu
326
+ *
327
+ * @param array $admin_areas The admin menu
328
+ * @return void
329
+ */
330
+ public static function admin_area (&$ admin_areas )
331
+ {
332
+ global $ txt ;
333
+
334
+ // Load the language file
335
+ loadLanguage ('SimpleReferrals/ ' );
336
+
337
+ // Add the new setting area
338
+ $ admin_areas ['config ' ]['areas ' ]['modsettings ' ]['subsections ' ]['referrals ' ] = [$ txt ['SimpleReferrals_settings ' ]];
339
+ }
340
+
307
341
/**
308
342
* SimpleReferrals::settings()
309
343
*
@@ -312,15 +346,37 @@ public static function admin(&$areas)
312
346
* @param array $config_vars The mod settings array
313
347
* @return void
314
348
*/
315
- public static function settings (& $ config_vars )
349
+ public static function settings ($ return_config = false )
316
350
{
317
- global $ txt ;
351
+ global $ context , $ txt , $ scripturl ;
352
+
353
+ $ context ['post_url ' ] = $ scripturl . '?action=admin;area=modsettings;sa=referrals;save ' ;
354
+ $ context ['sub_template ' ] = 'show_settings ' ;
355
+ $ context ['settings_title ' ] = $ txt ['SimpleReferrals_settings ' ];
356
+ $ context ['page_title ' ] .= ' - ' . $ txt ['SimpleReferrals_settings ' ];
357
+
358
+ // $config_vars []= ['title', 'SimpleReferrals_settings'];
359
+ $ config_vars = [
360
+ ['check ' , 'SimpleReferrals_allow_select ' , 'subtext ' => $ txt ['SimpleReferrals_allow_select_desc ' ]],
361
+ ['check ' , 'SimpleReferrals_enable_profile ' ],
362
+ ['check ' , 'SimpleReferrals_enable_posts ' ],
363
+ ['check ' , 'SimpleReferrals_display_link ' ],
364
+ ['check ' , 'SimpleReferrals_enable_stats ' ],
365
+ ['check ' , 'SimpleReferrals_send_alerts ' ],
366
+ ];
367
+
368
+ // Return config vars
369
+ if ($ return_config )
370
+ return $ config_vars ;
318
371
319
- $ config_vars []= ['title ' , 'SimpleReferrals_settings ' ];
320
- $ config_vars []= ['check ' , 'SimpleReferrals_allow_select ' , 'subtext ' => $ txt ['SimpleReferrals_allow_select_desc ' ]];
321
- $ config_vars []= ['check ' , 'SimpleReferrals_enable_profile ' ];
322
- $ config_vars []= ['check ' , 'SimpleReferrals_enable_posts ' ];
323
- $ config_vars []= ['check ' , 'SimpleReferrals_display_link ' ];
372
+ // Saving?
373
+ if (isset ($ _GET ['save ' ])) {
374
+ checkSession ();
375
+ saveDBSettings ($ config_vars );
376
+ clean_cache ();
377
+ redirectexit ('action=admin;area=modsettings;sa=referrals ' );
378
+ }
379
+ prepareDBSettingContext ($ config_vars );
324
380
}
325
381
326
382
/**
@@ -505,4 +561,165 @@ public static function do_recount()
505
561
$ context ['maintenance_finished ' ] = $ txt ['maintain_recountreferrals ' ];
506
562
redirectexit ('action=admin;area=maintain;sa=referrals;done=recountreferrals ' );
507
563
}
564
+
565
+ /**
566
+ * SimpleReferrals::forum_stats()
567
+ *
568
+ * Adds the top referrers in the forum
569
+ *
570
+ * @return void
571
+ */
572
+ public function forum_stats ()
573
+ {
574
+ global $ smcFunc , $ context , $ scripturl , $ settings , $ modSettings ;
575
+
576
+ // Referrals Top.
577
+ if (!empty ($ modSettings ['SimpleReferrals_enable_stats ' ]))
578
+ {
579
+ // Ref language
580
+ loadLanguage ('SimpleReferrals/ ' );
581
+
582
+ // Template
583
+ loadTemplate ('SimpleReferrals ' );
584
+
585
+ // Add the icon without adding a css file...
586
+ addInlineCss ('
587
+ .main_icons.most_referrals::before {
588
+ background: url( ' . $ settings ['default_images_url ' ] . '/icons/most_referrals.png);
589
+ }
590
+ ' );
591
+
592
+ // Top 10 referrals
593
+ $ context ['stats_blocks ' ]['most_referrals ' ] = [];
594
+ $ max_referrals = 1 ;
595
+ $ request = $ smcFunc ['db_query ' ]('' , '
596
+ SELECT mem.ref_count, mem.id_member, mem.real_name
597
+ FROM {db_prefix}members AS mem
598
+ WHERE mem.ref_count > 0
599
+ ORDER BY mem.ref_count DESC
600
+ LIMIT 10 ' ,
601
+ []
602
+ );
603
+ while ($ ref_row = $ smcFunc ['db_fetch_assoc ' ]($ request ))
604
+ {
605
+ $ context ['stats_blocks ' ]['most_referrals ' ][] = [
606
+ 'id ' => $ ref_row ['id_member ' ],
607
+ 'num ' => $ ref_row ['ref_count ' ],
608
+ 'link ' => '<a href=" ' . $ scripturl . '?action=profile;u= ' . $ ref_row ['id_member ' ] . '"> ' . $ ref_row ['real_name ' ] . '</a> ' ,
609
+ ];
610
+
611
+ if ($ max_referrals < $ ref_row ['ref_count ' ])
612
+ $ max_referrals = $ ref_row ['ref_count ' ];
613
+ }
614
+ $ smcFunc ['db_free_result ' ]($ request );
615
+
616
+ // Percentage
617
+ foreach ($ context ['stats_blocks ' ]['most_referrals ' ] as $ i => $ referral_count )
618
+ $ context ['stats_blocks ' ]['most_referrals ' ][$ i ]['percent ' ] = round (($ referral_count ['num ' ] * 100 ) / $ max_referrals );
619
+ }
620
+ }
621
+
622
+ /**
623
+ * SimpleReferrals::alertTypes()
624
+ *
625
+ * Adds the top referrers in the forum
626
+ *
627
+ * @param array $alert_types The type of content/alerts
628
+ * @return void
629
+ */
630
+ public static function alertTypes (&$ alert_types )
631
+ {
632
+ global $ modSettings ;
633
+
634
+ // No alertts, no fun
635
+ if (empty ($ modSettings ['SimpleReferrals_send_alerts ' ]))
636
+ return ;
637
+
638
+ // Language
639
+ loadLanguage ('SimpleReferrals/ ' );
640
+
641
+ $ alert_types ['referrals ' ] = [
642
+ 'new_referred ' => ['alert ' => 'yes ' , 'email ' => 'never ' ],
643
+ ];
644
+ }
645
+
646
+ /**
647
+ * SimpleReferrals::alertFetch()
648
+ *
649
+ * Fetch the alert fot the user
650
+ *
651
+ * @param array $alerts The alerts being sent out
652
+ * @return void
653
+ */
654
+ public static function alertFetch (&$ alerts )
655
+ {
656
+ global $ settings , $ scripturl , $ modSettings ;
657
+
658
+ // No alertts, no fun
659
+ if (empty ($ modSettings ['SimpleReferrals_send_alerts ' ]))
660
+ return ;
661
+
662
+ // Language
663
+ loadLanguage ('SimpleReferrals/ ' );
664
+
665
+ foreach ($ alerts as $ alert_id => $ alert )
666
+ {
667
+ if ($ alert ['content_type ' ] == 'referral ' )
668
+ {
669
+ $ alerts [$ alert_id ]['icon ' ] = '<img class="alert_icon" src=" ' . $ settings ['images_url ' ] . '/icons/most_referrals.png"> ' ;
670
+ $ alerts [$ alert_id ]['extra ' ]['content_link ' ] = $ scripturl . $ alert ['extra ' ]['referred_link ' ];
671
+ }
672
+ }
673
+ }
674
+
675
+ /**
676
+ * SimpleReferrals::sendAlert()
677
+ *
678
+ * Send an alert to the referral
679
+ *
680
+ * @param array $register_vars Contains $regOptions['register_vars']
681
+ * @param int $memID It's the id of the referred user
682
+ * @return void
683
+ */
684
+ public static function sendAlert ($ register_vars , $ memID )
685
+ {
686
+ global $ smcFunc , $ sourcedir ;
687
+
688
+ require_once ($ sourcedir . '/Subs-Notify.php ' );
689
+ $ prefs = getNotifyPrefs ($ register_vars ['referral ' ], 'new_referred ' , true );
690
+
691
+ // Send alert
692
+ // Check the value. If no value or it's empty, they didn't want alerts, oh well.
693
+ if (empty ($ prefs [$ register_vars ['referral ' ]]['new_referred ' ]))
694
+ return true ;
695
+
696
+ // Issue, update, move on.
697
+ $ smcFunc ['db_insert ' ]('insert ' ,
698
+ '{db_prefix}user_alerts ' ,
699
+ [
700
+ 'alert_time ' => 'int ' ,
701
+ 'id_member ' => 'int ' ,
702
+ 'id_member_started ' => 'int ' ,
703
+ 'member_name ' => 'string ' ,
704
+ 'content_type ' => 'string ' ,
705
+ 'content_id ' => 'int ' ,
706
+ 'content_action ' => 'string ' ,
707
+ 'is_read ' => 'int ' ,
708
+ 'extra ' => 'string '
709
+ ],
710
+ [
711
+ time (),
712
+ $ register_vars ['referral ' ],
713
+ $ memID ,
714
+ $ register_vars ['real_name ' ],
715
+ 'referral ' ,
716
+ $ memID ,
717
+ 'new_referred ' ,
718
+ 0 ,
719
+ $ smcFunc ['json_encode ' ](['referred_link ' => '?action=profile;u= ' . $ memID ])
720
+ ],
721
+ ['id_alert ' ]
722
+ );
723
+ updateMemberData ($ register_vars ['referral ' ], ['alerts ' => '+ ' ]);
724
+ }
508
725
}
0 commit comments