123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Block\Order;
- /**
- * @api
- * @since 100.0.2
- */
- class Comments extends \Magento\Framework\View\Element\Template
- {
- /**
- * @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory
- */
- protected $_invoiceCollectionFactory;
- /**
- * @var \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory
- */
- protected $_memoCollectionFactory;
- /**
- * @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory
- */
- protected $_shipmentCollectionFactory;
- /**
- * Current entity (model instance) with getCommentsCollection() method
- *
- * @var \Magento\Sales\Model\AbstractModel
- */
- protected $_entity;
- /**
- * Current comments collection
- *
- * @var \Magento\Sales\Model\ResourceModel\Order\Comment\Collection\AbstractCollection
- */
- protected $_commentCollection;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory $invoiceCollectionFactory
- * @param \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $memoCollectionFactory
- * @param \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $shipmentCollectionFactory
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\CollectionFactory $invoiceCollectionFactory,
- \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory $memoCollectionFactory,
- \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $shipmentCollectionFactory,
- array $data = []
- ) {
- parent::__construct($context, $data);
- $this->_invoiceCollectionFactory = $invoiceCollectionFactory;
- $this->_memoCollectionFactory = $memoCollectionFactory;
- $this->_shipmentCollectionFactory = $shipmentCollectionFactory;
- }
- /**
- * Sets comments parent model instance
- *
- * @param \Magento\Sales\Model\AbstractModel $entity
- * @return $this
- */
- public function setEntity($entity)
- {
- $this->_entity = $entity;
- $this->_commentCollection = null;
- // Changing model and resource model can lead to change of comment collection
- return $this;
- }
- /**
- * Gets comments parent model instance
- *
- * @return \Magento\Sales\Model\AbstractModel
- */
- public function getEntity()
- {
- return $this->_entity;
- }
- /**
- * Initialize model comments and return comment collection
- *
- * @return \Magento\Sales\Model\ResourceModel\Order\Comment\Collection\AbstractCollection
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getComments()
- {
- if ($this->_commentCollection === null) {
- $entity = $this->getEntity();
- if ($entity instanceof \Magento\Sales\Model\Order\Invoice) {
- $this->_commentCollection = $this->_invoiceCollectionFactory->create();
- } elseif ($entity instanceof \Magento\Sales\Model\Order\Creditmemo) {
- $this->_commentCollection = $this->_memoCollectionFactory->create();
- } elseif ($entity instanceof \Magento\Sales\Model\Order\Shipment) {
- $this->_commentCollection = $this->_shipmentCollectionFactory->create();
- } else {
- throw new \Magento\Framework\Exception\LocalizedException(__('We found an invalid entity model.'));
- }
- $this->_commentCollection->setParentFilter($entity)->setCreatedAtOrder()->addVisibleOnFrontFilter();
- }
- return $this->_commentCollection;
- }
- /**
- * Returns whether there are comments to show on frontend
- *
- * @return bool
- */
- public function hasComments()
- {
- return $this->getComments()->count() > 0;
- }
- }
|