6531cc9f5aaad2dcd3087aa7807ba262b8e5f04b
[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\Messaging\FlashMessage;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 // set default TCA values specific for the user
104 $TCAdefaultOverride = $this->getBackendUser()->getTSConfigProp('TCAdefaults');
105 if (is_array($TCAdefaultOverride)) {
106 $dataHandler->setDefaultsFromUserTS($TCAdefaultOverride);
107 }
108 $dataHandler->start($data, array());
109 $dataHandler->process_datamap();
110 BackendUtility::setUpdateSignal('updatePageTree');
111 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_create'));
112 } else {
113 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_noCreate'), FlashMessage::ERROR);
114 }
115 $theCode .= $flashMessage->render();
116 // Display result:
117 $menuItems = $pageRepository->getMenu($this->pObj->id, '*', 'sorting', '', false);
118 $lines = array();
119 foreach ($menuItems as $record) {
120 BackendUtility::workspaceOL('pages', $record);
121 if (is_array($record)) {
122 $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>';
123 }
124 }
125 $theCode .= '<div class="form-group"><h4>' . $this->getLanguageService()->getLL('wiz_newPages_currentMenu') . '</h4>' . implode('<br />', $lines) . '</div>';
126 } else {
127 // Display create form
128 $this->typeSelectHtml = $this->getTypeSelectHtml();
129 $tableData = array();
130 for ($a = 0; $a < 5; $a++) {
131 $tableData[] = $this->getFormLine($a);
132 }
133 $theCode .= '
134 <h4>' . $this->getLanguageService()->getLL('wiz_newPages') . ':</h4>
135 <div class="form-group t3js-wizardcrpages-container">
136 ' . implode(LF, $tableData) . '
137 </div>
138 <div class="form-group">
139 <input class="btn btn-default t3js-wizardcrpages-createnewfields" type="button" value="' . $this->getLanguageService()->getLL('wiz_newPages_addMoreLines') . '" />
140 </div>
141 <div class="form-group">
142 <div class="checkbox">
143 <label for="createInListEnd">
144 <input type="checkbox" name="createInListEnd" id="createInListEnd" value="1" />
145 ' . $this->getLanguageService()->getLL('wiz_newPages_listEnd') . '
146 </label>
147 </div>
148 <div class="checkbox">
149 <label for="hidePages">
150 <input type="checkbox" name="hidePages" id="hidePages" value="1" />
151 ' . $this->getLanguageService()->getLL('wiz_newPages_hidePages') . '
152 </label>
153 </div>
154 <div class="checkbox">
155 <label for="hidePagesInMenus">
156 <input type="checkbox" name="hidePagesInMenus" id="hidePagesInMenus" value="1" />
157 ' . $this->getLanguageService()->getLL('wiz_newPages_hidePagesInMenus') . '
158 </label>
159 </div>
160 </div>
161 <div class="form-group">
162 <input class="btn btn-default" type="submit" name="create" value="' . $this->getLanguageService()->getLL('wiz_newPages_lCreate') . '" />
163 <input class="btn btn-default" type="reset" value="' . $this->getLanguageService()->getLL('wiz_newPages_lReset') . '" />
164 </div>';
165
166 $this->getPageRenderer()->loadJquery();
167 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/WizardCrpages/WizardCreatePages');
168 // Add inline code
169 $inlineJavaScriptCode = 'var tpl = "' . addslashes(str_replace(array(LF, TAB), array('', ''), $this->getFormLine('#'))) . '", i, line, div, bg, label;';
170 $this->getPageRenderer()->addJsInlineCode('wizard_crpages', $inlineJavaScriptCode);
171 }
172 } else {
173 $theCode .= GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_errorMsg1'), FlashMessage::ERROR)->render();
174 }
175 // CSH
176 $theCode .= BackendUtility::cshItem('_MOD_web_func', 'tx_wizardcrpages', null, '<div class="t3-help">|</div>');
177 $out = $this->pObj->doc->header($this->getLanguageService()->getLL('wiz_crMany'));
178 $out .= '<div>' . $theCode . '</div>';
179 return $out;
180 }
181
182 /**
183 * Return one line in the form
184 *
185 * @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)
186 * @return string HTML code for one input line for one new page
187 */
188 protected function getFormLine($index)
189 {
190 if (is_numeric($index)) {
191 $label = $index + 1;
192 } else {
193 // used as template for JavaScript
194 $index = '{0}';
195 $label = '{1}';
196 }
197 $content = '' .
198 '<div class="form-section" id="form-line-' . $index . '">' .
199 '<div class="row">' .
200 '<div class="form-group col-sm-6">' .
201 '<label for="page_new_' . $index . '">' .
202 $this->getLanguageService()->getLL('wiz_newPages_page') . ' ' . $label . ':' .
203 '</label>' .
204 '<div class="form-control-wrap">' .
205 '<input class="form-control" type="text" id="page_new_' . $index . '" name="data[pages][NEW' . $index . '][title]" />' .
206 '</div>' .
207 '</div>' .
208 '<div class="form-group col-sm-6">' .
209 '<label>' .
210 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.type') .
211 '</label>' .
212 '<div class="form-control-wrap">' .
213 '<div class="input-group">' .
214 '<div id="page_new_icon_' . $index . '" class="input-group-addon input-group-icon">' .
215 $this->iconFactory->getIconForRecord('pages', array(), Icon::SIZE_SMALL)->render() .
216 '</div>' .
217 '<select class="form-control form-control-adapt t3js-wizardcrpages-select-doktype" name="data[pages][NEW' . $index . '][doktype]" data-target="#page_new_icon_' . $index . '">' .
218 $this->typeSelectHtml .
219 '</select>' .
220 '</div>' .
221 '</div>' .
222 '</div>' .
223 '</div>' .
224 '</div>';
225 return $content;
226 }
227
228 /**
229 * Get type selector
230 *
231 * @return string
232 */
233 protected function getTypeSelectHtml()
234 {
235 $content = '';
236
237 // find all available doktypes for the current user
238 $types = $GLOBALS['PAGES_TYPES'];
239 unset($types['default']);
240 $types = array_keys($types);
241 $types[] = PageRepository::DOKTYPE_DEFAULT;
242 if (!$this->getBackendUser()->isAdmin() && isset($this->getBackendUser()->groupData['pagetypes_select'])) {
243 $types = GeneralUtility::trimExplode(',', $this->getBackendUser()->groupData['pagetypes_select'], true);
244 }
245 $removeItems = isset($this->pagesTsConfig['doktype.']['removeItems']) ? GeneralUtility::trimExplode(',', $this->pagesTsConfig['doktype.']['removeItems'], true) : array();
246 $allowedDoktypes = array_diff($types, $removeItems);
247
248 // fetch all doktypes in the TCA
249 $availableDoktypes = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
250
251 // sort by group and allowedDoktypes
252 $groupedData = array();
253 foreach ($availableDoktypes as $doktypeData) {
254 // if it is a group, save the group label for the children underneath
255 if ($doktypeData[1] == '--div--') {
256 $groupLabel = $doktypeData[0];
257 } else {
258 if (in_array($doktypeData[1], $allowedDoktypes)) {
259 $groupedData[$groupLabel][] = $doktypeData;
260 }
261 }
262 }
263
264 // render the HTML
265 foreach ($groupedData as $groupLabel => $items) {
266 $groupContent = '';
267 foreach ($items as $item) {
268 $label = $this->getLanguageService()->sL($item[0], true);
269 $value = $item[1];
270 $icon = !empty($item[2]) ? FormEngineUtility::getIconHtml($item[2], $label, $label) : '';
271 $groupContent .= '<option value="' . htmlspecialchars($value) . '" data-icon="' . htmlspecialchars($icon) . '">' . $label . '</option>';
272 }
273 $groupLabel = $this->getLanguageService()->sL($groupLabel, true);
274 $content .= '<optgroup label="' . $groupLabel . '">' . $groupContent . '</optgroup>';
275 }
276
277 return $content;
278 }
279
280 /**
281 * Returns LanguageService
282 *
283 * @return \TYPO3\CMS\Lang\LanguageService
284 */
285 protected function getLanguageService()
286 {
287 return $GLOBALS['LANG'];
288 }
289
290 /**
291 * Returns the current BE user.
292 *
293 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
294 */
295 protected function getBackendUser()
296 {
297 return $GLOBALS['BE_USER'];
298 }
299 }