Completed
Push — master ( 4a0bdf...a7a78b )
by Harald
05:18 queued 02:35
created

newsletter::confirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 49
nc 1
nop 0
dl 0
loc 53
rs 9.5797
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
  use OSC\OM\HTML;
10
  use OSC\OM\Mail;
11
  use OSC\OM\OSCOM;
12
  use OSC\OM\Registry;
13
14
  class newsletter {
15
    var $show_choose_audience, $title, $content, $content_html;
16
17 View Code Duplication
    function __construct($title, $content, $content_html = null) {
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...
18
      $this->show_choose_audience = false;
19
      $this->title = $title;
20
      $this->content = $content;
21
      $this->content_html = $content_html;
22
    }
23
24
    function choose_audience() {
25
      return false;
26
    }
27
28
    function confirm() {
29
      $OSCOM_Db = Registry::get('Db');
30
31
      $Qmail = $OSCOM_Db->get('customers', 'count(*) as count', ['customers_newsletter' => '1']);
32
33
      $confirm_string = '<table border="0" cellspacing="0" cellpadding="2">' . "\n" .
34
                        '  <tr>' . "\n" .
35
                        '    <td class="main"><font color="#ff0000"><strong>' . OSCOM::getDef('text_count_customers', ['count' => $Qmail->valueInt('count')]) . '</strong></font></td>' . "\n" .
36
                        '  </tr>' . "\n" .
37
                        '  <tr>' . "\n" .
38
                        '    <td>&nbsp;</td>' . "\n" .
39
                        '  </tr>' . "\n" .
40
                        '  <tr>' . "\n" .
41
                        '    <td class="main"><strong>' . $this->title . '</strong></td>' . "\n" .
42
                        '  </tr>' . "\n" .
43
                        '  <tr>' . "\n" .
44
                        '    <td>&nbsp;</td>' . "\n" .
45
                        '  </tr>' . "\n" .
46
                        '  <tr>' . "\n" .
47
                        '    <td class="main">' . "\n" .
48
                        '      <ul class="nav nav-tabs" role="tablist">' . "\n" .
49
                        '        <li role="presentation" class="active"><a href="#html_preview" aria-controls="html_preview" role="tab" data-toggle="tab">' . OSCOM::getDef('email_type_html') . '</a></li>' . "\n" .
50
                        '        <li role="presentation"><a href="#plain_preview" aria-controls="plain_preview" role="tab" data-toggle="tab">' . OSCOM::getDef('email_type_plain') . '</a></li>' . "\n" .
51
                        '      </ul>' . "\n" .
52
                        '      <div class="tab-content">' . "\n" .
53
                        '        <div role="tabpanel" class="tab-pane active" id="html_preview">' . "\n" .
54
                        '          <iframe id="emailHtmlPreviewContent" style="width: 100%; height: 400px; border: 0;"></iframe>' . "\n" .
55
                        '          <script id="emailHtmlPreview" type="x-tmpl-mustache">' . "\n" .
56
                        '            ' . HTML::outputProtected($this->content_html) . "\n" .
57
                        '          </script>' . "\n" .
58
                        '          <script>' . "\n" .
59
                        '            $(function() {' . "\n" .
60
                        '              var content = $(\'<div />\').html($(\'#emailHtmlPreview\').html()).text();' . "\n" .
61
                        '              $(\'#emailHtmlPreviewContent\').contents().find(\'html\').html(content);' . "\n" .
62
                        '            });' . "\n" .
63
                        '          </script>' . "\n" .
64
                        '        </div>' . "\n" .
65
                        '        <div role="tabpanel" class="tab-pane" id="plain_preview">' . "\n" .
66
                        '          ' . nl2br(HTML::outputProtected($this->content)) . "\n" .
67
                        '        </div>' . "\n" .
68
                        '      </div>' . "\n" .
69
                        '    </td>' . "\n" .
70
                        '  </tr>' . "\n" .
71
                        '  <tr>' . "\n" .
72
                        '    <td>&nbsp;</td>' . "\n" .
73
                        '  </tr>' . "\n" .
74
                        '  <tr>' . "\n" .
75
                        '    <td class="smallText" align="right">' . HTML::button(OSCOM::getDef('image_send'), 'fa fa-envelope', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm_send')) . HTML::button(OSCOM::getDef('image_cancel'), 'fa fa-close', OSCOM::link(FILENAME_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'])) . '</td>' . "\n" .
76
                        '  </tr>' . "\n" .
77
                        '</table>';
78
79
      return $confirm_string;
80
    }
81
82
    function send($newsletter_id) {
83
      $OSCOM_Db = Registry::get('Db');
84
85
      $newsletterEmail = new Mail();
86
      $newsletterEmail->setFrom(STORE_OWNER_EMAIL_ADDRESS, STORE_OWNER);
87
      $newsletterEmail->setSubject($this->title);
88
89
      if (!empty($this->content)) {
90
        $newsletterEmail->setBodyPlain($this->content);
91
      }
92
93
      if (!empty($this->content_html)) {
94
        $newsletterEmail->setBodyHTML($this->content_html);
95
      }
96
97
      $Qmail = $OSCOM_Db->get('customers', [
98
        'customers_firstname',
99
        'customers_lastname',
100
        'customers_email_address'
101
      ], [
102
        'customers_newsletter' => '1'
103
      ]);
104
105 View Code Duplication
      while ($Qmail->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...
106
        $newsletterEmail->clearTo();
107
108
        $newsletterEmail->addTo($Qmail->value('customers_email_address'), $Qmail->value('customers_firstname') . ' ' . $Qmail->value('customers_lastname'));
109
110
        $newsletterEmail->send();
111
      }
112
113
      $OSCOM_Db->save('newsletters', [
114
        'date_sent' => 'now()',
115
        'status' => '1'
116
      ], [
117
        'newsletters_id' => (int)$newsletter_id
118
      ]);
119
    }
120
  }
121
?>
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...
122