991998de9169530fb2e39dfc84b406689775a7ef
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / NewRecordController.php
1 <?php
2 namespace TYPO3\CMS\Backend\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\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Backend\Template\DocumentTemplate;
23 use TYPO3\CMS\Backend\Tree\View\PagePositionMap;
24 use TYPO3\CMS\Backend\Tree\View\NewRecordPageTreeView;
25 use TYPO3\CMS\Core\Utility\HttpUtility;
26 use TYPO3\CMS\Frontend\Page\PageRepository;
27
28
29 /**
30 * Script class for 'db_new'
31 */
32 class NewRecordController {
33
34 /**
35 * @var array
36 */
37 public $pageinfo;
38
39 /**
40 * @var array
41 */
42 public $pidInfo;
43
44 /**
45 * @var array
46 */
47 protected $newRecordSortList;
48
49 /**
50 * @var int
51 */
52 public $newPagesInto;
53
54 /**
55 * @var int
56 */
57 public $newContentInto;
58
59 /**
60 * @var int
61 */
62 public $newPagesAfter;
63
64 /**
65 * Determines, whether "Select Position" for new page should be shown
66 *
67 * @var bool
68 */
69 protected $newPagesSelectPosition = TRUE;
70
71 /**
72 * @var array
73 */
74 public $web_list_modTSconfig;
75
76 /**
77 * @var array
78 */
79 public $allowedNewTables;
80
81 /**
82 * @var array
83 */
84 public $deniedNewTables;
85
86 /**
87 * @var array
88 */
89 public $web_list_modTSconfig_pid;
90
91 /**
92 * @var array
93 */
94 public $allowedNewTables_pid;
95
96 /**
97 * @var array
98 */
99 public $deniedNewTables_pid;
100
101 /**
102 * @var string
103 */
104 public $code;
105
106 /**
107 * @var string
108 */
109 public $R_URI;
110
111 /**
112 * @var int
113 */
114 public $id;
115
116 /**
117 * @var string
118 */
119 public $returnUrl;
120
121 /**
122 * pagesOnly flag.
123 *
124 * @var int
125 */
126 public $pagesOnly;
127
128 /**
129 * @var string
130 */
131 public $perms_clause;
132
133 /**
134 * Document template object
135 *
136 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
137 */
138 public $doc;
139
140 /**
141 * Accumulated HTML output
142 *
143 * @var string
144 */
145 public $content;
146
147 /**
148 * @var array
149 */
150 public $tRows;
151
152 /**
153 * Constructor
154 */
155 public function __construct() {
156 $GLOBALS['SOBE'] = $this;
157 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_misc.xlf');
158 $GLOBALS['BACK_PATH'] = '';
159
160 $this->init();
161 }
162
163 /**
164 * Constructor function for the class
165 *
166 * @return void
167 */
168 protected function init() {
169 $beUser = $this->getBackendUserAuthentication();
170 // Page-selection permission clause (reading)
171 $this->perms_clause = $beUser->getPagePermsClause(1);
172 // This will hide records from display - it has nothing to do with user rights!!
173 if ($pidList = $beUser->getTSConfigVal('options.hideRecords.pages')) {
174 if ($pidList = $this->getDatabaseConnection()->cleanIntList($pidList)) {
175 $this->perms_clause .= ' AND pages.uid NOT IN (' . $pidList . ')';
176 }
177 }
178 // Setting GPvars:
179 // The page id to operate from
180 $this->id = (int)GeneralUtility::_GP('id');
181 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
182 $this->pagesOnly = GeneralUtility::_GP('pagesOnly');
183 // Create instance of template class for output
184 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
185 $this->doc->backPath = $GLOBALS['BACK_PATH'];
186 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/db_new.html');
187 $this->doc->JScode = '';
188 // Setting up the context sensitive menu:
189 $this->doc->getContextMenuCode();
190 // Creating content
191 $this->content = '';
192 $this->content .= $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:db_new.php.pagetitle'));
193 // Id a positive id is supplied, ask for the page record with permission information contained:
194 if ($this->id > 0) {
195 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
196 }
197 // If a page-record was returned, the user had read-access to the page.
198 if ($this->pageinfo['uid']) {
199 // Get record of parent page
200 $this->pidInfo = BackendUtility::getRecord('pages', $this->pageinfo['pid']);
201 // Checking the permissions for the user with regard to the parent page: Can he create new pages, new content record, new page after?
202 if ($beUser->doesUserHaveAccess($this->pageinfo, 8)) {
203 $this->newPagesInto = 1;
204 }
205 if ($beUser->doesUserHaveAccess($this->pageinfo, 16)) {
206 $this->newContentInto = 1;
207 }
208 if (($beUser->isAdmin() || is_array($this->pidInfo)) && $beUser->doesUserHaveAccess($this->pidInfo, 8)) {
209 $this->newPagesAfter = 1;
210 }
211 } elseif ($beUser->isAdmin()) {
212 // Admins can do it all
213 $this->newPagesInto = 1;
214 $this->newContentInto = 1;
215 $this->newPagesAfter = 0;
216 } else {
217 // People with no permission can do nothing
218 $this->newPagesInto = 0;
219 $this->newContentInto = 0;
220 $this->newPagesAfter = 0;
221 }
222 }
223
224 /**
225 * Main processing, creating the list of new record tables to select from
226 *
227 * @return void
228 */
229 public function main() {
230 // If there was a page - or if the user is admin (admins has access to the root) we proceed:
231 if ($this->pageinfo['uid'] || $this->getBackendUserAuthentication()->isAdmin()) {
232 // Acquiring TSconfig for this module/current page:
233 $this->web_list_modTSconfig = BackendUtility::getModTSconfig($this->pageinfo['uid'], 'mod.web_list');
234 $this->allowedNewTables = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig['properties']['allowedNewTables'], TRUE);
235 $this->deniedNewTables = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig['properties']['deniedNewTables'], TRUE);
236 // Acquiring TSconfig for this module/parent page:
237 $this->web_list_modTSconfig_pid = BackendUtility::getModTSconfig($this->pageinfo['pid'], 'mod.web_list');
238 $this->allowedNewTables_pid = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig_pid['properties']['allowedNewTables'], TRUE);
239 $this->deniedNewTables_pid = GeneralUtility::trimExplode(',', $this->web_list_modTSconfig_pid['properties']['deniedNewTables'], TRUE);
240 // More init:
241 if (!$this->showNewRecLink('pages')) {
242 $this->newPagesInto = 0;
243 }
244 if (!$this->showNewRecLink('pages', $this->allowedNewTables_pid, $this->deniedNewTables_pid)) {
245 $this->newPagesAfter = 0;
246 }
247 // Set header-HTML and return_url
248 if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
249 $iconImgTag = IconUtility::getSpriteIconForRecord('pages', $this->pageinfo, array('title' => htmlspecialchars($this->pageinfo['_thePath'])));
250 $title = strip_tags($this->pageinfo[$GLOBALS['TCA']['pages']['ctrl']['label']]);
251 } else {
252 $iconImgTag = IconUtility::getSpriteIcon('apps-pagetree-root', array('title' => htmlspecialchars($this->pageinfo['_thePath'])));
253 $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
254 }
255 $this->code = '<span class="typo3-moduleHeader">' . $this->doc->wrapClickMenuOnIcon($iconImgTag, 'pages', $this->pageinfo['uid']) . htmlspecialchars(GeneralUtility::fixed_lgd_cs($title, 45)) . '</span><br />';
256 $this->R_URI = $this->returnUrl;
257 // GENERATE the HTML-output depending on mode (pagesOnly is the page wizard)
258 // Regular new element:
259 if (!$this->pagesOnly) {
260 $this->regularNew();
261 } elseif ($this->showNewRecLink('pages')) {
262 // Pages only wizard
263 $this->pagesOnly();
264 }
265 // Add all the content to an output section
266 $this->content .= $this->doc->section('', $this->code);
267 // Setting up the buttons and markers for docheader
268 $docHeaderButtons = $this->getButtons();
269 $markers['CSH'] = $docHeaderButtons['csh'];
270 $markers['CONTENT'] = $this->content;
271 // Build the <body> for the module
272 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:db_new.php.pagetitle'));
273 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
274 $this->content .= $this->doc->endPage();
275 $this->content = $this->doc->insertStylesAndJS($this->content);
276 }
277 }
278
279 /**
280 * Create the panel of buttons for submitting the form or otherwise perform operations.
281 *
282 * @return array All available buttons as an assoc. array
283 */
284 protected function getButtons() {
285 $buttons = array(
286 'csh' => '',
287 'back' => '',
288 'view' => '',
289 'new_page' => ''
290 );
291 // Regular new element:
292 if (!$this->pagesOnly) {
293 // New page
294 if ($this->showNewRecLink('pages')) {
295 $buttons['new_page'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('pagesOnly' => '1'))) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newPage', TRUE) . '">' . IconUtility::getSpriteIcon('actions-page-new') . '</a>';
296 }
297 // CSH
298 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_regular');
299 } elseif ($this->showNewRecLink('pages')) {
300 // Pages only wizard
301 // CSH
302 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'new_pages');
303 }
304 // Back
305 if ($this->R_URI) {
306 $buttons['back'] = '<a href="' . htmlspecialchars($this->R_URI) . '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . IconUtility::getSpriteIcon('actions-view-go-back') . '</a>';
307 }
308 if (is_array($this->pageinfo) && $this->pageinfo['uid']) {
309 // View
310 $pagesTSconfig = BackendUtility::getPagesTSconfig($this->pageinfo['uid']);
311 if (isset($pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) {
312 $excludeDokTypes = GeneralUtility::intExplode(',', $pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'], TRUE);
313 } else {
314 // exclude sysfolders and recycler by default
315 $excludeDokTypes = array(PageRepository::DOKTYPE_RECYCLER, PageRepository::DOKTYPE_SYSFOLDER, PageRepository::DOKTYPE_SPACER);
316 }
317 if (!in_array((int)$this->pageinfo['doktype'], $excludeDokTypes, TRUE)) {
318 $buttons['view'] = '<a href="#" onclick="' . htmlspecialchars(BackendUtility::viewOnClick($this->pageinfo['uid'], $this->doc->backPath, BackendUtility::BEgetRootLine($this->pageinfo['uid']))) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.showPage', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-view') . '</a>';
319 }
320 }
321 return $buttons;
322 }
323
324 /**
325 * Creates the position map for pages wizard
326 *
327 * @return void
328 */
329 public function pagesOnly() {
330 $numberOfPages = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'pages', '1=1' . BackendUtility::deleteClause('pages'));
331 if ($numberOfPages > 0) {
332 $this->code .= '
333 <h3>' . htmlspecialchars($this->getLanguageService()->getLL('selectPosition')) . ':</h3>
334 ';
335 $positionMap = GeneralUtility::makeInstance(PagePositionMap::class, NewRecordPageTreeView::class);
336 /** @var $positionMap \TYPO3\CMS\Backend\Tree\View\PagePositionMap */
337 $this->code .= $positionMap->positionTree($this->id, $this->pageinfo, $this->perms_clause, $this->R_URI);
338 } else {
339 // No pages yet, no need to prompt for position, redirect to page creation.
340 $javascript = stripslashes(BackendUtility::editOnClick('&edit[pages][0]=new&returnNewPageId=1', '', BackendUtility::getModuleUrl('db_new', array('id' => $this->id, 'pagesOnly' => '1'))));
341 $startPos = strpos($javascript, 'href=\'') + 6;
342 $endPos = strpos($javascript, '\';');
343 $url = substr($javascript, $startPos, $endPos - $startPos);
344 @ob_end_clean();
345 HttpUtility::redirect($url);
346 }
347 }
348
349 /**
350 * Create a regular new element (pages and records)
351 *
352 * @return void
353 */
354 public function regularNew() {
355 $lang = $this->getLanguageService();
356 // Initialize array for accumulating table rows:
357 $this->tRows = array();
358 // Get TSconfig for current page
359 $pageTS = BackendUtility::getPagesTSconfig($this->id);
360 // Finish initializing new pages options with TSconfig
361 // Each new page option may be hidden by TSconfig
362 // Enabled option for the position of a new page
363 $this->newPagesSelectPosition = !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageSelectPosition']);
364 // Pseudo-boolean (0/1) for backward compatibility
365 $displayNewPagesIntoLink = $this->newPagesInto && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageInside']) ? 1 : 0;
366 $displayNewPagesAfterLink = $this->newPagesAfter && !empty($pageTS['mod.']['wizards.']['newRecord.']['pages.']['show.']['pageAfter']) ? 1 : 0;
367 // Slight spacer from header:
368 $this->code .= '';
369 // New Page
370 $table = 'pages';
371 $v = $GLOBALS['TCA'][$table];
372 $pageIcon = IconUtility::getSpriteIconForRecord($table, array());
373 $newPageIcon = IconUtility::getSpriteIcon('actions-page-new');
374 $rowContent = '';
375 // New pages INSIDE this pages
376 $newPageLinks = array();
377 if ($displayNewPagesIntoLink && $this->isTableAllowedForThisPage($this->pageinfo, 'pages') && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') && $this->getBackendUserAuthentication()->workspaceCreateNewRecord(($this->pageinfo['_ORIG_uid'] ?: $this->id), 'pages')) {
378 // Create link to new page inside:
379 $newPageLinks[] = $this->linkWrap(IconUtility::getSpriteIconForRecord($table, array()) . $lang->sL($v['ctrl']['title'], TRUE) . ' (' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:db_new.php.inside', TRUE) . ')', $table, $this->id);
380 }
381 // New pages AFTER this pages
382 if ($displayNewPagesAfterLink && $this->isTableAllowedForThisPage($this->pidInfo, 'pages') && $this->getBackendUserAuthentication()->check('tables_modify', 'pages') && $this->getBackendUserAuthentication()->workspaceCreateNewRecord($this->pidInfo['uid'], 'pages')) {
383 $newPageLinks[] = $this->linkWrap($pageIcon . $lang->sL($v['ctrl']['title'], TRUE) . ' (' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:db_new.php.after', TRUE) . ')', 'pages', -$this->id);
384 }
385 // New pages at selection position
386 if ($this->newPagesSelectPosition) {
387 // Link to page-wizard:
388 $newPageLinks[] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('pagesOnly' => 1))) . '">' . $pageIcon . htmlspecialchars($lang->getLL('pageSelectPosition')) . '</a>';
389 }
390 // Assemble all new page links
391 $numPageLinks = count($newPageLinks);
392 for ($i = 0; $i < $numPageLinks; $i++) {
393 $rowContent .= '<li>' . $newPageLinks[$i] . '</li>';
394 }
395 // Add row header and half-line if not empty
396 if (!empty($rowContent)) {
397 $rowContent = '<ul class="list-tree"><li>' .$newPageIcon . '<strong>' .
398 $lang->getLL('createNewPage') . '</strong><ul>' . $rowContent . '</ul></li>';
399 }
400 // Compile table row to show the icon for "new page (select position)"
401 $startRows = array();
402 if ($this->showNewRecLink('pages') && !empty($rowContent)) {
403 $startRows[] = $rowContent;
404 }
405 $iconFile = array();
406 // New tables (but not pages) INSIDE this pages
407 $isAdmin = $this->getBackendUserAuthentication()->isAdmin();
408 $newContentIcon = IconUtility::getSpriteIcon('actions-document-new');
409 if ($this->newContentInto) {
410 if (is_array($GLOBALS['TCA'])) {
411 $groupName = '';
412 foreach ($GLOBALS['TCA'] as $table => $v) {
413 if ($table != 'pages'
414 && $this->showNewRecLink($table)
415 && $this->isTableAllowedForThisPage($this->pageinfo, $table)
416 && $this->getBackendUserAuthentication()->check('tables_modify', $table)
417 && (($v['ctrl']['rootLevel'] xor $this->id) || $v['ctrl']['rootLevel'] == -1)
418 && $this->getBackendUserAuthentication()->workspaceCreateNewRecord(($this->pageinfo['_ORIG_uid'] ? $this->pageinfo['_ORIG_uid'] : $this->id), $table)
419 ) {
420 $newRecordIcon = IconUtility::getSpriteIconForRecord($table, array());
421 $rowContent = '';
422 $thisTitle = '';
423 // Create new link for record:
424 $newLink = $this->linkWrap($newRecordIcon . $lang->sL($v['ctrl']['title'], TRUE), $table, $this->id);
425 // If the table is 'tt_content', create link to wizard
426 if ($table == 'tt_content') {
427 $groupName = $lang->getLL('createNewContent');
428 $rowContent = $newContentIcon . '<strong>' . $lang->getLL('createNewContent') . '</strong><ul>';
429 // If mod.web_list.newContentWiz.overrideWithExtension is set, use that extension's wizard instead:
430 $overrideExt = $this->web_list_modTSconfig['properties']['newContentWiz.']['overrideWithExtension'];
431 $pathToWizard = ExtensionManagementUtility::isLoaded($overrideExt) ? ExtensionManagementUtility::extRelPath($overrideExt) . 'mod1/db_new_content_el.php?' : BackendUtility::getModuleUrl('new_content_element') . '&';
432 $href = $pathToWizard . 'id=' . $this->id . '&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
433 $rowContent .= '<li>' . $newLink . ' ' . BackendUtility::wrapInHelp($table, '') . '</li><li><a href="' . htmlspecialchars($href) . '">' . $newContentIcon . htmlspecialchars($lang->getLL('clickForWizard')) . '</a></li></ul>';
434 } else {
435 // Get the title
436 if ($v['ctrl']['readOnly'] || $v['ctrl']['hideTable'] || $v['ctrl']['is_static']) {
437 continue;
438 }
439 if ($v['ctrl']['adminOnly'] && !$isAdmin) {
440 continue;
441 }
442 $nameParts = explode('_', $table);
443 $thisTitle = '';
444 $_EXTKEY = '';
445 if ($nameParts[0] == 'tx' || $nameParts[0] == 'tt') {
446 // Try to extract extension name
447 if (substr($v['ctrl']['title'], 0, 8) == 'LLL:EXT:') {
448 $_EXTKEY = substr($v['ctrl']['title'], 8);
449 $_EXTKEY = substr($_EXTKEY, 0, strpos($_EXTKEY, '/'));
450 if ($_EXTKEY != '') {
451 // First try to get localisation of extension title
452 $temp = explode(':', substr($v['ctrl']['title'], 9 + strlen($_EXTKEY)));
453 $langFile = $temp[0];
454 $thisTitle = $lang->sL('LLL:EXT:' . $_EXTKEY . '/' . $langFile . ':extension.title');
455 // If no localisation available, read title from ext_emconf.php
456 $extEmConfFile = ExtensionManagementUtility::extPath($_EXTKEY) . 'ext_emconf.php';
457 if (!$thisTitle && is_file($extEmConfFile)) {
458 $EM_CONF = array();
459 include $extEmConfFile;
460 $thisTitle = $EM_CONF[$_EXTKEY]['title'];
461 }
462 $iconFile[$_EXTKEY] = '<img ' . 'src="' . ExtensionManagementUtility::extRelPath($_EXTKEY) . $GLOBALS['TYPO3_LOADED_EXT'][$_EXTKEY]['ext_icon'] . '" ' . 'width="16" height="16" ' . 'alt="' . $thisTitle . '" />';
463 }
464 }
465 if (empty($thisTitle)) {
466 $_EXTKEY = $nameParts[1];
467 $thisTitle = $nameParts[1];
468 $iconFile[$_EXTKEY] = '';
469 }
470 } else {
471 if ($table === 'pages_language_overlay' && !$this->checkIfLanguagesExist()) {
472 continue;
473 }
474 $_EXTKEY = 'system';
475 $thisTitle = $lang->getLL('system_records');
476 $iconFile['system'] = IconUtility::getSpriteIcon('apps-pagetree-root');
477 }
478 if ($groupName == '' || $groupName != $_EXTKEY) {
479 $groupName = empty($v['ctrl']['groupName']) ? $_EXTKEY : $v['ctrl']['groupName'];
480 }
481 $rowContent .= $newLink;
482 }
483 // Compile table row:
484 if ($table == 'tt_content') {
485 $startRows[] = '<li>' . $rowContent . '</li>';
486 } else {
487 $this->tRows[$groupName]['title'] = $thisTitle;
488 $this->tRows[$groupName]['html'][] = $rowContent;
489 $this->tRows[$groupName]['table'][] = $table;
490 }
491 }
492 }
493 }
494 }
495 // User sort
496 if (isset($pageTS['mod.']['wizards.']['newRecord.']['order'])) {
497 $this->newRecordSortList = GeneralUtility::trimExplode(',', $pageTS['mod.']['wizards.']['newRecord.']['order'], TRUE);
498 }
499 uksort($this->tRows, array($this, 'sortNewRecordsByConfig'));
500 // Compile table row:
501 $finalRows = array();
502 $finalRows[] = implode('', $startRows);
503 foreach ($this->tRows as $key => $value) {
504 $row = '<li>' . $iconFile[$key] . ' <strong>' . $value['title'] . '</strong><ul>';
505 foreach ($value['html'] as $recordKey => $record) {
506 $row .= '<li>' . $record . ' ' . BackendUtility::wrapInHelp($value['table'][$recordKey], '') . '</li>';
507 }
508 $row .= '</ul></li>';
509 $finalRows[] = $row;
510 }
511 // Make table:
512 $this->code .= implode('', $finalRows);
513 }
514
515 /**
516 * User array sort function used by regularNew
517 *
518 * @param string $a First array element for compare
519 * @param string $b First array element for compare
520 * @return int -1 for lower, 0 for equal, 1 for greater
521 */
522 public function sortNewRecordsByConfig($a, $b) {
523 if (!empty($this->newRecordSortList)) {
524 if (in_array($a, $this->newRecordSortList) && in_array($b, $this->newRecordSortList)) {
525 // Both are in the list, return relative to position in array
526 $sub = array_search($a, $this->newRecordSortList) - array_search($b, $this->newRecordSortList);
527 $ret = ($sub < 0 ? -1 : $sub == 0) ? 0 : 1;
528 } elseif (in_array($a, $this->newRecordSortList)) {
529 // First element is in array, put to top
530 $ret = -1;
531 } elseif (in_array($b, $this->newRecordSortList)) {
532 // Second element is in array, put first to bottom
533 $ret = 1;
534 } else {
535 // No element is in array, return alphabetic order
536 $ret = strnatcasecmp($this->tRows[$a]['title'], $this->tRows[$b]['title']);
537 }
538 return $ret;
539 } else {
540 // Return alphabetic order
541 return strnatcasecmp($this->tRows[$a]['title'], $this->tRows[$b]['title']);
542 }
543 }
544
545 /**
546 * Ending page output and echo'ing content to browser.
547 *
548 * @return void
549 */
550 public function printContent() {
551 echo $this->content;
552 }
553
554 /**
555 * Links the string $code to a create-new form for a record in $table created on page $pid
556 *
557 * @param string $linkText Link text
558 * @param string $table Table name (in which to create new record)
559 * @param int $pid PID value for the "&edit['.$table.']['.$pid.']=new" command (positive/negative)
560 * @param bool $addContentTable If $addContentTable is set, then a new tt_content record is created together with pages
561 * @return string The link.
562 */
563 public function linkWrap($linkText, $table, $pid, $addContentTable = FALSE) {
564 $parameters = '&edit[' . $table . '][' . $pid . ']=new';
565 if ($table == 'pages' && $addContentTable) {
566 $parameters .= '&edit[tt_content][prev]=new&returnNewPageId=1';
567 } elseif ($table == 'pages_language_overlay') {
568 $parameters .= '&overrideVals[pages_language_overlay][doktype]=' . (int)$this->pageinfo['doktype'];
569 }
570 $onClick = BackendUtility::editOnClick($parameters, '', $this->returnUrl);
571 return '<a href="#" onclick="' . htmlspecialchars($onClick) . '">' . $linkText . '</a>';
572 }
573
574 /**
575 * Returns TRUE if the tablename $checkTable is allowed to be created on the page with record $pid_row
576 *
577 * @param array $pid_row Record for parent page.
578 * @param string $checkTable Table name to check
579 * @return bool Returns TRUE if the tablename $checkTable is allowed to be created on the page with record $pid_row
580 */
581 public function isTableAllowedForThisPage($pid_row, $checkTable) {
582 if (!is_array($pid_row)) {
583 return $this->getBackendUserAuthentication()->isAdmin();
584 }
585 // be_users and be_groups may not be created anywhere but in the root.
586 if ($checkTable == 'be_users' || $checkTable == 'be_groups') {
587 return FALSE;
588 }
589 // Checking doktype:
590 $doktype = (int)$pid_row['doktype'];
591 if (!($allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'])) {
592 $allowedTableList = $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
593 }
594 // If all tables or the table is listed as an allowed type, return TRUE
595 if (strstr($allowedTableList, '*') || GeneralUtility::inList($allowedTableList, $checkTable)) {
596 return TRUE;
597 }
598
599 return FALSE;
600 }
601
602 /**
603 * Returns TRUE if:
604 * - $allowedNewTables and $deniedNewTables are empty
605 * - the table is not found in $deniedNewTables and $allowedNewTables is not set or the $table tablename is found in $allowedNewTables
606 *
607 * If $table tablename is found in $allowedNewTables and $deniedNewTables, $deniedNewTables
608 * has priority over $allowedNewTables.
609 *
610 * @param string $table Table name to test if in allowedTables
611 * @param array $allowedNewTables Array of new tables that are allowed.
612 * @param array $deniedNewTables Array of new tables that are not allowed.
613 * @return bool Returns TRUE if a link for creating new records should be displayed for $table
614 */
615 public function showNewRecLink($table, array $allowedNewTables = array(), array $deniedNewTables = array()) {
616
617 if (!$this->getBackendUserAuthentication()->check('tables_modify', $table)) {
618 return FALSE;
619 }
620
621 $allowedNewTables = $allowedNewTables ?: $this->allowedNewTables;
622 $deniedNewTables = $deniedNewTables ?: $this->deniedNewTables;
623 // No deny/allow tables are set:
624 if (empty($allowedNewTables) && empty($deniedNewTables)) {
625 return TRUE;
626 }
627
628 return !in_array($table, $deniedNewTables) && (empty($allowedNewTables) || in_array($table, $allowedNewTables));
629 }
630
631 /**
632 * Checks if sys_language records are present
633 *
634 * @return bool
635 */
636 protected function checkIfLanguagesExist() {
637 $languageCount = $this->getDatabaseConnection()->exec_SELECTcountRows('uid', 'sys_language', '1=1');
638 if ($languageCount) {
639 $languageCount = TRUE;
640 }
641 return $languageCount;
642 }
643
644 /**
645 * Return language service instance
646 *
647 * @return \TYPO3\CMS\Lang\LanguageService
648 */
649 protected function getLanguageService() {
650 return $GLOBALS['LANG'];
651 }
652
653 /**
654 * Returns the global BackendUserAuthentication object.
655 *
656 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
657 */
658 protected function getBackendUserAuthentication() {
659 return $GLOBALS['BE_USER'];
660 }
661
662 /**
663 * Returns the database connection
664 *
665 * @return DatabaseConnection
666 */
667 protected function getDatabaseConnection() {
668 return $GLOBALS['TYPO3_DB'];
669 }
670
671 }