Issues (2407)

application/controller/account/edit.php (3 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerAccountEdit extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        if (!$this->customer->isLogged()) {
30
            $this->session->data['redirect'] = $this->url->link('account/edit', '', true);
31
32
            $this->response->redirect($this->url->link('account/login', '', true));
33
        }
34
35
        $this->load->language('account/edit');
36
37
        $this->document->setTitle($this->language->get('heading_title'));
38
39
        $this->load->model('account/customer');
40
41
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
42
            $this->model_account_customer->editCustomer($this->request->post);
43
44
            $this->session->data['success'] = $this->language->get('text_success');
45
46
            $this->response->redirect($this->url->link('account/account', '', true));
47
        }
48
49
        $data['breadcrumbs'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
50
51
        $data['breadcrumbs'][] = array(
52
            'text'      => $this->language->get('text_home'),
53
            'href'      => $this->url->link('common/home')
54
        );
55
56
        $data['breadcrumbs'][] = array(
57
            'text'      => $this->language->get('text_account'),
58
            'href'      => $this->url->link('account/account', '', true)
59
        );
60
61
        $data['breadcrumbs'][] = array(
62
            'text'      => $this->language->get('text_edit'),
63
            'href'      => $this->url->link('account/edit', '', true)
64
        );
65
66
        $data['heading_title'] = $this->language->get('heading_title');
67
68
        $data['text_your_details'] = $this->language->get('text_your_details');
69
        $data['text_additional'] = $this->language->get('text_additional');
70
        $data['text_select'] = $this->language->get('text_select');
71
        $data['text_loading'] = $this->language->get('text_loading');
72
73
        $data['entry_firstname'] = $this->language->get('entry_firstname');
74
        $data['entry_lastname'] = $this->language->get('entry_lastname');
75
        $data['entry_email'] = $this->language->get('entry_email');
76
        $data['entry_telephone'] = $this->language->get('entry_telephone');
77
        $data['entry_fax'] = $this->language->get('entry_fax');
78
79
        $data['button_continue'] = $this->language->get('button_continue');
80
        $data['button_back'] = $this->language->get('button_back');
81
        $data['button_upload'] = $this->language->get('button_upload');
82
83
        if (isset($this->error['warning'])) {
84
            $data['error_warning'] = $this->error['warning'];
85
        } else {
86
            $data['error_warning'] = '';
87
        }
88
89
        if (isset($this->error['firstname'])) {
90
            $data['error_firstname'] = $this->error['firstname'];
91
        } else {
92
            $data['error_firstname'] = '';
93
        }
94
95
        if (isset($this->error['lastname'])) {
96
            $data['error_lastname'] = $this->error['lastname'];
97
        } else {
98
            $data['error_lastname'] = '';
99
        }
100
101
        if (isset($this->error['email'])) {
102
            $data['error_email'] = $this->error['email'];
103
        } else {
104
            $data['error_email'] = '';
105
        }
106
107
        if (isset($this->error['telephone'])) {
108
            $data['error_telephone'] = $this->error['telephone'];
109
        } else {
110
            $data['error_telephone'] = '';
111
        }
112
113
        if (isset($this->error['custom_field'])) {
114
            $data['error_custom_field'] = $this->error['custom_field'];
115
        } else {
116
            $data['error_custom_field'] = array();
117
        }
118
119
        $data['action'] = $this->url->link('account/edit', '', true);
120
121
        if ($this->request->server['REQUEST_METHOD'] != 'POST') {
122
            $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
123
        }
124
125
        if (isset($this->request->post['firstname'])) {
126
            $data['firstname'] = $this->request->post['firstname'];
127
        } elseif (!empty($customer_info)) {
128
            $data['firstname'] = $customer_info['firstname'];
129
        } else {
130
            $data['firstname'] = '';
131
        }
132
133
        if (isset($this->request->post['lastname'])) {
134
            $data['lastname'] = $this->request->post['lastname'];
135
        } elseif (!empty($customer_info)) {
136
            $data['lastname'] = $customer_info['lastname'];
137
        } else {
138
            $data['lastname'] = '';
139
        }
140
141
        if (isset($this->request->post['email'])) {
142
            $data['email'] = $this->request->post['email'];
143
        } elseif (!empty($customer_info)) {
144
            $data['email'] = $customer_info['email'];
145
        } else {
146
            $data['email'] = '';
147
        }
148
149
        if (isset($this->request->post['telephone'])) {
150
            $data['telephone'] = $this->request->post['telephone'];
151
        } elseif (!empty($customer_info)) {
152
            $data['telephone'] = $customer_info['telephone'];
153
        } else {
154
            $data['telephone'] = '';
155
        }
156
157
        if (isset($this->request->post['fax'])) {
158
            $data['fax'] = $this->request->post['fax'];
159
        } elseif (!empty($customer_info)) {
160
            $data['fax'] = $customer_info['fax'];
161
        } else {
162
            $data['fax'] = '';
163
        }
164
165
        // Custom Fields
166
        $this->load->model('account/custom_field');
167
168
        $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
169
170
        if (isset($this->request->post['custom_field'])) {
171
            $data['account_custom_field'] = $this->request->post['custom_field'];
172
        } elseif (isset($customer_info)) {
173
            $data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
174
        } else {
175
            $data['account_custom_field'] = array();
176
        }
177
178
        $data['back'] = $this->url->link('account/account', '', true);
179
180
        $data['column'] = $this->load->controller('common/column');
181
        
182
        $data['content_top'] = $this->load->controller('common/content_top');
183
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
184
        $data['footer'] = $this->load->controller('common/footer');
185
        $data['header'] = $this->load->controller('common/header');
186
187
        $this->response->setOutput($this->load->view('account/edit', $data));
188
    }
189
190
    protected function validate()
191
    {
192
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
193
            $this->error['firstname'] = $this->language->get('error_firstname');
194
        }
195
196
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
197
            $this->error['lastname'] = $this->language->get('error_lastname');
198
        }
199
200
        if ((\voku\helper\UTF8::strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
201
            $this->error['email'] = $this->language->get('error_email');
202
        }
203
204
        if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
205
            $this->error['warning'] = $this->language->get('error_exists');
206
        }
207
208
        if ((\voku\helper\UTF8::strlen($this->request->post['telephone']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['telephone']) > 32)) {
209
            $this->error['telephone'] = $this->language->get('error_telephone');
210
        }
211
212
        // Custom field validation
213
        $this->load->model('account/custom_field');
214
215
        $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
216
217
        foreach ($custom_fields as $custom_field) {
218
            if (($custom_field['location'] == 'account') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
219
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
220
            } elseif (($custom_field['location'] == 'account') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
221
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
222
            }
223
        }
224
225
        return !$this->error;
226
    }
227
}
228