Change `notValid` to `invalid`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / TemplateGroupAddForm.class.php
CommitLineData
6570e38e
MW
1<?php
2namespace wcf\acp\form;
92bca149 3use wcf\data\template\group\TemplateGroup;
6570e38e 4use wcf\data\template\group\TemplateGroupAction;
6570e38e
MW
5use wcf\form\AbstractForm;
6use wcf\system\exception\UserInputException;
7use wcf\system\WCF;
12aa6f89 8use wcf\util\FileUtil;
6570e38e
MW
9use wcf\util\StringUtil;
10
11/**
12 * Shows the form for adding new template groups.
e3369fd2 13 *
6570e38e 14 * @author Marcel Werk
7d739af0 15 * @copyright 2001-2016 WoltLab GmbH
6570e38e 16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 17 * @package WoltLabSuite\Core\Acp\Form
6570e38e
MW
18 */
19class TemplateGroupAddForm extends AbstractForm {
20 /**
0fcfe5f6 21 * @inheritDoc
6570e38e
MW
22 */
23 public $activeMenuItem = 'wcf.acp.menu.link.template.group.add';
24
25 /**
0fcfe5f6 26 * @inheritDoc
6570e38e 27 */
058cbd6a 28 public $neededPermissions = ['admin.template.canManageTemplate'];
6570e38e
MW
29
30 /**
31 * template group name
06355ec3 32 * @var string
6570e38e
MW
33 */
34 public $templateGroupName = '';
35
36 /**
37 * template group folder
06355ec3 38 * @var integer
6570e38e
MW
39 */
40 public $templateGroupFolderName = '';
41
42 /**
43 * parent template group id
06355ec3 44 * @var integer
6570e38e
MW
45 */
46 public $parentTemplateGroupID = 0;
47
48 /**
49 * available template groups
06355ec3 50 * @var array
6570e38e 51 */
058cbd6a 52 public $availableTemplateGroups = [];
6570e38e
MW
53
54 /**
0fcfe5f6 55 * @inheritDoc
6570e38e
MW
56 */
57 public function readFormParameters() {
58 parent::readFormParameters();
59
60 if (isset($_POST['templateGroupName'])) $this->templateGroupName = StringUtil::trim($_POST['templateGroupName']);
12aa6f89
MW
61 if (!empty($_POST['templateGroupFolderName'])) {
62 $this->templateGroupFolderName = StringUtil::trim($_POST['templateGroupFolderName']);
63 if ($this->templateGroupFolderName) $this->templateGroupFolderName = FileUtil::addTrailingSlash($this->templateGroupFolderName);
64 }
6570e38e
MW
65 if (isset($_POST['parentTemplateGroupID'])) $this->parentTemplateGroupID = intval($_POST['parentTemplateGroupID']);
66 }
67
68 /**
0fcfe5f6 69 * @inheritDoc
6570e38e
MW
70 */
71 public function validate() {
72 parent::validate();
73
74 $this->validateName();
75 $this->validateFolderName();
5e1f952e
TD
76
77 if ($this->parentTemplateGroupID && !isset($this->availableTemplateGroups[$this->parentTemplateGroupID])) {
063bbf46 78 throw new UserInputException('parentTemplateGroupID', 'invalid');
5e1f952e 79 }
6570e38e
MW
80 }
81
82 /**
83 * Validates the template group name.
84 */
85 protected function validateName() {
86 if (empty($this->templateGroupName)) {
87 throw new UserInputException('templateGroupName');
88 }
2d63c13c 89
5c6ddd85 90 $sql = "SELECT COUNT(*)
6570e38e
MW
91 FROM wcf".WCF_N."_template_group
92 WHERE templateGroupName = ?";
93 $statement = WCF::getDB()->prepareStatement($sql);
058cbd6a 94 $statement->execute([$this->templateGroupName]);
5c6ddd85
MS
95
96 if ($statement->fetchSingleColumn()) {
6570e38e
MW
97 throw new UserInputException('templateGroupName', 'notUnique');
98 }
99 }
100
101 /**
102 * Validates the template group folder name.
103 */
104 protected function validateFolderName() {
105 if (empty($this->templateGroupFolderName)) {
106 throw new UserInputException('templateGroupFolderName');
107 }
108
ec6292fc 109 if (!preg_match('/^[a-z0-9_\- ]+\/$/i', $this->templateGroupFolderName)) {
063bbf46 110 throw new UserInputException('templateGroupFolderName', 'invalid');
6570e38e 111 }
2d63c13c 112
5c6ddd85 113 $sql = "SELECT COUNT(*)
6570e38e
MW
114 FROM wcf".WCF_N."_template_group
115 WHERE templateGroupFolderName = ?";
116 $statement = WCF::getDB()->prepareStatement($sql);
058cbd6a 117 $statement->execute([$this->templateGroupFolderName]);
5c6ddd85
MS
118
119 if ($statement->fetchSingleColumn()) {
6570e38e
MW
120 throw new UserInputException('templateGroupFolderName', 'notUnique');
121 }
122 }
123
124 /**
0fcfe5f6 125 * @inheritDoc
6570e38e
MW
126 */
127 public function save() {
128 parent::save();
129
058cbd6a 130 $this->objectAction = new TemplateGroupAction([], 'create', ['data' => array_merge($this->additionalFields, [
6570e38e
MW
131 'templateGroupName' => $this->templateGroupName,
132 'templateGroupFolderName' => $this->templateGroupFolderName,
63b9817b 133 'parentTemplateGroupID' => $this->parentTemplateGroupID ?: null
058cbd6a 134 ])]);
6570e38e
MW
135 $this->objectAction->executeAction();
136 $this->saved();
137
138 // reset values
139 $this->templateGroupName = $this->templateGroupFolderName = '';
9f4a92d4 140 $this->parentTemplateGroupID = 0;
6570e38e 141
47b90344
MS
142 // show success message
143 WCF::getTPL()->assign('success', true);
6570e38e
MW
144 }
145
146 /**
0fcfe5f6 147 * @inheritDoc
6570e38e
MW
148 */
149 public function readData() {
76eee28f
TD
150 $this->availableTemplateGroups = TemplateGroup::getSelectList([-1], 1);
151
c3abd92d 152 parent::readData();
6570e38e
MW
153 }
154
155 /**
0fcfe5f6 156 * @inheritDoc
6570e38e
MW
157 */
158 public function assignVariables() {
159 parent::assignVariables();
160
058cbd6a 161 WCF::getTPL()->assign([
6570e38e
MW
162 'action' => 'add',
163 'templateGroupName' => $this->templateGroupName,
164 'templateGroupFolderName' => $this->templateGroupFolderName,
165 'parentTemplateGroupID' => $this->parentTemplateGroupID,
166 'availableTemplateGroups' => $this->availableTemplateGroups
058cbd6a 167 ]);
6570e38e
MW
168 }
169}