Skip to content

Commit 2d402f1

Browse files
add forum stats and move settings to their own page
Signed-off-by: Diego Andrés <diegoandres_cortes@outlook.com>
1 parent c3e19dd commit 2d402f1

File tree

1 file changed

+118
-8
lines changed

1 file changed

+118
-8
lines changed

Sources/SimpleReferrals.php

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static function register()
169169
}
170170

171171
// 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))
172+
if (empty($modSettings['SimpleReferrals_allow_select']) || (!empty($modSettings['SimpleReferrals_allow_select']) && !empty(self::$_member_data)))
173173
{
174174
// Add fake custom field
175175
$context['custom_fields'][] = [
@@ -304,6 +304,38 @@ public static function admin(&$areas)
304304
$areas['maintenance']['areas']['maintain']['subsections']['referrals'] = [$txt['maintain_referrals'], 'admin_forum'];
305305
}
306306

307+
/**
308+
* SimpleReferrals::subaction()
309+
*
310+
* Append the subaction
311+
*
312+
* @param array $subActions mod settings subactions
313+
* @return void
314+
*/
315+
public static function mod_settings(&$subActions)
316+
{
317+
$subActions['referrals'] = 'SimpleReferrals::settings';
318+
}
319+
320+
/**
321+
* SimpleReferrals::admin_area()
322+
*
323+
* Insert the button in the menu
324+
*
325+
* @param array $admin_areas The admin menu
326+
* @return void
327+
*/
328+
public static function admin_area(&$admin_areas)
329+
{
330+
global $txt;
331+
332+
// Load the language file
333+
loadLanguage('SimpleReferrals/');
334+
335+
// Add the new setting area
336+
$admin_areas['config']['areas']['modsettings']['subsections']['referrals'] = [$txt['SimpleReferrals_settings']];
337+
}
338+
307339
/**
308340
* SimpleReferrals::settings()
309341
*
@@ -312,15 +344,36 @@ public static function admin(&$areas)
312344
* @param array $config_vars The mod settings array
313345
* @return void
314346
*/
315-
public static function settings(&$config_vars)
347+
public static function settings($return_config = false)
316348
{
317-
global $txt;
349+
global $context, $txt, $scripturl;
350+
351+
$context['post_url'] = $scripturl . '?action=admin;area=modsettings;sa=referrals;save';
352+
$context['sub_template'] = 'show_settings';
353+
$context['settings_title'] = $txt['SimpleReferrals_settings'];
354+
$context['page_title'] .= ' - ' . $txt['SimpleReferrals_settings'];
355+
356+
// $config_vars []= ['title', 'SimpleReferrals_settings'];
357+
$config_vars = [
358+
['check', 'SimpleReferrals_allow_select', 'subtext' => $txt['SimpleReferrals_allow_select_desc']],
359+
['check', 'SimpleReferrals_enable_profile'],
360+
['check', 'SimpleReferrals_enable_posts'],
361+
['check', 'SimpleReferrals_display_link'],
362+
['check', 'SimpleReferrals_enable_stats'],
363+
];
364+
365+
// Return config vars
366+
if ($return_config)
367+
return $config_vars;
318368

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'];
369+
// Saving?
370+
if (isset($_GET['save'])) {
371+
checkSession();
372+
saveDBSettings($config_vars);
373+
clean_cache();
374+
redirectexit('action=admin;area=modsettings;sa=referrals');
375+
}
376+
prepareDBSettingContext($config_vars);
324377
}
325378

326379
/**
@@ -505,4 +558,61 @@ public static function do_recount()
505558
$context['maintenance_finished'] = $txt['maintain_recountreferrals'];
506559
redirectexit('action=admin;area=maintain;sa=referrals;done=recountreferrals');
507560
}
561+
562+
/**
563+
* SimpleReferrals::forum_stats()
564+
*
565+
* Adds the top referrers in the forum
566+
*
567+
* @return void
568+
*/
569+
public function forum_stats()
570+
{
571+
global $smcFunc, $context, $scripturl, $settings, $modSettings;
572+
573+
// Referrals Top.
574+
if (!empty($modSettings['SimpleReferrals_enable_stats']))
575+
{
576+
// Ref language
577+
loadLanguage('SimpleReferrals/');
578+
579+
// Template
580+
loadTemplate('SimpleReferrals');
581+
582+
// Add the icon without adding a css file...
583+
addInlineCss('
584+
.main_icons.most_referrals::before {
585+
background: url('. $settings['default_images_url'] . '/icons/most_referrals.png);
586+
}
587+
');
588+
589+
// Top 10 referrals
590+
$context['stats_blocks']['most_referrals'] = [];
591+
$max_referrals = 1;
592+
$request = $smcFunc['db_query']('', '
593+
SELECT mem.ref_count, mem.id_member, mem.real_name
594+
FROM {db_prefix}members AS mem
595+
WHERE mem.ref_count > 0
596+
ORDER BY mem.ref_count DESC
597+
LIMIT 10',
598+
[]
599+
);
600+
while ($ref_row = $smcFunc['db_fetch_assoc']($request))
601+
{
602+
$context['stats_blocks']['most_referrals'][] = [
603+
'id' => $ref_row['id_member'],
604+
'num' => $ref_row['ref_count'],
605+
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $ref_row['id_member'] . '">' . $ref_row['real_name'] . '</a>',
606+
];
607+
608+
if ($max_referrals < $ref_row['ref_count'])
609+
$max_referrals = $ref_row['ref_count'];
610+
}
611+
$smcFunc['db_free_result']($request);
612+
613+
// Percentage
614+
foreach ($context['stats_blocks']['most_referrals'] as $i => $referral_count)
615+
$context['stats_blocks']['most_referrals'][$i]['percent'] = round(($referral_count['num'] * 100) / $max_referrals);
616+
}
617+
}
508618
}

0 commit comments

Comments
 (0)