</nav>
</header>
-{include file='formNotice'}
-
-<form method="post" action="{if $action == 'edit'}{link controller='LanguageEdit' id=$languageID}{/link}{else}{link controller='LanguageAdd'}{/link}{/if}">
- <section class="section">
- <dl{if $errorField == 'languageName'} class="formError"{/if}>
- <dt><label for="languageName">{lang}wcf.global.name{/lang}</label></dt>
- <dd>
- <input type="text" id="languageName" name="languageName" value="{$languageName}" class="long" required>
- {if $errorField == 'languageName'}
- <small class="innerError">
- {if $errorType == 'empty'}
- {lang}wcf.global.form.error.empty{/lang}
- {else}
- {lang}wcf.acp.language.add.languageName.error.{@$errorType}{/lang}
- {/if}
- </small>
- {/if}
- <small>{lang}wcf.acp.language.name.description{/lang}</small>
- </dd>
- </dl>
-
- <dl{if $errorField == 'languageCode'} class="formError"{/if}>
- <dt><label for="languageCode">{lang}wcf.acp.language.code{/lang}</label></dt>
- <dd>
- <input type="text" id="languageCode" name="languageCode" value="{$languageCode}" class="medium" required>
- {if $errorField == 'languageCode'}
- <small class="innerError">
- {if $errorType == 'empty'}
- {lang}wcf.global.form.error.empty{/lang}
- {else}
- {lang}wcf.acp.language.add.languageCode.error.{@$errorType}{/lang}
- {/if}
- </small>
- {/if}
- <small>{lang}wcf.acp.language.code.description{/lang}</small>
- </dd>
- </dl>
-
- <dl{if $errorField == 'countryCode'} class="formError"{/if}>
- <dt><label for="countryCode">{lang}wcf.acp.language.countryCode{/lang}</label></dt>
- <dd>
- <input type="text" id="countryCode" name="countryCode" value="{$countryCode}" class="medium" required>
- {if $errorField == 'countryCode'}
- <small class="innerError">
- {if $errorType == 'empty'}
- {lang}wcf.global.form.error.empty{/lang}
- {else}
- {lang}wcf.acp.language.add.countryCode.error.{@$errorType}{/lang}
- {/if}
- </small>
- {/if}
- <small>{lang}wcf.acp.language.countryCode.description{/lang}</small>
- </dd>
- </dl>
-
- <dl{if $errorField == 'locale'} class="formError"{/if}>
- <dt><label for="locale">{lang}wcf.acp.language.locale{/lang}</label></dt>
- <dd>
- <select id="locale" name="locale" class="medium" required>
- <option value="">{lang}wcf.global.noSelection{/lang}</option>
- {foreach from=$locales key='identifier' item='displayName'}
- <option value="{$identifier}"{if $identifier === $locale} selected{/if}>{$displayName}</option>
- {/foreach}
- </select>
- {if $errorField == 'locale'}
- <small class="innerError">
- {if $errorType == 'empty'}
- {lang}wcf.global.form.error.empty{/lang}
- {else}
- {lang}wcf.acp.language.add.locale.error.{@$errorType}{/lang}
- {/if}
- </small>
- {/if}
- <small>{lang}wcf.acp.language.locale.description{/lang}</small>
- </dd>
- </dl>
-
- {if $action == 'add'}
- <dl{if $errorField == 'sourceLanguageID'} class="formError"{/if}>
- <dt><label for="sourceLanguageID">{lang}wcf.acp.language.add.source{/lang}</label></dt>
- <dd>
- <select id="sourceLanguageID" name="sourceLanguageID">
- {foreach from=$languages item=language}
- <option value="{$language->languageID}"{if $language->languageID == $sourceLanguageID} selected{/if}>{$language->languageName} ({$language->languageCode})</option>
- {/foreach}
- </select>
- {if $errorField == 'sourceLanguageID'}
- <small class="innerError">
- {if $errorType == 'empty'}
- {lang}wcf.global.form.error.empty{/lang}
- {else}
- {lang}wcf.acp.language.add.source.error.{@$errorType}{/lang}
- {/if}
- </small>
- {/if}
- <small>{lang}wcf.acp.language.add.source.description{/lang}</small>
- </dd>
- </dl>
- {/if}
-
- {event name='fields'}
- </section>
-
- {event name='sections'}
-
- <div class="formSubmit">
- <input type="submit" value="{lang}wcf.global.button.submit{/lang}" accesskey="s">
- {csrfToken}
- </div>
-</form>
+{@$form->getHtml()}
{include file='footer'}
namespace wcf\acp\form;
use wcf\data\language\Language;
-use wcf\data\language\LanguageEditor;
-use wcf\form\AbstractForm;
-use wcf\system\exception\UserInputException;
+use wcf\data\language\LanguageAction;
+use wcf\form\AbstractFormBuilderForm;
+use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\data\processor\CustomFormDataProcessor;
+use wcf\system\form\builder\field\SingleSelectionFormField;
+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\form\builder\IFormDocument;
use wcf\system\language\LanguageFactory;
-use wcf\system\request\LinkHandler;
use wcf\system\WCF;
-use wcf\util\StringUtil;
/**
* Shows the language add form.
*
- * @author Marcel Werk
- * @copyright 2001-2019 WoltLab GmbH
+ * @property LanguageAction $objectAction
+ *
+ * @author Florian Gail, Marcel Werk
+ * @copyright 2001-2023 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
-class LanguageAddForm extends AbstractForm
+class LanguageAddForm extends AbstractFormBuilderForm
{
/**
* @inheritDoc
*/
public $activeMenuItem = 'wcf.acp.menu.link.language.list';
- /**
- * country code
- * @var string
- */
- public $countryCode = '';
-
/**
* @inheritDoc
*/
public $neededPermissions = ['admin.language.canManageLanguage'];
/**
- * language object
- * @var Language
- */
- public $language;
-
- /**
- * language name
- * @var string
- */
- public $languageName = '';
-
- /**
- * language code
- * @var string
- */
- public $languageCode = '';
-
- /**
- * list of available languages
- * @var Language[]
- */
- public $languages = [];
-
- /**
- * source language object
- * @var Language
+ * @inheritDoc
*/
- public $sourceLanguage;
+ public $objectActionClass = LanguageAction::class;
/**
- * source language id
- * @var int
+ * @inheritDoc
*/
- public $sourceLanguageID = 0;
+ public $objectEditLinkController = LanguageEditForm::class;
/**
* @var string[]
*/
public array $locales;
- public string $locale = '';
-
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
- public function readFormParameters()
+ protected function createForm()
{
- parent::readFormParameters();
-
- if (isset($_POST['countryCode'])) {
- $this->countryCode = StringUtil::trim($_POST['countryCode']);
- }
- if (isset($_POST['languageName'])) {
- $this->languageName = StringUtil::trim($_POST['languageName']);
- }
- if (isset($_POST['languageCode'])) {
- $this->languageCode = StringUtil::trim($_POST['languageCode']);
- }
- if (isset($_POST['sourceLanguageID'])) {
- $this->sourceLanguageID = \intval($_POST['sourceLanguageID']);
- }
- if (isset($_POST['locale'])) {
- $this->locale = $_POST['locale'];
- }
- }
-
- /**
- * @inheritDoc
- */
- public function validate()
- {
- parent::validate();
-
- // language name
- if (empty($this->languageName)) {
- throw new UserInputException('languageName');
- }
-
- // country code
- if (empty($this->countryCode)) {
- throw new UserInputException('countryCode');
- }
-
- // language code
- $this->validateLanguageCode();
-
- // source language id
- $this->validateSource();
-
- if (!isset($this->locales[$this->locale])) {
- throw new UserInputException('locale');
- }
- }
-
- /**
- * Validates the language code.
- */
- protected function validateLanguageCode()
- {
- if (empty($this->languageCode)) {
- throw new UserInputException('languageCode');
- }
- if (LanguageFactory::getInstance()->getLanguageByCode($this->languageCode)) {
- throw new UserInputException('languageCode', 'notUnique');
- }
- }
-
- /**
- * Validates given source language.
- */
- protected function validateSource()
- {
- if (empty($this->sourceLanguageID)) {
- throw new UserInputException('sourceLanguageID');
- }
-
- // get language
- $this->sourceLanguage = LanguageFactory::getInstance()->getLanguage($this->sourceLanguageID);
- if (!$this->sourceLanguage->languageID) {
- throw new UserInputException('sourceLanguageID');
- }
- }
-
- /**
- * @inheritDoc
- */
- public function save()
- {
- parent::save();
-
- $this->language = LanguageEditor::create(\array_merge($this->additionalFields, [
- 'countryCode' => \mb_strtolower($this->countryCode),
- 'languageName' => $this->languageName,
- 'languageCode' => \mb_strtolower($this->languageCode),
- 'locale' => $this->locale,
- ]));
- $languageEditor = new LanguageEditor($this->sourceLanguage);
- $languageEditor->copy($this->language);
-
- // copy content
- LanguageEditor::copyLanguageContent($this->sourceLanguage->languageID, $this->language->languageID);
-
- // reset caches
- LanguageFactory::getInstance()->clearCache();
- LanguageFactory::getInstance()->deleteLanguageCache();
-
- $this->saved();
-
- // show success message
- WCF::getTPL()->assign([
- 'success' => true,
- 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
- LanguageEditForm::class,
- ['id' => $this->language->languageID]
- ),
+ parent::createForm();
+
+ $locales = [
+ '' => WCF::getLanguage()->get('wcf.global.noSelection'),
+ ...$this->locales,
+ ];
+
+ $this->form->appendChildren([
+ FormContainer::create('data')
+ ->appendChildren([
+ TextFormField::create('languageName')
+ ->label('wcf.global.name')
+ ->description('wcf.acp.language.name.description')
+ ->maximumLength(255)
+ ->required(),
+ TextFormField::create('languageCode')
+ ->label('wcf.acp.language.code')
+ ->description('wcf.acp.language.code.description')
+ ->maximumLength(20)
+ ->required()
+ ->addValidator(new FormFieldValidator('unique', function (TextFormField $formField) {
+ if ($formField->getValidationErrors() !== []) {
+ return;
+ }
+
+ if (
+ $this->formObject instanceof Language
+ && \mb_strtolower($this->formObject->languageCode) === $formField->getValue()
+ ) {
+ return;
+ }
+
+ if (LanguageFactory::getInstance()->getLanguageByCode($formField->getValue())) {
+ $formField->addValidationError(new FormFieldValidationError(
+ 'notUnique',
+ 'wcf.acp.language.add.languageCode.error.notUnique'
+ ));
+ }
+ })),
+ TextFormField::create('countryCode')
+ ->label('wcf.acp.language.countryCode')
+ ->description('wcf.acp.language.countryCode.description')
+ ->maximumLength(10)
+ ->required(),
+ SingleSelectionFormField::create('locale')
+ ->label('wcf.acp.language.locale')
+ ->description('wcf.acp.language.locale.description')
+ ->options($locales)
+ ->addValidator(new FormFieldValidator(
+ 'locale',
+ static function (SingleSelectionFormField $formField) {
+ if ($formField->getValue() === '') {
+ return;
+ }
+
+ if ($formField->getValidationErrors() !== []) {
+ return;
+ }
+
+ $languageCodeField = $formField->getDocument()->getNodeById('languageCode');
+ \assert($languageCodeField instanceof TextFormField);
+
+ [$languageCode] = \explode('_', $formField->getValue());
+ if ($languageCodeField->getValue() !== $languageCode) {
+ $formField->addValidationError(
+ new FormFieldValidationError(
+ 'languageCodeMismatch',
+ 'wcf.acp.language.add.locale.error.languageCodeMismatch',
+ [
+ 'locale' => $formField->getValue(),
+ ]
+ )
+ );
+ }
+ }
+ )),
+ SingleSelectionFormField::create('sourceLanguageID')
+ ->label('wcf.acp.language.add.source')
+ ->description('wcf.acp.language.add.source.description')
+ ->options(LanguageFactory::getInstance()->getLanguages())
+ ->available($this->formAction === IFormDocument::FORM_MODE_CREATE)
+ ->required(),
+ ]),
]);
-
- // reset values
- $this->countryCode = $this->languageCode = $this->languageName = $this->locale = '';
- $this->sourceLanguageID = 0;
}
/**
* @inheritDoc
*/
- public function readData()
+ public function buildForm()
{
- parent::readData();
-
- $this->languages = LanguageFactory::getInstance()->getLanguages();
- }
-
- /**
- * @inheritDoc
- */
- public function assignVariables()
- {
- parent::assignVariables();
-
- WCF::getTPL()->assign([
- 'countryCode' => $this->countryCode,
- 'languageName' => $this->languageName,
- 'languageCode' => $this->languageCode,
- 'sourceLanguageID' => $this->sourceLanguageID,
- 'languages' => $this->languages,
- 'locale' => $this->locale,
- 'locales' => $this->locales,
- 'action' => 'add',
- ]);
+ parent::buildForm();
+
+ $this->form->getDataHandler()->addProcessor(new CustomFormDataProcessor(
+ 'lowercase',
+ static function (IFormDocument $document, array $parameters) {
+ $parameters['data']['languageCode'] = \mb_strtolower($parameters['data']['languageCode']);
+ $parameters['data']['countryCode'] = \mb_strtolower($parameters['data']['countryCode']);
+
+ return $parameters;
+ }
+ ));
+ $this->form->getDataHandler()->addProcessor(new CustomFormDataProcessor(
+ 'sourceLanguage',
+ function (IFormDocument $document, array $parameters) {
+ if ($this->formAction !== IFormDocument::FORM_MODE_CREATE) {
+ return $parameters;
+ }
+
+ if (isset($parameters['data']['sourceLanguageID'])) {
+ $parameters['sourceLanguageID'] = $parameters['data']['sourceLanguageID'];
+ unset($parameters['data']['sourceLanguageID']);
+ }
+
+ return $parameters;
+ }
+ ));
}
}
namespace wcf\acp\form;
+use CuyZ\Valinor\Mapper\MappingError;
use wcf\data\language\Language;
-use wcf\data\language\LanguageEditor;
-use wcf\form\AbstractForm;
+use wcf\http\Helper;
use wcf\system\exception\IllegalLinkException;
-use wcf\system\language\LanguageFactory;
-use wcf\system\WCF;
/**
* Shows the language edit form.
*
- * @author Marcel Werk
- * @copyright 2001-2019 WoltLab GmbH
+ * @property Language $formObject
+ *
+ * @author Florian Gail
+ * @copyright 2001-2023 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class LanguageEditForm extends LanguageAddForm
{
/**
- * language id
- * @var int
+ * @inheritDoc
*/
- public $languageID = 0;
+ public $formAction = 'edit';
/**
* @inheritDoc
{
parent::readParameters();
- if (isset($_REQUEST['id'])) {
- $this->languageID = \intval($_REQUEST['id']);
- }
- $this->language = new Language($this->languageID);
- if (!$this->language->languageID) {
+ try {
+ $queryParameters = Helper::mapQueryParameters(
+ $_GET,
+ <<<'EOT'
+ array {
+ id: positive-int
+ }
+ EOT
+ );
+ $this->formObject = new Language($queryParameters['id']);
+
+ if (!$this->formObject->getObjectID()) {
+ throw new IllegalLinkException();
+ }
+ } catch (MappingError) {
throw new IllegalLinkException();
}
}
-
- /**
- * @inheritDoc
- */
- protected function validateLanguageCode()
- {
- if ($this->language->languageCode != \mb_strtolower($this->languageCode)) {
- parent::validateLanguageCode();
- }
- }
-
- /**
- * @inheritDoc
- */
- protected function validateSource()
- {
- }
-
- /**
- * @inheritDoc
- */
- public function save()
- {
- AbstractForm::save();
-
- $editor = new LanguageEditor($this->language);
- $editor->update(\array_merge($this->additionalFields, [
- 'countryCode' => \mb_strtolower($this->countryCode),
- 'languageName' => $this->languageName,
- 'languageCode' => \mb_strtolower($this->languageCode),
- 'locale' => $this->locale,
- ]));
- LanguageFactory::getInstance()->clearCache();
- $this->saved();
-
- // show success message
- WCF::getTPL()->assign('success', true);
- }
-
- /**
- * @inheritDoc
- */
- public function readData()
- {
- parent::readData();
-
- if (!\count($_POST)) {
- $this->countryCode = $this->language->countryCode;
- $this->languageName = $this->language->languageName;
- $this->languageCode = $this->language->languageCode;
- $this->locale = $this->language->locale;
- }
- }
-
- /**
- * @inheritDoc
- */
- public function assignVariables()
- {
- parent::assignVariables();
-
- WCF::getTPL()->assign([
- 'languageID' => $this->languageID,
- 'language' => $this->language,
- 'action' => 'edit',
- ]);
- }
}