1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Controller\Adminhtml\Order;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
/**
* Class AddComment
*/
class AddComment extends \Magento\Sales\Controller\Adminhtml\Order implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Sales::comment';
/**
* ACL resource needed to send comment email notification
*/
const ADMIN_SALES_EMAIL_RESOURCE = 'Magento_Sales::emails';
/**
* Add order comment action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$order = $this->_initOrder();
if ($order) {
try {
$data = $this->getRequest()->getPost('history');
if (empty($data['comment']) && $data['status'] == $order->getDataByKey('status')) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The comment is missing. Enter and try again.')
);
}
$notify = $data['is_customer_notified'] ?? false;
$visible = $data['is_visible_on_front'] ?? false;
if ($notify && !$this->_authorization->isAllowed(self::ADMIN_SALES_EMAIL_RESOURCE)) {
$notify = false;
}
$history = $order->addStatusHistoryComment($data['comment'], $data['status']);
$history->setIsVisibleOnFront($visible);
$history->setIsCustomerNotified($notify);
$history->save();
$comment = trim(strip_tags($data['comment']));
$order->save();
/** @var OrderCommentSender $orderCommentSender */
$orderCommentSender = $this->_objectManager
->create(\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender::class);
$orderCommentSender->send($order, $notify, $comment);
return $this->resultPageFactory->create();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$response = ['error' => true, 'message' => $e->getMessage()];
} catch (\Exception $e) {
$response = ['error' => true, 'message' => __('We cannot add order history.')];
}
if (is_array($response)) {
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
return $resultJson;
}
}
return $this->resultRedirectFactory->create()->setPath('sales/*/');
}
}