Completed
Push — development ( 227e3a...98bb7e )
by Andrij
14:37
created

Feedback::_send_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Feedback::load_settings() 0 15 3
1
<?php
2
3
use cmsemail\email;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 *
12
 * Feedback module
13
 */
14
class Feedback extends MY_Controller
15
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
16
17
    public $username_max_len = 30;
18
19
    public $message_max_len = 600;
20
21
    public $theme_max_len = 150;
22
23
    public $admin_mail = 'admin@localhost';
24
25
    public $message = '';
26
27
    protected $formErrors = [];
28
29
    public function __construct() {
30
        parent::__construct();
31
        $this->load->module('core');
32
        $this->load_settings();
33
34
        $this->formErrors = [
35
                             'required'    => lang('Field is required'),
36
                             'min_length'  => lang('Length is less than the minimum'),
37
                             'valid_email' => lang('Email is not valid'),
38
                             'max_length'  => lang('Length greater than the maximum'),
39
                            ];
40
        $lang = new MY_Lang();
41
        $lang->load('feedback');
42
    }
43
44
    public function autoload() {
45
46
    }
47
48
    public function captcha_check($code) {
49
        if (!$this->dx_auth->captcha_check($code)) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return (bool) $this->dx_...->captcha_check($code);.
Loading history...
50
            return FALSE;
51
        } else {
52
            return TRUE;
53
        }
54
    }
55
56 View Code Duplication
    public function recaptcha_check() {
57
        $result = $this->dx_auth->is_recaptcha_match();
58
        if (!$result) {
59
            $this->form_validation->set_message('recaptcha_check', lang('Improper protection code', 'feedback'));
60
        }
61
62
        return $result;
63
    }
64
65
    // Index function
66
67
    public function index() {
68
        $this->template->registerMeta('ROBOTS', 'NOINDEX, NOFOLLOW');
69
70
        $this->core->set_meta_tags(lang('Feedback', 'feedback'));
71
72
        $this->load->library('form_validation');
73
74
        // Create captcha
75
        $this->dx_auth->captcha();
76
        $tpl_data['cap_image'] = $this->dx_auth->get_captcha_image();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tpl_data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tpl_data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
77
78
        $this->template->add_array($tpl_data);
79
80
        if (count($this->input->post()) > 0) {
81
            $this->form_validation->set_rules('name', lang('Your name', 'feedback'), 'trim|required|min_length[3]|max_length[' . $this->username_max_len . ']|xss_clean');
82
            $this->form_validation->set_rules('email', lang('Email', 'feedback'), 'trim|required|valid_email|xss_clean');
83
            $this->form_validation->set_rules('theme', lang('Subject', 'feedback'), 'trim|max_length[' . $this->theme_max_len . ']|xss_clean');
84
            $this->form_validation->set_rules('message', lang('Message', 'feedback'), 'trim|required|max_length[' . $this->message_max_len . ']|xss_clean');
85
86 View Code Duplication
            if ($this->dx_auth->use_recaptcha) {
87
                $this->form_validation->set_rules('recaptcha_response_field', lang('Protection code', 'feedback'), 'trim|xss_clean|required|callback_recaptcha_check');
88
            } else {
89
                $this->form_validation->set_rules('captcha', lang('Protection code', 'feedback'), 'trim|required|xss_clean|callback_captcha_check');
90
            }
91
92
            if ($this->form_validation->run($this) == FALSE) { // there are errors
0 ignored issues
show
introduced by
There should be no white space after an opening "{"
Loading history...
93
                $this->form_validation->set_error_delimiters('', '');
94
                CMSFactory\assetManager::create()->setData('validation', $this->form_validation);
95
                form_error();
96
            } else { // form is validate
0 ignored issues
show
introduced by
There should be no white space after an opening "{"
Loading history...
97
98
                $feedback_variables = [
99
                                       'Theme'       => $this->input->post('theme'),
100
                                       'userName'    => $this->input->post('name'),
101
                                       'userEmail'   => $this->input->post('email'),
102
                                       'userMessage' => $this->input->post('message'),
103
                                      ];
104
                 email::getInstance()->sendEmail($this->input->post('email'), 'feedback', $feedback_variables);
105
                CMSFactory\assetManager::create()->appendData('message_sent', TRUE);
0 ignored issues
show
Documentation introduced by
TRUE is of type boolean, but the function expects a string|integer|double.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
107
            }
108
        }
109
110
        CMSFactory\assetManager::create()->render('feedback');
111
    }
112
113
    private function load_settings() {
114
        $this->db->limit(1);
115
        $this->db->where('name', 'feedback');
116
        $query = $this->db->get('components')->row_array();
117
118
        $settings = unserialize($query['settings']);
119
120
        if (is_int($settings['message_max_len'])) {
121
            $this->message_max_len = $settings['message_max_len'];
122
        }
123
124
        if ($settings['email']) {
125
            $this->admin_mail = $settings['email'];
126
        }
127
    }
128
129
}
130
131
/* End of file sample_module.php */