diff --git a/classes/CustomerMessage.php b/classes/CustomerMessage.php index d6b7108dc..79fb9d228 100644 --- a/classes/CustomerMessage.php +++ b/classes/CustomerMessage.php @@ -55,5 +55,19 @@ class CustomerMessageCore extends ObjectModel $fields['date_add'] = pSQL($this->date_add); return $fields; } + + public static function getMessagesByOrderId($id_order) + { + return Db::getInstance()->ExecuteS(' + SELECT cm.*, c.`firstname` AS cfirstname, c.`lastname` AS clastname, e.`firstname` AS efirstname, e.`lastname` AS elastname, (COUNT(cm.id_customer_message) = 0 AND ct.id_customer != 0) AS is_new_for_me + FROM `'._DB_PREFIX_.'customer_message` cm + LEFT JOIN `'._DB_PREFIX_.'customer_thread` ct ON ct.`id_customer_thread` = cm.`id_customer_thread` + LEFT JOIN `'._DB_PREFIX_.'customer` c ON ct.`id_customer` = c.`id_customer` + LEFT OUTER JOIN `'._DB_PREFIX_.'employee` e ON e.`id_employee` = cm.`id_employee` + WHERE ct.id_order = '.(int)$id_order.' + GROUP BY cm.id_customer_message + ORDER BY cm.date_add DESC'); + } + } diff --git a/classes/CustomerThread.php b/classes/CustomerThread.php index 6c695d40e..5093ab16c 100644 --- a/classes/CustomerThread.php +++ b/classes/CustomerThread.php @@ -79,6 +79,13 @@ class CustomerThreadCore extends ObjectModel SELECT * FROM '._DB_PREFIX_.'customer_thread ct LEFT JOIN '._DB_PREFIX_.'customer_message cm ON ct.id_customer_thread = cm.id_customer_thread WHERE id_customer = '.(int)($id_customer)); - } + } + + public static function getIdCustomerThreadByEmailAndIdOrder($email, $id_order) + { + return Db::getInstance()->getValue(' + SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm + WHERE cm.email = \''.pSQL($email).'\' AND cm.id_shop = '.(int)Context::getContext()->shop->getId(true).' AND cm.id_order = '.(int)$id_order.''); + } } diff --git a/controllers/ContactController.php b/controllers/ContactController.php index 3ad04c7af..2e9171b83 100644 --- a/controllers/ContactController.php +++ b/controllers/ContactController.php @@ -106,9 +106,7 @@ class ContactControllerCore extends FrontController SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop = '.(int)$this->id_current_shop.' AND token = \''.pSQL(Tools::getValue('token')).'\'') ) OR ( - $id_customer_thread = (int)Db::getInstance()->getValue(' - SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm - WHERE cm.email = \''.pSQL($from).'\' AND cm.id_shop = '.(int)$this->id_current_shop.' AND cm.id_order = '.(int)(Tools::getValue('id_order')).'') + $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, (int)Tools::getValue('id_order')) ))) { $fields = Db::getInstance()->ExecuteS(' diff --git a/controllers/OrderDetailController.php b/controllers/OrderDetailController.php index 0bb9ad883..81ae4010b 100644 --- a/controllers/OrderDetailController.php +++ b/controllers/OrderDetailController.php @@ -59,12 +59,34 @@ class OrderDetailControllerCore extends FrontController $order = new Order($idOrder); if (Validate::isLoadedObject($order) AND $order->id_customer == $this->context->customer->id) { - $message = new Message(); - $message->id_customer = (int)$this->context->customer->id; - $message->message = $msgText; - $message->id_order = (int)($idOrder); - $message->private = false; - $message->add(); + //check if a thread already exist + $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($this->context->customer->email, $order->id); + + p(var_dump($id_customer_thread)); + + $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->getId(true); + if ($id_product = (int)Tools::getValue('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); + $cm->id_customer_thread = $ct->id; + $cm->message = Tools::htmlentitiesutf8(Tools::nl2br($msgText)); + $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']); + $cm->add(); + if (!Configuration::get('PS_MAIL_EMAIL_MESSAGE')) $to = strval(Configuration::get('PS_SHOP_EMAIL')); else @@ -73,16 +95,18 @@ class OrderDetailControllerCore extends FrontController $to = strval($to->email); } $toName = strval(Configuration::get('PS_SHOP_NAME')); - $customer = $this->context->customer->id; + $customer = $this->context->customer; + 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)($message->id_order), - '{message}' => $message->message), + '{id_order}' => (int)($order->id), + '{message}' => $msgText), $to, $toName, $customer->email, $customer->firstname.' '.$customer->lastname); + if (Tools::getValue('ajax') != 'true') Tools::redirect('index.php?controller=order-detail&id_order='.(int)$idOrder); } @@ -140,7 +164,7 @@ class OrderDetailControllerCore extends FrontController 'deliveryAddressFormatedValues' => $deliveryAddressFormatedValues, 'deliveryState' => (Validate::isLoadedObject($addressDelivery) AND $addressDelivery->id_state) ? new State($addressDelivery->id_state) : false, 'is_guest' => false, - 'messages' => Message::getMessagesByOrderId((int)($order->id)), + 'messages' => CustomerMessage::getMessagesByOrderId((int)($order->id)), 'CUSTOMIZE_FILE' => Product::CUSTOMIZE_FILE, 'CUSTOMIZE_TEXTFIELD' => _CUSTOMIZE_TEXTFIELD_, 'isRecyclable' => Configuration::get('PS_RECYCLABLE_PACK'), diff --git a/themes/prestashop/js/history.js b/themes/prestashop/js/history.js index e27ca7260..39921e96e 100644 --- a/themes/prestashop/js/history.js +++ b/themes/prestashop/js/history.js @@ -107,7 +107,7 @@ function updateOrderLineDisplay(domCheckbox){ //send a message in relation to the order with ajax function sendOrderMessage (){ paramString = "controller=order-detail&ajax=true"; - $('form#sendOrderMessage').find('input, textarea').each(function(){ + $('form#sendOrderMessage').find('input, textarea, select').each(function(){ paramString += '&' + $(this).attr('name') + '=' + encodeURI($(this).val()); }); $.ajax({ diff --git a/themes/prestashop/order-detail.tpl b/themes/prestashop/order-detail.tpl index 7c4d48fb3..fc5a6eb4d 100644 --- a/themes/prestashop/order-detail.tpl +++ b/themes/prestashop/order-detail.tpl @@ -356,6 +356,15 @@

{l s='Add a message:'}

{l s='If you would like to add a comment about your order, please write it below.'}

+

+ + +