PopupController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 139
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
C indexAction() 0 60 15
A convertArrayToJsParams() 0 10 4
A getHtml() 0 15 2
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
10
/**
11
 * Class PopupController
12
 *
13
 * @package FRUIT\Popup\Controller
14
 */
15
class PopupController extends ActionController
16
{
17
18
    /**
19
     * Same as class name
20
     */
21
    public $prefixId = 'tx_popup_pi1';
22
23
    /**
24
     * The extension key.
25
     */
26
    public $extKey = 'popup';
27
28
    /**
29
     * @var Popup
30
     */
31
    protected $popup;
32
33
    /**
34
     * @var array
35
     */
36
    protected $customParams = [];
37
38
    /**
39
     * Init the popup frontend plugin
40
     *
41
     * @return void
42
     */
43
    protected function init()
44
    {
45
        $this->popup = GeneralUtility::makeInstance('FRUIT\\Popup\\Popup');
46
        $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...
47
        $this->customParams = $this->popup->advancedParams;
48
    } # function - init
49
50
    /**
51
     * Generate JS code for opening pop-up
52
     * Main Plugin function for T3
53
     *
54
     * @return string
55
     */
56
    public function indexAction()
57
    {
58
        // Init
59
        $this->init();
60
61
        $link = $this->configurationManager->getContentObject()->data['tx_popup_auto'];
62
        if (!$link) {
63
            return '';
64
        }
65
        // create the url
66
        $url = $this->configurationManager->getContentObject()
67
            ->getTypoLink_URL($link);
68
        if (!$url) {
69
            return '';
70
        }
71
72
        $ts = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
73
        $conf = $ts['plugin.']['tx_popup_pi1.'];
74
75
        // get session data
76
        if ($conf['advancedParams.']['once_per_session']) {
77
            $popups = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->prefixId);
78
            if ($conf['advancedParams.']['once_per_link']) {
79
                if ($popups['links'][$link]) {
80
                    return '';
81
                } else {
82
                    $popups['links'][$link] = 1;
83
                }
84
            } else {
85
                if ($popups['pages'][$GLOBALS['TSFE']->id]) { // we have been on the current page
86
                    return '';
87
                } else {
88
                    $popups['pages'][$GLOBALS['TSFE']->id] = 1;
89
                }
90
            }
91
            $GLOBALS['TSFE']->fe_user->setKey('ses', $this->prefixId, $popups);
92
        }
93
94
        $cParams = [];
95
        foreach ($this->customParams as $name => $type) {
96
            $v = $conf['advancedParams.'][$name];
97
            $cParams[$name] = ($v === '1' || $v == 'yes' || $v == 'on') ? true : false;
98
        }
99
100
        $params = (array)$conf['allowedParams.'];
101
        if ($cParams['maximize']) {
102
            $params['left'] = 0;
103
            $params['top'] = 0;
104
            $params['width'] = '\' + screen.width + \'';
105
            $params['height'] = '\' + screen.height + \'';
106
        } elseif ($cParams['center'] && $params['width'] > 0 && $params['height'] > 0) {
107
            $params['left'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
108
            $params['top'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
109
            $params['screenX'] = '\' + ((screen.width - ' . $params['width'] . ') / 2) + \'';
110
            $params['screenY'] = '\' + ((screen.height - ' . $params['height'] . ') / 2) + \'';
111
        }
112
113
        $params = $this->convertArrayToJsParams($params);
114
        return $this->getHtml($url, $params, $cParams);
115
    }
116
117
    /**
118
     * @param array $array
119
     * @return string
120
     */
121
    protected function convertArrayToJsParams(array $array)
122
    {
123
        $tmp_params = [];
124
        while (list($key, $val) = each($array)) {
125
            if (isset($val) && $val != '') {
126
                $tmp_params[] = $key . '=' . $val;
127
            }
128
        }
129
        return implode(",", $tmp_params);
130
    }
131
132
    /**
133
     * @param $url
134
     * @param $params
135
     * @param $cParams
136
     * @return string
137
     */
138
    protected function getHtml($url, $params, $cParams)
139
    {
140
        $window = uniqid('_popup_');
141
142
        $content = '<script type="text/javascript">';
143
        $content .= "var $window = window.open('$url', 'Window', '$params');\n";
144
        // thanks to Tom Binder for the timeout
145
        if ($cParams['popunder']) {
146
            $content .= "if ($window) { window.setTimeout('$window.blur()',500); window.focus(); }";
147
        } else {
148
            $content .= "if ($window) { window.setTimeout('$window.focus()',500); }";
149
        }
150
151
        return $content . "\n</script>\n";
152
    }
153
}
154