Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Feb 19, 2025
1 parent 767f86c commit 18d5ca9
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 485 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
}
},
"require": {
"php": ">=8.0"
"php": ">=8.1"
},
"repositories": [
{
"url": "https://github.com/SimpleMachines/SMF.git",
"type": "vcs"
}
],
"minimum-stability": "alpha",
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.0",
"squizlabs/php_codesniffer": "^3.6",
"scrutinizer/ocular": "^1.9",
"simplemachines/smf": "^3.0@alpha"
"simplemachines/smf": "dev-release-3.0"
},
"scripts": {
"phpunit": "phpunit --testdox",
Expand Down
21 changes: 11 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 55 additions & 51 deletions src/CustomForm/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@

namespace CustomForm;

use SMF\ActionInterface;
use SMF\ActionRouter;
use SMF\ActionTrait;
use SMF\Config;
use SMF\ErrorHandler;
use SMF\Group;
use SMF\IntegrationHook;
use SMF\ItemList;
use SMF\Lang;
use SMF\PageIndex;
use SMF\Parser;
use SMF\Routable;
use SMF\SecurityToken;
use SMF\Slug;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
use SMF\{
ActionInterface,
ActionTrait,
Config,
Lang,
Routable,
Slug,
Theme,
User,
Utils
};

class CustomForm implements ActionInterface, Routable
{
Expand Down Expand Up @@ -63,15 +57,15 @@ public function execute(): void
public function ListForms(): void
{
if (!allowedTo('customform_view_perms')) {
redirectexit();
Utils::redirectexit();
}

loadLanguage('CustomForm');
loadTemplate('CustomForm');
loadCSSFile('customform.css', ['minimize' => true]);
Lang::load('CustomForm');
Theme::loadTemplate('CustomForm');
Theme::loadCSSFile('customform.css', ['minimize' => true]);

Utils::$context['forms'] = [];
$entries = Form::fetchAll(false);
$entries = Form::fetchMany(null, false);

foreach ($entries as $form) {
if (allowedTo('custom_forms_' . $form->id)) {
Expand All @@ -94,7 +88,7 @@ public function ListForms(): void
Utils::$context['page_message'] = Config::$modSettings['customform_view_text'] ?? '';
Utils::$context['sub_template'] = 'forms';
Utils::$context['meta_description'] = Utils::htmlspecialchars(Utils::$context['page_message']);
$this->setMetaProperty('type', 'website');
$this->util->setMetaProperty('type', 'website');
}

public function ThankYou(): void
Expand All @@ -108,8 +102,8 @@ public function ThankYou(): void
];
Utils::$context['robot_no_index'] = true;
Utils::$context['sub_template'] = 'ThankYou';
loadTemplate('CustomForm');
loadLanguage('CustomForm');
Theme::loadTemplate('CustomForm');
Lang::load('CustomForm');
}

public function ViewForm(): void
Expand All @@ -121,14 +115,18 @@ public function ViewForm(): void
$form_id = (int) ($_REQUEST['n'] ?? 0);

if (!allowedTo('custom_forms_' . $form_id)) {
redirectexit('action=form');
Utils::redirectexit('action=form');
}

/* @var Form $form_data */
if (!($form_data = Form::load($form_id))) {
redirectexit('action=form;');
Utils::redirectexit('action=form;');
}

$vars = ['yes'=>Lang::$txt['yes'],'no'=>Lang::$txt['no'],];
$vars = [
'yes' => Lang::$txt['yes'],
'no' => Lang::$txt['no'],
];
$post_errors = [];

if (isset($_POST['n'])) {
Expand All @@ -146,14 +144,12 @@ public function ViewForm(): void
}

if ($post_errors === []) {
$subject = $this->util->replace_vars($form_data->subject, $vars);
$output = $this->util->replace_vars($form_data->output, $vars);
$subject = $this->util->replaceVars($form_data->subject, $vars);
$output = $this->util->replaceVars($form_data->output, $vars);

$class_name = str_contains($form_data->output_type, '\\')
? $form_data->output_type
: 'CustomForm\Output\\' . $form_data->output_type;
$output_type = $form_data->getOutputInstance();

if (!class_exists($class_name)) {
if (!$output_type) {
fatal_error(
sprintf(
'Output type "%s" not found for form "%s" at ID #%s.',
Expand All @@ -164,19 +160,16 @@ public function ViewForm(): void
false,
);
}

$output_type = new $class_name();
$output_type->send($subject, $output, $form_data);

$exit = match ($form_data->form_exit)
{
$exit = match ($form_data->form_exit) {
'board', '' => 'board=' . $form_data->board_id,
'forum' => '',
'form' => 'action=form',
'thanks' => 'action=form;thankyou',
default => $form_data->form_exit,
};
redirectexit($exit);
Utils::redirectexit($exit);
}
}

Expand All @@ -185,7 +178,7 @@ public function ViewForm(): void

private function prepareContext(array $post_errors, Form $form_data): void
{
loadLanguage('CustomForm+Post+Errors');
Lang::load('CustomForm+Post+Errors');
Utils::$context['fields'] = [];
Utils::$context['page_title'] = $form_data->title;
Utils::$context['form_id'] = $form_data->id;
Expand All @@ -195,9 +188,9 @@ private function prepareContext(array $post_errors, Form $form_data): void
Utils::$context['post_errors'] = $post_errors;
Utils::$context['template_layers'][] = 'errors';
}
loadTemplate('CustomFormUserland', null, false);
loadTemplate('CustomForm');
loadCSSFile('customform.css', ['minimize' => true]);
Theme::loadTemplate('CustomFormUserland', '', false);
Theme::loadTemplate('CustomForm');
Theme::loadCSSFile('customform.css', ['minimize' => true]);
$template_function = 'template_' . $form_data->template_function;
$template = function_exists($template_function)
? $form_data->template_function
Expand Down Expand Up @@ -249,13 +242,14 @@ private function prepareContext(array $post_errors, Form $form_data): void
* Builds a routing path based on URL query parameters.
*
* @param array $params URL query parameters.
*
* @return array Contains two elements: ['route' => [], 'params' => []].
* The 'route' element contains the routing path. The 'params' element
* contains any $params that weren't incorporated into the route.
*/
public static function buildRoute(array $params): array
{
$route[] = $params['action'];
$route = [$params['action']];
unset($params['action']);

if (isset($params['n'])) {
Expand All @@ -276,14 +270,18 @@ public static function buildRoute(array $params): array
unset($params['n']);
}

return ['route' => $route, 'params' => $params];
return [
'route' => $route,
'params' => $params,
];
}

/**
* Parses a route to get URL query parameters.
*
* @param array $route Array of routing path components.
* @param array $params Any existing URL query parameters.
*
* @return array URL query parameters
*/
public static function parseRoute(array $route, array $params = []): array
Expand All @@ -308,9 +306,7 @@ public static function parseRoute(array $route, array $params = []): array
/**
* Constructor. Protected to force instantiation via self::load().
*/
protected function __construct()
{
}
protected function __construct() {}

private function require_verification(): bool
{
Expand All @@ -322,7 +318,12 @@ private function validateInput(): array
$post_errors = [];

if (($sc_error = checkSession('post', '', false)) != '') {
$post_errors[] = [['error_' . $sc_error, '']];
$post_errors[] = [
[
'error_' . $sc_error,
'',
],
];
}

if ($this->require_verification()) {
Expand All @@ -332,11 +333,14 @@ private function validateInput(): array

if (($verification_errors = create_control_verification($verificationOptions, true)) !== true) {
foreach ($verification_errors as $verification_error) {
$post_errors[] = ['error_' . $verification_error, ''];
$post_errors[] = [
'error_' . $verification_error,
'',
];
}
}
}

return $post_errors;
}
}
}
Loading

0 comments on commit 18d5ca9

Please sign in to comment.