1c84f97454a1202ea53503d982be8313e8f3955e
[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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Frontend\Page\PageRepository;
21
22 /**
23 * Creates the "Create pages" wizard
24 *
25 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
26 */
27 class CreatePagesWizardModuleFunctionController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
28
29 /**
30 * Complete tsConfig
31 *
32 * @var array
33 */
34 protected $tsConfig = array();
35
36 /**
37 * Part of tsConfig with TCEFORM.pages. settings
38 *
39 * @var array
40 */
41 protected $pagesTsConfig = array();
42
43 /**
44 * @var string
45 */
46 protected $backPath = '';
47
48 /**
49 * The type select HTML
50 */
51 protected $typeSelectHtml = '';
52
53 /**
54 * Main function creating the content for the module.
55 *
56 * @return string HTML content for the module, actually a "section" made through the parent object in $this->pObj
57 */
58 public function main() {
59 $this->getLanguageService()->includeLLFile('EXT:wizard_crpages/locallang.xlf');
60 $theCode = '';
61 $this->tsConfig = BackendUtility::getPagesTSconfig($this->pObj->id);
62 $this->pagesTsConfig = isset($this->tsConfig['TCEFORM.']['pages.']) ? $this->tsConfig['TCEFORM.']['pages.'] : array();
63 $this->backPath = $GLOBALS['BACK_PATH'];
64
65 // Create new pages here?
66 $pageRecord = BackendUtility::getRecord('pages', $this->pObj->id, 'uid', ' AND ' . $this->getBackendUser()->getPagePermsClause(8));
67 $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
68 $menuItems = $pageRepository->getMenu($this->pObj->id, '*', 'sorting', '', FALSE);
69 if (is_array($pageRecord)) {
70 $data = GeneralUtility::_GP('data');
71 if (is_array($data['pages'])) {
72 if (GeneralUtility::_GP('createInListEnd')) {
73 $endI = end($menuItems);
74 $thePid = -(int)$endI['uid'];
75 if (!$thePid) {
76 $thePid = $this->pObj->id;
77 }
78 } else {
79 $thePid = $this->pObj->id;
80 }
81 $firstRecord = TRUE;
82 $previousIdentifier = '';
83 foreach ($data['pages'] as $identifier => $dat) {
84 if (!trim($dat['title'])) {
85 unset($data['pages'][$identifier]);
86 } else {
87 $data['pages'][$identifier]['hidden'] = GeneralUtility::_GP('hidePages') ? 1 : 0;
88 $data['pages'][$identifier]['nav_hide'] = GeneralUtility::_GP('hidePagesInMenus') ? 1 : 0;
89 if ($firstRecord) {
90 $firstRecord = FALSE;
91 $data['pages'][$identifier]['pid'] = $thePid;
92 } else {
93 $data['pages'][$identifier]['pid'] = '-' . $previousIdentifier;
94 }
95 $previousIdentifier = $identifier;
96 }
97 }
98 if (!empty($data['pages'])) {
99 reset($data);
100 $dataHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
101 $dataHandler->stripslashes_values = 0;
102 // set default TCA values specific for the user
103 $TCAdefaultOverride = $this->getBackendUser()->getTSConfigProp('TCAdefaults');
104 if (is_array($TCAdefaultOverride)) {
105 $dataHandler->setDefaultsFromUserTS($TCAdefaultOverride);
106 }
107 $dataHandler->start($data, array());
108 $dataHandler->process_datamap();
109 BackendUtility::setUpdateSignal('updatePageTree');
110 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_create'));
111 } else {
112 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_noCreate'), FlashMessage::ERROR);
113 }
114 $theCode .= $flashMessage->render();
115 // Display result:
116 $menuItems = $pageRepository->getMenu($this->pObj->id, '*', 'sorting', '', FALSE);
117 $lines = array();
118 foreach ($menuItems as $record) {
119 BackendUtility::workspaceOL('pages', $record);
120 if (is_array($record)) {
121 $lines[] = '<nobr>' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('pages', $record, array('title' => BackendUtility::titleAttribForPages($record, '', FALSE))) . htmlspecialchars(GeneralUtility::fixed_lgd_cs($record['title'], $this->getBackendUser()->uc['titleLen'])) . '</nobr>';
122 }
123 }
124 $theCode .= '<h4>' . $this->getLanguageService()->getLL('wiz_newPages_currentMenu') . '</h4>' . implode('<br />', $lines);
125 } else {
126 // Display create form
127 $this->typeSelectHtml = $this->getTypeSelectHtml();
128 $lines = array();
129 $tableData = array();
130 for ($a = 0; $a < 9; $a++) {
131 $tableData[] = $this->getFormLine($a);
132 }
133 $lines[] = '<div class="table-fit"><table id="formFieldContainer" class="table table-striped table-hover"><tbody id="formFieldContainerBody">' . implode(LF, $tableData) . '</tbody></table></div>';
134 $theCode .= '<h4>' . $this->getLanguageService()->getLL('wiz_newPages') . ':</h4>' . implode('', $lines) . '<input class="btn btn-default" type="button" id="createNewFormFields" value="' . $this->getLanguageService()->getLL('wiz_newPages_addMoreLines') . '" />';
135 $theCode .= '<div class="checkbox"><label for="createInListEnd"><input type="checkbox" name="createInListEnd" id="createInListEnd" value="1" />' . $this->getLanguageService()->getLL('wiz_newPages_listEnd') . '</label></div>';
136 $theCode .= '<div class="checkbox"><label for="hidePages"><input type="checkbox" name="hidePages" id="hidePages" value="1" />' . $this->getLanguageService()->getLL('wiz_newPages_hidePages') . '</label></div>';
137 $theCode .= '<div class="checkbox"><label for="hidePagesInMenus"><input type="checkbox" name="hidePagesInMenus" id="hidePagesInMenus" value="1" />' . $this->getLanguageService()->getLL('wiz_newPages_hidePagesInMenus') . '</label></div>';
138 $theCode .= '<input class="btn btn-default" type="submit" name="create" value="' . $this->getLanguageService()->getLL('wiz_newPages_lCreate') . '" /> <input class="btn btn-default" type="reset" value="' . $this->getLanguageService()->getLL('wiz_newPages_lReset') . '" />';
139
140 /** @var \TYPO3\CMS\Core\Page\PageRenderer $pageRenderer */
141 $pageRenderer = $GLOBALS['TBE_TEMPLATE']->getPageRenderer();
142 $pageRenderer->loadJquery();
143 $pageRenderer->loadRequireJsModule('TYPO3/CMS/WizardCrpages/WizardCreatePages');
144 // Add inline code
145 $inlineJavaScriptCode = 'var tpl = "' . addslashes(str_replace(array(LF, TAB), array('', ''), $this->getFormLine('#'))) . '", i, line, div, bg, label;';
146 $pageRenderer->addJsInlineCode('wizard_crpages', $inlineJavaScriptCode);
147 }
148 } else {
149 $theCode .= GeneralUtility::makeInstance(FlashMessage::class, '', $this->getLanguageService()->getLL('wiz_newPages_errorMsg1'), FlashMessage::ERROR)->render();
150 }
151 // CSH
152 $theCode .= BackendUtility::cshItem('_MOD_web_func', 'tx_wizardcrpages', NULL, '<div class="t3-help">|</div>');
153 $out = $this->pObj->doc->header($this->getLanguageService()->getLL('wiz_crMany'));
154 $out .= $this->pObj->doc->section('', $theCode, FALSE, TRUE);
155 return $out;
156 }
157
158 /**
159 * Return one line in the form
160 *
161 * @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)
162 * @return string HTML code for one input line for one new page
163 */
164 protected function getFormLine($index) {
165 if (is_numeric($index)) {
166 $label = $index + 1;
167 } else {
168 // used as template for JavaScript
169 $index = '{0}';
170 $label = '{1}';
171 }
172 $content = '<label for="page_new_' . $index . '"> ' . $this->getLanguageService()->getLL('wiz_newPages_page') . ' ' . $label;
173 $content .= ':&nbsp;</label>';
174 // Title
175 $content .= '<input type="text" id="page_new_' . $index . '" name="data[pages][NEW' . $index . '][title]"' . $this->pObj->doc->formWidth(35) . ' />&nbsp';
176 // type selector
177 $content .= '<span>' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.type') . '</span>';
178 $content .= '<select class="select icon-select" name="data[pages][NEW' . $index . '][doktype]" style="background: url(&quot;' . $this->backPath . 'sysext/t3skin/icons/gfx/i/pages.gif&quot;) no-repeat scroll 0% 50% rgb(255, 255, 255); padding: 1px 1px 1px 24px;">';
179 $content .= $this->typeSelectHtml;
180 $content .= '</select>';
181 return '<tr id="form-line-' . $index . '"><td>' . $content . '</td></tr>';
182 }
183
184 /**
185 * Get type selector
186 *
187 * @return string
188 */
189 protected function getTypeSelectHtml() {
190 $content = '';
191
192 // find all available doktypes for the current user
193 $types = $GLOBALS['PAGES_TYPES'];
194 unset($types['default']);
195 $types = array_keys($types);
196 $types[] = PageRepository::DOKTYPE_DEFAULT;
197 if (!$this->getBackendUser()->isAdmin() && isset($this->getBackendUser()->groupData['pagetypes_select'])) {
198 $types = GeneralUtility::trimExplode(',', $this->getBackendUser()->groupData['pagetypes_select'], TRUE);
199 }
200 $removeItems = isset($this->pagesTsConfig['doktype.']['removeItems']) ? GeneralUtility::trimExplode(',', $this->pagesTsConfig['doktype.']['removeItems'], TRUE) : array();
201 $allowedDoktypes = array_diff($types, $removeItems);
202
203 // fetch all doktypes in the TCA
204 $availableDoktypes = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
205
206 // sort by group and allowedDoktypes
207 $groupedData = array();
208 foreach ($availableDoktypes as $doktypeData) {
209 // if it is a group, save the group label for the children underneath
210 if ($doktypeData[1] == '--div--') {
211 $groupLabel = $doktypeData[0];
212 } else {
213 if (in_array($doktypeData[1], $allowedDoktypes)) {
214 $groupedData[$groupLabel][] = $doktypeData;
215 }
216 }
217 }
218
219 // render the HTML
220 foreach ($groupedData as $groupLabel => $items) {
221 $groupContent = '';
222 foreach ($items as $item) {
223 $label = $this->getLanguageService()->sL($item[0], TRUE);
224 $value = $item[1];
225 $icon = ($item[2] ? $this->getStyleAttributeWithIcon($item[2]) : '');
226 $groupContent .= '<option ' . $icon . ' value="' . htmlspecialchars($value) . '">' . $label . '</option>';
227 }
228 $groupLabel = $this->getLanguageService()->sL($groupLabel, TRUE);
229 $content .= '<optgroup class="c-divider" label="' . $groupLabel . '">' . $groupContent . '</optgroup>';
230 }
231
232 return $content;
233 }
234
235 /**
236 * Returns the style attribute for a select element with the icon in it
237 *
238 * @param string $src
239 * @return string
240 */
241 protected function getStyleAttributeWithIcon($src) {
242 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($this->backPath, 'gfx/' . $src, '', 1);
243 return 'style="background: #fff url(&quot;' . $icon. '&quot;) 0% 50% no-repeat; height: 16px; padding-top: 2px; padding-left: 22px;"';
244 }
245
246 /**
247 * Returns LanguageService
248 *
249 * @return \TYPO3\CMS\Lang\LanguageService
250 */
251 protected function getLanguageService() {
252 return $GLOBALS['LANG'];
253 }
254
255 /**
256 * Returns the current BE user.
257 *
258 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
259 */
260 protected function getBackendUser() {
261 return $GLOBALS['BE_USER'];
262 }
263 }