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
;
9 use wcf\util\StringUtil
;
12 * Shows the form for adding new template groups.
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
19 class TemplateGroupAddForm
extends AbstractForm
{
23 public $activeMenuItem = 'wcf.acp.menu.link.template.group.add';
28 public $neededPermissions = ['admin.template.canManageTemplate'];
34 public $templateGroupName = '';
37 * template group folder
40 public $templateGroupFolderName = '';
43 * parent template group id
46 public $parentTemplateGroupID = 0;
49 * available template groups
52 public $availableTemplateGroups = [];
57 public function readFormParameters() {
58 parent
::readFormParameters();
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
);
65 if (isset($_POST['parentTemplateGroupID'])) $this->parentTemplateGroupID
= intval($_POST['parentTemplateGroupID']);
71 public function validate() {
74 $this->validateName();
75 $this->validateFolderName();
77 if ($this->parentTemplateGroupID
&& !isset($this->availableTemplateGroups
[$this->parentTemplateGroupID
])) {
78 throw new UserInputException('parentTemplateGroupID', 'invalid');
83 * Validates the template group name.
85 protected function validateName() {
86 if (empty($this->templateGroupName
)) {
87 throw new UserInputException('templateGroupName');
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
]);
96 if ($statement->fetchSingleColumn()) {
97 throw new UserInputException('templateGroupName', 'notUnique');
102 * Validates the template group folder name.
104 protected function validateFolderName() {
105 if (empty($this->templateGroupFolderName
)) {
106 throw new UserInputException('templateGroupFolderName');
109 if (!preg_match('/^[a-z0-9_\- ]+\/$/i', $this->templateGroupFolderName
)) {
110 throw new UserInputException('templateGroupFolderName', 'invalid');
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
]);
119 if ($statement->fetchSingleColumn()) {
120 throw new UserInputException('templateGroupFolderName', 'notUnique');
127 public function save() {
130 $this->objectAction
= new TemplateGroupAction([], 'create', ['data' => array_merge($this->additionalFields
, [
131 'templateGroupName' => $this->templateGroupName
,
132 'templateGroupFolderName' => $this->templateGroupFolderName
,
133 'parentTemplateGroupID' => $this->parentTemplateGroupID ?
: null
135 $this->objectAction
->executeAction();
139 $this->templateGroupName
= $this->templateGroupFolderName
= '';
140 $this->parentTemplateGroupID
= 0;
142 // show success message
143 WCF
::getTPL()->assign('success', true);
149 public function readData() {
150 $this->availableTemplateGroups
= TemplateGroup
::getSelectList([-1], 1);
158 public function assignVariables() {
159 parent
::assignVariables();
161 WCF
::getTPL()->assign([
163 'templateGroupName' => $this->templateGroupName
,
164 'templateGroupFolderName' => $this->templateGroupFolderName
,
165 'parentTemplateGroupID' => $this->parentTemplateGroupID
,
166 'availableTemplateGroups' => $this->availableTemplateGroups