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