order.php (18625B)
1 <?php 2 class ControllerMailOrder extends Controller { 3 public function index(&$route, &$args) { 4 if (isset($args[0])) { 5 $order_id = $args[0]; 6 } else { 7 $order_id = 0; 8 } 9 10 if (isset($args[1])) { 11 $order_status_id = $args[1]; 12 } else { 13 $order_status_id = 0; 14 } 15 16 if (isset($args[2])) { 17 $comment = $args[2]; 18 } else { 19 $comment = ''; 20 } 21 22 if (isset($args[3])) { 23 $notify = $args[3]; 24 } else { 25 $notify = ''; 26 } 27 28 // We need to grab the old order status ID 29 $order_info = $this->model_checkout_order->getOrder($order_id); 30 31 if ($order_info) { 32 // If order status is 0 then becomes greater than 0 send main html email 33 if (!$order_info['order_status_id'] && $order_status_id) { 34 $this->add($order_info, $order_status_id, $comment, $notify); 35 } 36 37 // If order status is not 0 then send update text email 38 if ($order_info['order_status_id'] && $order_status_id && $notify) { 39 $this->edit($order_info, $order_status_id, $comment, $notify); 40 } 41 } 42 } 43 44 public function add($order_info, $order_status_id, $comment, $notify) { 45 // Check for any downloadable products 46 $download_status = false; 47 48 $order_products = $this->model_checkout_order->getOrderProducts($order_info['order_id']); 49 50 foreach ($order_products as $order_product) { 51 // Check if there are any linked downloads 52 $product_download_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_download` WHERE product_id = '" . (int)$order_product['product_id'] . "'"); 53 54 if ($product_download_query->row['total']) { 55 $download_status = true; 56 } 57 } 58 59 // Load the language for any mails that might be required to be sent out 60 $language = new Language($order_info['language_code']); 61 $language->load($order_info['language_code']); 62 $language->load('mail/order_add'); 63 64 // HTML Mail 65 $data['title'] = sprintf($language->get('text_subject'), $order_info['store_name'], $order_info['order_id']); 66 67 $data['text_greeting'] = sprintf($language->get('text_greeting'), $order_info['store_name']); 68 $data['text_link'] = $language->get('text_link'); 69 $data['text_download'] = $language->get('text_download'); 70 $data['text_order_detail'] = $language->get('text_order_detail'); 71 $data['text_instruction'] = $language->get('text_instruction'); 72 $data['text_order_id'] = $language->get('text_order_id'); 73 $data['text_date_added'] = $language->get('text_date_added'); 74 $data['text_payment_method'] = $language->get('text_payment_method'); 75 $data['text_shipping_method'] = $language->get('text_shipping_method'); 76 $data['text_email'] = $language->get('text_email'); 77 $data['text_telephone'] = $language->get('text_telephone'); 78 $data['text_ip'] = $language->get('text_ip'); 79 $data['text_order_status'] = $language->get('text_order_status'); 80 $data['text_payment_address'] = $language->get('text_payment_address'); 81 $data['text_shipping_address'] = $language->get('text_shipping_address'); 82 $data['text_product'] = $language->get('text_product'); 83 $data['text_model'] = $language->get('text_model'); 84 $data['text_quantity'] = $language->get('text_quantity'); 85 $data['text_price'] = $language->get('text_price'); 86 $data['text_total'] = $language->get('text_total'); 87 $data['text_footer'] = $language->get('text_footer'); 88 89 $data['logo'] = $order_info['store_url'] . 'image/' . $this->config->get('config_logo'); 90 $data['store_name'] = $order_info['store_name']; 91 $data['store_url'] = $order_info['store_url']; 92 $data['customer_id'] = $order_info['customer_id']; 93 $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_info['order_id']; 94 95 if ($download_status) { 96 $data['download'] = $order_info['store_url'] . 'index.php?route=account/download'; 97 } else { 98 $data['download'] = ''; 99 } 100 101 $data['order_id'] = $order_info['order_id']; 102 $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added'])); 103 $data['payment_method'] = $order_info['payment_method']; 104 $data['shipping_method'] = $order_info['shipping_method']; 105 $data['email'] = $order_info['email']; 106 $data['telephone'] = $order_info['telephone']; 107 $data['ip'] = $order_info['ip']; 108 109 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'"); 110 111 if ($order_status_query->num_rows) { 112 $data['order_status'] = $order_status_query->row['name']; 113 } else { 114 $data['order_status'] = ''; 115 } 116 117 if ($comment && $notify) { 118 $data['comment'] = nl2br($comment); 119 } else { 120 $data['comment'] = ''; 121 } 122 123 if ($order_info['payment_address_format']) { 124 $format = $order_info['payment_address_format']; 125 } else { 126 $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; 127 } 128 129 $find = array( 130 '{firstname}', 131 '{lastname}', 132 '{company}', 133 '{address_1}', 134 '{address_2}', 135 '{city}', 136 '{postcode}', 137 '{zone}', 138 '{zone_code}', 139 '{country}' 140 ); 141 142 $replace = array( 143 'firstname' => $order_info['payment_firstname'], 144 'lastname' => $order_info['payment_lastname'], 145 'company' => $order_info['payment_company'], 146 'address_1' => $order_info['payment_address_1'], 147 'address_2' => $order_info['payment_address_2'], 148 'city' => $order_info['payment_city'], 149 'postcode' => $order_info['payment_postcode'], 150 'zone' => $order_info['payment_zone'], 151 'zone_code' => $order_info['payment_zone_code'], 152 'country' => $order_info['payment_country'] 153 ); 154 155 $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)))); 156 157 if ($order_info['shipping_address_format']) { 158 $format = $order_info['shipping_address_format']; 159 } else { 160 $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; 161 } 162 163 $find = array( 164 '{firstname}', 165 '{lastname}', 166 '{company}', 167 '{address_1}', 168 '{address_2}', 169 '{city}', 170 '{postcode}', 171 '{zone}', 172 '{zone_code}', 173 '{country}' 174 ); 175 176 $replace = array( 177 'firstname' => $order_info['shipping_firstname'], 178 'lastname' => $order_info['shipping_lastname'], 179 'company' => $order_info['shipping_company'], 180 'address_1' => $order_info['shipping_address_1'], 181 'address_2' => $order_info['shipping_address_2'], 182 'city' => $order_info['shipping_city'], 183 'postcode' => $order_info['shipping_postcode'], 184 'zone' => $order_info['shipping_zone'], 185 'zone_code' => $order_info['shipping_zone_code'], 186 'country' => $order_info['shipping_country'] 187 ); 188 189 $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)))); 190 191 $this->load->model('tool/upload'); 192 193 // Products 194 $data['products'] = array(); 195 196 foreach ($order_products as $order_product) { 197 $option_data = array(); 198 199 $order_options = $this->model_checkout_order->getOrderOptions($order_info['order_id'], $order_product['order_product_id']); 200 201 foreach ($order_options as $order_option) { 202 if ($order_option['type'] != 'file') { 203 $value = $order_option['value']; 204 } else { 205 $upload_info = $this->model_tool_upload->getUploadByCode($order_option['value']); 206 207 if ($upload_info) { 208 $value = $upload_info['name']; 209 } else { 210 $value = ''; 211 } 212 } 213 214 $option_data[] = array( 215 'name' => $order_option['name'], 216 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) 217 ); 218 } 219 220 $data['products'][] = array( 221 'name' => $order_product['name'], 222 'model' => $order_product['model'], 223 'option' => $option_data, 224 'quantity' => $order_product['quantity'], 225 'price' => $this->currency->format($order_product['price'] + ($this->config->get('config_tax') ? $order_product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']), 226 'total' => $this->currency->format($order_product['total'] + ($this->config->get('config_tax') ? ($order_product['tax'] * $order_product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']) 227 ); 228 } 229 230 // Vouchers 231 $data['vouchers'] = array(); 232 233 $order_vouchers = $this->model_checkout_order->getOrderVouchers($order_info['order_id']); 234 235 foreach ($order_vouchers as $order_voucher) { 236 $data['vouchers'][] = array( 237 'description' => $order_voucher['description'], 238 'amount' => $this->currency->format($order_voucher['amount'], $order_info['currency_code'], $order_info['currency_value']), 239 ); 240 } 241 242 // Order Totals 243 $data['totals'] = array(); 244 245 $order_totals = $this->model_checkout_order->getOrderTotals($order_info['order_id']); 246 247 foreach ($order_totals as $order_total) { 248 $data['totals'][] = array( 249 'title' => $order_total['title'], 250 'text' => $this->currency->format($order_total['value'], $order_info['currency_code'], $order_info['currency_value']), 251 ); 252 } 253 254 $this->load->model('setting/setting'); 255 256 $from = $this->model_setting_setting->getSettingValue('config_email', $order_info['store_id']); 257 258 if (!$from) { 259 $from = $this->config->get('config_email'); 260 } 261 262 $mail = new Mail($this->config->get('config_mail_engine')); 263 $mail->parameter = $this->config->get('config_mail_parameter'); 264 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 265 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 266 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 267 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 268 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 269 270 $mail->setTo($order_info['email']); 271 $mail->setFrom($from); 272 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')); 273 $mail->setSubject(html_entity_decode(sprintf($language->get('text_subject'), $order_info['store_name'], $order_info['order_id']), ENT_QUOTES, 'UTF-8')); 274 $mail->setHtml($this->load->view('mail/order_add', $data)); 275 $mail->send(); 276 } 277 278 public function edit($order_info, $order_status_id, $comment) { 279 $language = new Language($order_info['language_code']); 280 $language->load($order_info['language_code']); 281 $language->load('mail/order_edit'); 282 283 $data['text_order_id'] = $language->get('text_order_id'); 284 $data['text_date_added'] = $language->get('text_date_added'); 285 $data['text_order_status'] = $language->get('text_order_status'); 286 $data['text_link'] = $language->get('text_link'); 287 $data['text_comment'] = $language->get('text_comment'); 288 $data['text_footer'] = $language->get('text_footer'); 289 290 $data['order_id'] = $order_info['order_id']; 291 $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added'])); 292 293 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'"); 294 295 if ($order_status_query->num_rows) { 296 $data['order_status'] = $order_status_query->row['name']; 297 } else { 298 $data['order_status'] = ''; 299 } 300 301 if ($order_info['customer_id']) { 302 $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_info['order_id']; 303 } else { 304 $data['link'] = ''; 305 } 306 307 $data['comment'] = strip_tags($comment); 308 309 $this->load->model('setting/setting'); 310 311 $from = $this->model_setting_setting->getSettingValue('config_email', $order_info['store_id']); 312 313 if (!$from) { 314 $from = $this->config->get('config_email'); 315 } 316 317 $mail = new Mail($this->config->get('config_mail_engine')); 318 $mail->parameter = $this->config->get('config_mail_parameter'); 319 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 320 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 321 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 322 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 323 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 324 325 $mail->setTo($order_info['email']); 326 $mail->setFrom($from); 327 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')); 328 $mail->setSubject(html_entity_decode(sprintf($language->get('text_subject'), $order_info['store_name'], $order_info['order_id']), ENT_QUOTES, 'UTF-8')); 329 $mail->setText($this->load->view('mail/order_edit', $data)); 330 $mail->send(); 331 } 332 333 // Admin Alert Mail 334 public function alert(&$route, &$args) { 335 if (isset($args[0])) { 336 $order_id = $args[0]; 337 } else { 338 $order_id = 0; 339 } 340 341 if (isset($args[1])) { 342 $order_status_id = $args[1]; 343 } else { 344 $order_status_id = 0; 345 } 346 347 if (isset($args[2])) { 348 $comment = $args[2]; 349 } else { 350 $comment = ''; 351 } 352 353 if (isset($args[3])) { 354 $notify = $args[3]; 355 } else { 356 $notify = ''; 357 } 358 359 $order_info = $this->model_checkout_order->getOrder($order_id); 360 361 if ($order_info && !$order_info['order_status_id'] && $order_status_id && in_array('order', (array)$this->config->get('config_mail_alert'))) { 362 $this->load->language('mail/order_alert'); 363 364 // HTML Mail 365 $data['text_received'] = $this->language->get('text_received'); 366 $data['text_order_id'] = $this->language->get('text_order_id'); 367 $data['text_date_added'] = $this->language->get('text_date_added'); 368 $data['text_order_status'] = $this->language->get('text_order_status'); 369 $data['text_product'] = $this->language->get('text_product'); 370 $data['text_total'] = $this->language->get('text_total'); 371 $data['text_comment'] = $this->language->get('text_comment'); 372 373 $data['order_id'] = $order_info['order_id']; 374 $data['date_added'] = date($this->language->get('date_format_short'), strtotime($order_info['date_added'])); 375 376 $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); 377 378 if ($order_status_query->num_rows) { 379 $data['order_status'] = $order_status_query->row['name']; 380 } else { 381 $data['order_status'] = ''; 382 } 383 384 $this->load->model('tool/upload'); 385 386 $data['products'] = array(); 387 388 $order_products = $this->model_checkout_order->getOrderProducts($order_id); 389 390 foreach ($order_products as $order_product) { 391 $option_data = array(); 392 393 $order_options = $this->model_checkout_order->getOrderOptions($order_info['order_id'], $order_product['order_product_id']); 394 395 foreach ($order_options as $order_option) { 396 if ($order_option['type'] != 'file') { 397 $value = $order_option['value']; 398 } else { 399 $upload_info = $this->model_tool_upload->getUploadByCode($order_option['value']); 400 401 if ($upload_info) { 402 $value = $upload_info['name']; 403 } else { 404 $value = ''; 405 } 406 } 407 408 $option_data[] = array( 409 'name' => $order_option['name'], 410 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) 411 ); 412 } 413 414 $data['products'][] = array( 415 'name' => $order_product['name'], 416 'model' => $order_product['model'], 417 'quantity' => $order_product['quantity'], 418 'option' => $option_data, 419 'total' => html_entity_decode($this->currency->format($order_product['total'] + ($this->config->get('config_tax') ? ($order_product['tax'] * $order_product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') 420 ); 421 } 422 423 $data['vouchers'] = array(); 424 425 $order_vouchers = $this->model_checkout_order->getOrderVouchers($order_id); 426 427 foreach ($order_vouchers as $order_voucher) { 428 $data['vouchers'][] = array( 429 'description' => $order_voucher['description'], 430 'amount' => html_entity_decode($this->currency->format($order_voucher['amount'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') 431 ); 432 } 433 434 $data['totals'] = array(); 435 436 $order_totals = $this->model_checkout_order->getOrderTotals($order_id); 437 438 foreach ($order_totals as $order_total) { 439 $data['totals'][] = array( 440 'title' => $order_total['title'], 441 'value' => html_entity_decode($this->currency->format($order_total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') 442 ); 443 } 444 445 $data['comment'] = strip_tags($order_info['comment']); 446 447 $mail = new Mail($this->config->get('config_mail_engine')); 448 $mail->parameter = $this->config->get('config_mail_parameter'); 449 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 450 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 451 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 452 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 453 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 454 455 $mail->setTo($this->config->get('config_email')); 456 $mail->setFrom($this->config->get('config_email')); 457 $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')); 458 $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_subject'), $this->config->get('config_name'), $order_info['order_id']), ENT_QUOTES, 'UTF-8')); 459 $mail->setText($this->load->view('mail/order_alert', $data)); 460 $mail->send(); 461 462 // Send to additional alert emails 463 $emails = explode(',', $this->config->get('config_mail_alert_email')); 464 465 foreach ($emails as $email) { 466 if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) { 467 $mail->setTo($email); 468 $mail->send(); 469 } 470 } 471 } 472 } 473 }