Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Added support for XFRM (#1)
- Move check of accepting rules into ControllerPlugin
- Fix issue with possible calls `setupThreadCreate()` from another actions: now used `actionPostThread()` method
- Event listener for entity structure now listens all entity structures, but modifies structure only for required entities
- Added event listener for addon post install
  • Loading branch information
CrazyHackGUT committed Oct 16, 2021
1 parent a520a49 commit 9b27abf
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 44 deletions.
47 changes: 47 additions & 0 deletions ControllerPlugin/SubforumRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* This file is a part of free add-on "Forum Rules Accept".
* Developed by HLModerators.
*
* License - MIT.
*/

namespace HLModerators\ForumRulesAccept\ControllerPlugin;


use XF\ControllerPlugin\AbstractPlugin;
use XF\Mvc\Entity\Entity;
use XF\Mvc\FormAction;

class SubforumRules extends AbstractPlugin
{
/**
* Performs checking user accept with subforum rules.
*
* @param Entity $entity
*/
public function assertRulesIsAccepted(Entity $entity): void
{
/** @var string $rulesUrl */
$rulesUrl = $entity->hlmod_rules_url;
if (!empty($rulesUrl) && $rulesUrl !== $this->filter('hlmod_rules_url_accept', 'str,no-trim'))
{
throw $this->errorException(\XF::phrase('hlmod_forum_rules_accept.you_must_accept_rules'), 400);
}
}

/**
* Append the operation for saving the subforum rules URL to entity
* to already created form action object.
*
* @param FormAction $formAction
* @param Entity $entity
*/
public function setupSaveUrl(FormAction $formAction, Entity $entity): void
{
$formAction->setupEntityInput($entity, [
'hlmod_rules_url' => $this->filter('hlmod_rules_url', 'str')
]);
}
}
52 changes: 50 additions & 2 deletions EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,69 @@
namespace HLModerators\ForumRulesAccept;


use XF\Entity\AddOn;
use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Manager;
use XF\Mvc\Entity\Structure;
use function count;
use function in_array;

class EventListener
{
const OPTIONAL_ADD_ONS = [
'XFRM' => ['xf_rm_category']
];

const ENTITIES = [
'XF:Forum',
'XFRM:Category'
];

/**
* Extends the forum entity structure
* Extends the entity structure
*
* @param Manager $em
* @param Structure $structure
*/
public static function onForumEntityStructure(Manager $em, Structure &$structure)
public static function onEntityStructure(Manager $em, Structure &$structure): void
{
if (!in_array($structure->shortName, self::ENTITIES))
{
return;
}

$structure->columns['hlmod_rules_url'] = ['type' => Entity::STR, 'maxLength' => 255,
'default' => ''];
}

/**
* Listens the add-on installs for creating tables where this is required.
*
* @param \XF\AddOn\AddOn $addOn
* @param AddOn $installedAddOn
* @param array $json
* @param array $stateChanges
*/
public static function onAddOnInstall(\XF\AddOn\AddOn $addOn, AddOn $installedAddOn,
array $json, array &$stateChanges): void
{
$addOnId = $addOn->getAddOnId();
$tables = self::OPTIONAL_ADD_ONS[$addOnId] ?? [];
if (!count($tables))
{
return;
}

/** @var Setup|null $setup */
$setup = $addOn->getSetup();
if (!$setup)
{
return;
}

foreach ($tables as $tableName)
{
$setup->addRulesColumn($tableName);
}
}
}
82 changes: 75 additions & 7 deletions Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,95 @@ class Setup extends AbstractSetup
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;

public function installStep1()
const TABLES = ['xf_forum', 'xf_rm_category'];

#region Install steps

/**
* Adds the columns to required tables (if exists).
*/
public function installStep1(): void
{
foreach (self::TABLES as $tableName)
{
$this->addRulesColumn($tableName);
}
}

#endregion

#region Upgrade steps

/**
* Sets the default column value for correct working if add-on is
* disabled.
*/
public function upgrade1000011Step1(): void
{
$this->alterTable('xf_forum', function (Alter $table)
{
$table->addColumn('hlmod_rules_url', 'varchar', 255)
$table->changeColumn('hlmod_rules_url')
->setDefault('');
});
}

public function upgrade1000011Step1()
/**
* Adds the column to XFRM table (if exists).
*/
public function upgrade1000210Step1(): void
{
$this->alterTable('xf_forum', function (Alter $table)
$this->addRulesColumn('xf_rm_category');
}

#endregion

#region Uninstall steps

/**
* Drops the column from all tables.
*/
public function uninstallStep1(): void
{
foreach (self::TABLES as $tableName)
{
$table->changeColumn('hlmod_rules_url')
$this->dropRulesColumn($tableName);
}
}

#endregion

/**
* Adds the column for rules URL saving in specified table.
*
* @param string $tableName
*/
public function addRulesColumn(string $tableName): void
{
if (!$this->tableExists($tableName))
{
return;
}

$this->alterTable($tableName, function (Alter $table)
{
$table->addColumn('hlmod_rules_url', 'varchar', 255)
->setDefault('');
});
}

public function uninstallStep1()
/**
* Drops the column for rules URL saving in specified table.
*
* @param string $tableName
*/
public function dropRulesColumn(string $tableName): void
{
$this->alterTable('xf_forum', function (Alter $table)
if (!$this->tableExists($tableName))
{
return;
}

$this->alterTable($tableName, function (Alter $table)
{
$table->dropColumns(['hlmod_rules_url']);
});
Expand Down
5 changes: 2 additions & 3 deletions XF/Admin/Controller/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ protected function saveTypeData(FormAction $form, \XF\Entity\Node $node, \XF\Ent
{
parent::saveTypeData($form, $node, $data);

$form->setupEntityInput($data, [
'hlmod_rules_url' => $this->filter('hlmod_rules_url', 'str')
]);
$this->plugin('HLModerators\ForumRulesAccept:SubforumRules')
->setupSaveUrl($form, $data);
}
}
15 changes: 9 additions & 6 deletions XF/Pub/Controller/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@

class Forum extends XFCP_Forum
{
protected function setupThreadCreate(\XF\Entity\Forum $forum)
public function actionPostThread(ParameterBag $params)
{
/** @var string $rulesUrl */
$rulesUrl = $forum->hlmod_rules_url;
if (!empty($rulesUrl) && $rulesUrl !== $this->filter('hlmod_rules_url_accept', 'str,no-trim'))
if ($this->isPost())
{
throw $this->errorException(\XF::phrase('hlmod_forum_rules_accept.you_must_accept_rules'), 400);
$userId = \XF::visitor()->user_id;
$nodeId = $params->node_id ?: $params->node_name;
$node = $this->assertViewableForum($nodeId, ['DraftThreads|' . $userId]);

$this->plugin('HLModerators\ForumRulesAccept:SubforumRules')
->assertRulesIsAccepted($node);
}

return parent::setupThreadCreate($forum);
return parent::actionPostThread($params);
}
}
23 changes: 23 additions & 0 deletions XFRM/Admin/Controller/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is a part of free add-on "Forum Rules Accept".
* Developed by HLModerators.
*
* License - MIT.
*/

namespace HLModerators\ForumRulesAccept\XFRM\Admin\Controller;


class Category extends XFCP_Category
{
protected function categorySaveProcess(\XFRM\Entity\Category $category)
{
$formAction = parent::categorySaveProcess($category);
$this->plugin('HLModerators\ForumRulesAccept:SubforumRules')
->setupSaveUrl($formAction, $category);

return $formAction;
}
}
30 changes: 30 additions & 0 deletions XFRM/Pub/Controller/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* This file is a part of free add-on "Forum Rules Accept".
* Developed by HLModerators.
*
* License - MIT.
*/

namespace HLModerators\ForumRulesAccept\XFRM\Pub\Controller;


use XF\Mvc\ParameterBag;

class Category extends XFCP_Category
{
public function actionAdd(ParameterBag $params)
{
if ($this->isPost())
{
$nodeId = $params->resource_category_id;
$node = $this->assertViewableCategory($nodeId);

$this->plugin('HLModerators\ForumRulesAccept:SubforumRules')
->assertRulesIsAccepted($node);
}

return parent::actionAdd($params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"from_class": "XFRM\\Admin\\Controller\\Category",
"to_class": "HLModerators\\ForumRulesAccept\\XFRM\\Admin\\Controller\\Category",
"execute_order": 25,
"active": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"from_class": "XFRM\\Pub\\Controller\\Category",
"to_class": "HLModerators\\ForumRulesAccept\\XFRM\\Pub\\Controller\\Category",
"execute_order": 25,
"active": true
}
6 changes: 6 additions & 0 deletions _output/class_extensions/_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
},
"XF-Pub-Controller-Forum_HLModerators-ForumRulesAccept-XF-Pub-Controller-Forum.json": {
"hash": "05b5337ac132d7abb87afd8b5d912d2c"
},
"XFRM-Admin-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Admin-Controller-Category.json": {
"hash": "6ac10f36c910897e79d139ed2083cafb"
},
"XFRM-Pub-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Pub-Controller-Category.json": {
"hash": "7503daf99de0a66af420153bf915f677"
}
}
7 changes: 5 additions & 2 deletions _output/code_event_listeners/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"entity_structure_0aafc4dbdd2a0e1d7cac25a19c8598bb.json": {
"hash": "9e6d77c98287490e82170fd208eb49e7"
"addon_post_install_8ee79074ea27c92ea8b088fda7b15e8a.json": {
"hash": "834cce9c3decfe74533e38e0c0aca806"
},
"entity_structure_60416710f20a783e44a318ae7c6e8ccf.json": {
"hash": "d8f51f40311eb09ff0f4bf7cc16e1240"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"event_id": "addon_post_install",
"execute_order": 25,
"callback_class": "HLModerators\\ForumRulesAccept\\EventListener",
"callback_method": "onAddOnInstall",
"active": true,
"hint": "",
"description": "Listens the add-on installs for creating tables where this is required"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"event_id": "entity_structure",
"execute_order": 25,
"callback_class": "HLModerators\\ForumRulesAccept\\EventListener",
"callback_method": "onForumEntityStructure",
"callback_method": "onEntityStructure",
"active": true,
"hint": "XF\\Entity\\Forum",
"description": "Extends the forum entity structure"
"hint": "",
"description": "Extends the entity structure"
}
10 changes: 10 additions & 0 deletions _output/extension_hint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// ################## THIS IS A GENERATED FILE ##################
// DO NOT EDIT DIRECTLY. EDIT THE CLASS EXTENSIONS IN THE CONTROL PANEL.

namespace HLModerators\ForumRulesAccept\XFRM\Admin\Controller
{
class XFCP_Category extends \XFRM\Admin\Controller\Category {}
}

namespace HLModerators\ForumRulesAccept\XFRM\Pub\Controller
{
class XFCP_Category extends \XFRM\Pub\Controller\Category {}
}

namespace HLModerators\ForumRulesAccept\XF\Admin\Controller
{
class XFCP_Forum extends \XF\Admin\Controller\Forum {}
Expand Down
Loading

0 comments on commit 9b27abf

Please sign in to comment.