Passed
Pull Request — master (#4)
by Raúl
02:31
created

ht_pagantis::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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
23
    /**
24
     * ht_pagantis constructor.
25
     */
26
    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...
27
        $this->title = MODULE_HEADER_TAGS_PAGANTIS_TITLE;
28
        $this->description = MODULE_HEADER_TAGS_PAGANTIS_DESCRIPTION;
29
        $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...
30
31
        if (defined('MODULE_HEADER_TAGS_PAGANTIS_STATUS')
32
            && defined('MODULE_PAYMENT_PAGANTIS_STATUS')
33
            && defined('MODULE_PAYMENT_PAGANTIS_SIMULATOR')
34
        ) {
35
            $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...
36
                (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...
37
                (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...
38
        }
39
40
        $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...
41
        $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...
42
        $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...
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    private function getExtraConfig()
49
    {
50
        $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

50
        $checkTable = /** @scrutinizer ignore-call */ tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONFIG."'");
Loading history...
51
        $response = array();
52
        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

52
        if (/** @scrutinizer ignore-call */ tep_db_num_rows($checkTable) > 0) {
Loading history...
53
            $query       = "select * from ".TABLE_PAGANTIS_CONFIG;
54
            $result      = tep_db_query($query);
55
            $response    = array();
56
            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

56
            while ($resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result)) {
Loading history...
57
                $response[$resultArray['config']] = $resultArray['value'];
58
            }
59
        }
60
        return $response;
61
    }
62
63
    /**
64
     * @param string $config
65
     * @return array
66
     */
67
    private function getConfig($config = '')
68
    {
69
        $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...
70
        $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

70
        $result      = /** @scrutinizer ignore-call */ tep_db_query($query);
Loading history...
71
        $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

71
        $resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result);
Loading history...
72
        return $resultArray['configuration_value'];
73
    }
74
75
    /**
76
     * Execute function
77
     */
78
    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...
79
    {
80
        global $languages_id, $order;
81
82
        $productId = $GLOBALS["HTTP_GET_VARS"]["products_id"];
83
        $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...
84
85
        //Show promoted html
86
        if ($this->isPromoted($productId) && $checkoutPage!='1') {
87
            echo "<div id='promotedText' style='display:none'><br/>".$this->extraConfig['PAGANTIS_PROMOTED_PRODUCT_CODE']."</div>";
88
            echo '<script>'. PHP_EOL;
89
            echo '        function loadPromoted()'. PHP_EOL;
90
            echo '        {'. PHP_EOL;
91
            echo 'var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';'. PHP_EOL;
92
            echo 'if (positionSelector === \'default\') {'. PHP_EOL;
93
            echo 'positionSelector = \'.buttonSet\''. PHP_EOL;
94
            echo '}'. PHP_EOL;
95
            echo 'var docFather = document.querySelector(positionSelector);'.PHP_EOL;
96
            echo 'if (typeof docFather != \'undefined\') {'. PHP_EOL;
97
            echo 'var promotedNode = document.getElementById("promotedText");'.PHP_EOL;
98
            echo 'docFather.appendChild(promotedNode);'.PHP_EOL;
99
            echo 'promotedNode.style.display=""' . PHP_EOL;
100
            echo '               clearInterval(window.OSPromotedId);'. PHP_EOL;
101
            echo '               return true;'. PHP_EOL;
102
            echo '       }'. PHP_EOL;
103
            echo '               return false;'. PHP_EOL;
104
            echo '       }'. PHP_EOL;
105
            echo '       window.OSPromotedId = setInterval(function () {'. PHP_EOL;
106
            echo '          loadPromoted();'. PHP_EOL;
107
            echo '       }, 2000);'. PHP_EOL;
108
            echo '</script>'. PHP_EOL;
109
        }
110
111
        if (isset($productId) || $checkoutPage) {
112
            $simulatorCode = 'pgSDK';
113
            if ($languages_id == '2' || $languages_id == null) {
114
                $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...
115
                $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'] = 'pmtSDK.simulator.types.SIMPLE';
116
                $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'] = 'pmtSDK.simulator.skins.BLUE';
117
                $simulatorCode = 'pmtSDK';
118
            }
119
120
            //Promoted amount on checkout Pae
121
            $promotedAmount = 0;
122
            foreach ((array)$order->products as $item) {
123
                $productId = explode('{', $item['id'], 1);
124
                $productId = array_shift($productId);
125
                $promotedProduct = $this->isPromoted($productId);
126
                if ($promotedProduct) {
127
                    $promotedAmount+=number_format(($item['price'] * $item['qty']), 2);
128
                }
129
            }
130
131
            echo "<script src='".$this->sdkFile."'></script>". PHP_EOL;
132
            echo '<script>'. PHP_EOL;
133
            echo '        function loadSimulator()'. PHP_EOL;
134
            echo '        {'. PHP_EOL;
135
            echo '           if (typeof '.$simulatorCode.' != \'undefined\') {'. PHP_EOL;
136
            echo '               var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';'. PHP_EOL;
137
            echo '               var priceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';'. PHP_EOL;
138
            echo '               var checkoutPriceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';'. PHP_EOL;
139
            echo '               var quantitySelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']. '\';'. PHP_EOL;
140
            echo '               var checkoutPage =     \'' . $checkoutPage.'\';'. PHP_EOL;
141
            echo '               var promotedAmount =     \'' . $promotedAmount.'\';'. PHP_EOL;
142
143
            echo '               if (positionSelector === \'default\') {'. PHP_EOL;
144
            echo '                   positionSelector = \'.buttonSet\''. PHP_EOL;
145
            echo '               }'. PHP_EOL;
146
147
            echo '               if (priceSelector === \'default\') {'. PHP_EOL;
148
            echo '                   priceSelector = \'#bodyContent>form>div>h1\''. PHP_EOL;
149
            echo '               }'. PHP_EOL;
150
151
            echo '               if (checkoutPriceSelector == \'default\' && checkoutPage == \'1\')  {'. PHP_EOL;
152
            echo '                   priceSelector = \'#columnRight > .infoBoxContainer > .infoBoxContents > tbody > tr:last-child > td\';'. PHP_EOL;
153
            echo '               }'. PHP_EOL;
154
            echo '               '.$simulatorCode.'.product_simulator = {};'. PHP_EOL;
155
            echo '               '.$simulatorCode.'.product_simulator.id = \'product-simulator\';'. PHP_EOL;
156
            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

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

243
        /** @scrutinizer ignore-call */ 
244
        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...
244
245
    }
246
247
    /**
248
     * remove
249
     */
250
    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...
251
        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

251
        /** @scrutinizer ignore-call */ 
252
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
Loading history...
252
    }
253
254
    /**
255
     * @return array
256
     */
257
    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...
258
        return array('MODULE_HEADER_TAGS_PAGANTIS_STATUS');
259
    }
260
261
    /**
262
     * @param $productId
263
     *
264
     * @return bool
265
     */
266
    private function isPromoted($productId)
267
    {
268
        //HOOK WHILE PROMOTED AMOUNT IS NOT WORKING
269
        //return false;
270
271
        if (!isset($productId)) {
272
            return false;
273
        }
274
275
        if ($this->extraConfig['PAGANTIS_PROMOTION'] == '') {
276
            $promotedProducts = array();
277
        } else {
278
            $promotedProducts = array_values((array)unserialize($this->extraConfig['PAGANTIS_PROMOTION']));
279
        }
280
281
        return (in_array($productId, $promotedProducts));
282
    }
283
}
284
?>
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...