diff --git a/ControllerPlugin/SubforumRules.php b/ControllerPlugin/SubforumRules.php
new file mode 100644
index 0000000..4bdecce
--- /dev/null
+++ b/ControllerPlugin/SubforumRules.php
@@ -0,0 +1,47 @@
+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')
+ ]);
+ }
+}
diff --git a/EventListener.php b/EventListener.php
index 8913eca..81ba07d 100644
--- a/EventListener.php
+++ b/EventListener.php
@@ -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);
+ }
+ }
}
diff --git a/Setup.php b/Setup.php
index 0b9f17a..b059715 100644
--- a/Setup.php
+++ b/Setup.php
@@ -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']);
});
diff --git a/XF/Admin/Controller/Forum.php b/XF/Admin/Controller/Forum.php
index 21a24df..d24e3e9 100644
--- a/XF/Admin/Controller/Forum.php
+++ b/XF/Admin/Controller/Forum.php
@@ -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);
}
}
diff --git a/XF/Pub/Controller/Forum.php b/XF/Pub/Controller/Forum.php
index 4953545..5f2ad92 100644
--- a/XF/Pub/Controller/Forum.php
+++ b/XF/Pub/Controller/Forum.php
@@ -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);
}
}
diff --git a/XFRM/Admin/Controller/Category.php b/XFRM/Admin/Controller/Category.php
new file mode 100644
index 0000000..74103fe
--- /dev/null
+++ b/XFRM/Admin/Controller/Category.php
@@ -0,0 +1,23 @@
+plugin('HLModerators\ForumRulesAccept:SubforumRules')
+ ->setupSaveUrl($formAction, $category);
+
+ return $formAction;
+ }
+}
diff --git a/XFRM/Pub/Controller/Category.php b/XFRM/Pub/Controller/Category.php
new file mode 100644
index 0000000..6aaa537
--- /dev/null
+++ b/XFRM/Pub/Controller/Category.php
@@ -0,0 +1,30 @@
+isPost())
+ {
+ $nodeId = $params->resource_category_id;
+ $node = $this->assertViewableCategory($nodeId);
+
+ $this->plugin('HLModerators\ForumRulesAccept:SubforumRules')
+ ->assertRulesIsAccepted($node);
+ }
+
+ return parent::actionAdd($params);
+ }
+}
diff --git a/_output/class_extensions/XFRM-Admin-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Admin-Controller-Category.json b/_output/class_extensions/XFRM-Admin-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Admin-Controller-Category.json
new file mode 100644
index 0000000..d9e5e0e
--- /dev/null
+++ b/_output/class_extensions/XFRM-Admin-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Admin-Controller-Category.json
@@ -0,0 +1,6 @@
+{
+ "from_class": "XFRM\\Admin\\Controller\\Category",
+ "to_class": "HLModerators\\ForumRulesAccept\\XFRM\\Admin\\Controller\\Category",
+ "execute_order": 25,
+ "active": true
+}
\ No newline at end of file
diff --git a/_output/class_extensions/XFRM-Pub-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Pub-Controller-Category.json b/_output/class_extensions/XFRM-Pub-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Pub-Controller-Category.json
new file mode 100644
index 0000000..905698d
--- /dev/null
+++ b/_output/class_extensions/XFRM-Pub-Controller-Category_HLModerators-ForumRulesAccept-XFRM-Pub-Controller-Category.json
@@ -0,0 +1,6 @@
+{
+ "from_class": "XFRM\\Pub\\Controller\\Category",
+ "to_class": "HLModerators\\ForumRulesAccept\\XFRM\\Pub\\Controller\\Category",
+ "execute_order": 25,
+ "active": true
+}
\ No newline at end of file
diff --git a/_output/class_extensions/_metadata.json b/_output/class_extensions/_metadata.json
index e9247fb..3a29451 100644
--- a/_output/class_extensions/_metadata.json
+++ b/_output/class_extensions/_metadata.json
@@ -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"
}
}
\ No newline at end of file
diff --git a/_output/code_event_listeners/_metadata.json b/_output/code_event_listeners/_metadata.json
index 09abcef..8941152 100644
--- a/_output/code_event_listeners/_metadata.json
+++ b/_output/code_event_listeners/_metadata.json
@@ -1,5 +1,8 @@
{
- "entity_structure_0aafc4dbdd2a0e1d7cac25a19c8598bb.json": {
- "hash": "9e6d77c98287490e82170fd208eb49e7"
+ "addon_post_install_8ee79074ea27c92ea8b088fda7b15e8a.json": {
+ "hash": "834cce9c3decfe74533e38e0c0aca806"
+ },
+ "entity_structure_60416710f20a783e44a318ae7c6e8ccf.json": {
+ "hash": "d8f51f40311eb09ff0f4bf7cc16e1240"
}
}
\ No newline at end of file
diff --git a/_output/code_event_listeners/addon_post_install_8ee79074ea27c92ea8b088fda7b15e8a.json b/_output/code_event_listeners/addon_post_install_8ee79074ea27c92ea8b088fda7b15e8a.json
new file mode 100644
index 0000000..d7239a0
--- /dev/null
+++ b/_output/code_event_listeners/addon_post_install_8ee79074ea27c92ea8b088fda7b15e8a.json
@@ -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"
+}
\ No newline at end of file
diff --git a/_output/code_event_listeners/entity_structure_0aafc4dbdd2a0e1d7cac25a19c8598bb.json b/_output/code_event_listeners/entity_structure_60416710f20a783e44a318ae7c6e8ccf.json
similarity index 52%
rename from _output/code_event_listeners/entity_structure_0aafc4dbdd2a0e1d7cac25a19c8598bb.json
rename to _output/code_event_listeners/entity_structure_60416710f20a783e44a318ae7c6e8ccf.json
index 56a72bd..42fb3da 100644
--- a/_output/code_event_listeners/entity_structure_0aafc4dbdd2a0e1d7cac25a19c8598bb.json
+++ b/_output/code_event_listeners/entity_structure_60416710f20a783e44a318ae7c6e8ccf.json
@@ -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"
}
\ No newline at end of file
diff --git a/_output/extension_hint.php b/_output/extension_hint.php
index 2fd7989..a1da137 100644
--- a/_output/extension_hint.php
+++ b/_output/extension_hint.php
@@ -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 {}
diff --git a/_output/phrases/_metadata.json b/_output/phrases/_metadata.json
index 032fdda..10d1bd7 100644
--- a/_output/phrases/_metadata.json
+++ b/_output/phrases/_metadata.json
@@ -1,15 +1,15 @@
{
"hlmod_forum_rules_accept.explain.txt": {
"global_cache": false,
- "version_id": 1000011,
- "version_string": "1.0.0 Alpha 1",
- "hash": "418b96b333e785d747ede338392dcd33"
+ "version_id": 1000210,
+ "version_string": "1.0.2 Alpha",
+ "hash": "a7fcf99b7277d97b3f11f61000a76014"
},
"hlmod_forum_rules_accept.i_read_and_accept_forum_rules.txt": {
"global_cache": false,
- "version_id": 1000070,
- "version_string": "1.0.0",
- "hash": "1fc744408fd958b5a6edd91defb01a06"
+ "version_id": 1000210,
+ "version_string": "1.0.2 Alpha",
+ "hash": "05869853848d2da35f667ba16889870f"
},
"hlmod_forum_rules_accept.label.txt": {
"global_cache": false,
@@ -19,8 +19,8 @@
},
"hlmod_forum_rules_accept.you_must_accept_rules.txt": {
"global_cache": false,
- "version_id": 1000170,
- "version_string": "1.0.1",
- "hash": "d78b151c7217bd689d2cdbafdcaee797"
+ "version_id": 1000210,
+ "version_string": "1.0.2 Alpha",
+ "hash": "5de54197c15a1a7bda39cbea30eb0971"
}
}
\ No newline at end of file
diff --git a/_output/phrases/hlmod_forum_rules_accept.explain.txt b/_output/phrases/hlmod_forum_rules_accept.explain.txt
index ba7d805..ae82160 100644
--- a/_output/phrases/hlmod_forum_rules_accept.explain.txt
+++ b/_output/phrases/hlmod_forum_rules_accept.explain.txt
@@ -1,2 +1,2 @@
-If this URL is set, then user should accept rules before every thread creation in this forum.
-Note: this option does not apply to subforums.
\ No newline at end of file
+If this URL is set, then user should accept rules before every content creation in this node.
+Note: this option does not apply to children nodes.
\ No newline at end of file
diff --git a/_output/phrases/hlmod_forum_rules_accept.i_read_and_accept_forum_rules.txt b/_output/phrases/hlmod_forum_rules_accept.i_read_and_accept_forum_rules.txt
index 22b6d37..1e28642 100644
--- a/_output/phrases/hlmod_forum_rules_accept.i_read_and_accept_forum_rules.txt
+++ b/_output/phrases/hlmod_forum_rules_accept.i_read_and_accept_forum_rules.txt
@@ -1 +1 @@
-I read and accept subforum rules
\ No newline at end of file
+I read and accept node rules
\ No newline at end of file
diff --git a/_output/phrases/hlmod_forum_rules_accept.you_must_accept_rules.txt b/_output/phrases/hlmod_forum_rules_accept.you_must_accept_rules.txt
index 52dee7d..ff936f3 100644
--- a/_output/phrases/hlmod_forum_rules_accept.you_must_accept_rules.txt
+++ b/_output/phrases/hlmod_forum_rules_accept.you_must_accept_rules.txt
@@ -1 +1 @@
-You must accept the rules for creating a thread
\ No newline at end of file
+You must accept the rules for posting a content in this node
\ No newline at end of file
diff --git a/_output/template_modifications/_metadata.json b/_output/template_modifications/_metadata.json
index a268c67..5c3d190 100644
--- a/_output/template_modifications/_metadata.json
+++ b/_output/template_modifications/_metadata.json
@@ -1,11 +1,17 @@
{
"admin/hlmod_forum_rules_accept_forum_edit_row.json": {
- "hash": "a54324f77cab384754110160be865fbf"
+ "hash": "061184c69f86460698593f74d2d410bb"
+ },
+ "admin/hlmod_forum_rules_accept_xfrm_category_edit_row.json": {
+ "hash": "3b58baba466d7ca353f1ce34e5370404"
},
"public/hlmod_forum_rules_accept_forum_post_qthread_accept.json": {
"hash": "25468dd593c7f83f6cd3e001c9bcf089"
},
"public/hlmod_forum_rules_accept_forum_post_thread_accept.json": {
"hash": "2ef277dd547d19b581d810d7c367bf0e"
+ },
+ "public/hlmod_forum_rules_accept_xfrm_add_resource_accept.json": {
+ "hash": "257febefef41f7b6eac6dde9a80a69eb"
}
}
\ No newline at end of file
diff --git a/_output/template_modifications/admin/hlmod_forum_rules_accept_forum_edit_row.json b/_output/template_modifications/admin/hlmod_forum_rules_accept_forum_edit_row.json
index 0df20d9..849050c 100644
--- a/_output/template_modifications/admin/hlmod_forum_rules_accept_forum_edit_row.json
+++ b/_output/template_modifications/admin/hlmod_forum_rules_accept_forum_edit_row.json
@@ -5,5 +5,5 @@
"enabled": true,
"action": "str_replace",
"find": "