ModelAccountAddress::addAddress()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 28
rs 9.6666
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 ModelAccountAddress extends \Divine\Engine\Core\Model
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
    public function addAddress($data)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->db->query("
28
            INSERT INTO address
29
            SET customer_id = '" . (int)$this->customer->getId() . "',
30
                firstname = '" . $this->db->escape($data['firstname']) . "',
31
                lastname = '" . $this->db->escape($data['lastname']) . "',
32
                company = '" . $this->db->escape($data['company']) . "',
33
                address_1 = '" . $this->db->escape($data['address_1']) . "',
34
                address_2 = '" . $this->db->escape($data['address_2']) . "',
35
                postcode = '" . $this->db->escape($data['postcode']) . "',
36
                city = '" . $this->db->escape($data['city']) . "',
37
                zone_id = '" . (int)$data['zone_id'] . "',
38
                country_id = '" . (int)$data['country_id'] . "',
39
                custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "'
40
        ");
41
42
        $address_id = $this->db->getLastId();
43
44
        if (!empty($data['default'])) {
45
            $this->db->query("
46
                UPDATE customer 
47
                SET address_id = '" . (int)$address_id . "' 
48
                WHERE customer_id = '" . (int)$this->customer->getId() . "'
49
            ");
50
        }
51
52
        return $address_id;
53
    }
54
55
    public function editAddress($address_id, $data)
56
    {
57
        $this->db->query("
58
            UPDATE address 
59
            SET firstname = '" . $this->db->escape($data['firstname']) . "', 
60
                lastname = '" . $this->db->escape($data['lastname']) . "', 
61
                company = '" . $this->db->escape($data['company']) . "', 
62
                address_1 = '" . $this->db->escape($data['address_1']) . "', 
63
                address_2 = '" . $this->db->escape($data['address_2']) . "', 
64
                postcode = '" . $this->db->escape($data['postcode']) . "', 
65
                city = '" . $this->db->escape($data['city']) . "', 
66
                zone_id = '" . (int)$data['zone_id'] . "', 
67
                country_id = '" . (int)$data['country_id'] . "', 
68
                custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "' 
69
            WHERE address_id  = '" . (int)$address_id . "' 
70
                AND customer_id = '" . (int)$this->customer->getId() . "'
71
        ");
72
73
        if (!empty($data['default'])) {
74
            $this->db->query("
75
                UPDATE customer 
76
                SET address_id = '" . (int)$address_id . "' 
77
                WHERE customer_id = '" . (int)$this->customer->getId() . "'
78
            ");
79
        }
80
    }
81
82
    public function deleteAddress($address_id)
83
    {
84
        $this->db->query("
85
            DELETE 
86
            FROM address 
87
            WHERE address_id = '" . (int)$address_id . "' 
88
                AND customer_id = '" . (int)$this->customer->getId() . "'
89
        ");
90
    }
91
92
    public function getAddress($address_id)
93
    {
94
        $address_query = $this->db->query("
95
            SELECT DISTINCT * 
96
            FROM address 
97
            WHERE address_id = '" . (int)$address_id . "' 
98
                AND customer_id = '" . (int)$this->customer->getId() . "'
99
        ");
100
101
        if ($address_query->num_rows) {
102
            $country_query = $this->db->query("
103
                SELECT * 
104
                FROM `country` 
105
                WHERE country_id = '" . (int)$address_query->row['country_id'] . "'
106
            ");
107
108
            if ($country_query->num_rows) {
109
                $country = $country_query->row['name'];
110
                $iso_code_2 = $country_query->row['iso_code_2'];
111
                $iso_code_3 = $country_query->row['iso_code_3'];
112
                $address_format = $country_query->row['address_format'];
113
            } else {
114
                $country = '';
115
                $iso_code_2 = '';
116
                $iso_code_3 = '';
117
                $address_format = '';
118
            }
119
120
            $zone_query = $this->db->query("
121
                SELECT * 
122
                FROM `zone` 
123
                WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'
124
            ");
125
126
            if ($zone_query->num_rows) {
127
                $zone = $zone_query->row['name'];
128
                $zone_code = $zone_query->row['code'];
129
            } else {
130
                $zone = '';
131
                $zone_code = '';
132
            }
133
134
            $address_data = array(
135
                'address_id'     => $address_query->row['address_id'],
136
                'firstname'      => $address_query->row['firstname'],
137
                'lastname'       => $address_query->row['lastname'],
138
                'company'        => $address_query->row['company'],
139
                'address_1'      => $address_query->row['address_1'],
140
                'address_2'      => $address_query->row['address_2'],
141
                'postcode'       => $address_query->row['postcode'],
142
                'city'           => $address_query->row['city'],
143
                'zone_id'        => $address_query->row['zone_id'],
144
                'zone'           => $zone,
145
                'zone_code'      => $zone_code,
146
                'country_id'     => $address_query->row['country_id'],
147
                'country'        => $country,
148
                'iso_code_2'     => $iso_code_2,
149
                'iso_code_3'     => $iso_code_3,
150
                'address_format' => $address_format,
151
                'custom_field'   => json_decode($address_query->row['custom_field'], true)
152
            );
153
154
            return $address_data;
155
        } else {
156
            return false;
157
        }
158
    }
159
160
    public function getAddresses()
161
    {
162
        $address_data = array();
163
164
        $query = $this->db->query("
165
            SELECT * 
166
            FROM address 
167
            WHERE customer_id = '" . (int)$this->customer->getId() . "'
168
        ");
169
170
        foreach ($query->rows as $result) {
171
            $country_query = $this->db->query("
172
                SELECT * 
173
                FROM `country` 
174
                WHERE country_id = '" . (int)$result['country_id'] . "'
175
            ");
176
177
            if ($country_query->num_rows) {
178
                $country = $country_query->row['name'];
179
                $iso_code_2 = $country_query->row['iso_code_2'];
180
                $iso_code_3 = $country_query->row['iso_code_3'];
181
                $address_format = $country_query->row['address_format'];
182
            } else {
183
                $country = '';
184
                $iso_code_2 = '';
185
                $iso_code_3 = '';
186
                $address_format = '';
187
            }
188
189
            $zone_query = $this->db->query("
190
                SELECT * 
191
                FROM `zone` 
192
                WHERE zone_id = '" . (int)$result['zone_id'] . "'
193
            ");
194
195
            if ($zone_query->num_rows) {
196
                $zone = $zone_query->row['name'];
197
                $zone_code = $zone_query->row['code'];
198
            } else {
199
                $zone = '';
200
                $zone_code = '';
201
            }
202
203
            $address_data[$result['address_id']] = array(
204
                'address_id'     => $result['address_id'],
205
                'firstname'      => $result['firstname'],
206
                'lastname'       => $result['lastname'],
207
                'company'        => $result['company'],
208
                'address_1'      => $result['address_1'],
209
                'address_2'      => $result['address_2'],
210
                'postcode'       => $result['postcode'],
211
                'city'           => $result['city'],
212
                'zone_id'        => $result['zone_id'],
213
                'zone'           => $zone,
214
                'zone_code'      => $zone_code,
215
                'country_id'     => $result['country_id'],
216
                'country'        => $country,
217
                'iso_code_2'     => $iso_code_2,
218
                'iso_code_3'     => $iso_code_3,
219
                'address_format' => $address_format,
220
                'custom_field'   => json_decode($result['custom_field'], true)
221
222
            );
223
        }
224
225
        return $address_data;
226
    }
227
228
    public function getTotalAddresses()
229
    {
230
        $query = $this->db->query("
231
            SELECT COUNT(*) AS total 
232
            FROM address 
233
            WHERE customer_id = '" . (int)$this->customer->getId() . "'
234
        ");
235
236
        return $query->row['total'];
237
    }
238
}
239