chocolatdemariage/www/override/controllers/front/OrderDetailController.php

124 lines
5.8 KiB
PHP
Raw Normal View History

2017-07-06 17:41:10 +02:00
<?php
class OrderDetailController extends OrderDetailControllerCore
{
public function postProcess()
{
if (Tools::isSubmit('submitMessage')) {
$idOrder = (int)Tools::getValue('id_order');
$msgText = Tools::getValue('msgText');
if (!$idOrder || !Validate::isUnsignedId($idOrder)) {
$this->errors[] = Tools::displayError('The order is no longer valid.');
} elseif (empty($msgText)) {
$this->errors[] = Tools::displayError('The message cannot be blank.');
} elseif (!Validate::isMessage($msgText)) {
$this->errors[] = Tools::displayError('This message is invalid (HTML is not allowed).');
}
if (!count($this->errors)) {
$order = new Order($idOrder);
if (Validate::isLoadedObject($order) && $order->id_customer == $this->context->customer->id) {
//check if a thread already exist
$id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($this->context->customer->email, $order->id);
$id_product = (int)Tools::getValue('id_product');
$cm = new CustomerMessage();
if (!$id_customer_thread) {
$ct = new CustomerThread();
$ct->id_contact = 0;
$ct->id_customer = (int)$order->id_customer;
$ct->id_shop = (int)$this->context->shop->id;
if ($id_product && $order->orderContainProduct($id_product)) {
$ct->id_product = $id_product;
}
$ct->id_order = (int)$order->id;
$ct->id_lang = (int)$this->context->language->id;
$ct->email = $this->context->customer->email;
$ct->status = 'open';
$ct->token = Tools::passwdGen(12);
$ct->add();
} else {
$ct = new CustomerThread((int)$id_customer_thread);
$ct->status = 'open';
$ct->update();
}
$cm->id_customer_thread = $ct->id;
$cm->message = $msgText;
$cm->ip_address = (int)ip2long($_SERVER['REMOTE_ADDR']);
$cm->add();
if (!Configuration::get('PS_MAIL_EMAIL_MESSAGE')) {
$to = strval(Configuration::get('PS_SHOP_EMAIL'));
} else {
$to = new Contact((int)Configuration::get('PS_MAIL_EMAIL_MESSAGE'));
$to = strval($to->email);
}
$toName = strval(Configuration::get('PS_SHOP_NAME'));
$customer = $this->context->customer;
$product = new Product($id_product);
$product_name = '';
if (Validate::isLoadedObject($product) && isset($product->name[(int)$this->context->language->id])) {
$product_name = $product->name[(int)$this->context->language->id];
}
if (Validate::isLoadedObject($customer)) {
Mail::Send($this->context->language->id, 'order_customer_comment', Mail::l('Message from a customer'),
array(
'{lastname}' => $customer->lastname,
'{firstname}' => $customer->firstname,
'{email}' => $customer->email,
'{id_order}' => (int)$order->id,
'{order_name}' => $order->getUniqReference(),
'{message}' => Tools::nl2br($msgText),
'{product_name}' => $product_name
),
$to, $toName, $customer->email, $customer->firstname.' '.$customer->lastname);
}
if (Tools::getValue('ajax') != 'true') {
Tools::redirect('index.php?controller=order-detail&id_order='.(int)$idOrder.'&confirm=true');
}
$this->context->smarty->assign('message_confirmation', true);
} else {
$this->errors[] = Tools::displayError('Order not found');
}
}
}
2017-08-11 15:28:44 +02:00
// Validation du BAT
if (Tools::isSubmit('submitValidateBAT')) {
$id_order = (int)Tools::getValue('id_order');
$order = new Order($id_order);
$new_order_state = Configuration::get('PS_BATVALIDATE_ID_ORDER_STATE');
$order_state = new OrderState($new_order_state);
if (!Validate::isLoadedObject($order_state)) {
$this->errors[] = Tools::displayError('The new order status is invalid.');
} else {
// Check OrderHistory
$orderHistory = $order->getHistory($id_lang);
if (count($history) > 0) {
foreach($orderHistory as $h) {
if ($h['id_order_state'] == $new_order_state) {
$this->errors[] = Tools::displayError('The order has already been assigned this status.');
break;
}
}
}
2017-07-06 17:41:10 +02:00
2017-08-11 15:28:44 +02:00
// Create new OrderHistory
if (count($this->errors) == 0) {
$history = new OrderHistory();
$history->id_order = $order->id;
$history->changeIdOrderState($new_order_state, $order);
$history->add(true);
}
}
}
}
2017-07-06 17:41:10 +02:00
}