Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / UserOptionCategoryAddForm.class.php
CommitLineData
c6d24026
MW
1<?php
2namespace wcf\acp\form;
3use wcf\data\user\option\category\UserOptionCategoryAction;
4use wcf\data\user\option\category\UserOptionCategoryEditor;
5use wcf\form\AbstractForm;
6use wcf\system\exception\UserInputException;
7use wcf\system\language\I18nHandler;
3fb859c9 8use wcf\system\request\LinkHandler;
c6d24026
MW
9use wcf\system\WCF;
10
11/**
12 * Shows the form for adding new user option categories.
e3369fd2 13 *
c6d24026 14 * @author Marcel Werk
7b7b9764 15 * @copyright 2001-2019 WoltLab GmbH
c6d24026 16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 17 * @package WoltLabSuite\Core\Acp\Form
c6d24026
MW
18 */
19class UserOptionCategoryAddForm extends AbstractForm {
20 /**
0fcfe5f6 21 * @inheritDoc
c6d24026
MW
22 */
23 public $activeMenuItem = 'wcf.acp.menu.link.user.option.category.add';
24
25 /**
0fcfe5f6 26 * @inheritDoc
c6d24026 27 */
058cbd6a 28 public $neededPermissions = ['admin.user.canManageUserOption'];
c6d24026
MW
29
30 /**
31 * category name
06355ec3 32 * @var string
c6d24026
MW
33 */
34 public $categoryName = '';
35
36 /**
37 * show order
06355ec3 38 * @var integer
c6d24026
MW
39 */
40 public $showOrder = 0;
41
42 /**
0fcfe5f6 43 * @inheritDoc
c6d24026
MW
44 */
45 public function readParameters() {
46 parent::readParameters();
e3369fd2 47
c6d24026
MW
48 I18nHandler::getInstance()->register('categoryName');
49 }
50
51 /**
0fcfe5f6 52 * @inheritDoc
c6d24026
MW
53 */
54 public function readFormParameters() {
55 parent::readFormParameters();
56
57 I18nHandler::getInstance()->readValues();
58
59 if (I18nHandler::getInstance()->isPlainValue('categoryName')) $this->categoryName = I18nHandler::getInstance()->getValue('categoryName');
60 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
61 }
62
63 /**
0fcfe5f6 64 * @inheritDoc
c6d24026
MW
65 */
66 public function validate() {
67 parent::validate();
68
69 if (!I18nHandler::getInstance()->validateValue('categoryName', true)) {
c20f41f9 70 throw new UserInputException('categoryName', 'multilingual');
c6d24026
MW
71 }
72 }
73
74 /**
0fcfe5f6 75 * @inheritDoc
c6d24026
MW
76 */
77 public function save() {
78 parent::save();
79
80 // save label
058cbd6a 81 $this->objectAction = new UserOptionCategoryAction([], 'create', ['data' => array_merge($this->additionalFields, [
c6d24026
MW
82 'parentCategoryName' => 'profile',
83 'categoryName' => $this->categoryName,
84 'showOrder' => $this->showOrder
058cbd6a 85 ])]);
c6d24026
MW
86 $this->objectAction->executeAction();
87
88 // update name
89 $returnValues = $this->objectAction->getReturnValues();
90 $categoryID = $returnValues['returnValues']->categoryID;
91 I18nHandler::getInstance()->save('categoryName', 'wcf.user.option.category.category'.$categoryID, 'wcf.user.option');
92 $categoryEditor = new UserOptionCategoryEditor($returnValues['returnValues']);
058cbd6a 93 $categoryEditor->update([
c6d24026 94 'categoryName' => 'category'.$categoryID
058cbd6a 95 ]);
c6d24026
MW
96 $this->saved();
97
98 // reset values
99 $this->categoryName = '';
100 $this->showOrder = 0;
d59c86bd
AE
101
102 I18nHandler::getInstance()->reset();
c6d24026 103
47b90344 104 // show success message
3fb859c9
MW
105 WCF::getTPL()->assign([
106 'success' => true,
5916be9f 107 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(UserOptionCategoryEditForm::class, ['id' => $categoryID]),
3fb859c9 108 ]);
c6d24026
MW
109 }
110
111 /**
0fcfe5f6 112 * @inheritDoc
c6d24026
MW
113 */
114 public function assignVariables() {
115 parent::assignVariables();
116
117 I18nHandler::getInstance()->assignVariables();
118
058cbd6a 119 WCF::getTPL()->assign([
c6d24026
MW
120 'action' => 'add',
121 'categoryName' => $this->categoryName,
122 'showOrder' => $this->showOrder
058cbd6a 123 ]);
c6d24026
MW
124 }
125}