Passed
Pull Request — master (#4)
by
unknown
02:11
created

ht_pagantis::ht_pagantis()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 2
b 0
f 0
nc 8
nop 0
dl 0
loc 20
rs 8.8333
1
<?php
2
/*
3
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2010 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
define('TABLE_PAGANTIS_CONFIG', 'pagantis_config');
14
define('MODULE_HEADER_TAGS_PAGANTIS_SDK', 'https://cdn.pagantis.com/js/pg-v2/sdk.js');
15
16
class ht_pagantis {
17
    var $code = 'ht_pagantis';
18
    var $group = 'header_tags';
19
    var $title;
20
    var $description;
21
    var $enabled = false;
22
    var $langCode = null;
23
24
    /**
25
     * ht_pagantis constructor.
26
     */
27
    function ht_pagantis() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
        $this->title = MODULE_HEADER_TAGS_PAGANTIS_TITLE;
29
        $this->description = MODULE_HEADER_TAGS_PAGANTIS_DESCRIPTION;
30
        $this->sort_order = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property sort_order does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32
        if (defined('MODULE_HEADER_TAGS_PAGANTIS_STATUS')
33
            && defined('MODULE_PAYMENT_PAGANTIS_STATUS')
34
            && defined('MODULE_PAYMENT_PAGANTIS_SIMULATOR')
35
        ) {
36
            $this->enabled = ((MODULE_HEADER_TAGS_PAGANTIS_STATUS == 'True') &&
0 ignored issues
show
introduced by
The condition MODULE_HEADER_TAGS_PAGANTIS_STATUS == 'True' is always false.
Loading history...
37
                (MODULE_PAYMENT_PAGANTIS_STATUS == 'True') &&
0 ignored issues
show
Bug introduced by
The constant MODULE_PAYMENT_PAGANTIS_STATUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
                (MODULE_PAYMENT_PAGANTIS_SIMULATOR == 'True')) ;
0 ignored issues
show
Bug introduced by
The constant MODULE_PAYMENT_PAGANTIS_SIMULATOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        }
40
41
        $this->extraConfig = $this->getExtraConfig();
0 ignored issues
show
Bug Best Practice introduced by
The property extraConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        $this->pk = $this->getConfig('MODULE_PAYMENT_PAGANTIS_PK');
0 ignored issues
show
Bug Best Practice introduced by
The property pk does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->sdkFile = MODULE_HEADER_TAGS_PAGANTIS_SDK;
0 ignored issues
show
Bug Best Practice introduced by
The property sdkFile does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
        if (defined('MODULE_HEADER_TAGS_PAGANTIS_LANG_CODE')) {
46
            $this->langCode = MODULE_HEADER_TAGS_PAGANTIS_LANG_CODE;
47
        }
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    private function getExtraConfig()
54
    {
55
        $checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONFIG."'");
0 ignored issues
show
Bug introduced by
The function tep_db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $checkTable = /** @scrutinizer ignore-call */ tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONFIG."'");
Loading history...
56
        $response = array();
57
        if (tep_db_num_rows($checkTable) > 0) {
0 ignored issues
show
Bug introduced by
The function tep_db_num_rows was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        if (/** @scrutinizer ignore-call */ tep_db_num_rows($checkTable) > 0) {
Loading history...
58
            $query       = "select * from ".TABLE_PAGANTIS_CONFIG;
59
            $result      = tep_db_query($query);
60
            $response    = array();
61
            while ($resultArray = tep_db_fetch_array($result)) {
0 ignored issues
show
Bug introduced by
The function tep_db_fetch_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            while ($resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result)) {
Loading history...
62
                $response[$resultArray['config']] = $resultArray['value'];
63
            }
64
        }
65
        return $response;
66
    }
67
68
    /**
69
     * @param string $config
70
     * @return array
71
     */
72
    private function getConfig($config = '')
73
    {
74
        $query       = "select * from ".TABLE_CONFIGURATION . " where configuration_key ='" . $config . "'";
0 ignored issues
show
Bug introduced by
The constant TABLE_CONFIGURATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        $result      = tep_db_query($query);
0 ignored issues
show
Bug introduced by
The function tep_db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        $result      = /** @scrutinizer ignore-call */ tep_db_query($query);
Loading history...
76
        $resultArray = tep_db_fetch_array($result);
0 ignored issues
show
Bug introduced by
The function tep_db_fetch_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result);
Loading history...
77
        return $resultArray['configuration_value'];
78
    }
79
80
    /**
81
     * Execute function
82
     */
83
    function execute()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
    {
85
        global $languages_id, $order;
86
87
        $productId = $GLOBALS["HTTP_GET_VARS"]["products_id"];
88
        $checkoutPage = strpos($_SERVER[REQUEST_URI], "checkout_payment.php") > 0;
0 ignored issues
show
Bug introduced by
The constant REQUEST_URI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
90
        //Show promoted html
91
        if ($this->isPromoted($productId) && $checkoutPage!='1') {
92
            echo "<div id='promotedText' style='display:none'><br/>".$this->extraConfig['PAGANTIS_PROMOTED_PRODUCT_CODE']."</div>";
93
            echo '<script>'. PHP_EOL;
94
            echo '        function loadPromoted()'. PHP_EOL;
95
            echo '        {'. PHP_EOL;
96
            echo 'var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';'. PHP_EOL;
97
            echo 'if (positionSelector === \'default\') {'. PHP_EOL;
98
            echo 'positionSelector = \'.buttonSet\''. PHP_EOL;
99
            echo '}'. PHP_EOL;
100
            echo 'var docFather = document.querySelector(positionSelector);'.PHP_EOL;
101
            echo 'if (typeof docFather != \'undefined\') {'. PHP_EOL;
102
            echo 'var promotedNode = document.getElementById("promotedText");'.PHP_EOL;
103
            echo 'docFather.appendChild(promotedNode);'.PHP_EOL;
104
            echo 'promotedNode.style.display=""' . PHP_EOL;
105
            echo '               clearInterval(window.OSPromotedId);'. PHP_EOL;
106
            echo '               return true;'. PHP_EOL;
107
            echo '       }'. PHP_EOL;
108
            echo '               return false;'. PHP_EOL;
109
            echo '       }'. PHP_EOL;
110
            echo '       window.OSPromotedId = setInterval(function () {'. PHP_EOL;
111
            echo '          loadPromoted();'. PHP_EOL;
112
            echo '       }, 2000);'. PHP_EOL;
113
            echo '</script>'. PHP_EOL;
114
        }
115
116
        if (isset($productId) || $checkoutPage) {
117
            $simulatorCode = 'pgSDK';
118
            if ($this->langCode == 'es' || $this->langCode == null) {
119
                $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'] = 'pmtSDK.simulator.positions.INNER';
0 ignored issues
show
Bug Best Practice introduced by
The property extraConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
120
                $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'] = 'pmtSDK.simulator.types.SIMPLE';
121
                $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'] = 'pmtSDK.simulator.skins.BLUE';
122
                $simulatorCode = 'pmtSDK';
123
            }
124
125
            //Promoted amount on checkout Pae
126
            $promotedAmount = 0;
127
            foreach ((array)$order->products as $item) {
128
                $productId = explode('{', $item['id'], 1);
129
                $productId = array_shift($productId);
130
                $promotedProduct = $this->isPromoted($productId);
131
                if ($promotedProduct) {
132
                    $promotedAmount+=number_format(($item['price'] * $item['qty']), 2);
133
                }
134
            }
135
136
            echo "<script src='".$this->sdkFile."'></script>". PHP_EOL;
137
            echo '<script>'. PHP_EOL;
138
            echo '        function loadSimulator()'. PHP_EOL;
139
            echo '        {'. PHP_EOL;
140
            echo '           if (typeof '.$simulatorCode.' != \'undefined\') {'. PHP_EOL;
141
            echo '               var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';'. PHP_EOL;
142
            echo '               var priceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';'. PHP_EOL;
143
            echo '               var checkoutPriceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';'. PHP_EOL;
144
            echo '               var quantitySelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']. '\';'. PHP_EOL;
145
            echo '               var checkoutPage =     \'' . $checkoutPage.'\';'. PHP_EOL;
146
            echo '               var promotedAmount =     \'' . $promotedAmount.'\';'. PHP_EOL;
147
148
            echo '               if (positionSelector === \'default\') {'. PHP_EOL;
149
            echo '                   positionSelector = \'.buttonSet\''. PHP_EOL;
150
            echo '               }'. PHP_EOL;
151
152
            echo '               if (priceSelector === \'default\') {'. PHP_EOL;
153
            echo '                   priceSelector = \'#bodyContent>form>div>h1\''. PHP_EOL;
154
            echo '               }'. PHP_EOL;
155
156
            echo '               if (checkoutPriceSelector == \'default\' && checkoutPage == \'1\')  {'. PHP_EOL;
157
            echo '                   priceSelector = \'#columnRight > .infoBoxContainer > .infoBoxContents > tbody > tr:last-child > td\';'. PHP_EOL;
158
            echo '               }'. PHP_EOL;
159
            echo '               '.$simulatorCode.'.product_simulator = {};'. PHP_EOL;
160
            echo '               '.$simulatorCode.'.product_simulator.id = \'product-simulator\';'. PHP_EOL;
161
            echo '               '.$simulatorCode.'.product_simulator.publicKey = \'' . $this->pk . '\';'. PHP_EOL;
0 ignored issues
show
Bug introduced by
Are you sure $this->pk of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
            echo '               '.$simulatorCode.'.product_simulator.publicKey = \'' . /** @scrutinizer ignore-type */ $this->pk . '\';'. PHP_EOL;
Loading history...
162
            echo '               '.$simulatorCode.'.product_simulator.selector = positionSelector;'. PHP_EOL;
163
            echo '               '.$simulatorCode.'.product_simulator.numInstalments = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'] . '\';'. PHP_EOL;
164
            echo '               '.$simulatorCode.'.product_simulator.type = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'] . ';'. PHP_EOL;
165
            echo '               '.$simulatorCode.'.product_simulator.skin = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'] . ';'. PHP_EOL;
166
            echo '               '.$simulatorCode.'.product_simulator.position = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'] . ';'. PHP_EOL;
167
168
            //Amount in product page
169
            echo '               var promotedProduct = \'' . $this->isPromoted($productId) .'\';'. PHP_EOL;
170
            echo '               if(checkoutPage != \'1\' ) {';
171
            echo '               '.$simulatorCode.'.product_simulator.itemAmountSelector = priceSelector;'. PHP_EOL;
172
            echo '                   if(promotedProduct == \'1\') { ' . PHP_EOL;
173
            echo '                    '.$simulatorCode.'.product_simulator.itemPromotedAmountSelector = priceSelector;'. PHP_EOL;
174
            echo '                   }' . PHP_EOL;
175
            echo '               }';
176
177
            //Amount in checkout page
178
            echo '               if(checkoutPage == \'1\' ) { ' . PHP_EOL;
179
            echo '               '.$simulatorCode.'.product_simulator.totalAmountSelector = priceSelector;'. PHP_EOL;
180
            echo '                  if(promotedAmount != \'0\' && checkoutPage == \'1\' ) { ' . PHP_EOL;
181
            echo '                  '   .$simulatorCode.'.product_simulator.totalPromotedAmount = promotedAmount;'. PHP_EOL;
182
            echo '                  }' . PHP_EOL;
183
            echo '               }' . PHP_EOL;
184
            echo '               '.$simulatorCode.'.simulator.init('.$simulatorCode.'.product_simulator);'. PHP_EOL;
185
            echo '               clearInterval(window.OSSimulatorId);'. PHP_EOL;
186
            echo '               return true;'. PHP_EOL;
187
            echo '           }'. PHP_EOL;
188
            echo '           return false;'. PHP_EOL;
189
            echo '       }'. PHP_EOL;
190
            echo '       window.OSSimulatorId = setInterval(function () {'. PHP_EOL;
191
            echo '          loadSimulator();'. PHP_EOL;
192
            echo '       }, 2000);'. PHP_EOL;
193
            echo '</script>'. PHP_EOL;
194
195
            //Checkout simulator
196
            if ($checkoutPage) {
197
                echo '<script>' . PHP_EOL;
198
                echo 'function checkSelected(value)'. PHP_EOL;
199
                echo '{ '. PHP_EOL;
200
                echo 'var simulator = document.getElementsByClassName("buttonSet");'  . PHP_EOL;
201
                echo ' if(simulator == "undefined") { return false;  } '. PHP_EOL;
202
                echo 'var pagantisCheckbox = document.querySelector("input[value=\'pagantis\']"); ' . PHP_EOL;
203
                echo 'var grandparentNode = pagantisCheckbox.parentNode.parentNode;' . PHP_EOL;
204
                echo 'if(grandparentNode == value) { var status="" } ' . PHP_EOL;
205
                echo 'else { var status="none";} '. PHP_EOL;
206
                echo 'simulator[0].style.display=status; ' . PHP_EOL;
207
                echo '}'. PHP_EOL;
208
209
                echo 'function showSimulator()'. PHP_EOL;
210
                echo '{'. PHP_EOL;
211
                echo 'var elements = document.querySelectorAll("tr[class^=\'moduleRow\']");' . PHP_EOL;
212
                echo 'if(elements == null) { return false };' . PHP_EOL;
213
214
                echo 'for(var i = 0, max = elements.length; i < max; i++) { elements[i].onclick = function() {
215
                        checkSelected(this);
216
                    } }' . PHP_EOL;
217
                echo 'clearInterval(window.OSdisplayId);';
218
                echo 'return true;'. PHP_EOL;
219
                echo '};'. PHP_EOL;
220
221
                echo '       window.OSdisplayId = setInterval(function () {'. PHP_EOL;
222
                echo '          showSimulator();'. PHP_EOL;
223
                echo '       }, 2000);'. PHP_EOL;
224
225
226
                echo '</script>'. PHP_EOL;
227
            }
228
        }
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    function isEnabled() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
235
        return $this->enabled;
236
    }
237
238
    /**
239
     * @return bool
240
     */
241
    function check() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
242
        return defined('MODULE_HEADER_TAGS_PAGANTIS_STATUS');
243
    }
244
245
    /**
246
     * install
247
     */
248
    function install() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
249
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Pagantis Module', 'MODULE_HEADER_TAGS_PAGANTIS_STATUS', 'True', '', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
0 ignored issues
show
Bug introduced by
The constant TABLE_CONFIGURATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function tep_db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
        /** @scrutinizer ignore-call */ 
250
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Pagantis Module', 'MODULE_HEADER_TAGS_PAGANTIS_STATUS', 'True', '', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
Loading history...
250
251
    }
252
253
    /**
254
     * remove
255
     */
256
    function remove() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
257
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
0 ignored issues
show
Bug introduced by
The constant TABLE_CONFIGURATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function tep_db_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

257
        /** @scrutinizer ignore-call */ 
258
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
Loading history...
258
    }
259
260
    /**
261
     * @return array
262
     */
263
    function keys() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
264
        return array('MODULE_HEADER_TAGS_PAGANTIS_STATUS');
265
    }
266
267
    /**
268
     * @param $productId
269
     *
270
     * @return bool
271
     */
272
    private function isPromoted($productId)
273
    {
274
        if (!isset($productId)) {
275
            return false;
276
        }
277
278
        if ($this->extraConfig['PAGANTIS_PROMOTION'] == '') {
279
            $promotedProducts = array();
280
        } else {
281
            $promotedProducts = array_values((array)unserialize($this->extraConfig['PAGANTIS_PROMOTION']));
282
        }
283
284
        return (in_array($productId, $promotedProducts));
285
    }
286
}
287
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...