Completed
Push — master ( 85914a...2ce878 )
by Harald
06:39 queued 03:01
created

ht_google_adwords_conversion::execute()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 6
nop 0
dl 0
loc 45
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2015 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  use OSC\OM\HTML;
14
  use OSC\OM\Registry;
15
16
  class ht_google_adwords_conversion {
17
    var $code = 'ht_google_adwords_conversion';
18
    var $group = 'footer_scripts';
19
    var $title;
20
    var $description;
21
    var $sort_order;
22
    var $enabled = false;
23
24
    protected $lang;
25
26 View Code Duplication
    function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
      $this->lang = Registry::get('Language');
28
29
      $this->title = MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_TITLE;
30
      $this->description = MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_DESCRIPTION;
31
32
      if ( defined('MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_STATUS') ) {
33
        $this->sort_order = MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_SORT_ORDER;
34
        $this->enabled = (MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_STATUS == 'True');
35
      }
36
    }
37
38
    function execute() {
39
      global $PHP_SELF, $oscTemplate;
40
41
      $OSCOM_Db = Registry::get('Db');
42
43
      if (MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_JS_PLACEMENT != 'Footer') {
44
        $this->group = 'header_tags';
45
      }
46
47
      if ( ($PHP_SELF == 'checkout_success.php') && isset($_SESSION['customer_id']) ) {
48
        $Qorder = $OSCOM_Db->get('orders', ['orders_id', 'currency', 'currency_value'], ['customers_id' => $_SESSION['customer_id']], 'date_purchased desc', 1);
49
50
        if ($Qorder->fetch() !== false) {
51
          $Qsubtotal = $OSCOM_Db->get('orders_total', 'value', ['orders_id' => $Qorder->valueInt('orders_id'), 'class' => 'ot_subtotal']);
52
53
          $conversion_id = (int)MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_ID;
54
          $conversion_language = HTML::outputProtected($this->lang->get('code'));
55
          $conversion_format = (int)MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_FORMAT;
56
          $conversion_color = HTML::outputProtected(MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_COLOR);
57
          $conversion_label = HTML::outputProtected(MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_LABEL);
58
          $conversion_value = $this->format_raw($Qsubtotal->value('value'), $Qorder->value('currency'), $Qorder->value('currency_value'));
59
60
          $output = <<<EOD
61
<script>
62
/* <![CDATA[ */
63
var google_conversion_id = {$conversion_id};
64
var google_conversion_language = "{$conversion_language}";
65
var google_conversion_format = "{$conversion_format}";
66
var google_conversion_color = "{$conversion_color}";
67
var google_conversion_label = "{$conversion_label}";
68
var google_conversion_value = {$conversion_value};
69
/* ]]> */
70
</script>
71
<script src="//www.googleadservices.com/pagead/conversion.js"></script>
72
<noscript>
73
<div style="display:inline;">
74
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/{$conversion_id}/?value={$conversion_value}&amp;label={$conversion_label}&amp;guid=ON&amp;script=0"/>
75
</div>
76
</noscript>
77
EOD;
78
79
          $oscTemplate->addBlock($output, $this->group);
80
        }
81
      }
82
    }
83
84 View Code Duplication
    function format_raw($number, $currency_code = '', $currency_value = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
      global $currencies;
86
87
      if (empty($currency_code) || !$currencies->is_set($currency_code)) {
88
        $currency_code = $_SESSION['currency'];
89
      }
90
91
      if (empty($currency_value) || !is_numeric($currency_value)) {
92
        $currency_value = $currencies->currencies[$currency_code]['value'];
93
      }
94
95
      return number_format(tep_round($number * $currency_value, $currencies->currencies[$currency_code]['decimal_places']), $currencies->currencies[$currency_code]['decimal_places'], '.', '');
96
    }
97
98
    function isEnabled() {
99
      return $this->enabled;
100
    }
101
102
    function check() {
103
      return defined('MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_STATUS');
104
    }
105
106 View Code Duplication
    function install() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
      $OSCOM_Db = Registry::get('Db');
108
109
      $OSCOM_Db->save('configuration', [
110
        'configuration_title' => 'Enable Google AdWords Conversion Module',
111
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_STATUS',
112
        'configuration_value' => 'True',
113
        'configuration_description' => 'Do you want to allow the Google AdWords Conversion Module on your checkout success page?',
114
        'configuration_group_id' => '6',
115
        'sort_order' => '1',
116
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
117
        'date_added' => 'now()'
118
      ]);
119
120
      $OSCOM_Db->save('configuration', [
121
        'configuration_title' => 'Conversion ID',
122
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_ID',
123
        'configuration_value' => '',
124
        'configuration_description' => 'The Google AdWords Conversion ID',
125
        'configuration_group_id' => '6',
126
        'sort_order' => '0',
127
        'date_added' => 'now()'
128
      ]);
129
130
      $OSCOM_Db->save('configuration', [
131
        'configuration_title' => 'Tracking Notification Layout',
132
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_FORMAT',
133
        'configuration_value' => '1',
134
        'configuration_description' => 'A small message will appear on your site telling customers that their visits on your site are being tracked. We recommend you use it.',
135
        'configuration_group_id' => '6',
136
        'sort_order' => '0',
137
        'set_function' => 'tep_cfg_google_adwords_conversion_set_format(',
138
        'use_function' => 'tep_cfg_google_adwords_conversion_get_format',
139
        'date_added' => 'now()'
140
      ]);
141
142
      $OSCOM_Db->save('configuration', [
143
        'configuration_title' => 'Page Background Color',
144
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_COLOR',
145
        'configuration_value' => 'ffffff',
146
        'configuration_description' => 'Enter a HTML color to match the color of your website background page.',
147
        'configuration_group_id' => '6',
148
        'sort_order' => '0',
149
        'date_added' => 'now()'
150
      ]);
151
152
      $OSCOM_Db->save('configuration', [
153
        'configuration_title' => 'Conversion Label',
154
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_LABEL',
155
        'configuration_value' => '',
156
        'configuration_description' => 'The alphanumeric code generated by Google for your AdWords Conversion',
157
        'configuration_group_id' => '6',
158
        'sort_order' => '0',
159
        'date_added' => 'now()'
160
      ]);
161
162
      $OSCOM_Db->save('configuration', [
163
        'configuration_title' => 'Javascript Placement',
164
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_JS_PLACEMENT',
165
        'configuration_value' => 'Footer',
166
        'configuration_description' => 'Should the Google AdWords Conversion javascript be loaded in the header or footer?',
167
        'configuration_group_id' => '6',
168
        'sort_order' => '0',
169
        'set_function' => 'tep_cfg_select_option(array(\'Header\', \'Footer\'), ',
170
        'date_added' => 'now()'
171
      ]);
172
173
      $OSCOM_Db->save('configuration', [
174
        'configuration_title' => 'Sort Order',
175
        'configuration_key' => 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_SORT_ORDER',
176
        'configuration_value' => '0',
177
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
178
        'configuration_group_id' => '6',
179
        'sort_order' => '0',
180
        'date_added' => 'now()'
181
      ]);
182
    }
183
184
    function remove() {
185
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
186
    }
187
188
    function keys() {
189
      return array('MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_STATUS', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_ID', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_FORMAT', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_COLOR', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_LABEL', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_JS_PLACEMENT', 'MODULE_HEADER_TAGS_GOOGLE_ADWORDS_CONVERSION_SORT_ORDER');
190
    }
191
  }
192
193
  function tep_cfg_google_adwords_conversion_set_format($key_value, $field_key) {
194
    $format = array('1' => 'Single Line', '2' => 'Two Lines', '3' => 'No Indicator');
195
196
    $string = '';
197
198
    foreach ( $format as $key => $value ) {
199
      $string .= '<br /><input type="radio" name="configuration[' . $field_key . ']" value="' . $key . '"';
200
201
      if ($key_value == $key) $string .= ' checked="checked"';
202
203
      $string .= ' /> ' . $value;
204
    }
205
206
    return $string;
207
  }
208
209
  function tep_cfg_google_adwords_conversion_get_format($value) {
210
    $format = array('1' => 'Single Line', '2' => 'Two Lines', '3' => 'No Indicator');
211
212
    return $format[$value];
213
  }
214
?>
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...
215