Skip to content

Commit

Permalink
Use the FormBuilder to create/edit media provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyperghost committed Nov 14, 2024
1 parent 0eefa53 commit 82ef3ef
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 285 deletions.
81 changes: 1 addition & 80 deletions wcfsetup/install/files/acp/templates/bbcodeMediaProviderAdd.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,6 @@
</nav>
</header>

{include file='shared_formNotice'}

<form method="post" action="{if $action == 'add'}{link controller='BBCodeMediaProviderAdd'}{/link}{else}{link controller='BBCodeMediaProviderEdit' object=$mediaProvider}{/link}{/if}">
<div class="section">
<dl{if $errorField == 'title'} class="formError"{/if}>
<dt><label for="title">{lang}wcf.acp.bbcode.mediaProvider.title{/lang}</label></dt>
<dd>
<input type="text" id="title" name="title" value="{$title}" required autofocus class="long">
{if $errorField == 'title'}
<small class="innerError">
{if $errorType == 'empty'}
{lang}wcf.global.form.error.empty{/lang}
{else}
{lang}wcf.acp.bbcode.mediaProvider.title.error.{$errorType}{/lang}
{/if}
</small>
{/if}
</dd>
</dl>

<dl{if $errorField == 'regex'} class="formError"{/if}>
<dt><label for="regex">{lang}wcf.acp.bbcode.mediaProvider.regex{/lang}</label></dt>
<dd>
<textarea id="regex" name="regex" cols="40" rows="5" required>{$regex}</textarea>
{if $errorField == 'regex'}
<small class="innerError">
{if $errorType == 'empty'}
{lang}wcf.global.form.error.empty{/lang}
{else}
{lang}wcf.acp.bbcode.mediaProvider.regex.error.{$errorType}{/lang}
{/if}
</small>
{/if}
<small>{lang}wcf.acp.bbcode.mediaProvider.regex.description{/lang}</small>
</dd>
</dl>

<dl{if $errorField == 'html'} class="formError"{/if}>
<dt><label for="html">{lang}wcf.acp.bbcode.mediaProvider.html{/lang}</label></dt>
<dd>
<textarea id="html" name="html" cols="40" rows="10">{$html}</textarea>
{if $errorField == 'html'}
<small class="innerError">
{if $errorType == 'empty'}
{lang}wcf.global.form.error.empty{/lang}
{else}
{lang}wcf.acp.bbcode.mediaProvider.html.error.{$errorType}{/lang}
{/if}
</small>
{/if}
<small>{lang}wcf.acp.bbcode.mediaProvider.html.description{/lang}</small>
</dd>
</dl>

<dl{if $errorField == 'className'} class="formError"{/if}>
<dt><label for="className">{lang}wcf.acp.bbcode.mediaProvider.className{/lang}</label></dt>
<dd>
<input type="text" id="className" name="className" value="{$className}" pattern="^\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$" class="long">
{if $errorField == 'className'}
<small class="innerError">
{if $errorType == 'empty'}
{lang}wcf.global.form.error.empty{/lang}
{else}
{lang}wcf.acp.bbcode.mediaProvider.className.error.{@$errorType}{/lang}
{/if}
</small>
{/if}
</dd>
</dl>

{event name='dataFields'}
</div>

{event name='sections'}

<div class="formSubmit">
<input type="submit" value="{lang}wcf.global.button.submit{/lang}" accesskey="s">
{csrfToken}
</div>
</form>
{unsafe:$form->getHtml()}

{include file='footer'}
212 changes: 83 additions & 129 deletions wcfsetup/install/files/lib/acp/form/BBCodeMediaProviderAddForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,33 @@
use wcf\data\bbcode\media\provider\BBCodeMediaProvider;
use wcf\data\bbcode\media\provider\BBCodeMediaProviderAction;
use wcf\data\bbcode\media\provider\BBCodeMediaProviderEditor;
use wcf\form\AbstractForm;
use wcf\system\exception\UserInputException;
use wcf\form\AbstractFormBuilderForm;
use wcf\system\bbcode\media\provider\IBBCodeMediaProvider;
use wcf\system\form\builder\container\FormContainer;
use wcf\system\form\builder\field\ClassNameFormField;
use wcf\system\form\builder\field\MultilineTextFormField;
use wcf\system\form\builder\field\TextFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\Regex;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\StringUtil;

/**
* Shows the BBCode media provider add form.
*
* @author Tim Duesterhus
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @author Olaf Braun, Tim Duesterhus
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property BBCodeMediaProvider $formObjects
*/
class BBCodeMediaProviderAddForm extends AbstractForm
class BBCodeMediaProviderAddForm extends AbstractFormBuilderForm
{
/**
* @inheritDoc
*/
public $activeMenuItem = 'wcf.acp.menu.link.bbcode.mediaProvider.add';

/**
* media provider class name
* @var string
*/
public $className = '';

/**
* media provider package id
* @var int
*/
public $packageID = PACKAGE_ID;

/**
* html value
* @var string
*/
public $html = '';

/**
* @inheritDoc
*/
Expand All @@ -54,125 +42,91 @@ class BBCodeMediaProviderAddForm extends AbstractForm
*/
public $templateName = 'bbcodeMediaProviderAdd';

/**
* title value
* @var string
*/
public $title = '';

/**
* regex value
* @var string
*/
public $regex = '';

/**
* @inheritDoc
*/
public function readFormParameters()
{
parent::readFormParameters();

if (isset($_POST['title'])) {
$this->title = StringUtil::trim($_POST['title']);
}
if (isset($_POST['regex'])) {
$this->regex = StringUtil::trim($_POST['regex']);
}
if (isset($_POST['html'])) {
$this->html = StringUtil::trim($_POST['html']);
}
if (isset($_POST['className'])) {
$this->className = StringUtil::trim($_POST['className']);
}
}
public $objectActionClass = BBCodeMediaProviderAction::class;

/**
* @inheritDoc
*/
public function validate()
{
parent::validate();
public $objectEditLinkController = BBCodeMediaProviderEditForm::class;

// validate fields
if (empty($this->title)) {
throw new UserInputException('title');
}
if (empty($this->regex)) {
throw new UserInputException('regex');
}
if (empty($this->className) && empty($this->html)) {
throw new UserInputException('html');
}
// validate class name
if (!empty($this->className) && !\class_exists($this->className)) {
throw new UserInputException('className', 'notFound');
}

$lines = \explode("\n", StringUtil::unifyNewlines($this->regex));

foreach ($lines as $line) {
if (!Regex::compile($line)->isValid()) {
throw new UserInputException('regex', 'invalid');
}
}
#[\Override]
protected function createForm()
{
parent::createForm();

$this->form->appendChildren([
FormContainer::create('general')
->appendChildren([
TextFormField::create('title')
->label('wcf.acp.bbcode.mediaProvider.title')
->required(),
MultilineTextFormField::create('regex')
->label('wcf.acp.bbcode.mediaProvider.regex')
->description('wcf.acp.bbcode.mediaProvider.regex.description')
->required()
->addValidator(
new FormFieldValidator('regexValidator', function (MultilineTextFormField $formField) {
$lines = \explode("\n", StringUtil::unifyNewlines($formField->getValue()));

foreach ($lines as $line) {
if (!Regex::compile($line)->isValid()) {
$formField->addValidationError(
new FormFieldValidationError(
'invalid',
'wcf.acp.bbcode.mediaProvider.regex.error.invalid'
)
);
}
}
})
),
MultilineTextFormField::create('html')
->label('wcf.acp.bbcode.mediaProvider.html')
->description('wcf.acp.bbcode.mediaProvider.html.description')
->addValidator(
new FormFieldValidator('emptyValidator', function (MultilineTextFormField $formField) {
$classNameFormField = $formField->getDocument()->getNodeById('className');
\assert($classNameFormField instanceof ClassNameFormField);

if (empty($formField->getValue()) && empty($classNameFormField->getValue())) {
$formField->addValidationError(
new FormFieldValidationError('empty')
);
}
})
),
ClassNameFormField::create('className')
->label('wcf.acp.bbcode.mediaProvider.className')
->implementedInterface(IBBCodeMediaProvider::class)
])
]);
}

/**
* @inheritDoc
*/
#[\Override]
public function save()
{
parent::save();

$name = 'placeholder_' . StringUtil::getRandomID();

// save media provider
$this->objectAction = new BBCodeMediaProviderAction([], 'create', [
'data' => \array_merge($this->additionalFields, [
'title' => $this->title,
'regex' => $this->regex,
'html' => $this->html,
'className' => $this->className,
'packageID' => $this->packageID,
'name' => $name,
]),
]);
$returnValues = $this->objectAction->executeAction();
$this->saved();

/** @var BBCodeMediaProvider $provider */
$provider = $returnValues['returnValues'];
(new BBCodeMediaProviderEditor($provider))->update([
'name' => 'com.woltlab.wcf.generic' . $provider->providerID,
]);

// reset values
$this->title = $this->regex = $this->html = $this->className = '';
if ($this->formAction === "create") {
$this->additionalFields['packageID'] = PACKAGE_ID;
$this->additionalFields['name'] = 'placeholder_' . StringUtil::getRandomID();
}

// show success message
WCF::getTPL()->assign([
'success' => true,
'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
BBCodeMediaProviderEditForm::class,
['id' => $provider->providerID]
),
]);
parent::save();
}

/**
* @inheritDoc
*/
public function assignVariables()
#[\Override]
public function saved()
{
parent::assignVariables();
if ($this->formAction === "create") {
/** @var BBCodeMediaProvider $provider */
$provider = $this->objectAction->getReturnValues()['returnValues'];
(new BBCodeMediaProviderEditor($provider))->update([
'name' => 'com.woltlab.wcf.generic' . $provider->providerID,
]);
}

WCF::getTPL()->assign([
'action' => 'add',
'title' => $this->title,
'regex' => $this->regex,
'html' => $this->html,
'className' => $this->className,
]);
parent::saved();
}
}
Loading

0 comments on commit 82ef3ef

Please sign in to comment.