Passed
Pull Request — master (#4)
by Raúl
03:43
created

ht_pagantis::getExtraConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 9.9666
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
15
class ht_pagantis {
16
    var $code = 'ht_pagantis';
17
    var $group = 'header_tags';
18
    var $title;
19
    var $description;
20
    var $enabled = false;
21
22
23
    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...
24
        $this->title = MODULE_HEADER_TAGS_PAGANTIS_TITLE;
25
        $this->description = MODULE_HEADER_TAGS_PAGANTIS_DESCRIPTION;
26
        $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...
27
28
        if ( defined('MODULE_HEADER_TAGS_PAGANTIS_STATUS') ) {
29
            $this->enabled = (MODULE_HEADER_TAGS_PAGANTIS_STATUS == 'True');
30
        }
31
        $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...
32
        $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...
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    private function getExtraConfig()
39
    {
40
        $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

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

42
        if (/** @scrutinizer ignore-call */ tep_db_num_rows($checkTable) > 0) {
Loading history...
43
            $query       = "select * from ".TABLE_PAGANTIS_CONFIG;
44
            $result      = tep_db_query($query);
45
            $response    = array();
46
            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

46
            while ($resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result)) {
Loading history...
47
                $response[$resultArray['config']] = $resultArray['value'];
48
            }
49
        }
50
        return $response;
51
    }
52
53
    /**
54
     * @param string $config
55
     * @return array
56
     */
57
    private function getConfig($config = '')
58
    {
59
            $query       = "select configuration_value 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...
60
            $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

60
            $result      = /** @scrutinizer ignore-call */ tep_db_query($query);
Loading history...
61
            $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
            $resultArray = /** @scrutinizer ignore-call */ tep_db_fetch_array($result);
Loading history...
62
            return $resultArray['configuration_value'];
63
    }
64
65
    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...
66
        echo "<script src='https://cdn.pagantis.com/js/pg-v2/sdk.js'></script>";
67
        echo '<script>';
68
        echo '        function findPriceSelector()';
69
        echo '        { ';
70
        echo '           var priceDOM = document.getElementById("our_price_display");';
71
        echo '            if (priceDOM != null) {';
72
        echo '                return \'#our_price_display\';';
73
        echo '            } else { ';
74
        echo '                priceDOM = document.querySelector(".current-price span[itemprop=price]");';
75
        echo '              if (priceDOM != null) { ';
76
        echo '                  return ".current-price span[itemprop=price]"; ';
77
        echo '              }';
78
        echo '            }';
79
        echo '          return \'default\';';
80
        echo '        }';
81
82
        echo '        function findQuantitySelector()';
83
        echo '        {';
84
        echo '            var quantityDOM = document.getElementById("quantity_wanted");';
85
        echo '            if (quantityDOM != null) {';
86
        echo '                return \'#quantity_wanted\';';
87
        echo '            }';
88
        echo '            return \'default\';';
89
        echo '        }';
90
91
        echo '        function loadSimulator()';
92
        echo '        {';
93
        echo '           if (typeof pgSDK != \'undefined\') {';
94
        echo '               var price = null;';
95
        echo '               var quantity = null;';
96
        echo '               var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';';
97
        echo '               var priceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';';
98
        echo '               var quantitySelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']. '\';';
99
100
        echo '               if (positionSelector === \'default\') {';
101
        echo '                   positionSelector = \'.pagantisSimulator\'';
102
        echo '               }';
103
104
        echo '               if (priceSelector === \'default\') {';
105
        echo '                   priceSelector = findPriceSelector();';
106
        echo '                }';
107
108
        echo '               if (quantitySelector === \'default\') {';
109
        echo '               quantitySelector = findQuantitySelector();';
110
        echo '                   if (quantitySelector === \'default\') {';
111
        echo '                       quantity = \'1\';';
112
        echo '                    }';
113
        echo '                }';
114
115
        echo '               pgSDK.product_simulator = {};';
116
        echo '               pgSDK.product_simulator.id = \'product-simulator\';';
117
        echo '               pgSDK.product_simulator.publicKey = \'' . $this->pk . '\';';
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

117
        echo '               pgSDK.product_simulator.publicKey = \'' . /** @scrutinizer ignore-type */ $this->pk . '\';';
Loading history...
118
        echo '               pgSDK.product_simulator.selector = positionSelector;';
119
        echo '               pgSDK.product_simulator.numInstalments = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'] . '\';';
120
        echo '               pgSDK.product_simulator.type = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'] . ';';
121
        echo '               pgSDK.product_simulator.skin = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'] . ';';
122
        echo '               pgSDK.product_simulator.position = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'] . ';';
123
124
        echo '               if (priceSelector !== \'default\') {';
125
        echo '                   pgSDK.product_simulator.itemAmountSelector = priceSelector;';
126
        echo '                }';
127
        echo '               if (quantitySelector !== \'default\' && quantitySelector !== \'none\') {';
128
        echo '                   pgSDK.product_simulator.itemQuantitySelector = quantitySelector;';
129
        echo '               }';
130
        echo '               if (price != null) {';
131
        echo '                   pgSDK.product_simulator.itemAmount = price;';
132
        echo '               }';
133
        echo '               if (quantity != null) {';
134
        echo '                   pgSDK.product_simulator.itemQuantity = quantity;';
135
        echo '               }';
136
137
        echo '               pgSDK.simulator.init(pgSDK.product_simulator);';
138
        echo '               clearInterval(window.PSSimulatorId);';
139
        echo '               return true;';
140
        echo '           }';
141
        echo '           return false;';
142
        echo '       }';
143
        echo '       if (!loadSimulator()) {';
144
        echo '           window.PSSimulatorId = setInterval(function () {';
145
        echo '               loadSimulator();';
146
        echo '           }, 2000);';
147
        echo '       }';
148
        echo '</script>';
149
        echo '<div class="pagantisSimulator"></div>';
150
    }
151
152
    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...
153
        return $this->enabled;
154
    }
155
156
    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...
157
        return defined('MODULE_HEADER_TAGS_PAGANTIS_STATUS');
158
    }
159
160
    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...
161
        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 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

161
        /** @scrutinizer ignore-call */ 
162
        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...
Bug introduced by
The constant TABLE_CONFIGURATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
162
163
    }
164
165
    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...
166
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
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

166
        /** @scrutinizer ignore-call */ 
167
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
Loading history...
Bug introduced by
The constant TABLE_CONFIGURATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
167
    }
168
169
    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...
170
        return array('MODULE_HEADER_TAGS_PAGANTIS_STATUS');
171
    }
172
}
173
?>
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...
174