Completed
Push — master ( 17598f...0cca5d )
by Tim
14:26
created

PopupController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace FRUIT\Popup\Controller;
4
5
use FRUIT\Popup\Popup;
6
use TYPO3\CMS\Core\Utility\GeneralUtility;
7
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
8
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
9
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
10
11
/**
12
 * Class PopupController
13
 *
14
 * @package FRUIT\Popup\Controller
15
 */
16
class PopupController extends ActionController
17
{
18
19
    /**
20
     * Same as class name
21
     */
22
    public $prefixId = 'tx_popup_pi1';
23
24
    /**
25
     * The extension key.
26
     */
27
    public $extKey = 'popup';
28
29
    /**
30
     * @var Popup
31
     */
32
    protected $popup;
33
34
    /**
35
     * @var array
36
     */
37
    protected $customParams = [];
38
39
    /**
40
     * Init the popup frontend plugin
41
     *
42
     * @return void
43
     */
44
    protected function init()
45
    {
46
        $this->popup = GeneralUtility::makeInstance('FRUIT\\Popup\\Popup');
47
        $this->allowedParams = $this->popup->allowedParams;
0 ignored issues
show
Bug introduced by
The property allowedParams does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
        $this->customParams = $this->popup->advancedParams;
49
    } # function - init
50
51
    /**
52
     * Generate JS code for opening pop-up
53
     * Main Plugin function for T3
54
     *
55
     * @return string
56
     */
57
    public function indexAction()
58
    {
59
        // Init
60
        $this->init();
61
62
        $link = $this->configurationManager->getContentObject()->data['tx_popup_auto'];
63
        if (!$link) {
64
            GeneralUtility::devLog('No link defined', $this->extKey);
65
            return '';
66
        }
67
68
        $ts = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
69
        $conf = $ts['plugin.']['tx_popup_pi1.'];
70
71
        // get session data
72
        if ($conf['advancedParams.']['once_per_session']) {
73
            $popups = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefixId);
74
            if ($conf['advancedParams.']['once_per_link']) {
75
                if ($popups['links'][$link]) {
76
                    GeneralUtility::devLog('Pop-up link "' . $link . '" already shown.', $this->extKey);
77
                    return '';
78
                } else {
79
                    $popups['links'][$link] = 1;
80
                }
81
            } else {
82
                if ($popups['pages'][$GLOBALS['TSFE']->id]) { // we have been on the current page
83
                    GeneralUtility::devLog('Pop-up already shown on this page.', $this->extKey);
84
                    return '';
85
                } else {
86
                    $popups['pages'][$GLOBALS['TSFE']->id] = 1;
87
                }
88
            }
89
            $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefixId, $popups);
90
        }
91
92
        // create the url
93
        $url = $this->configurationManager->getContentObject()
94
            ->getTypoLink_URL($link);
95
        if (!$url) {
96
            GeneralUtility::devLog('No valid pop-up (e.g. a hidden page):' . $link, $this->extKey);
97
            return '';
98
        }
99
100
        // get the JS window parameters directly (protect from errors in TS?)
101
        $params = $conf['allowedParams.'];
102
103
        // get the custom parameters
104
        $cParams = [];
105
        foreach ($this->customParams as $name => $type) {
106
            $v = $conf['advancedParams.'][$name];
107
            $cParams[$name] = ($v === '1' || $v == 'yes' || $v == 'on') ? true : false;
108
        }
109
110
111
        // thanks to Alex Widschwendter
112
        if ($cParams['maximize']) {
113
            $params['left'] = 0;
114
            $params['top'] = 0;
115
            $params['width'] = '\' + screen.width + \'';
116
            $params['height'] = '\' + screen.height + \'';
117
        } // thanks to Daniel Rampanelli
118
        elseif ($cParams['center'] && $params['width'] > 0 && $params['height'] > 0) {
119
            $params['left'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
120
            $params['top'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
121
            $params['screenX'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
122
            $params['screenY'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
123
        }
124
125
        $tmp_params = [];
126
        while (list($key, $val) = each($params)) {
127
            if (isset($val) && $val != '') {
128
                $tmp_params[] = $key . '=' . $val;
129
            }
130
        }
131
        $params = implode(",", $tmp_params);
132
133
134
        // Build Javascript
135
        $content = '<script type="text/javascript">
136
		/*<![CDATA[*/
137
		<!--
138
		';
139
        $window = uniqid('_popup_');
140
141
        $content .= "var $window = window.open('$url', 'Window', '$params');\n";
142
143
        // thanks to Tom Binder for the timeout
144
        if ($cParams['popunder']) {
145
            $content .= "if ($window) { window.setTimeout('$window.blur()',500); window.focus(); }";
146
        } else {
147
            $content .= "if ($window) { window.setTimeout('$window.focus()',500); }";
148
        }
149
150
        $content .= "\n// -->\n/*]]>*/</script>\n";
151
152
        return $content;
153
    }
154
}
155