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