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