[TASK] Add missing licence header to viewhelper classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / ViewHelpers / Link / NewRecordViewHelper.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Backend\ViewHelpers\Link;
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\ViewHelper\AbstractTagBasedViewHelper;
22
23 /**
24 * Use this ViewHelper to provide 'create new record' links.
25 * The ViewHelper will pass the command to FormEngine.
26 *
27 * The table argument is mandatory, it decides what record is to be created.
28 *
29 * The pid argument will put the new record on this page, if 0 given it will
30 * be placed to the root page.
31 *
32 * The uid argument accepts only negative values. If this is given, the new
33 * record will be placed (by sorting field) behind the record with the uid.
34 * It will end up on the same pid as this given record, so the pid must not
35 * be given explicitly by pid argument.
36 *
37 * An exception will be thrown, if both uid and pid are given.
38 * An exception will be thrown, if the uid argument is not a negative integer.
39 *
40 * To edit records, use the editRecordViewHelper
41 *
42 * Examples
43 * --------
44 *
45 * Link to create a new record of a_table after record 17 on the same pid::
46 *
47 * <be:link.newRecord table="a_table" returnUrl="foo/bar" uid="-17"/>
48 *
49 * Output::
50 *
51 * <a href="/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar">
52 * Edit record
53 * </a>
54 *
55 * Link to create a new record of a_table on root page::
56 *
57 * <be:link.newRecord table="a_table" returnUrl="foo/bar""/>
58 *
59 * Output::
60 *
61 * <a href="/typo3/index.php?route=/record/edit&edit[a_table][]=new&returnUrl=foo/bar">
62 * Edit record
63 * </a>
64 *
65 * Link to create a new record of a_table on page 17::
66 *
67 * <be:link.newRecord table="a_table" returnUrl="foo/bar" pid="17"/>
68 *
69 * Output::
70 *
71 * <a href="/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar">
72 * Edit record
73 * </a>
74 */
75 class NewRecordViewHelper extends AbstractTagBasedViewHelper
76 {
77 /**
78 * @var string
79 */
80 protected $tagName = 'a';
81
82 public function initializeArguments()
83 {
84 parent::initializeArguments();
85 $this->registerUniversalTagAttributes();
86 $this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
87 $this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
88 $this->registerArgument('table', 'string', 'target database table', true);
89 $this->registerArgument('returnUrl', 'string', '', false, '');
90 }
91
92 /**
93 * @return string
94 * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
95 */
96 public function render(): string
97 {
98 if ($this->arguments['uid'] && $this->arguments['pid']) {
99 throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526129969);
100 }
101 if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) {
102 throw new \InvalidArgumentException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526134901);
103 }
104
105 if (empty($this->arguments['returnUrl'])) {
106 $this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
107 }
108
109 $params = [
110 'edit' => [$this->arguments['table'] => [$this->arguments['uid'] ?? $this->arguments['pid'] ?? 0 => 'new']],
111 'returnUrl' => $this->arguments['returnUrl']
112 ];
113 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
114 $uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
115 $this->tag->addAttribute('href', $uri);
116 $this->tag->setContent($this->renderChildren());
117 $this->tag->forceClosingTag(true);
118 return $this->tag->render();
119 }
120 }