Comments.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Order;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Comments extends \Magento\Framework\View\Element\Template
  12. {
  13. /**
  14. * @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory
  15. */
  16. protected $_invoiceCollectionFactory;
  17. /**
  18. * @var \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory
  19. */
  20. protected $_memoCollectionFactory;
  21. /**
  22. * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory
  23. */
  24. protected $_shipmentCollectionFactory;
  25. /**
  26. * Current entity (model instance) with getCommentsCollection() method
  27. *
  28. * @var \Magento\Sales\Model\AbstractModel
  29. */
  30. protected $_entity;
  31. /**
  32. * Current comments collection
  33. *
  34. * @var \Magento\Sales\Model\ResourceModel\Order\Comment\Collection\AbstractCollection
  35. */
  36. protected $_commentCollection;
  37. /**
  38. * @param \Magento\Framework\View\Element\Template\Context $context
  39. * @param \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory $invoiceCollectionFactory
  40. * @param \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $memoCollectionFactory
  41. * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $shipmentCollectionFactory
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Element\Template\Context $context,
  46. \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory $invoiceCollectionFactory,
  47. \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $memoCollectionFactory,
  48. \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $shipmentCollectionFactory,
  49. array $data = []
  50. ) {
  51. parent::__construct($context, $data);
  52. $this->_invoiceCollectionFactory = $invoiceCollectionFactory;
  53. $this->_memoCollectionFactory = $memoCollectionFactory;
  54. $this->_shipmentCollectionFactory = $shipmentCollectionFactory;
  55. }
  56. /**
  57. * Sets comments parent model instance
  58. *
  59. * @param \Magento\Sales\Model\AbstractModel $entity
  60. * @return $this
  61. */
  62. public function setEntity($entity)
  63. {
  64. $this->_entity = $entity;
  65. $this->_commentCollection = null;
  66. // Changing model and resource model can lead to change of comment collection
  67. return $this;
  68. }
  69. /**
  70. * Gets comments parent model instance
  71. *
  72. * @return \Magento\Sales\Model\AbstractModel
  73. */
  74. public function getEntity()
  75. {
  76. return $this->_entity;
  77. }
  78. /**
  79. * Initialize model comments and return comment collection
  80. *
  81. * @return \Magento\Sales\Model\ResourceModel\Order\Comment\Collection\AbstractCollection
  82. * @throws \Magento\Framework\Exception\LocalizedException
  83. */
  84. public function getComments()
  85. {
  86. if ($this->_commentCollection === null) {
  87. $entity = $this->getEntity();
  88. if ($entity instanceof \Magento\Sales\Model\Order\Invoice) {
  89. $this->_commentCollection = $this->_invoiceCollectionFactory->create();
  90. } elseif ($entity instanceof \Magento\Sales\Model\Order\Creditmemo) {
  91. $this->_commentCollection = $this->_memoCollectionFactory->create();
  92. } elseif ($entity instanceof \Magento\Sales\Model\Order\Shipment) {
  93. $this->_commentCollection = $this->_shipmentCollectionFactory->create();
  94. } else {
  95. throw new \Magento\Framework\Exception\LocalizedException(__('We found an invalid entity model.'));
  96. }
  97. $this->_commentCollection->setParentFilter($entity)->setCreatedAtOrder()->addVisibleOnFrontFilter();
  98. }
  99. return $this->_commentCollection;
  100. }
  101. /**
  102. * Returns whether there are comments to show on frontend
  103. *
  104. * @return bool
  105. */
  106. public function hasComments()
  107. {
  108. return $this->getComments()->count() > 0;
  109. }
  110. }