Completed
Push — master ( 40dd32...757859 )
by Harald
13:15 queued 06:40
created

product_notification::product_notification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 9.4285
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) 2002 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  use OSC\OM\HTML;
14
  use OSC\OM\Mail;
15
  use OSC\OM\OSCOM;
16
  use OSC\OM\Registry;
17
18
  class product_notification {
19
    var $show_choose_audience, $title, $content;
20
21
    function product_notification($title, $content) {
22
      $this->show_choose_audience = true;
23
      $this->title = $title;
24
      $this->content = $content;
25
    }
26
27
    function choose_audience() {
28
      $OSCOM_Db = Registry::get('Db');
29
      $OSCOM_Language = Registry::get('Language');
30
31
      $products_array = [];
32
33
      $Qproducts = $OSCOM_Db->get([
34
        'products p',
35
        'products_description pd'
36
      ], [
37
        'pd.products_id',
38
        'pd.products_name'
39
      ], [
40
        'pd.language_id' => $OSCOM_Language->getId(),
41
        'pd.products_id' => [
42
          'rel' => 'p.products_id'
43
        ],
44
        'p.products_status' => '1'
45
      ], 'pd.products_name');
46
47
      while ($Qproducts->fetch()) {
48
        $products_array[] = [
49
          'id' => $Qproducts->valueInt('products_id'),
50
          'text' => $Qproducts->value('products_name')
51
        ];
52
      }
53
54
$choose_audience_string = '<script type="text/javascript"><!--
55
function mover(move) {
56
  if (move == \'remove\') {
57
    for (x=0; x<(document.notifications.products.length); x++) {
58
      if (document.notifications.products.options[x].selected) {
59
        with(document.notifications.elements[\'chosen[]\']) {
60
          options[options.length] = new Option(document.notifications.products.options[x].text,document.notifications.products.options[x].value);
61
        }
62
        document.notifications.products.options[x] = null;
63
        x = -1;
64
      }
65
    }
66
  }
67
  if (move == \'add\') {
68
    for (x=0; x<(document.notifications.elements[\'chosen[]\'].length); x++) {
69
      if (document.notifications.elements[\'chosen[]\'].options[x].selected) {
70
        with(document.notifications.products) {
71
          options[options.length] = new Option(document.notifications.elements[\'chosen[]\'].options[x].text,document.notifications.elements[\'chosen[]\'].options[x].value);
72
        }
73
        document.notifications.elements[\'chosen[]\'].options[x] = null;
74
        x = -1;
75
      }
76
    }
77
  }
78
  return true;
79
}
80
81
function selectAll(FormName, SelectBox) {
82
  temp = "document." + FormName + ".elements[\'" + SelectBox + "\']";
83
  Source = eval(temp);
84
85
  for (x=0; x<(Source.length); x++) {
86
    Source.options[x].selected = "true";
87
  }
88
89
  if (x<1) {
90
    alert(\'' . JS_PLEASE_SELECT_PRODUCTS . '\');
91
    return false;
92
  } else {
93
    return true;
94
  }
95
}
96
//--></script>';
97
98
      $global_button = HTML::button(BUTTON_GLOBAL, 'fa fa-globe', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm&global=true'));
99
100
      $cancel_button = HTML::button(IMAGE_CANCEL, 'fa fa-close', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID']));
101
102
      $choose_audience_string .= '<form name="notifications" action="' . OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm') . '" method="post" onsubmit="return selectAll(\'notifications\', \'chosen[]\')"><table border="0" width="100%" cellspacing="0" cellpadding="2">' . "\n" .
103
                                 '  <tr>' . "\n" .
104
                                 '    <td align="center" class="smallText"><strong>' . TEXT_PRODUCTS . '</strong><br />' . HTML::selectField('products', $products_array, '', 'size="20" style="width: 20em;" multiple') . '</td>' . "\n" .
105
                                 '    <td align="center" class="smallText">&nbsp;<br />' . $global_button . '<br /><br /><br /><input type="button" value="' . BUTTON_SELECT . '" style="width: 8em;" onClick="mover(\'remove\');"><br /><br /><input type="button" value="' . BUTTON_UNSELECT . '" style="width: 8em;" onClick="mover(\'add\');"><br /><br /><br />' . HTML::button(IMAGE_SEND, 'fa fa-envelope') . '<br /><br />' . $cancel_button . '</td>' . "\n" .
106
                                 '    <td align="center" class="smallText"><strong>' . TEXT_SELECTED_PRODUCTS . '</strong><br />' . HTML::selectField('chosen[]', array(), '', 'size="20" style="width: 20em;" multiple') . '</td>' . "\n" .
107
                                 '  </tr>' . "\n" .
108
                                 '</table></form>';
109
110
      return $choose_audience_string;
111
    }
112
113
    function confirm() {
114
      $OSCOM_Db = Registry::get('Db');
115
116
      $audience = array();
117
118
      if (isset($_GET['global']) && ($_GET['global'] == 'true')) {
119
        $Qproducts = $OSCOM_Db->get('products_notifications', 'distinct customers_id');
120
121
        while ($Qproducts->fetch()) {
122
          $audience[$Qproducts->valueInt('customers_id')] = '1';
123
        }
124
125
        $Qcustomers = $OSCOM_Db->get('customers_info', 'customers_info_id', ['global_product_notifications' => '1']);
126
127
        while ($Qcustomers->fetch()) {
128
          $audience[$Qcustomers->valueInt('customers_info_id')] = '1';
129
        }
130
      } else {
131
        $chosen = [];
132
133 View Code Duplication
        foreach ($_POST['chosen'] as $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
          if (is_numeric($id) && !in_array($id, $chosen)) {
135
            $chosen[] = $id;
136
          }
137
        }
138
139
        $ids = array_map(function($k) {
140
          return ':products_id_' . $k;
141
        }, array_keys($chosen));
142
143
        $Qproducts = $OSCOM_Db->prepare('select distinct customers_id from :table_products_notifications where products_id in (' . implode(', ', $ids) . ')');
144
145
        foreach ($chosen as $k => $v) {
146
          $Qproducts->bindInt(':products_id_' . $k, $v);
147
        }
148
149
        $Qproducts->execute();
150
151
        while ($Qproducts->fetch()) {
152
          $audience[$Qproducts->valueInt('customers_id')] = '1';
153
        }
154
155
        $Qcustomers = $OSCOM_Db->get('customers_info', 'customers_info_id', ['global_product_notifications' => '1']);
156
157
        while ($Qcustomers->fetch()) {
158
          $audience[$Qcustomers->valueInt('customers_info_id')] = '1';
159
        }
160
      }
161
162
      $confirm_string = '<table border="0" cellspacing="0" cellpadding="2">' . "\n" .
163
                        '  <tr>' . "\n" .
164
                        '    <td class="main"><font color="#ff0000"><strong>' . sprintf(TEXT_COUNT_CUSTOMERS, sizeof($audience)) . '</strong></font></td>' . "\n" .
165
                        '  </tr>' . "\n" .
166
                        '  <tr>' . "\n" .
167
                        '    <td>&nbsp;</td>' . "\n" .
168
                        '  </tr>' . "\n" .
169
                        '  <tr>' . "\n" .
170
                        '    <td class="main"><strong>' . $this->title . '</strong></td>' . "\n" .
171
                        '  </tr>' . "\n" .
172
                        '  <tr>' . "\n" .
173
                        '    <td>&nbsp;</td>' . "\n" .
174
                        '  </tr>' . "\n" .
175
                        '  <tr>' . "\n" .
176
                        '    <td class="main"><tt>' . nl2br($this->content) . '</tt></td>' . "\n" .
177
                        '  </tr>' . "\n" .
178
                        '  <tr>' . "\n" .
179
                        '    <td>&nbsp;</td>' . "\n" .
180
                        '  </tr>' . "\n" .
181
                        '  <tr>' . HTML::form('confirm', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm_send')) . "\n" .
182
                        '    <td class="smallText" align="right">';
183
      if (sizeof($audience) > 0) {
184
        if (isset($_GET['global']) && ($_GET['global'] == 'true')) {
185
          $confirm_string .= HTML::hiddenField('global', 'true');
186
        } else {
187
          for ($i = 0, $n = sizeof($chosen); $i < $n; $i++) {
0 ignored issues
show
Bug introduced by
The variable $chosen does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
188
            $confirm_string .= HTML::hiddenField('chosen[]', $chosen[$i]);
189
          }
190
        }
191
        $confirm_string .= HTML::button(IMAGE_SEND, 'fa fa-envelope');
192
      }
193
      $confirm_string .= HTML::button(IMAGE_CANCEL, 'fa fa-close', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=send')) . '</td>' . "\n" .
194
                         '  </tr>' . "\n" .
195
                         '</table>';
196
197
      return $confirm_string;
198
    }
199
200
    function send($newsletter_id) {
201
      $OSCOM_Db = Registry::get('Db');
202
203
      $audience = array();
204
205
      if (isset($_POST['global']) && ($_POST['global'] == 'true')) {
206
        $Qproducts = $OSCOM_Db->get([
207
          'customers c',
208
          'products_notifications pn'
209
        ], [
210
          'distinct pn.customers_id',
211
          'c.customers_firstname',
212
          'c.customers_lastname',
213
          'c.customers_email_address'
214
        ], [
215
          'c.customers_id' => [
216
            'rel' => 'pn.customers_id'
217
          ]
218
        ]);
219
220 View Code Duplication
        while ($Qproducts->fetch()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
221
          $audience[$Qproducts->valueInt('customers_id')] = [
222
            'firstname' => $Qproducts->value('customers_firstname'),
223
            'lastname' => $Qproducts->value('customers_lastname'),
224
            'email_address' => $Qproducts->value('customers_email_address')
225
          ];
226
        }
227
228
        $Qcustomers = $OSCOM_Db->get([
229
          'customers c',
230
          'customers_info ci'
231
        ], [
232
          'c.customers_id',
233
          'c.customers_firstname',
234
          'c.customers_lastname',
235
          'c.customers_email_address'
236
        ], [
237
          'c.customers_id' => [
238
            'rel' => 'ci.customers_info_id'
239
          ],
240
          'ci.global_product_notifications' => '1'
241
        ]);
242
243 View Code Duplication
        while ($Qcustomers->fetch()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
244
          $audience[$Qcustomers->valueInt('customers_id')] = [
245
            'firstname' => $Qcustomers->value('customers_firstname'),
246
            'lastname' => $Qcustomers->value('customers_lastname'),
247
            'email_address' => $Qcustomers->value('customers_email_address')
248
          ];
249
        }
250
      } else {
251
        $chosen = [];
252
253 View Code Duplication
        foreach ($_POST['chosen'] as $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
254
          if (is_numeric($id) && !in_array($id, $chosen)) {
255
            $chosen[] = $id;
256
          }
257
        }
258
259
        $ids = array_map(function($k) {
260
          return ':products_id_' . $k;
261
        }, array_keys($chosen));
262
263
        $Qproducts = $OSCOM_Db->prepare('select distinct pn.customers_id, c.customers_firstname, c.customers_lastname, c.customers_email_address from :table_customers c, :table_products_notifications pn where c.customers_id = pn.customers_id and pn.products_id in (' . implode(', ', $ids) . ')');
264
265
        foreach ($chosen as $k => $v) {
266
          $Qproducts->bindInt(':products_id_' . $k, $v);
267
        }
268
269
        $Qproducts->execute();
270
271 View Code Duplication
        while ($Qproducts->fetch()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
272
          $audience[$Qproducts->valueInt('customers_id')] = [
273
            'firstname' => $Qproducts->value('customers_firstname'),
274
            'lastname' => $Qproducts->value('customers_lastname'),
275
            'email_address' => $Qproducts->value('customers_email_address')
276
          ];
277
        }
278
279
        $Qcustomers = $OSCOM_Db->get([
280
          'customers c',
281
          'customers_info ci'
282
        ], [
283
          'c.customers_id',
284
          'c.customers_firstname',
285
          'c.customers_lastname',
286
          'c.customers_email_address'
287
        ], [
288
          'c.customers_id' => [
289
            'rel' => 'ci.customers_info_id'
290
          ],
291
          'ci.global_product_notifications' => '1'
292
        ]);
293
294 View Code Duplication
        while ($Qcustomers->fetch()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
295
          $audience[$Qcustomers->valueInt('customers_id')] = [
296
            'firstname' => $Qcustomers->value('customers_firstname'),
297
            'lastname' => $Qcustomers->value('customers_lastname'),
298
            'email_address' => $Qcustomers->value('customers_email_address')
299
          ];
300
        }
301
      }
302
303
      $notificationEmail = new Mail();
304
      $notificationEmail->setFrom(STORE_OWNER_EMAIL_ADDRESS, STORE_OWNER);
305
      $notificationEmail->setSubject($this->title);
306
      $notificationEmail->setBody($this->content);
307
308
      foreach ( $audience as $key => $value ) {
309
        $notificationEmail->clearTo();
310
311
        $notificationEmail->addTo($value['email_address'], $value['firstname'] . ' ' . $value['lastname']);
312
313
        $notificationEmail->send();
314
      }
315
316
      $OSCOM_Db->save('newsletters', [
317
        'date_sent' => 'now()',
318
        'status' => '1'
319
      ], [
320
        'newsletters_id' => (int)$newsletter_id
321
      ]);
322
    }
323
  }
324
?>
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...
325