1: <?php
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: class Mage_Review_Model_Review extends Mage_Core_Model_Abstract
45: {
46:
47: 48: 49: 50: 51:
52: protected $_eventPrefix = 'review';
53:
54: 55: 56: 57:
58: const ENTITY_PRODUCT = 1;
59:
60: 61: 62: 63:
64: const ENTITY_PRODUCT_CODE = 'product';
65: const ENTITY_CUSTOMER_CODE = 'customer';
66: const ENTITY_CATEGORY_CODE = 'category';
67:
68: const STATUS_APPROVED = 1;
69: const STATUS_PENDING = 2;
70: const STATUS_NOT_APPROVED = 3;
71:
72: protected function _construct()
73: {
74: $this->_init('review/review');
75: }
76:
77: public function getProductCollection()
78: {
79: return Mage::getResourceModel('review/review_product_collection');
80: }
81:
82: public function getStatusCollection()
83: {
84: return Mage::getResourceModel('review/review_status_collection');
85: }
86:
87: public function getTotalReviews($entityPkValue, $approvedOnly=false, $storeId=0)
88: {
89: return $this->getResource()->getTotalReviews($entityPkValue, $approvedOnly, $storeId);
90: }
91:
92: public function aggregate()
93: {
94: $this->getResource()->aggregate($this);
95: return $this;
96: }
97:
98: public function getEntitySummary($product, $storeId=0)
99: {
100: $summaryData = Mage::getModel('review/review_summary')
101: ->setStoreId($storeId)
102: ->load($product->getId());
103: $summary = new Varien_Object();
104: $summary->setData($summaryData->getData());
105: $product->setRatingSummary($summary);
106: }
107:
108: public function getPendingStatus()
109: {
110: return self::STATUS_PENDING;
111: }
112:
113: public function getReviewUrl()
114: {
115: return Mage::getUrl('review/product/view', array('id' => $this->getReviewId()));
116: }
117:
118: public function validate()
119: {
120: $errors = array();
121:
122: if (!Zend_Validate::is($this->getTitle(), 'NotEmpty')) {
123: $errors[] = Mage::helper('review')->__('Review summary can\'t be empty');
124: }
125:
126: if (!Zend_Validate::is($this->getNickname(), 'NotEmpty')) {
127: $errors[] = Mage::helper('review')->__('Nickname can\'t be empty');
128: }
129:
130: if (!Zend_Validate::is($this->getDetail(), 'NotEmpty')) {
131: $errors[] = Mage::helper('review')->__('Review can\'t be empty');
132: }
133:
134: if (empty($errors)) {
135: return true;
136: }
137: return $errors;
138: }
139:
140: 141: 142: 143: 144:
145: protected function _afterDeleteCommit()
146: {
147: $this->getResource()->afterDeleteCommit($this);
148: return parent::_afterDeleteCommit();
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function appendSummary($collection)
158: {
159: $entityIds = array();
160: foreach ($collection->getItems() as $_itemId => $_item) {
161: $entityIds[] = $_item->getEntityId();
162: }
163:
164: if (sizeof($entityIds) == 0) {
165: return $this;
166: }
167:
168: $summaryData = Mage::getResourceModel('review/review_summary_collection')
169: ->addEntityFilter($entityIds)
170: ->addStoreFilter(Mage::app()->getStore()->getId())
171: ->load();
172:
173: foreach ($collection->getItems() as $_item ) {
174: foreach ($summaryData as $_summary) {
175: if ($_summary->getEntityPkValue() == $_item->getEntityId()) {
176: $_item->setRatingSummary($_summary);
177: }
178: }
179: }
180:
181: return $this;
182: }
183:
184: protected function _beforeDelete()
185: {
186: $this->_protectFromNonAdmin();
187: return parent::_beforeDelete();
188: }
189:
190: 191: 192: 193: 194:
195: public function isApproved()
196: {
197: return $this->getStatusId() == self::STATUS_APPROVED;
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function isAvailableOnStore($store = null)
207: {
208: $store = Mage::app()->getStore($store);
209: if ($store) {
210: return in_array($store->getId(), (array)$this->getStores());
211: }
212:
213: return false;
214: }
215:
216: 217: 218: 219: 220: 221:
222: public function getEntityIdByCode($entityCode)
223: {
224: return $this->getResource()->getEntityIdByCode($entityCode);
225: }
226: }
227: