ModelCheckoutOrder   F
last analyzed

Complexity

Total Complexity 82

Size/Duplication

Total Lines 1001
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 633
c 0
b 0
f 0
dl 0
loc 1001
rs 1.967
wmc 82

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteOrder() 0 33 1
C addOrder() 0 111 9
F addOrderHistory() 0 584 59
B editOrder() 0 115 6
B getOrder() 0 143 7

How to fix   Complexity   

Complex Class

Complex classes like ModelCheckoutOrder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ModelCheckoutOrder, and based on these observations, apply Extract Interface, too.

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 ModelCheckoutOrder 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 addOrder($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 `order` 
29
            SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', 
30
                store_name = '" . $this->db->escape($data['store_name']) . "', 
31
                store_url = '" . $this->db->escape($data['store_url']) . "', 
32
                customer_id = '" . (int)$data['customer_id'] . "', 
33
                customer_group_id = '" . (int)$data['customer_group_id'] . "', 
34
                firstname = '" . $this->db->escape($data['firstname']) . "', 
35
                lastname = '" . $this->db->escape($data['lastname']) . "', 
36
                email = '" . $this->db->escape($data['email']) . "', 
37
                telephone = '" . $this->db->escape($data['telephone']) . "', 
38
                fax = '" . $this->db->escape($data['fax']) . "', 
39
                custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', 
40
                payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', 
41
                payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', 
42
                payment_company = '" . $this->db->escape($data['payment_company']) . "', 
43
                payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', 
44
                payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', 
45
                payment_city = '" . $this->db->escape($data['payment_city']) . "', 
46
                payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', 
47
                payment_country = '" . $this->db->escape($data['payment_country']) . "', 
48
                payment_country_id = '" . (int)$data['payment_country_id'] . "', 
49
                payment_zone = '" . $this->db->escape($data['payment_zone']) . "', 
50
                payment_zone_id = '" . (int)$data['payment_zone_id'] . "', 
51
                payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', 
52
                payment_custom_field = '" . $this->db->escape(isset($data['payment_custom_field']) ? json_encode($data['payment_custom_field']) : '') . "', 
53
                payment_method = '" . $this->db->escape($data['payment_method']) . "', 
54
                payment_code = '" . $this->db->escape($data['payment_code']) . "', 
55
                shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', 
56
                shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', 
57
                shipping_company = '" . $this->db->escape($data['shipping_company']) . "', 
58
                shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', 
59
                shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', 
60
                shipping_city = '" . $this->db->escape($data['shipping_city']) . "', 
61
                shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', 
62
                shipping_country = '" . $this->db->escape($data['shipping_country']) . "', 
63
                shipping_country_id = '" . (int)$data['shipping_country_id'] . "', 
64
                shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', 
65
                shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', 
66
                shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', 
67
                shipping_custom_field = '" . $this->db->escape(isset($data['shipping_custom_field']) ? json_encode($data['shipping_custom_field']) : '') . "', 
68
                shipping_method = '" . $this->db->escape($data['shipping_method']) . "', 
69
                shipping_code = '" . $this->db->escape($data['shipping_code']) . "', 
70
                comment = '" . $this->db->escape($data['comment']) . "', 
71
                total = '" . (float)$data['total'] . "', 
72
                commission = '" . (float)$data['commission'] . "', 
73
                marketing_id = '" . (int)$data['marketing_id'] . "', 
74
                tracking = '" . $this->db->escape($data['tracking']) . "', 
75
                language_id = '" . (int)$data['language_id'] . "', 
76
                currency_id = '" . (int)$data['currency_id'] . "', 
77
                currency_code = '" . $this->db->escape($data['currency_code']) . "', 
78
                currency_value = '" . (float)$data['currency_value'] . "', 
79
                ip = '" . $this->db->escape($data['ip']) . "', 
80
                forwarded_ip = '" .  $this->db->escape($data['forwarded_ip']) . "', 
81
                user_agent = '" . $this->db->escape($data['user_agent']) . "', 
82
                accept_language = '" . $this->db->escape($data['accept_language']) . "', 
83
                date_added = NOW(), 
84
                date_modified = NOW()
85
        ");
86
87
        $order_id = $this->db->getLastId();
88
89
        // Products
90
        if (isset($data['products'])) {
91
            foreach ($data['products'] as $product) {
92
                $this->db->query("
93
                    INSERT INTO order_product 
94
                    SET order_id = '" . (int)$order_id . "', 
95
                        product_id = '" . (int)$product['product_id'] . "', 
96
                        name = '" . $this->db->escape($product['name']) . "', 
97
                        model = '" . $this->db->escape($product['model']) . "', 
98
                        quantity = '" . (int)$product['quantity'] . "', 
99
                        price = '" . (float)$product['price'] . "', 
100
                        total = '" . (float)$product['total'] . "', 
101
                        reward = '" . (int)$product['reward'] . "'
102
                ");
103
104
                $order_product_id = $this->db->getLastId();
105
106
                foreach ($product['option'] as $option) {
107
                    $this->db->query("
108
                        INSERT INTO order_option 
109
                        SET order_id = '" . (int)$order_id . "', 
110
                            order_product_id = '" . (int)$order_product_id . "', 
111
                            product_option_id = '" . (int)$option['product_option_id'] . "', 
112
                            product_option_value_id = '" . (int)$option['product_option_value_id'] . "', 
113
                            name = '" . $this->db->escape($option['name']) . "', 
114
                            `value` = '" . $this->db->escape($option['value']) . "', 
115
                            `type` = '" . $this->db->escape($option['type']) . "'
116
                    ");
117
                }
118
            }
119
        }
120
121
        // Totals
122
        if (isset($data['totals'])) {
123
            foreach ($data['totals'] as $total) {
124
                $this->db->query("
125
                    INSERT INTO order_total 
126
                    SET order_id = '" . (int)$order_id . "', 
127
                        code = '" . $this->db->escape($total['code']) . "', 
128
                        title = '" . $this->db->escape($total['title']) . "', 
129
                        `value` = '" . (float)$total['value'] . "', 
130
                        sort_order = '" . (int)$total['sort_order'] . "'
131
                ");
132
            }
133
        }
134
135
        return $order_id;
136
    }
137
138
    public function editOrder($order_id, $data)
139
    {
140
        // Void the order first
141
        $this->addOrderHistory($order_id, 0);
142
143
        $this->db->query("
144
            UPDATE `order` 
145
            SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', 
146
                store_name = '" . $this->db->escape($data['store_name']) . "', 
147
                store_url = '" . $this->db->escape($data['store_url']) . "', 
148
                customer_id = '" . (int)$data['customer_id'] . "', 
149
                customer_group_id = '" . (int)$data['customer_group_id'] . "', 
150
                firstname = '" . $this->db->escape($data['firstname']) . "', 
151
                lastname = '" . $this->db->escape($data['lastname']) . "', 
152
                email = '" . $this->db->escape($data['email']) . "', 
153
                telephone = '" . $this->db->escape($data['telephone']) . "', 
154
                fax = '" . $this->db->escape($data['fax']) . "', 
155
                custom_field = '" . $this->db->escape(json_encode($data['custom_field'])) . "', 
156
                payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', 
157
                payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', 
158
                payment_company = '" . $this->db->escape($data['payment_company']) . "', 
159
                payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', 
160
                payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', 
161
                payment_city = '" . $this->db->escape($data['payment_city']) . "', 
162
                payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', 
163
                payment_country = '" . $this->db->escape($data['payment_country']) . "', 
164
                payment_country_id = '" . (int)$data['payment_country_id'] . "', 
165
                payment_zone = '" . $this->db->escape($data['payment_zone']) . "', 
166
                payment_zone_id = '" . (int)$data['payment_zone_id'] . "', 
167
                payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', 
168
                payment_custom_field = '" . $this->db->escape(json_encode($data['payment_custom_field'])) . "', 
169
                payment_method = '" . $this->db->escape($data['payment_method']) . "', 
170
                payment_code = '" . $this->db->escape($data['payment_code']) . "', 
171
                shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', 
172
                shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', 
173
                shipping_company = '" . $this->db->escape($data['shipping_company']) . "', 
174
                shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', 
175
                shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', 
176
                shipping_city = '" . $this->db->escape($data['shipping_city']) . "', 
177
                shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', 
178
                shipping_country = '" . $this->db->escape($data['shipping_country']) . "', 
179
                shipping_country_id = '" . (int)$data['shipping_country_id'] . "', 
180
                shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', 
181
                shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', 
182
                shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', 
183
                shipping_custom_field = '" . $this->db->escape(json_encode($data['shipping_custom_field'])) . "', 
184
                shipping_method = '" . $this->db->escape($data['shipping_method']) . "', 
185
                shipping_code = '" . $this->db->escape($data['shipping_code']) . "', 
186
                comment = '" . $this->db->escape($data['comment']) . "', 
187
                total = '" . (float)$data['total'] . "', 
188
                commission = '" . (float)$data['commission'] . "', 
189
                date_modified = NOW() 
190
            WHERE order_id = '" . (int)$order_id . "'
191
        ");
192
193
        $this->db->query("
194
            DELETE 
195
			FROM order_product 
196
            WHERE order_id = '" . (int)$order_id . "'
197
        ");
198
199
        $this->db->query("
200
            DELETE 
201
			FROM order_option 
202
            WHERE order_id = '" . (int)$order_id . "'
203
        ");
204
205
        // Products
206
        if (isset($data['products'])) {
207
            foreach ($data['products'] as $product) {
208
                $this->db->query("
209
                    INSERT INTO order_product 
210
                    SET order_id = '" . (int)$order_id . "', 
211
                        product_id = '" . (int)$product['product_id'] . "', 
212
                        name = '" . $this->db->escape($product['name']) . "', 
213
                        model = '" . $this->db->escape($product['model']) . "', 
214
                        quantity = '" . (int)$product['quantity'] . "', 
215
                        price = '" . (float)$product['price'] . "', 
216
                        total = '" . (float)$product['total'] . "', 
217
                        reward = '" . (int)$product['reward'] . "'
218
                ");
219
220
                $order_product_id = $this->db->getLastId();
221
222
                foreach ($product['option'] as $option) {
223
                    $this->db->query("
224
                        INSERT INTO order_option 
225
                        SET order_id = '" . (int)$order_id . "', 
226
                            order_product_id = '" . (int)$order_product_id . "', 
227
                            product_option_id = '" . (int)$option['product_option_id'] . "', 
228
                            product_option_value_id = '" . (int)$option['product_option_value_id'] . "', 
229
                            name = '" . $this->db->escape($option['name']) . "', 
230
                            `value` = '" . $this->db->escape($option['value']) . "', 
231
                            `type` = '" . $this->db->escape($option['type']) . "'
232
                    ");
233
                }
234
            }
235
        }
236
237
        // Totals
238
        $this->db->query("
239
            DELETE 
240
			FROM order_total 
241
            WHERE order_id = '" . (int)$order_id . "'
242
        ");
243
244
        if (isset($data['totals'])) {
245
            foreach ($data['totals'] as $total) {
246
                $this->db->query("
247
                    INSERT INTO order_total 
248
                    SET order_id = '" . (int)$order_id . "', 
249
                        code = '" . $this->db->escape($total['code']) . "', 
250
                        title = '" . $this->db->escape($total['title']) . "', 
251
                        `value` = '" . (float)$total['value'] . "', 
252
                        sort_order = '" . (int)$total['sort_order'] . "'
253
                ");
254
            }
255
        }
256
    }
257
258
    public function deleteOrder($order_id)
259
    {
260
        // Void the order first
261
        $this->addOrderHistory($order_id, 0);
262
263
        $this->db->query("
264
            DELETE 
265
			FROM `order` 
266
            WHERE order_id = '" . (int)$order_id . "'
267
        ");
268
269
        $this->db->query("
270
            DELETE 
271
			FROM `order_product` 
272
            WHERE order_id = '" . (int)$order_id . "'
273
        ");
274
275
        $this->db->query("
276
            DELETE 
277
			FROM `order_option` 
278
            WHERE order_id = '" . (int)$order_id . "'
279
        ");
280
281
        $this->db->query("
282
            DELETE 
283
			FROM `order_total` 
284
            WHERE order_id = '" . (int)$order_id . "'
285
        ");
286
287
        $this->db->query("
288
            DELETE 
289
			FROM `order_history` 
290
            WHERE order_id = '" . (int)$order_id . "'
291
        ");
292
    }
293
294
    public function getOrder($order_id)
295
    {
296
        $order_query = $this->db->query("
297
            SELECT *, 
298
                (
299
                    SELECT os.name 
300
                    FROM `order_status` os 
301
                    WHERE os.order_status_id = o.order_status_id 
302
                        AND os.language_id = o.language_id) AS order_status 
303
            FROM `order` o 
304
            WHERE o.order_id = '" . (int)$order_id . "'
305
        ");
306
307
        if ($order_query->num_rows) {
308
            $country_query = $this->db->query("
309
                SELECT * 
310
                FROM `country` 
311
                WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'
312
            ");
313
314
            if ($country_query->num_rows) {
315
                $payment_iso_code_2 = $country_query->row['iso_code_2'];
316
                $payment_iso_code_3 = $country_query->row['iso_code_3'];
317
            } else {
318
                $payment_iso_code_2 = '';
319
                $payment_iso_code_3 = '';
320
            }
321
322
            $zone_query = $this->db->query("
323
                SELECT * 
324
                FROM `zone` 
325
                WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'
326
            ");
327
328
            if ($zone_query->num_rows) {
329
                $payment_zone_code = $zone_query->row['code'];
330
            } else {
331
                $payment_zone_code = '';
332
            }
333
334
            $country_query = $this->db->query("
335
                SELECT * 
336
                FROM `country` 
337
                WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'
338
            ");
339
340
            if ($country_query->num_rows) {
341
                $shipping_iso_code_2 = $country_query->row['iso_code_2'];
342
                $shipping_iso_code_3 = $country_query->row['iso_code_3'];
343
            } else {
344
                $shipping_iso_code_2 = '';
345
                $shipping_iso_code_3 = '';
346
            }
347
348
            $zone_query = $this->db->query("
349
                SELECT * FROM `zone` 
350
                WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'
351
            ");
352
353
            if ($zone_query->num_rows) {
354
                $shipping_zone_code = $zone_query->row['code'];
355
            } else {
356
                $shipping_zone_code = '';
357
            }
358
359
            $this->load->model('localisation/language');
360
361
            $language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']);
362
363
            if ($language_info) {
364
                $language_code = $language_info['code'];
365
            } else {
366
                $language_code = $this->config->get('config_language');
367
            }
368
369
            return array(
370
                'order_id'                => $order_query->row['order_id'],
371
                'invoice_no'              => $order_query->row['invoice_no'],
372
                'invoice_prefix'          => $order_query->row['invoice_prefix'],
373
                'store_name'              => $order_query->row['store_name'],
374
                'store_url'               => $order_query->row['store_url'],
375
                'customer_id'             => $order_query->row['customer_id'],
376
                'firstname'               => $order_query->row['firstname'],
377
                'lastname'                => $order_query->row['lastname'],
378
                'email'                   => $order_query->row['email'],
379
                'telephone'               => $order_query->row['telephone'],
380
                'fax'                     => $order_query->row['fax'],
381
                'custom_field'            => json_decode($order_query->row['custom_field'], true),
382
                'payment_firstname'       => $order_query->row['payment_firstname'],
383
                'payment_lastname'        => $order_query->row['payment_lastname'],
384
                'payment_company'         => $order_query->row['payment_company'],
385
                'payment_address_1'       => $order_query->row['payment_address_1'],
386
                'payment_address_2'       => $order_query->row['payment_address_2'],
387
                'payment_postcode'        => $order_query->row['payment_postcode'],
388
                'payment_city'            => $order_query->row['payment_city'],
389
                'payment_zone_id'         => $order_query->row['payment_zone_id'],
390
                'payment_zone'            => $order_query->row['payment_zone'],
391
                'payment_zone_code'       => $payment_zone_code,
392
                'payment_country_id'      => $order_query->row['payment_country_id'],
393
                'payment_country'         => $order_query->row['payment_country'],
394
                'payment_iso_code_2'      => $payment_iso_code_2,
395
                'payment_iso_code_3'      => $payment_iso_code_3,
396
                'payment_address_format'  => $order_query->row['payment_address_format'],
397
                'payment_custom_field'    => json_decode($order_query->row['payment_custom_field'], true),
398
                'payment_method'          => $order_query->row['payment_method'],
399
                'payment_code'            => $order_query->row['payment_code'],
400
                'shipping_firstname'      => $order_query->row['shipping_firstname'],
401
                'shipping_lastname'       => $order_query->row['shipping_lastname'],
402
                'shipping_company'        => $order_query->row['shipping_company'],
403
                'shipping_address_1'      => $order_query->row['shipping_address_1'],
404
                'shipping_address_2'      => $order_query->row['shipping_address_2'],
405
                'shipping_postcode'       => $order_query->row['shipping_postcode'],
406
                'shipping_city'           => $order_query->row['shipping_city'],
407
                'shipping_zone_id'        => $order_query->row['shipping_zone_id'],
408
                'shipping_zone'           => $order_query->row['shipping_zone'],
409
                'shipping_zone_code'      => $shipping_zone_code,
410
                'shipping_country_id'     => $order_query->row['shipping_country_id'],
411
                'shipping_country'        => $order_query->row['shipping_country'],
412
                'shipping_iso_code_2'     => $shipping_iso_code_2,
413
                'shipping_iso_code_3'     => $shipping_iso_code_3,
414
                'shipping_address_format' => $order_query->row['shipping_address_format'],
415
                'shipping_custom_field'   => json_decode($order_query->row['shipping_custom_field'], true),
416
                'shipping_method'         => $order_query->row['shipping_method'],
417
                'shipping_code'           => $order_query->row['shipping_code'],
418
                'comment'                 => $order_query->row['comment'],
419
                'total'                   => $order_query->row['total'],
420
                'order_status_id'         => $order_query->row['order_status_id'],
421
                'order_status'            => $order_query->row['order_status'],
422
                'commission'              => $order_query->row['commission'],
423
                'language_id'             => $order_query->row['language_id'],
424
                'language_code'           => $language_code,
425
                'currency_id'             => $order_query->row['currency_id'],
426
                'currency_code'           => $order_query->row['currency_code'],
427
                'currency_value'          => $order_query->row['currency_value'],
428
                'ip'                      => $order_query->row['ip'],
429
                'forwarded_ip'            => $order_query->row['forwarded_ip'],
430
                'user_agent'              => $order_query->row['user_agent'],
431
                'accept_language'         => $order_query->row['accept_language'],
432
                'date_added'              => $order_query->row['date_added'],
433
                'date_modified'           => $order_query->row['date_modified']
434
            );
435
        } else {
436
            return false;
437
        }
438
    }
439
440
    public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false)
0 ignored issues
show
Unused Code introduced by
The parameter $override is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

440
    public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, /** @scrutinizer ignore-unused */ $override = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
441
    {
442
        $order_info = $this->getOrder($order_id);
443
        
444
        if ($order_info) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
445
446
            // If current order status is not processing or complete but new status is processing or complete then commence completing the order
447
            if (!in_array($order_info['order_status_id'], array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status'))) && in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
448
                // Redeem reward points
449
                $order_total_query = $this->db->query("
450
                    SELECT * 
451
                    FROM `order_total` 
452
                    WHERE order_id = '" . (int)$order_id . "' 
453
                    ORDER BY sort_order ASC
454
                ");
455
456
                foreach ($order_total_query->rows as $order_total) {
457
                    $this->load->model('extension/total/' . $order_total['code']);
458
                }
459
460
                // Stock subtraction
461
                $order_product_query = $this->db->query("
462
                    SELECT * 
463
                    FROM order_product 
464
                    WHERE order_id = '" . (int)$order_id . "'
465
                ");
466
467
                foreach ($order_product_query->rows as $order_product) {
468
                    $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n ...quantity = (quantity - does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
469
                        UPDATE product 
470
                        SET quantity = (quantity - " . (int)$order_product['quantity'] . ") 
471
                        WHERE product_id = '" . (int)$order_product['product_id'] . "' 
472
                            AND subtract = '1'
473
                    ");
474
475
                    $order_option_query = $this->db->query("
476
                        SELECT * 
477
                        FROM order_option 
478
                        WHERE order_id = '" . (int)$order_id . "' 
479
                            AND order_product_id = '" . (int)$order_product['order_product_id'] . "'
480
                    ");
481
482
                    foreach ($order_option_query->rows as $option) {
483
                        $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n ...quantity = (quantity - does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
484
                            UPDATE product_option_value 
485
                            SET quantity = (quantity - " . (int)$order_product['quantity'] . ") 
486
                            WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' 
487
                            AND subtract = '1'
488
                        ");
489
                    }
490
                }
491
            }
492
493
            // Update the DB with the new statuses
494
            $this->db->query("
495
                UPDATE `order` 
496
                SET order_status_id = '" . (int)$order_status_id . "', 
497
                    date_modified = NOW() 
498
                WHERE order_id = '" . (int)$order_id . "'
499
            ");
500
501
            $this->db->query("
502
                INSERT INTO order_history 
503
                SET order_id = '" . (int)$order_id . "', 
504
                    order_status_id = '" . (int)$order_status_id . "', 
505
                    notify = '" . (int)$notify . "', 
506
                    comment = '" . $this->db->escape($comment) . "', 
507
                    date_added = NOW()
508
            ");
509
510
            // If old order status is the processing or complete status but new status is not then commence restock, and remove reward history
511
            if (in_array($order_info['order_status_id'], array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status'))) && !in_array($order_status_id, array_merge($this->config->get('config_processing_status'), $this->config->get('config_complete_status')))) {
512
                // Restock
513
                $product_query = $this->db->query("
514
                    SELECT * 
515
                    FROM order_product 
516
                    WHERE order_id = '" . (int)$order_id . "'
517
                ");
518
519
                foreach ($product_query->rows as $product) {
520
                    $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n ...quantity = (quantity + does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
521
                        UPDATE `product` 
522
                        SET quantity = (quantity + " . (int)$product['quantity'] . ") 
523
                        WHERE product_id = '" . (int)$product['product_id'] . "' 
524
                            AND subtract = '1'
525
                    ");
526
527
                    $option_query = $this->db->query("
528
                        SELECT * 
529
                        FROM order_option 
530
                        WHERE order_id = '" . (int)$order_id . "' 
531
                            AND order_product_id = '" . (int)$product['order_product_id'] . "'
532
                    ");
533
534
                    foreach ($option_query->rows as $option) {
535
                        $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n ...quantity = (quantity + does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
536
                            UPDATE product_option_value 
537
                            SET quantity = (quantity + " . (int)$product['quantity'] . ") 
538
                            WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' 
539
                                AND subtract = '1'
540
                        ");
541
                    }
542
                }
543
544
                // Remove reward points history
545
                $this->load->model('account/order');
546
547
                $order_total_query = $this->db->query("
548
                    SELECT * 
549
                    FROM `order_total` 
550
                    WHERE order_id = '" . (int)$order_id . "' 
551
                    ORDER BY sort_order ASC
552
                ");
553
554
                foreach ($order_total_query->rows as $order_total) {
555
                    $this->load->model('extension/total/' . $order_total['code']);
556
557
                    if (property_exists($this->{'model_extension_total_' . $order_total['code']}, 'unconfirm')) {
558
                        $this->{'model_extension_total_' . $order_total['code']}->unconfirm($order_id);
559
                    }
560
                }
561
            }
562
563
            $this->cache->delete('product');
564
            
565
            // If order status is 0 then becomes greater than 0 send main html email
566
            if (!$order_info['order_status_id'] && $order_status_id) {
567
                // Check for any downloadable products
568
                $download_status = false;
569
    
570
                $order_product_query = $this->db->query("
571
                    SELECT * 
572
                    FROM order_product 
573
                    WHERE order_id = '" . (int)$order_id . "'
574
                ");
575
    
576
                foreach ($order_product_query->rows as $order_product) {
577
                    // Check if there are any linked downloads
578
                    $product_download_query = $this->db->query("
579
                        SELECT COUNT(*) AS total 
580
                        FROM `product_to_download` 
581
                        WHERE product_id = '" . (int)$order_product['product_id'] . "'
582
                    ");
583
    
584
                    if ($product_download_query->row['total']) {
585
                        $download_status = true;
586
                    }
587
                }
588
    
589
                // Load the language for any mails that might be required to be sent out
590
                $language = new \Divine\Engine\Library\Language($order_info['language_code']);
591
                $language->load($order_info['language_code']);
592
                $language->load('mail/order');
593
    
594
                $order_status_query = $this->db->query("
595
                    SELECT * 
596
                    FROM order_status 
597
                    WHERE order_status_id = '" . (int)$order_status_id . "' 
598
                        AND language_id = '" . (int)$order_info['language_id'] . "'
599
                ");
600
    
601
                if ($order_status_query->num_rows) {
602
                    $order_status = $order_status_query->row['name'];
603
                } else {
604
                    $order_status = '';
605
                }
606
    
607
                $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
608
    
609
                // HTML Mail
610
                $data = array();
611
    
612
                $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);
613
    
614
                $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']);
615
                $data['text_link'] = $language->get('text_new_link');
616
                $data['text_download'] = $language->get('text_new_download');
617
                $data['text_order_detail'] = $language->get('text_new_order_detail');
618
                $data['text_instruction'] = $language->get('text_new_instruction');
619
                $data['text_order_id'] = $language->get('text_new_order_id');
620
                $data['text_date_added'] = $language->get('text_new_date_added');
621
                $data['text_payment_method'] = $language->get('text_new_payment_method');
622
                $data['text_shipping_method'] = $language->get('text_new_shipping_method');
623
                $data['text_email'] = $language->get('text_new_email');
624
                $data['text_telephone'] = $language->get('text_new_telephone');
625
                $data['text_ip'] = $language->get('text_new_ip');
626
                $data['text_order_status'] = $language->get('text_new_order_status');
627
                $data['text_payment_address'] = $language->get('text_new_payment_address');
628
                $data['text_shipping_address'] = $language->get('text_new_shipping_address');
629
                $data['text_product'] = $language->get('text_new_product');
630
                $data['text_model'] = $language->get('text_new_model');
631
                $data['text_quantity'] = $language->get('text_new_quantity');
632
                $data['text_price'] = $language->get('text_new_price');
633
                $data['text_total'] = $language->get('text_new_total');
634
                $data['text_footer'] = $language->get('text_new_footer');
635
    
636
                $data['logo'] = $_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $this->config->get('config_logo');
637
                $data['store_name'] = $order_info['store_name'];
638
                $data['store_url'] = $order_info['store_url'];
639
                $data['customer_id'] = $order_info['customer_id'];
640
                $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;
641
    
642
                if ($download_status) {
643
                    $data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
644
                } else {
645
                    $data['download'] = '';
646
                }
647
    
648
                $data['order_id'] = $order_id;
649
                $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
650
                $data['payment_method'] = $order_info['payment_method'];
651
                $data['shipping_method'] = $order_info['shipping_method'];
652
                $data['email'] = $order_info['email'];
653
                $data['telephone'] = $order_info['telephone'];
654
                $data['ip'] = $order_info['ip'];
655
                $data['order_status'] = $order_status;
656
    
657
                if ($comment && $notify) {
658
                    $data['comment'] = nl2br($comment);
659
                } else {
660
                    $data['comment'] = '';
661
                }
662
    
663
                if ($order_info['payment_address_format']) {
664
                    $format = $order_info['payment_address_format'];
665
                } else {
666
                    $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
667
                }
668
    
669
                $find = array(
670
                    '{firstname}',
671
                    '{lastname}',
672
                    '{company}',
673
                    '{address_1}',
674
                    '{address_2}',
675
                    '{city}',
676
                    '{postcode}',
677
                    '{zone}',
678
                    '{zone_code}',
679
                    '{country}'
680
                );
681
    
682
                $replace = array(
683
                    'firstname' => $order_info['payment_firstname'],
684
                    'lastname'  => $order_info['payment_lastname'],
685
                    'company'   => $order_info['payment_company'],
686
                    'address_1' => $order_info['payment_address_1'],
687
                    'address_2' => $order_info['payment_address_2'],
688
                    'city'      => $order_info['payment_city'],
689
                    'postcode'  => $order_info['payment_postcode'],
690
                    'zone'      => $order_info['payment_zone'],
691
                    'zone_code' => $order_info['payment_zone_code'],
692
                    'country'   => $order_info['payment_country']
693
                );
694
    
695
                $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /\s\s+/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
696
    
697
                if ($order_info['shipping_address_format']) {
698
                    $format = $order_info['shipping_address_format'];
699
                } else {
700
                    $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
701
                }
702
    
703
                $find = array(
704
                    '{firstname}',
705
                    '{lastname}',
706
                    '{company}',
707
                    '{address_1}',
708
                    '{address_2}',
709
                    '{city}',
710
                    '{postcode}',
711
                    '{zone}',
712
                    '{zone_code}',
713
                    '{country}'
714
                );
715
    
716
                $replace = array(
717
                    'firstname' => $order_info['shipping_firstname'],
718
                    'lastname'  => $order_info['shipping_lastname'],
719
                    'company'   => $order_info['shipping_company'],
720
                    'address_1' => $order_info['shipping_address_1'],
721
                    'address_2' => $order_info['shipping_address_2'],
722
                    'city'      => $order_info['shipping_city'],
723
                    'postcode'  => $order_info['shipping_postcode'],
724
                    'zone'      => $order_info['shipping_zone'],
725
                    'zone_code' => $order_info['shipping_zone_code'],
726
                    'country'   => $order_info['shipping_country']
727
                );
728
    
729
                $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /\s\s+/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
730
    
731
                $this->load->model('tool/upload');
732
    
733
                // Products
734
                $data['products'] = array();
735
    
736
                foreach ($order_product_query->rows as $product) {
737
                    $option_data = array();
738
    
739
                    $order_option_query = $this->db->query("
740
                        SELECT * 
741
                        FROM order_option 
742
                        WHERE order_id = '" . (int)$order_id . "' 
743
                        AND order_product_id = '" . (int)$product['order_product_id'] . "'
744
                    ");
745
    
746
                    foreach ($order_option_query->rows as $option) {
747
                        if ($option['type'] != 'file') {
748
                            $value = $option['value'];
749
                        } else {
750
                            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
751
    
752
                            if ($upload_info) {
753
                                $value = $upload_info['name'];
754
                            } else {
755
                                $value = '';
756
                            }
757
                        }
758
    
759
                        $option_data[] = array(
760
                            'name'  => $option['name'],
761
                            'value' => (\voku\helper\UTF8::strlen($value) > 20 ? \voku\helper\UTF8::substr($value, 0, 20) . '..' : $value)
0 ignored issues
show
Bug introduced by
Are you sure voku\helper\UTF8::substr($value, 0, 20) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

761
                            'value' => (\voku\helper\UTF8::strlen($value) > 20 ? /** @scrutinizer ignore-type */ \voku\helper\UTF8::substr($value, 0, 20) . '..' : $value)
Loading history...
762
                        );
763
                    }
764
    
765
                    $data['products'][] = array(
766
                        'name'     => $product['name'],
767
                        'model'    => $product['model'],
768
                        'option'   => $option_data,
769
                        'quantity' => $product['quantity'],
770
                        'price'    => $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value']),
771
                        'total'    => $this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value'])
772
                    );
773
                }
774
    
775
                // Order Totals
776
                $data['totals'] = array();
777
                
778
                $order_total_query = $this->db->query("SELECT * FROM `order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
779
    
780
                foreach ($order_total_query->rows as $total) {
781
                    $data['totals'][] = array(
782
                        'title' => $total['title'],
783
                        'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
784
                    );
785
                }
786
    
787
                // Text Mail
788
                $text  = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
789
                $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
790
                $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
791
                $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
792
    
793
                if ($comment && $notify) {
794
                    $text .= $language->get('text_new_instruction') . "\n\n";
795
                    $text .= $comment . "\n\n";
796
                }
797
    
798
                // Products
799
                $text .= $language->get('text_new_products') . "\n";
800
    
801
                foreach ($order_product_query->rows as $product) {
802
                    $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
803
    
804
                    $order_option_query = $this->db->query("
805
                        SELECT * 
806
                        FROM order_option 
807
                        WHERE order_id = '" . (int)$order_id . "' 
808
                            AND order_product_id = '" . $product['order_product_id'] . "'
809
                    ");
810
    
811
                    foreach ($order_option_query->rows as $option) {
812
                        if ($option['type'] != 'file') {
813
                            $value = $option['value'];
814
                        } else {
815
                            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
816
    
817
                            if ($upload_info) {
818
                                $value = $upload_info['name'];
819
                            } else {
820
                                $value = '';
821
                            }
822
                        }
823
    
824
                        $text .= chr(9) . '-' . $option['name'] . ' ' . (\voku\helper\UTF8::strlen($value) > 20 ? \voku\helper\UTF8::substr($value, 0, 20) . '..' : $value) . "\n";
825
                    }
826
                }
827
    
828
                $text .= "\n";
829
    
830
                $text .= $language->get('text_new_order_total') . "\n";
831
    
832
                foreach ($order_total_query->rows as $total) {
833
                    $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
834
                }
835
    
836
                $text .= "\n";
837
    
838
                if ($order_info['customer_id']) {
839
                    $text .= $language->get('text_new_link') . "\n";
840
                    $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
841
                }
842
    
843
                if ($download_status) {
844
                    $text .= $language->get('text_new_download') . "\n";
845
                    $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
846
                }
847
    
848
                // Comment
849
                if ($order_info['comment']) {
850
                    $text .= $language->get('text_new_comment') . "\n\n";
851
                    $text .= $order_info['comment'] . "\n\n";
852
                }
853
    
854
                $text .= $language->get('text_new_footer') . "\n\n";
855
    
856
                $mail = new \Divine\Engine\Library\Mail();
857
                $mail->protocol = $this->config->get('config_mail_protocol');
858
                $mail->parameter = $this->config->get('config_mail_parameter');
859
                $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
860
                $mail->smtp_username = $this->config->get('config_mail_smtp_username');
861
                $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
862
                $mail->smtp_port = $this->config->get('config_mail_smtp_port');
863
                $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
864
    
865
                $mail->setTo($order_info['email']);
866
                $mail->setFrom($this->config->get('config_email'));
867
                $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
868
                $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
869
                $mail->setHtml($this->load->view('mail/order', $data));
870
                $mail->setText($text);
871
                $mail->send();
872
    
873
                // Admin Alert Mail
874
                if (in_array('order', (array)$this->config->get('config_mail_alert'))) {
875
                    $subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
876
    
877
                    // HTML Mail
878
                    $data['text_greeting'] = $language->get('text_new_received');
879
    
880
                    if ($comment) {
881
                        if ($order_info['comment']) {
882
                            $data['comment'] = nl2br($comment) . '<br/><br/>' . $order_info['comment'];
883
                        } else {
884
                            $data['comment'] = nl2br($comment);
885
                        }
886
                    } else {
887
                        if ($order_info['comment']) {
888
                            $data['comment'] = $order_info['comment'];
889
                        } else {
890
                            $data['comment'] = '';
891
                        }
892
                    }
893
    
894
                    $data['text_download'] = '';
895
    
896
                    $data['text_footer'] = '';
897
    
898
                    $data['text_link'] = '';
899
                    $data['link'] = '';
900
                    $data['download'] = '';
901
    
902
                    // Text
903
                    $text  = $language->get('text_new_received') . "\n\n";
904
                    $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
905
                    $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
906
                    $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";
907
                    $text .= $language->get('text_new_products') . "\n";
908
    
909
                    foreach ($order_product_query->rows as $product) {
910
                        $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
911
    
912
                        $order_option_query = $this->db->query("
913
                            SELECT * 
914
                            FROM order_option 
915
                            WHERE order_id = '" . (int)$order_id . "' 
916
                                AND order_product_id = '" . $product['order_product_id'] . "'
917
                        ");
918
    
919
                        foreach ($order_option_query->rows as $option) {
920
                            if ($option['type'] != 'file') {
921
                                $value = $option['value'];
922
                            } else {
923
                                $value = \voku\helper\UTF8::substr($option['value'], 0, \voku\helper\UTF8::strrpos($option['value'], '.'));
0 ignored issues
show
Bug introduced by
It seems like voku\helper\UTF8::strrpos($option['value'], '.') can also be of type false; however, parameter $length of voku\helper\UTF8::substr() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

923
                                $value = \voku\helper\UTF8::substr($option['value'], 0, /** @scrutinizer ignore-type */ \voku\helper\UTF8::strrpos($option['value'], '.'));
Loading history...
924
                            }
925
    
926
                            $text .= chr(9) . '-' . $option['name'] . ' ' . (\voku\helper\UTF8::strlen($value) > 20 ? \voku\helper\UTF8::substr($value, 0, 20) . '..' : $value) . "\n";
927
                        }
928
                    }
929
    
930
                    $text .= "\n";
931
    
932
                    $text .= $language->get('text_new_order_total') . "\n";
933
    
934
                    foreach ($order_total_query->rows as $total) {
935
                        $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
936
                    }
937
    
938
                    $text .= "\n";
939
    
940
                    if ($order_info['comment']) {
941
                        $text .= $language->get('text_new_comment') . "\n\n";
942
                        $text .= $order_info['comment'] . "\n\n";
943
                    }
944
    
945
                    $mail = new \Divine\Engine\Library\Mail();
946
                    $mail->protocol = $this->config->get('config_mail_protocol');
947
                    $mail->parameter = $this->config->get('config_mail_parameter');
948
                    $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
949
                    $mail->smtp_username = $this->config->get('config_mail_smtp_username');
950
                    $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
951
                    $mail->smtp_port = $this->config->get('config_mail_smtp_port');
952
                    $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
953
    
954
                    $mail->setTo($this->config->get('config_email'));
955
                    $mail->setFrom($this->config->get('config_email'));
956
                    $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
957
                    $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
958
                    $mail->setHtml($this->load->view('mail/order', $data));
959
                    $mail->setText($text);
960
                    $mail->send();
961
    
962
                    // Send to additional alert emails
963
                    $emails = explode(',', $this->config->get('config_alert_email'));
964
    
965
                    foreach ($emails as $email) {
966
                        if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
967
                            $mail->setTo($email);
968
                            $mail->send();
969
                        }
970
                    }
971
                }
972
            }
973
    
974
            // If order status is not 0 then send update text email
975
            if ($order_info['order_status_id'] && $order_status_id && $notify) {
976
                $language = new \Divine\Engine\Library\Language($order_info['language_code']);
977
                $language->load($order_info['language_code']);
978
                $language->load('mail/order');
979
    
980
                $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
981
    
982
                $message  = $language->get('text_update_order') . ' ' . $order_id . "\n";
983
                $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n\n";
984
    
985
                $order_status_query = $this->db->query("
986
                    SELECT * 
987
                    FROM order_status 
988
                    WHERE order_status_id = '" . (int)$order_status_id . "' 
989
                        AND language_id = '" . (int)$order_info['language_id'] . "'
990
                ");
991
    
992
                if ($order_status_query->num_rows) {
993
                    $message .= $language->get('text_update_order_status') . "\n\n";
994
                    $message .= $order_status_query->row['name'] . "\n\n";
995
                }
996
    
997
                if ($order_info['customer_id']) {
998
                    $message .= $language->get('text_update_link') . "\n";
999
                    $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
1000
                }
1001
    
1002
                if ($comment) {
1003
                    $message .= $language->get('text_update_comment') . "\n\n";
1004
                    $message .= strip_tags($comment) . "\n\n";
1005
                }
1006
    
1007
                $message .= $language->get('text_update_footer');
1008
    
1009
                $mail = new \Divine\Engine\Library\Mail();
1010
                $mail->protocol = $this->config->get('config_mail_protocol');
1011
                $mail->parameter = $this->config->get('config_mail_parameter');
1012
                $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
1013
                $mail->smtp_username = $this->config->get('config_mail_smtp_username');
1014
                $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
1015
                $mail->smtp_port = $this->config->get('config_mail_smtp_port');
1016
                $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
1017
    
1018
                $mail->setTo($order_info['email']);
1019
                $mail->setFrom($this->config->get('config_email'));
1020
                $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
1021
                $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
1022
                $mail->setText($message);
1023
                $mail->send();
1024
            }
1025
        }
1026
    }
1027
}
1028