Completed
Push — master ( 1d51dd...853185 )
by Tim
03:44
created

AbstractLinkViewHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * Link to anything ;).
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\ViewHelpers\Link;
9
10
use Psr\Log\LoggerInterface;
11
use TYPO3\CMS\Core\Log\LogManager;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Core\Utility\MathUtility;
14
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
15
16
/**
17
 * Link to anything ;).
18
 */
19
abstract class AbstractLinkViewHelper extends AbstractTagBasedViewHelper
20
{
21
    /**
22
     * Tag type.
23
     *
24
     * @var string
25
     */
26
    protected $tagName = 'a';
27
28
    /**
29
     * Store the last href to avoid escaping for the URI view Helper.
30
     *
31
     * @var string
32
     */
33
    protected $lastHref = '';
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    protected $logger;
39
    
40
    /**
41
     * Build up the object
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
47
    }
48
49
    /**
50
     * Arguments initialization.
51
     */
52
    public function initializeArguments()
53
    {
54
        $this->registerUniversalTagAttributes();
55
        $this->registerTagAttribute('target', 'string', 'Target of link', false);
56
        $this->registerTagAttribute(
57
            'rel',
58
            'string',
59
            'Specifies the relationship between the current document and the linked document',
60
            false
61
        );
62
    }
63
64
    /**
65
     * render the link.
66
     *
67
     * @param int|null $pageUid          target page. See TypoLink destination
68
     * @param array    $additionalParams query parameters to be attached to the resulting URI
69
     * @param bool     $absolute
70
     *
71
     * @return string Rendered page URI
72
     */
73
    public function renderLink($pageUid = null, array $additionalParams = [], $absolute = false)
74
    {
75
        $uriBuilder = $this->controllerContext->getUriBuilder();
76
        $this->lastHref = (string) $uriBuilder->reset()
77
            ->setTargetPageUid($pageUid)
78
            ->setArguments($additionalParams)
79
            ->setCreateAbsoluteUri($absolute)
80
            ->build();
81
        if ('' !== $this->lastHref) {
82
            $this->tag->addAttribute('href', $this->lastHref);
83
            $this->tag->setContent($this->renderChildren());
84
            $result = $this->tag->render();
85
        } else {
86
            $result = $this->renderChildren();
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * Get the right page Uid.
94
     *
95
     * @param int         $pageUid
96
     * @param string|null $contextName
97
     *
98
     * @return int
99
     */
100
    protected function getPageUid($pageUid, $contextName = null)
101
    {
102
        if (MathUtility::canBeInterpretedAsInteger($pageUid) && $pageUid > 0) {
103
            return (int) $pageUid;
104
        }
105
106
        // by settings
107
        if ($contextName && $this->templateVariableContainer->exists('settings')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contextName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
108
            $settings = $this->templateVariableContainer->get('settings');
109
            if (isset($settings[$contextName]) && MathUtility::canBeInterpretedAsInteger($settings[$contextName])) {
110
                return (int) $settings[$contextName];
111
            }
112
        }
113
114
        return (int) $GLOBALS['TSFE']->id;
115
    }
116
}
117