Skip to content

Commit cb2b903

Browse files
committed
Begin with a well-defined sub-action interface
Signed-off-by: John Rayes <live627@gmail.com>
1 parent 7b217e4 commit cb2b903

61 files changed

Lines changed: 1167 additions & 2358 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Sources/Actions/Activate.php

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use SMF\Lang;
2626
use SMF\Logging;
2727
use SMF\Mail;
28+
use SMF\ProvidesSubActionInterface;
29+
use SMF\ProvidesSubActionTrait;
2830
use SMF\Routable;
2931
use SMF\Security;
3032
use SMF\Theme;
@@ -34,35 +36,11 @@
3436
/**
3537
* Activates a user's account.
3638
*/
37-
class Activate implements ActionInterface, Routable
39+
class Activate implements ActionInterface, ProvidesSubActionInterface, Routable
3840
{
3941
use ActionRouter;
4042
use ActionTrait;
41-
42-
/*******************
43-
* Public properties
44-
*******************/
45-
46-
/**
47-
* @var string
48-
*
49-
* The sub-action to call.
50-
*/
51-
public string $subaction = '';
52-
53-
/**************************
54-
* Public static properties
55-
**************************/
56-
57-
/**
58-
* @var array
59-
*
60-
* Available sub-actions.
61-
*/
62-
public static array $subactions = [
63-
'activate' => 'activate',
64-
'resend' => 'resend',
65-
];
43+
use ProvidesSubActionTrait;
6644

6745
/*********************
6846
* Internal properties
@@ -106,17 +84,15 @@ public function execute(): void
10684
return;
10785
}
10886

109-
if (empty($this->subaction)) {
87+
$this->findRequestedSubAction($_REQUEST['sa'] ?? null);
88+
89+
if (empty($this->sub_action)) {
11090
$this->showResendRequest();
11191

11292
return;
11393
}
11494

115-
$call = method_exists($this, self::$subactions[$this->subaction]) ? [$this, self::$subactions[$this->subaction]] : Utils::getCallable(self::$subactions[$this->subaction]);
116-
117-
if (!empty($call)) {
118-
call_user_func($call);
119-
}
95+
$this->callSubAction($_REQUEST['sa'] ?? null);
12096
}
12197

12298
/**
@@ -234,6 +210,9 @@ public static function parseRoute(array $route, array $params = []): array
234210
*/
235211
protected function __construct()
236212
{
213+
$this->addSubAction('activate', [$this, 'activate']);
214+
$this->addSubAction('resend', [$this, 'resend']);
215+
237216
// Logged in users should not bother to activate their accounts
238217
if (!empty(User::$me->id)) {
239218
Utils::redirectexit('action=profile');
@@ -259,10 +238,6 @@ protected function __construct()
259238
if (!empty($_REQUEST['code'])) {
260239
$_REQUEST['sa'] = 'activate';
261240
}
262-
263-
if (!empty($_REQUEST['sa']) && isset(self::$subactions[$_REQUEST['sa']])) {
264-
$this->subaction = $_REQUEST['sa'];
265-
}
266241
}
267242

268243
/**

Sources/Actions/Admin/Attachments.php

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use SMF\ItemList;
3030
use SMF\Lang;
3131
use SMF\Menu;
32+
use SMF\ProvidesSubActionInterface;
33+
use SMF\ProvidesSubActionTrait;
3234
use SMF\Sapi;
3335
use SMF\SecurityToken;
3436
use SMF\Theme;
@@ -41,47 +43,12 @@
4143
/**
4244
* Maintains and manages attachments and avatars.
4345
*/
44-
class Attachments implements ActionInterface
46+
class Attachments implements ActionInterface, ProvidesSubActionInterface
4547
{
4648
use ActionTrait;
47-
49+
use ProvidesSubActionTrait;
4850
use BackwardCompatibility;
4951

50-
/*******************
51-
* Public properties
52-
*******************/
53-
54-
/**
55-
* @var string
56-
*
57-
* The requested sub-action.
58-
* This should be set by the constructor.
59-
*/
60-
public string $subaction = 'browse';
61-
62-
/**************************
63-
* Public static properties
64-
**************************/
65-
66-
/**
67-
* @var array
68-
*
69-
* Available sub-actions.
70-
*/
71-
public static array $subactions = [
72-
'attachments' => 'attachmentSettings',
73-
'avatars' => 'avatarSettings',
74-
'browse' => 'browse',
75-
'maintenance' => 'maintain',
76-
'remove' => 'remove',
77-
'byage' => 'removeByAge',
78-
'bysize' => 'removeBySize',
79-
'removeall' => 'removeAll',
80-
'repair' => 'repair',
81-
'attachpaths' => 'paths',
82-
'transfer' => 'transfer',
83-
];
84-
8552
/****************
8653
* Public methods
8754
****************/
@@ -91,11 +58,9 @@ class Attachments implements ActionInterface
9158
*/
9259
public function execute(): void
9360
{
94-
$call = method_exists($this, self::$subactions[$this->subaction]) ? [$this, self::$subactions[$this->subaction]] : Utils::getCallable(self::$subactions[$this->subaction]);
61+
IntegrationHook::call('integrate_manage_attachments', [&$this->sub_actions]);
9562

96-
if (!empty($call)) {
97-
call_user_func($call);
98-
}
63+
$this->callSubAction($_REQUEST['sa'] ?? null);
9964
}
10065

10166
/**
@@ -2572,6 +2537,19 @@ public static function attachDirStatus(string $dir, int $expected_files): array
25722537
*/
25732538
protected function __construct()
25742539
{
2540+
$this->setDefaultSubAction('browse');
2541+
$this->addSubAction('attachments', [$this, 'attachmentSettings']);
2542+
$this->addSubAction('avatars', [$this, 'avatarSettings']);
2543+
$this->addSubAction('browse', [$this, 'browse']);
2544+
$this->addSubAction('maintenance', [$this, 'maintain']);
2545+
$this->addSubAction('remove', [$this, 'remove']);
2546+
$this->addSubAction('byage', [$this, 'removeByAge']);
2547+
$this->addSubAction('bysize', [$this, 'removeBySize']);
2548+
$this->addSubAction('removeall', [$this, 'removeAll']);
2549+
$this->addSubAction('repair', [$this, 'repair']);
2550+
$this->addSubAction('attachpaths', [$this, 'paths']);
2551+
$this->addSubAction('transfer', [$this, 'transfer']);
2552+
25752553
// You have to be able to moderate the forum to do this.
25762554
User::$me->isAllowedTo('manage_attachments');
25772555

@@ -2585,14 +2563,6 @@ protected function __construct()
25852563
'description' => Lang::$txt['attachments_desc'],
25862564
];
25872565

2588-
IntegrationHook::call('integrate_manage_attachments', [&self::$subactions]);
2589-
2590-
if (!empty($_REQUEST['sa']) && isset(self::$subactions[strtolower($_REQUEST['sa'])])) {
2591-
$this->subaction = strtolower($_REQUEST['sa']);
2592-
}
2593-
2594-
Utils::$context['sub_action'] = &$this->subaction;
2595-
25962566
// Default page title is good.
25972567
Utils::$context['page_title'] = Lang::$txt['attachments_avatars'];
25982568
}

Sources/Actions/Admin/Bans.php

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use SMF\Lang;
3232
use SMF\Logging;
3333
use SMF\Menu;
34+
use SMF\ProvidesSubActionInterface;
35+
use SMF\ProvidesSubActionTrait;
3436
use SMF\SecurityToken;
3537
use SMF\Theme;
3638
use SMF\Time;
@@ -40,42 +42,12 @@
4042
/**
4143
* This class contains all the methods used for the ban center.
4244
*/
43-
class Bans implements ActionInterface
45+
class Bans implements ActionInterface, ProvidesSubActionInterface
4446
{
4547
use ActionTrait;
46-
48+
use ProvidesSubActionTrait;
4749
use BackwardCompatibility;
4850

49-
/*******************
50-
* Public properties
51-
*******************/
52-
53-
/**
54-
* @var string
55-
*
56-
* The requested sub-action.
57-
* This should be set by the constructor.
58-
*/
59-
public string $subaction = 'list';
60-
61-
/**************************
62-
* Public static properties
63-
**************************/
64-
65-
/**
66-
* @var array
67-
*
68-
* Available sub-actions.
69-
*/
70-
public static array $subactions = [
71-
'list' => 'list',
72-
'edit' => 'edit',
73-
'add' => 'edit',
74-
'browse' => 'browseTriggers',
75-
'edittrigger' => 'editTrigger',
76-
'log' => 'log',
77-
];
78-
7951
/****************
8052
* Public methods
8153
****************/
@@ -87,11 +59,19 @@ public function execute(): void
8759
{
8860
User::$me->isAllowedTo('manage_bans');
8961

90-
$call = method_exists($this, self::$subactions[$this->subaction]) ? [$this, self::$subactions[$this->subaction]] : Utils::getCallable(self::$subactions[$this->subaction]);
62+
IntegrationHook::call('integrate_manage_bans', [&$this->sub_actions]);
63+
64+
$this->findRequestedSubAction($_REQUEST['sa'] ?? null);
9165

92-
if (!empty($call)) {
93-
call_user_func($call);
66+
// Mark the appropriate menu entry as selected
67+
if (array_key_exists($this->sub_action, Menu::$loaded['admin']->tab_data['tabs'])) {
68+
Menu::$loaded['admin']->tab_data['tabs'][$this->sub_action]['is_selected'] = true;
9469
}
70+
71+
Utils::$context['page_title'] = Lang::$txt['ban_title'];
72+
Utils::$context['sub_action'] = $this->sub_action;
73+
74+
$this->callSubAction();
9575
}
9676

9777
/**
@@ -1472,6 +1452,13 @@ public static function list_getNumBanLogEntries(): int
14721452
*/
14731453
protected function __construct()
14741454
{
1455+
$this->addSubAction('list', [$this, 'list']);
1456+
$this->addSubAction('edit', [$this, 'edit']);
1457+
$this->addSubAction('add', [$this, 'edit']);
1458+
$this->addSubAction('browse', [$this, 'browseTriggers']);
1459+
$this->addSubAction('edittrigger', [$this, 'editTrigger']);
1460+
$this->addSubAction('log', [$this, 'log']);
1461+
14751462
Theme::loadTemplate('ManageBans');
14761463

14771464
// Tab data might already be set if this was called from Logs::execute().
@@ -1502,20 +1489,6 @@ protected function __construct()
15021489
],
15031490
];
15041491
}
1505-
1506-
IntegrationHook::call('integrate_manage_bans', [&self::$subactions]);
1507-
1508-
if (!empty($_REQUEST['sa']) && isset(self::$subactions[$_REQUEST['sa']])) {
1509-
$this->subaction = $_REQUEST['sa'];
1510-
}
1511-
1512-
// Mark the appropriate menu entry as selected
1513-
if (array_key_exists($this->subaction, Menu::$loaded['admin']->tab_data['tabs'])) {
1514-
Menu::$loaded['admin']->tab_data['tabs'][$this->subaction]['is_selected'] = true;
1515-
}
1516-
1517-
Utils::$context['page_title'] = Lang::$txt['ban_title'];
1518-
Utils::$context['sub_action'] = $this->subaction;
15191492
}
15201493

15211494
/**

0 commit comments

Comments
 (0)