[TASK] Add missing licence header to viewhelper classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / ViewHelpers / Uri / NewRecordViewHelper.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Backend\ViewHelpers\Uri;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24
25 /**
26 * Use this ViewHelper to provide 'create new record' links.
27 * The ViewHelper will pass the command to FormEngine.
28 *
29 * The table argument is mandatory, it decides what record is to be created.
30 *
31 * The pid argument will put the new record on this page, if 0 given it will
32 * be placed to the root page.
33 *
34 * The uid argument accepts only negative values. If this is given, the new
35 * record will be placed (by sorting field) behind the record with the uid.
36 * It will end up on the same pid as this given record, so the pid must not
37 * be given explicitly by pid argument.
38 *
39 * An exception will be thrown, if both uid and pid are given.
40 * An exception will be thrown, if the uid argument is not a negative integer.
41 *
42 * To edit records, use the editRecordViewHelper
43 *
44 * Examples
45 * ========
46 *
47 * Uri to create a new record of a_table after record 17 on the same pid::
48 *
49 * <be:uri.newRecord table="a_table" returnUrl="foo/bar" uid="-17"/>
50 *
51 * ``/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar``
52 *
53 * Uri to create a new record of a_table on root page::
54 *
55 * <be:uri.newRecord table="a_table" returnUrl="foo/bar""/>
56 *
57 * ``/typo3/index.php?route=/record/edit&edit[a_table][]=new&returnUrl=foo/bar``
58 *
59 * Uri to create a new record of a_table on page 17::
60 *
61 * <be:uri.newRecord table="a_table" returnUrl="foo/bar" pid="17"/>
62 *
63 * ``/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar``
64 */
65 class NewRecordViewHelper extends AbstractTagBasedViewHelper
66 {
67 use CompileWithRenderStatic;
68
69 public function initializeArguments()
70 {
71 $this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
72 $this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
73 $this->registerArgument('table', 'string', 'target database table', true);
74 $this->registerArgument('returnUrl', 'string', '', false, '');
75 }
76
77 /**
78 * @param array $arguments
79 * @param \Closure $renderChildrenClosure
80 * @param RenderingContextInterface $renderingContext
81 *
82 * @return string
83 * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
84 */
85 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
86 {
87 if ($arguments['uid'] && $arguments['pid']) {
88 throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526136338);
89 }
90 if (isset($arguments['uid']) && $arguments['uid'] >= 0) {
91 throw new \InvalidArgumentException('Uid must be negative integer, ' . $arguments['uid'] . ' given', 1526136362);
92 }
93
94 if (empty($arguments['returnUrl'])) {
95 $arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
96 }
97
98 $params = [
99 'edit' => [$arguments['table'] => [$arguments['uid'] ?? $arguments['pid'] ?? 0 => 'new']],
100 'returnUrl' => $arguments['returnUrl']
101 ];
102 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
103 return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
104 }
105 }