[TASK] Remove DocumentTemplate section in Wizard controllers
[Packages/TYPO3.CMS.git] / typo3 / sysext / wizard_crpages / Classes / Controller / CreatePagesWizardModuleFunctionController.php
1 <?php
2 namespace TYPO3\CMS\WizardCrpages\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Messaging\FlashMessage;
23 use TYPO3\CMS\Frontend\Page\PageRepository;
24
25 /**
26 * Creates the "Create pages" wizard
27 */
28 class CreatePagesWizardModuleFunctionController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule
29 {
30 /**
31 * Complete tsConfig
32 *
33 * @var array
34 */
35 protected $tsConfig = array();
36
37 /**
38 * Part of tsConfig with TCEFORM.pages. settings
39 *
40 * @var array
41 */
42 protected $pagesTsConfig = array();
43
44 /**
45 * The type select HTML
46 */
47 protected $typeSelectHtml = '';
48
49 /**
50 * @var IconFactory
51 */
52 protected $iconFactory;
53
54 /**
55 * Main function creating the content for the module.
56 *
57 * @return string HTML content for the module, actually a "section" made through the parent object in $this->pObj
58 */
59 public function main()
60 {
61 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
62 $this->getLanguageService()->includeLLFile('EXT:wizard_crpages/Resources/Private/Language/locallang.xlf');
63 $theCode = '';
64 $this->tsConfig = BackendUtility::getPagesTSconfig($this->pObj->id);
65 $this->pagesTsConfig = isset($this->tsConfig['TCEFORM.']['pages.']) ? $this->tsConfig['TCEFORM.']['pages.'] : array();
66
67 // Create new pages here?
68 $pageRecord = BackendUtility::getRecord('pages', $this->pObj->id, 'uid', ' AND ' . $this->getBackendUser()->getPagePermsClause(8));
69 $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
70 $menuItems = $pageRepository->getMenu($this->pObj->id, '*', 'sorting', '', false);
71 if (is_array($pageRecord)) {
72 $data = GeneralUtility::_GP('data');
73 if (is_array($data['pages'])) {
74 if (GeneralUtility::_GP('createInListEnd')) {
75 $endI = end($menuItems);
76 $thePid = -(int)$endI['uid'];
77 if (!$thePid) {
78 $thePid = $this->pObj->id;
79 }
80 } else {
81 $thePid = $this->pObj->id;
82 }
83 $firstRecord = true;
84 $previousIdentifier = '';
85 foreach ($data['pages'] as $identifier => $dat) {
86 if (!trim($dat['title'])) {
87 unset($data['pages'][$identifier]);
88 } else {
89 $data['pages'][$identifier]['hidden'] = GeneralUtility::_GP('hidePages') ? 1 : 0;
90 $data['pages'][$identifier]['nav_hide'] = GeneralUtility::_GP('hidePagesInMenus') ? 1 : 0;
91 if ($firstRecord) {
92 $firstRecord = false;
93 $data['pages'][$identifier]['pid'] = $thePid;
94 } else {
95 $data['pages'][$identifier]['pid'] = '-' . $previousIdentifier;
96 }
97 $previousIdentifier = $identifier;
98 }
99 }
100 if (!empty($data['pages'])) {
101 reset($data);
102 $dataHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
103 $dataHandler->stripslashes_values = 0;
104 // set default TCA values specific for the user
105 $TCAdefaultOverride = $this->getBackendUser()->getTSConfigProp('TCAdefaults');
106 if (is_array($TCAdefaultOverride)) {
107 $dataHandler->setDefaultsFromUserTS($TCAdefaultOverride);
108 }
109 $dataHandler->start($data, array());
110 $dataHandler->process_datamap();
111 BackendUtility::setUpdateSignal('updatePageTree');
112 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_create'));
113 } else {
114 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_noCreate'), FlashMessage::ERROR);
115 }
116 $theCode .= $flashMessage->render();
117 // Display result:
118 $menuItems = $pageRepository->getMenu($this->pObj->id, '*', 'sorting', '', false);
119 $lines = array();
120 foreach ($menuItems as $record) {
121 BackendUtility::workspaceOL('pages', $record);
122 if (is_array($record)) {
123 $lines[] = '<span class="text-nowrap" title="' . BackendUtility::titleAttribForPages($record, '', false) . '">' . $this->iconFactory->getIconForRecord('pages', $record, Icon::SIZE_SMALL)->render() . htmlspecialchars(GeneralUtility::fixed_lgd_cs($record['title'], $this->getBackendUser()->uc['titleLen'])) . '</span>';
124 }
125 }
126 $theCode .= '<h4>' . $this->getLanguageService()->getLL('wiz_newPages_currentMenu') . '</h4>' . implode('<br />', $lines);
127 } else {
128 // Display create form
129 $this->typeSelectHtml = $this->getTypeSelectHtml();
130 $tableData = array();
131 for ($a = 0; $a < 5; $a++) {
132 $tableData[] = $this->getFormLine($a);
133 }
134 $theCode .= '
135 <h4>' . $this->getLanguageService()->getLL('wiz_newPages') . ':</h4>
136 <div class="form-group t3js-wizardcrpages-container">
137 ' . implode(LF, $tableData) . '
138 </div>
139 <div class="form-group">
140 <input class="btn btn-default t3js-wizardcrpages-createnewfields" type="button" value="' . $this->getLanguageService()->getLL('wiz_newPages_addMoreLines') . '" />
141 </div>
142 <div class="form-group">
143 <div class="checkbox">
144 <label for="createInListEnd">
145 <input type="checkbox" name="createInListEnd" id="createInListEnd" value="1" />
146 ' . $this->getLanguageService()->getLL('wiz_newPages_listEnd') . '
147 </label>
148 </div>
149 <div class="checkbox">
150 <label for="hidePages">
151 <input type="checkbox" name="hidePages" id="hidePages" value="1" />
152 ' . $this->getLanguageService()->getLL('wiz_newPages_hidePages') . '
153 </label>
154 </div>
155 <div class="checkbox">
156 <label for="hidePagesInMenus">
157 <input type="checkbox" name="hidePagesInMenus" id="hidePagesInMenus" value="1" />
158 ' . $this->getLanguageService()->getLL('wiz_newPages_hidePagesInMenus') . '
159 </label>
160 </div>
161 </div>
162 <div class="form-group">
163 <input class="btn btn-default" type="submit" name="create" value="' . $this->getLanguageService()->getLL('wiz_newPages_lCreate') . '" />
164 <input class="btn btn-default" type="reset" value="' . $this->getLanguageService()->getLL('wiz_newPages_lReset') . '" />
165 </div>';
166
167 $this->getPageRenderer()->loadJquery();
168 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/WizardCrpages/WizardCreatePages');
169 // Add inline code
170 $inlineJavaScriptCode = 'var tpl = "' . addslashes(str_replace(array(LF, TAB), array('', ''), $this->getFormLine('#'))) . '", i, line, div, bg, label;';
171 $this->getPageRenderer()->addJsInlineCode('wizard_crpages', $inlineJavaScriptCode);
172 }
173 } else {
174 $theCode .= GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_errorMsg1'), FlashMessage::ERROR)->render();
175 }
176 // CSH
177 $theCode .= BackendUtility::cshItem('_MOD_web_func', 'tx_wizardcrpages', null, '<div class="t3-help">|</div>');
178 $out = $this->pObj->doc->header($this->getLanguageService()->getLL('wiz_crMany'));
179 $out .= '<div>' . $theCode . '</div>';
180 return $out;
181 }
182
183 /**
184 * Return one line in the form
185 *
186 * @param int|string $index An integer: the line counter for which to create the line. Use "#" to create an template for javascript (used by ExtJS)
187 * @return string HTML code for one input line for one new page
188 */
189 protected function getFormLine($index)
190 {
191 if (is_numeric($index)) {
192 $label = $index + 1;
193 } else {
194 // used as template for JavaScript
195 $index = '{0}';
196 $label = '{1}';
197 }
198 $content = '' .
199 '<div class="form-section" id="form-line-' . $index . '">' .
200 '<div class="row">' .
201 '<div class="form-group col-sm-6">' .
202 '<label for="page_new_' . $index . '">' .
203 $this->getLanguageService()->getLL('wiz_newPages_page') . ' ' . $label . ':' .
204 '</label>' .
205 '<div class="form-control-wrap">' .
206 '<input class="form-control" type="text" id="page_new_' . $index . '" name="data[pages][NEW' . $index . '][title]" />' .
207 '</div>' .
208 '</div>' .
209 '<div class="form-group col-sm-6">' .
210 '<label>' .
211 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.type') .
212 '</label>' .
213 '<div class="form-control-wrap">' .
214 '<div class="input-group">' .
215 '<div id="page_new_icon_' . $index . '" class="input-group-addon input-group-icon">' .
216 $this->iconFactory->getIconForRecord('pages', array(), Icon::SIZE_SMALL)->render() .
217 '</div>' .
218 '<select class="form-control form-control-adapt t3js-wizardcrpages-select-doktype" name="data[pages][NEW' . $index . '][doktype]" data-target="#page_new_icon_' . $index . '">' .
219 $this->typeSelectHtml .
220 '</select>' .
221 '</div>' .
222 '</div>' .
223 '</div>' .
224 '</div>' .
225 '</div>';
226 return $content;
227 }
228
229 /**
230 * Get type selector
231 *
232 * @return string
233 */
234 protected function getTypeSelectHtml()
235 {
236 $content = '';
237
238 // find all available doktypes for the current user
239 $types = $GLOBALS['PAGES_TYPES'];
240 unset($types['default']);
241 $types = array_keys($types);
242 $types[] = PageRepository::DOKTYPE_DEFAULT;
243 if (!$this->getBackendUser()->isAdmin() && isset($this->getBackendUser()->groupData['pagetypes_select'])) {
244 $types = GeneralUtility::trimExplode(',', $this->getBackendUser()->groupData['pagetypes_select'], true);
245 }
246 $removeItems = isset($this->pagesTsConfig['doktype.']['removeItems']) ? GeneralUtility::trimExplode(',', $this->pagesTsConfig['doktype.']['removeItems'], true) : array();
247 $allowedDoktypes = array_diff($types, $removeItems);
248
249 // fetch all doktypes in the TCA
250 $availableDoktypes = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
251
252 // sort by group and allowedDoktypes
253 $groupedData = array();
254 foreach ($availableDoktypes as $doktypeData) {
255 // if it is a group, save the group label for the children underneath
256 if ($doktypeData[1] == '--div--') {
257 $groupLabel = $doktypeData[0];
258 } else {
259 if (in_array($doktypeData[1], $allowedDoktypes)) {
260 $groupedData[$groupLabel][] = $doktypeData;
261 }
262 }
263 }
264
265 // render the HTML
266 foreach ($groupedData as $groupLabel => $items) {
267 $groupContent = '';
268 foreach ($items as $item) {
269 $label = $this->getLanguageService()->sL($item[0], true);
270 $value = $item[1];
271 $icon = !empty($item[2]) ? FormEngineUtility::getIconHtml($item[2], $label, $label) : '';
272 $groupContent .= '<option value="' . htmlspecialchars($value) . '" data-icon="' . htmlspecialchars($icon) . '">' . $label . '</option>';
273 }
274 $groupLabel = $this->getLanguageService()->sL($groupLabel, true);
275 $content .= '<optgroup label="' . $groupLabel . '">' . $groupContent . '</optgroup>';
276 }
277
278 return $content;
279 }
280
281 /**
282 * Returns LanguageService
283 *
284 * @return \TYPO3\CMS\Lang\LanguageService
285 */
286 protected function getLanguageService()
287 {
288 return $GLOBALS['LANG'];
289 }
290
291 /**
292 * Returns the current BE user.
293 *
294 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
295 */
296 protected function getBackendUser()
297 {
298 return $GLOBALS['BE_USER'];
299 }
300 }