Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / TemplateAddForm.class.php
CommitLineData
6570e38e
MW
1<?php
2namespace wcf\acp\form;
e568316b
MS
3use wcf\data\package\Package;
4use wcf\data\package\PackageCache;
6570e38e 5use wcf\data\template\group\TemplateGroup;
6570e38e
MW
6use wcf\data\template\group\TemplateGroupList;
7use wcf\data\template\Template;
8use wcf\data\template\TemplateAction;
9use wcf\form\AbstractForm;
dd1bae1b 10use wcf\system\database\util\PreparedStatementConditionBuilder;
43a250e6 11use wcf\system\exception\IllegalLinkException;
6570e38e
MW
12use wcf\system\exception\UserInputException;
13use wcf\system\WCF;
14use wcf\util\StringUtil;
15
16/**
17 * Shows the form for adding new templates.
e3369fd2 18 *
6570e38e 19 * @author Marcel Werk
ca4ba303 20 * @copyright 2001-2014 WoltLab GmbH
6570e38e
MW
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package com.woltlab.wcf
23 * @subpackage acp.form
24 * @category Community Framework
25 */
26class TemplateAddForm extends AbstractForm {
27 /**
0ad90fc3 28 * @see \wcf\page\AbstractPage::$activeMenuItem
6570e38e
MW
29 */
30 public $activeMenuItem = 'wcf.acp.menu.link.template.add';
31
32 /**
0ad90fc3 33 * @see \wcf\page\AbstractPage::$neededPermissions
6570e38e
MW
34 */
35 public $neededPermissions = array('admin.template.canManageTemplate');
36
37 /**
38 * template name
06355ec3 39 * @var string
6570e38e
MW
40 */
41 public $tplName = '';
42
43 /**
44 * template group id
06355ec3 45 * @var integer
6570e38e
MW
46 */
47 public $templateGroupID = 0;
48
49 /**
50 * template source code
06355ec3 51 * @var string
6570e38e
MW
52 */
53 public $templateSource = '';
54
55 /**
56 * available template groups
06355ec3 57 * @var array
6570e38e
MW
58 */
59 public $availableTemplateGroups = array();
60
61 /**
62 * template's package id
06355ec3 63 * @var integer
6570e38e 64 */
e66ddcba 65 public $packageID = 1;
6570e38e 66
43a250e6
MW
67 /**
68 * id of copied template
06355ec3 69 * @var integer
43a250e6
MW
70 */
71 public $copy = 0;
72
73 /**
74 * copied template object
0ad90fc3 75 * @var \wcf\data\template\Template
43a250e6
MW
76 */
77 public $copiedTemplate = null;
78
e568316b
MS
79 /**
80 * application the template belongs to
81 * @var string
82 */
83 public $application = '';
84
43a250e6 85 /**
0ad90fc3 86 * @see \wcf\page\IPage::readParameters()
43a250e6
MW
87 */
88 public function readParameters() {
89 parent::readParameters();
e3369fd2 90
43a250e6
MW
91 if (!empty($_REQUEST['copy'])) {
92 $this->copy = intval($_REQUEST['copy']);
93 $this->copiedTemplate = new Template($this->copy);
94 if (!$this->copiedTemplate->templateID) {
95 throw new IllegalLinkException();
96 }
97
e568316b 98 $this->application = $this->copiedTemplate->application;
43a250e6
MW
99 $this->packageID = $this->copiedTemplate->packageID;
100 }
101 }
102
6570e38e 103 /**
0ad90fc3 104 * @see \wcf\form\IForm::readFormParameters()
6570e38e
MW
105 */
106 public function readFormParameters() {
107 parent::readFormParameters();
108
109 if (isset($_POST['tplName'])) $this->tplName = StringUtil::trim($_POST['tplName']);
025652a3 110 if (isset($_POST['templateSource'])) $this->templateSource = StringUtil::unifyNewlines($_POST['templateSource']);
6570e38e
MW
111 if (isset($_POST['templateGroupID'])) $this->templateGroupID = intval($_POST['templateGroupID']);
112
113 // get package id for this template
f5b00751
MS
114 if (!$this->packageID) {
115 $sql = "SELECT packageID
116 FROM wcf".WCF_N."_template
117 WHERE templateName = ?
118 AND templateGroupID IS NULL";
119 $statement = WCF::getDB()->prepareStatement($sql);
120 $statement->execute(array($this->tplName));
121 $row = $statement->fetchArray();
122 if ($row !== false) {
123 $this->packageID = $row['packageID'];
124 }
6570e38e
MW
125 }
126 }
127
128 /**
0ad90fc3 129 * @see \wcf\form\IForm::validate()
6570e38e
MW
130 */
131 public function validate() {
132 parent::validate();
133
134 $this->validateName();
135 $this->validateGroup();
136 }
137
138 /**
139 * Validates the template name.
140 */
141 protected function validateName() {
142 if (empty($this->tplName)) {
143 throw new UserInputException('tplName');
144 }
e3369fd2 145
12aa6f89 146 if (!preg_match('/^[a-z0-9_\-]+$/i', $this->tplName)) {
6570e38e
MW
147 throw new UserInputException('tplName', 'notValid');
148 }
149
dd1bae1b
MS
150 $conditionBuilder = new PreparedStatementConditionBuilder();
151 $conditionBuilder->add('templateName = ?', array($this->tplName));
152 $conditionBuilder->add('templateGroupID = ?', array($this->templateGroupID));
153
154 if ($this->copiedTemplate !== null) {
155 $conditionBuilder->add('(packageID = ? OR application = ?)', array($this->packageID, $this->copiedTemplate->application));
156 }
157 else {
158 $conditionBuilder->add('packageID = ?', array($this->packageID));
159 }
160
6570e38e
MW
161 $sql = "SELECT COUNT(*) AS count
162 FROM wcf".WCF_N."_template
dd1bae1b 163 ".$conditionBuilder;
6570e38e 164 $statement = WCF::getDB()->prepareStatement($sql);
dd1bae1b 165 $statement->execute($conditionBuilder->getParameters());
6570e38e
MW
166 $row = $statement->fetchArray();
167 if ($row['count']) {
168 throw new UserInputException('tplName', 'notUnique');
169 }
170 }
171
172 /**
173 * Validates the selected template group.
174 */
175 protected function validateGroup() {
176 if (!$this->templateGroupID) {
177 throw new UserInputException('templateGroupID');
178 }
179
180 $templateGroup = new TemplateGroup($this->templateGroupID);
181 if (!$templateGroup->templateGroupID) {
182 throw new UserInputException('templateGroupID');
183 }
184 }
185
186 /**
0ad90fc3 187 * @see \wcf\form\IForm::save()
6570e38e
MW
188 */
189 public function save() {
190 parent::save();
191
e568316b 192 if (empty($this->application)) {
bd84f715 193 $this->application = Package::getAbbreviation(PackageCache::getInstance()->getPackage($this->packageID)->package);
e568316b
MS
194 }
195
e94d7556 196 $this->objectAction = new TemplateAction(array(), 'create', array('data' => array_merge($this->additionalFields, array(
e568316b 197 'application' => $this->application,
12aa6f89 198 'templateName' => $this->tplName,
6570e38e 199 'packageID' => $this->packageID,
12aa6f89 200 'templateGroupID' => $this->templateGroupID
e94d7556 201 )), 'source' => $this->templateSource));
6570e38e
MW
202 $this->objectAction->executeAction();
203 $this->saved();
204
205 // reset values
a701c8d6 206 $this->tplName = $this->templateSource = '';
6570e38e
MW
207 $this->templateGroupID = 0;
208
209 // show success
210 WCF::getTPL()->assign(array(
211 'success' => true
212 ));
213 }
214
215 /**
0ad90fc3 216 * @see \wcf\page\IPage::readData()
6570e38e
MW
217 */
218 public function readData() {
219 parent::readData();
2d63c13c 220
6570e38e 221 $templateGroupList = new TemplateGroupList();
cd1fcbfa 222 $templateGroupList->sqlOrderBy = "templateGroupName";
6570e38e
MW
223 $templateGroupList->readObjects();
224 $this->availableTemplateGroups = $templateGroupList->getObjects();
225
43a250e6
MW
226 if (!count($_POST) && $this->copiedTemplate !== null) {
227 $this->tplName = $this->copiedTemplate->templateName;
228 $this->templateSource = $this->copiedTemplate->getSource();
6570e38e
MW
229 }
230 }
231
232 /**
0ad90fc3 233 * @see \wcf\page\IPage::assignVariables()
6570e38e
MW
234 */
235 public function assignVariables() {
236 parent::assignVariables();
237
238 WCF::getTPL()->assign(array(
239 'action' => 'add',
240 'tplName' => $this->tplName,
241 'templateGroupID' => $this->templateGroupID,
242 'templateSource' => $this->templateSource,
43a250e6
MW
243 'availableTemplateGroups' => $this->availableTemplateGroups,
244 'copy' => $this->copy
6570e38e
MW
245 ));
246 }
247}