1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Sales
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26: class Mage_Sales_Block_Order_Comments extends Mage_Core_Block_Template
27: {
28: /**
29: * Current entity (model instance) with getCommentsCollection() method
30: *
31: * @var Mage_Sales_Model_Abstract
32: */
33: protected $_entity;
34:
35: /**
36: * Currect comments collection
37: *
38: * @var Mage_Sales_Model_Mysql4_Order_Comment_Collection_Abstract
39: */
40: protected $_commentCollection;
41:
42: /**
43: * Sets comments parent model instance
44: *
45: * @param Mage_Sales_Model_Abstract
46: * @return Mage_Sales_Block_Order_Comments
47: */
48: public function setEntity($entity)
49: {
50: $this->_entity = $entity;
51: $this->_commentCollection = null; // Changing model and resource model can lead to change of comment collection
52: return $this;
53: }
54:
55: /**
56: * Gets comments parent model instance
57: *
58: * @return Mage_Sales_Model_Abstract
59: */
60: public function getEntity()
61: {
62: return $this->_entity;
63: }
64:
65: /**
66: * Initialize model comments and return comment collection
67: *
68: * @return Mage_Sales_Model_Mysql4_Order_Comment_Collection_Abstract
69: */
70: public function getComments()
71: {
72: if (is_null($this->_commentCollection)) {
73: $entity = $this->getEntity();
74: if ($entity instanceof Mage_Sales_Model_Order_Invoice) {
75: $collectionClass = 'sales/order_invoice_comment_collection';
76: } else if ($entity instanceof Mage_Sales_Model_Order_Creditmemo) {
77: $collectionClass = 'sales/order_creditmemo_comment_collection';
78: } else if ($entity instanceof Mage_Sales_Model_Order_Shipment) {
79: $collectionClass = 'sales/order_shipment_comment_collection';
80: } else {
81: Mage::throwException(Mage::helper('sales')->__('Invalid entity model'));
82: }
83:
84: $this->_commentCollection = Mage::getResourceModel($collectionClass);
85: $this->_commentCollection->setParentFilter($entity)
86: ->setCreatedAtOrder()
87: ->addVisibleOnFrontFilter();
88: }
89:
90: return $this->_commentCollection;
91: }
92:
93: /**
94: * Returns whether there are comments to show on frontend
95: *
96: * @return bool
97: */
98: public function hasComments()
99: {
100: return $this->getComments()->count() > 0;
101: }
102: }
103: