Merge branch '3.1' into 5.2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / TemplateGroupAddForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\template\group\TemplateGroup;
4 use wcf\data\template\group\TemplateGroupAction;
5 use wcf\form\AbstractForm;
6 use wcf\system\exception\UserInputException;
7 use wcf\system\WCF;
8 use wcf\util\FileUtil;
9 use wcf\util\StringUtil;
10
11 /**
12 * Shows the form for adding new template groups.
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 TemplateGroupAddForm extends AbstractForm {
20 /**
21 * @inheritDoc
22 */
23 public $activeMenuItem = 'wcf.acp.menu.link.template.group.add';
24
25 /**
26 * @inheritDoc
27 */
28 public $neededPermissions = ['admin.template.canManageTemplate'];
29
30 /**
31 * template group name
32 * @var string
33 */
34 public $templateGroupName = '';
35
36 /**
37 * template group folder
38 * @var integer
39 */
40 public $templateGroupFolderName = '';
41
42 /**
43 * parent template group id
44 * @var integer
45 */
46 public $parentTemplateGroupID = 0;
47
48 /**
49 * available template groups
50 * @var array
51 */
52 public $availableTemplateGroups = [];
53
54 /**
55 * @inheritDoc
56 */
57 public function readFormParameters() {
58 parent::readFormParameters();
59
60 if (isset($_POST['templateGroupName'])) $this->templateGroupName = StringUtil::trim($_POST['templateGroupName']);
61 if (!empty($_POST['templateGroupFolderName'])) {
62 $this->templateGroupFolderName = StringUtil::trim($_POST['templateGroupFolderName']);
63 if ($this->templateGroupFolderName) $this->templateGroupFolderName = FileUtil::addTrailingSlash($this->templateGroupFolderName);
64 }
65 if (isset($_POST['parentTemplateGroupID'])) $this->parentTemplateGroupID = intval($_POST['parentTemplateGroupID']);
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public function validate() {
72 parent::validate();
73
74 $this->validateName();
75 $this->validateFolderName();
76
77 if ($this->parentTemplateGroupID && !isset($this->availableTemplateGroups[$this->parentTemplateGroupID])) {
78 throw new UserInputException('parentTemplateGroupID', 'invalid');
79 }
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 }
89
90 $sql = "SELECT COUNT(*)
91 FROM wcf".WCF_N."_template_group
92 WHERE templateGroupName = ?";
93 $statement = WCF::getDB()->prepareStatement($sql);
94 $statement->execute([$this->templateGroupName]);
95
96 if ($statement->fetchSingleColumn()) {
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
109 if (!preg_match('/^[a-z0-9_\- ]+\/$/i', $this->templateGroupFolderName)) {
110 throw new UserInputException('templateGroupFolderName', 'invalid');
111 }
112
113 $sql = "SELECT COUNT(*)
114 FROM wcf".WCF_N."_template_group
115 WHERE templateGroupFolderName = ?";
116 $statement = WCF::getDB()->prepareStatement($sql);
117 $statement->execute([$this->templateGroupFolderName]);
118
119 if ($statement->fetchSingleColumn()) {
120 throw new UserInputException('templateGroupFolderName', 'notUnique');
121 }
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function save() {
128 parent::save();
129
130 $this->objectAction = new TemplateGroupAction([], 'create', ['data' => array_merge($this->additionalFields, [
131 'templateGroupName' => $this->templateGroupName,
132 'templateGroupFolderName' => $this->templateGroupFolderName,
133 'parentTemplateGroupID' => $this->parentTemplateGroupID ?: null
134 ])]);
135 $this->objectAction->executeAction();
136 $this->saved();
137
138 // reset values
139 $this->templateGroupName = $this->templateGroupFolderName = '';
140 $this->parentTemplateGroupID = 0;
141
142 // show success message
143 WCF::getTPL()->assign('success', true);
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function readData() {
150 $this->availableTemplateGroups = TemplateGroup::getSelectList([-1], 1);
151
152 parent::readData();
153 }
154
155 /**
156 * @inheritDoc
157 */
158 public function assignVariables() {
159 parent::assignVariables();
160
161 WCF::getTPL()->assign([
162 'action' => 'add',
163 'templateGroupName' => $this->templateGroupName,
164 'templateGroupFolderName' => $this->templateGroupFolderName,
165 'parentTemplateGroupID' => $this->parentTemplateGroupID,
166 'availableTemplateGroups' => $this->availableTemplateGroups
167 ]);
168 }
169 }