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