Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / TemplateAddForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\package\Package;
4 use wcf\data\package\PackageCache;
5 use wcf\data\template\group\TemplateGroup;
6 use wcf\data\template\group\TemplateGroupList;
7 use wcf\data\template\Template;
8 use wcf\data\template\TemplateAction;
9 use wcf\form\AbstractForm;
10 use wcf\system\database\util\PreparedStatementConditionBuilder;
11 use wcf\system\exception\IllegalLinkException;
12 use wcf\system\exception\UserInputException;
13 use wcf\system\WCF;
14 use wcf\util\StringUtil;
15
16 /**
17 * Shows the form for adding new templates.
18 *
19 * @author Marcel Werk
20 * @copyright 2001-2014 WoltLab GmbH
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 */
26 class TemplateAddForm extends AbstractForm {
27 /**
28 * @see \wcf\page\AbstractPage::$activeMenuItem
29 */
30 public $activeMenuItem = 'wcf.acp.menu.link.template.add';
31
32 /**
33 * @see \wcf\page\AbstractPage::$neededPermissions
34 */
35 public $neededPermissions = array('admin.template.canManageTemplate');
36
37 /**
38 * template name
39 * @var string
40 */
41 public $tplName = '';
42
43 /**
44 * template group id
45 * @var integer
46 */
47 public $templateGroupID = 0;
48
49 /**
50 * template source code
51 * @var string
52 */
53 public $templateSource = '';
54
55 /**
56 * available template groups
57 * @var array
58 */
59 public $availableTemplateGroups = array();
60
61 /**
62 * template's package id
63 * @var integer
64 */
65 public $packageID = 1;
66
67 /**
68 * id of copied template
69 * @var integer
70 */
71 public $copy = 0;
72
73 /**
74 * copied template object
75 * @var \wcf\data\template\Template
76 */
77 public $copiedTemplate = null;
78
79 /**
80 * application the template belongs to
81 * @var string
82 */
83 public $application = '';
84
85 /**
86 * @see \wcf\page\IPage::readParameters()
87 */
88 public function readParameters() {
89 parent::readParameters();
90
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
98 $this->application = $this->copiedTemplate->application;
99 $this->packageID = $this->copiedTemplate->packageID;
100 }
101 }
102
103 /**
104 * @see \wcf\form\IForm::readFormParameters()
105 */
106 public function readFormParameters() {
107 parent::readFormParameters();
108
109 if (isset($_POST['tplName'])) $this->tplName = StringUtil::trim($_POST['tplName']);
110 if (isset($_POST['templateSource'])) $this->templateSource = StringUtil::unifyNewlines($_POST['templateSource']);
111 if (isset($_POST['templateGroupID'])) $this->templateGroupID = intval($_POST['templateGroupID']);
112
113 // get package id for this template
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 }
125 }
126 }
127
128 /**
129 * @see \wcf\form\IForm::validate()
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 }
145
146 if (!preg_match('/^[a-z0-9_\-]+$/i', $this->tplName)) {
147 throw new UserInputException('tplName', 'notValid');
148 }
149
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
161 $sql = "SELECT COUNT(*) AS count
162 FROM wcf".WCF_N."_template
163 ".$conditionBuilder;
164 $statement = WCF::getDB()->prepareStatement($sql);
165 $statement->execute($conditionBuilder->getParameters());
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 /**
187 * @see \wcf\form\IForm::save()
188 */
189 public function save() {
190 parent::save();
191
192 if (empty($this->application)) {
193 $this->application = Package::getAbbreviation(PackageCache::getInstance()->getPackage($this->packageID)->package);
194 }
195
196 $this->objectAction = new TemplateAction(array(), 'create', array('data' => array_merge($this->additionalFields, array(
197 'application' => $this->application,
198 'templateName' => $this->tplName,
199 'packageID' => $this->packageID,
200 'templateGroupID' => $this->templateGroupID
201 )), 'source' => $this->templateSource));
202 $this->objectAction->executeAction();
203 $this->saved();
204
205 // reset values
206 $this->tplName = $this->templateSource = '';
207 $this->templateGroupID = 0;
208
209 // show success
210 WCF::getTPL()->assign(array(
211 'success' => true
212 ));
213 }
214
215 /**
216 * @see \wcf\page\IPage::readData()
217 */
218 public function readData() {
219 parent::readData();
220
221 $templateGroupList = new TemplateGroupList();
222 $templateGroupList->sqlOrderBy = "templateGroupName";
223 $templateGroupList->readObjects();
224 $this->availableTemplateGroups = $templateGroupList->getObjects();
225
226 if (!count($_POST) && $this->copiedTemplate !== null) {
227 $this->tplName = $this->copiedTemplate->templateName;
228 $this->templateSource = $this->copiedTemplate->getSource();
229 }
230 }
231
232 /**
233 * @see \wcf\page\IPage::assignVariables()
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,
243 'availableTemplateGroups' => $this->availableTemplateGroups,
244 'copy' => $this->copy
245 ));
246 }
247 }