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
;
14 use wcf\util\StringUtil
;
17 * Shows the form for adding new templates.
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
26 class TemplateAddForm
extends AbstractForm
{
28 * @see \wcf\page\AbstractPage::$activeMenuItem
30 public $activeMenuItem = 'wcf.acp.menu.link.template.add';
33 * @see \wcf\page\AbstractPage::$neededPermissions
35 public $neededPermissions = array('admin.template.canManageTemplate');
47 public $templateGroupID = 0;
50 * template source code
53 public $templateSource = '';
56 * available template groups
59 public $availableTemplateGroups = array();
62 * template's package id
65 public $packageID = 1;
68 * id of copied template
74 * copied template object
75 * @var \wcf\data\template\Template
77 public $copiedTemplate = null;
80 * application the template belongs to
83 public $application = '';
86 * @see \wcf\page\IPage::readParameters()
88 public function readParameters() {
89 parent
::readParameters();
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();
98 $this->application
= $this->copiedTemplate
->application
;
99 $this->packageID
= $this->copiedTemplate
->packageID
;
104 * @see \wcf\form\IForm::readFormParameters()
106 public function readFormParameters() {
107 parent
::readFormParameters();
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']);
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'];
129 * @see \wcf\form\IForm::validate()
131 public function validate() {
134 $this->validateName();
135 $this->validateGroup();
139 * Validates the template name.
141 protected function validateName() {
142 if (empty($this->tplName
)) {
143 throw new UserInputException('tplName');
146 if (!preg_match('/^[a-z0-9_\-]+$/i', $this->tplName
)) {
147 throw new UserInputException('tplName', 'notValid');
150 $conditionBuilder = new PreparedStatementConditionBuilder();
151 $conditionBuilder->add('templateName = ?', array($this->tplName
));
152 $conditionBuilder->add('templateGroupID = ?', array($this->templateGroupID
));
154 if ($this->copiedTemplate
!== null) {
155 $conditionBuilder->add('(packageID = ? OR application = ?)', array($this->packageID
, $this->copiedTemplate
->application
));
158 $conditionBuilder->add('packageID = ?', array($this->packageID
));
161 $sql = "SELECT COUNT(*) AS count
162 FROM wcf".WCF_N
."_template
164 $statement = WCF
::getDB()->prepareStatement($sql);
165 $statement->execute($conditionBuilder->getParameters());
166 $row = $statement->fetchArray();
168 throw new UserInputException('tplName', 'notUnique');
173 * Validates the selected template group.
175 protected function validateGroup() {
176 if (!$this->templateGroupID
) {
177 throw new UserInputException('templateGroupID');
180 $templateGroup = new TemplateGroup($this->templateGroupID
);
181 if (!$templateGroup->templateGroupID
) {
182 throw new UserInputException('templateGroupID');
187 * @see \wcf\form\IForm::save()
189 public function save() {
192 if (empty($this->application
)) {
193 $this->application
= Package
::getAbbreviation(PackageCache
::getInstance()->getPackage($this->packageID
)->package
);
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();
206 $this->tplName
= $this->templateSource
= '';
207 $this->templateGroupID
= 0;
210 WCF
::getTPL()->assign(array(
216 * @see \wcf\page\IPage::readData()
218 public function readData() {
221 $templateGroupList = new TemplateGroupList();
222 $templateGroupList->sqlOrderBy
= "templateGroupName";
223 $templateGroupList->readObjects();
224 $this->availableTemplateGroups
= $templateGroupList->getObjects();
226 if (!count($_POST) && $this->copiedTemplate
!== null) {
227 $this->tplName
= $this->copiedTemplate
->templateName
;
228 $this->templateSource
= $this->copiedTemplate
->getSource();
233 * @see \wcf\page\IPage::assignVariables()
235 public function assignVariables() {
236 parent
::assignVariables();
238 WCF
::getTPL()->assign(array(
240 'tplName' => $this->tplName
,
241 'templateGroupID' => $this->templateGroupID
,
242 'templateSource' => $this->templateSource
,
243 'availableTemplateGroups' => $this->availableTemplateGroups
,
244 'copy' => $this->copy