Implemented history support for boxes and pages
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / BoxEditForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\box\Box;
4 use wcf\data\box\BoxAction;
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\form\AbstractForm;
7 use wcf\system\acl\simple\SimpleAclHandler;
8 use wcf\system\box\IConditionBoxController;
9 use wcf\system\condition\ConditionHandler;
10 use wcf\system\exception\IllegalLinkException;
11 use wcf\system\language\LanguageFactory;
12 use wcf\system\version\VersionTracker;
13 use wcf\system\WCF;
14
15 /**
16 * Shows the box edit form.
17 *
18 * @author Marcel Werk
19 * @copyright 2001-2017 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package WoltLabSuite\Core\Acp\Form
22 * @since 3.0
23 */
24 class BoxEditForm extends BoxAddForm {
25 /**
26 * @inheritDoc
27 */
28 public $activeMenuItem = 'wcf.acp.menu.link.cms.box.list';
29
30 /**
31 * box id
32 * @var integer
33 */
34 public $boxID = 0;
35
36 /**
37 * box object
38 * @var Box
39 */
40 public $box;
41
42 /**
43 * @inheritDoc
44 */
45 public function readParameters() {
46 parent::readParameters();
47
48 if (isset($_REQUEST['id'])) $this->boxID = intval($_REQUEST['id']);
49 $this->box = new Box($this->boxID);
50 if (!$this->box->boxID) {
51 throw new IllegalLinkException();
52 }
53 if ($this->box->boxType == 'menu') {
54 // it's not allowed to edit menu boxes directly
55 throw new IllegalLinkException();
56 }
57 if ($this->box->isMultilingual) $this->isMultilingual = 1;
58 }
59
60 /**
61 * @inheritDoc
62 */
63 protected function readBoxType() {
64 // not required for editing
65 }
66
67 /**
68 * @inheritDoc
69 */
70 protected function validateName() {
71 if (mb_strtolower($this->name) != mb_strtolower($this->box->name)) {
72 parent::validateName();
73 }
74 }
75
76 /**
77 * @inheritDoc
78 */
79 public function save() {
80 AbstractForm::save();
81
82 $content = [];
83 if ($this->boxType == 'system' || $this->isMultilingual) {
84 foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
85 $content[$language->languageID] = [
86 'title' => !empty($this->title[$language->languageID]) ? $this->title[$language->languageID] : '',
87 'content' => !empty($this->content[$language->languageID]) ? $this->content[$language->languageID] : '',
88 'htmlInputProcessor' => isset($this->htmlInputProcessors[$language->languageID]) ? $this->htmlInputProcessors[$language->languageID] : null,
89 'imageID' => !empty($this->imageID[$language->languageID]) ? $this->imageID[$language->languageID] : null
90 ];
91 }
92 }
93 else {
94 $content[0] = [
95 'title' => !empty($this->title[0]) ? $this->title[0] : '',
96 'content' => !empty($this->content[0]) ? $this->content[0] : '',
97 'htmlInputProcessor' => isset($this->htmlInputProcessors[0]) ? $this->htmlInputProcessors[0] : null,
98 'imageID' => !empty($this->imageID[0]) ? $this->imageID[0] : null
99 ];
100 }
101
102 $data = [
103 'name' => $this->name,
104 'isMultilingual' => $this->isMultilingual,
105 'boxType' => $this->boxType,
106 'position' => $this->position,
107 'showOrder' => $this->showOrder,
108 'visibleEverywhere' => $this->visibleEverywhere,
109 'cssClassName' => $this->cssClassName,
110 'showHeader' => $this->showHeader,
111 'linkPageID' => $this->linkPageID,
112 'linkPageObjectID' => $this->linkPageObjectID ?: 0,
113 'externalURL' => $this->externalURL
114 ];
115 if ($this->boxControllerID) {
116 $data['objectTypeID'] = $this->boxControllerID;
117 }
118
119 $this->objectAction = new BoxAction([$this->box], 'update', ['data' => array_merge($this->additionalFields, $data), 'content' => $content, 'pageIDs' => $this->pageIDs]);
120 $this->objectAction->executeAction();
121
122 // delete old conditions
123 if ($this->box->objectTypeID) {
124 $className = ObjectTypeCache::getInstance()->getObjectType($this->box->objectTypeID)->className;
125
126 /** @var IConditionBoxController $oldController */
127 $oldController = new $className;
128
129 /** @noinspection PhpUndefinedMethodInspection */
130 if ($oldController instanceof IConditionBoxController && $oldController->getConditionDefinition() && (!$this->boxController || (!($this->boxController->getProcessor() instanceof IConditionBoxController)) || !$this->boxController->getProcessor()->getConditionDefinition())) {
131 ConditionHandler::getInstance()->deleteConditions($oldController->getConditionDefinition(), [$this->box->boxID]);
132 }
133 }
134
135 if ($this->boxController) {
136 // pass updated box to box controller as in `BoxAddForm::save()`
137 $box = new Box($this->box->boxID);
138 if ($this->boxController->getProcessor() instanceof IConditionBoxController) {
139 $this->boxController->getProcessor()->setBox($box, false);
140 }
141 else {
142 $this->boxController->getProcessor()->setBox($box);
143 }
144
145 $this->boxController->getProcessor()->saveAdditionalData();
146 }
147
148 SimpleAclHandler::getInstance()->setValues('com.woltlab.wcf.box', $this->box->boxID, $this->aclValues);
149
150 // call saved event
151 $this->saved();
152
153 // show success message
154 WCF::getTPL()->assign('success', true);
155 }
156
157 /**
158 * @inheritDoc
159 */
160 public function readData() {
161 if (!empty($_POST) && !WCF::getSession()->getPermission('admin.content.cms.canUseMedia')) {
162 foreach ($this->box->getBoxContents() as $languageID => $content) {
163 $this->imageID[$languageID] = $content->imageID;
164 }
165
166 $this->readBoxImages();
167 }
168
169 parent::readData();
170
171 if (empty($_POST)) {
172 $this->name = $this->box->name;
173 $this->boxType = $this->box->boxType;
174 $this->position = $this->box->position;
175 $this->showOrder = $this->box->showOrder;
176 $this->cssClassName = $this->box->cssClassName;
177 $this->boxControllerID = $this->box->objectTypeID;
178 if ($this->box->showHeader) $this->showHeader = 1;
179 else $this->showHeader = 0;
180 if ($this->box->visibleEverywhere) $this->visibleEverywhere = 1;
181 else $this->visibleEverywhere = 0;
182 $this->pageIDs = $this->box->getPageIDs();
183 $this->linkPageID = $this->box->linkPageID;
184 $this->linkPageObjectID = $this->box->linkPageObjectID;
185 $this->externalURL = $this->box->externalURL;
186 if ($this->linkPageID) $this->linkType = 'internal';
187 if ($this->externalURL) $this->linkType = 'external';
188
189 foreach ($this->box->getBoxContents() as $languageID => $content) {
190 $this->title[$languageID] = $content->title;
191 $this->content[$languageID] = $content->content;
192 $this->imageID[$languageID] = $content->imageID;
193 }
194
195 if ($this->boxControllerID) {
196 $this->boxController = ObjectTypeCache::getInstance()->getObjectType($this->boxControllerID);
197 if ($this->boxController->getProcessor() instanceof IConditionBoxController) {
198 $this->boxController->getProcessor()->setBox($this->box);
199 }
200 }
201
202 $this->aclValues = SimpleAclHandler::getInstance()->getValues('com.woltlab.wcf.box', $this->box->boxID);
203
204 $this->readBoxImages();
205 }
206 }
207
208 /**
209 * @inheritDoc
210 */
211 public function assignVariables() {
212 parent::assignVariables();
213
214 WCF::getTPL()->assign([
215 'action' => 'edit',
216 'boxID' => $this->boxID,
217 'box' => $this->box,
218 'lastVersion' => VersionTracker::getInstance()->getLastVersion('com.woltlab.wcf.box', $this->boxID)
219 ]);
220 }
221 }