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: class Mage_Sales_Block_Recurring_Profile_View extends Mage_Core_Block_Template
31: {
32: 33: 34:
35: protected $_profile = null;
36:
37: 38: 39: 40: 41:
42: protected $_shouldRenderInfo = false;
43:
44: 45: 46: 47: 48:
49: protected $_info = array();
50:
51: 52: 53: 54: 55:
56: protected $_relatedOrders = null;
57:
58: 59: 60:
61: public function prepareViewData()
62: {
63: $this->addData(array(
64: 'reference_id' => $this->_profile->getReferenceId(),
65: 'can_cancel' => $this->_profile->canCancel(),
66: 'cancel_url' => $this->getUrl('*/*/updateState', array('profile' => $this->_profile->getId(), 'action' => 'cancel')),
67: 'can_suspend' => $this->_profile->canSuspend(),
68: 'suspend_url' => $this->getUrl('*/*/updateState', array('profile' => $this->_profile->getId(), 'action' => 'suspend')),
69: 'can_activate' => $this->_profile->canActivate(),
70: 'activate_url' => $this->getUrl('*/*/updateState', array('profile' => $this->_profile->getId(), 'action' => 'activate')),
71: 'can_update' => $this->_profile->canFetchUpdate(),
72: 'update_url' => $this->getUrl('*/*/updateProfile', array('profile' => $this->_profile->getId())),
73: 'back_url' => $this->getUrl('*/*/'),
74: 'confirmation_message' => Mage::helper('sales')->__('Are you sure you want to do this?'),
75: ));
76: }
77:
78: 79: 80: 81: 82:
83: public function getRenderedInfo()
84: {
85: return $this->_info;
86: }
87:
88: 89: 90:
91: public function prepareReferenceInfo()
92: {
93: $this->_shouldRenderInfo = true;
94:
95: foreach (array('method_code', 'reference_id', 'schedule_description', 'state') as $key) {
96: $this->_addInfo(array(
97: 'label' => $this->_profile->getFieldLabel($key),
98: 'value' => $this->_profile->renderData($key),
99: ));
100: }
101:
102:
103:
104:
105:
106:
107:
108: }
109:
110: 111: 112:
113: public function prepareItemInfo()
114: {
115: $this->_shouldRenderInfo = true;
116: $key = 'order_item_info';
117:
118: foreach (array('name' => Mage::helper('catalog')->__('Product Name'),
119: 'sku' => Mage::helper('catalog')->__('SKU'),
120: 'qty' => Mage::helper('catalog')->__('Quantity'),
121: ) as $itemKey => $label
122: ) {
123: $value = $this->_profile->getInfoValue($key, $itemKey);
124: if ($value) {
125: $this->_addInfo(array('label' => $label, 'value' => $value,));
126: }
127: }
128:
129: $request = $this->_profile->getInfoValue($key, 'info_buyRequest');
130: if (empty($request)) {
131: return;
132: }
133:
134: $request = unserialize($request);
135: if (empty($request['options'])) {
136: return;
137: }
138:
139: $options = Mage::getModel('catalog/product_option')->getCollection()
140: ->addIdsToFilter(array_keys($request['options']))
141: ->addTitleToResult($this->_profile->getInfoValue($key, 'store_id'))
142: ->addValuesToResult();
143:
144: $productMock = Mage::getModel('catalog/product');
145: $quoteItemOptionMock = Mage::getModel('sales/quote_item_option');
146: foreach ($options as $option) {
147: $quoteItemOptionMock->setId($option->getId());
148:
149: $group = $option->groupFactory($option->getType())
150: ->setOption($option)
151: ->setRequest(new Varien_Object($request))
152: ->setProduct($productMock)
153: ->setUseQuotePath(true)
154: ->setQuoteItemOption($quoteItemOptionMock)
155: ->validateUserValue($request['options']);
156:
157: $skipHtmlEscaping = false;
158: if ('file' == $option->getType()) {
159: $skipHtmlEscaping = true;
160:
161: $downloadParams = array(
162: 'id' => $this->_profile->getId(),
163: 'option_id' => $option->getId(),
164: 'key' => $request['options'][$option->getId()]['secret_key']
165: );
166: $group->setCustomOptionDownloadUrl('sales/download/downloadProfileCustomOption')
167: ->setCustomOptionUrlParams($downloadParams);
168: }
169:
170: $optionValue = $group->prepareForCart();
171:
172: $this->_addInfo(array(
173: 'label' => $option->getTitle(),
174: 'value' => $group->getFormattedOptionValue($optionValue),
175: 'skip_html_escaping' => $skipHtmlEscaping
176: ));
177: }
178: }
179:
180: 181: 182:
183: public function prepareScheduleInfo()
184: {
185: $this->_shouldRenderInfo = true;
186:
187: foreach (array('start_datetime', 'suspension_threshold') as $key) {
188: $this->_addInfo(array(
189: 'label' => $this->_profile->getFieldLabel($key),
190: 'value' => $this->_profile->renderData($key),
191: ));
192: }
193:
194: foreach ($this->_profile->exportScheduleInfo() as $i) {
195: $this->_addInfo(array(
196: 'label' => $i->getTitle(),
197: 'value' => $i->getSchedule(),
198: ));
199: }
200: }
201:
202: 203: 204:
205: public function prepareFeesInfo()
206: {
207: $this->_shouldRenderInfo = true;
208:
209: $this->_addInfo(array(
210: 'label' => $this->_profile->getFieldLabel('currency_code'),
211: 'value' => $this->_profile->getCurrencyCode()
212: ));
213: foreach (array('init_amount', 'trial_billing_amount', 'billing_amount', 'tax_amount', 'shipping_amount') as $key) {
214: $value = $this->_profile->getData($key);
215: if ($value) {
216: $this->_addInfo(array(
217: 'label' => $this->_profile->getFieldLabel($key),
218: 'value' => Mage::helper('core')->formatCurrency($value, false),
219: 'is_amount' => true,
220: ));
221: }
222: }
223: }
224:
225: 226: 227:
228: public function prepareAddressInfo()
229: {
230: $this->_shouldRenderInfo = true;
231:
232: if ('shipping' == $this->getAddressType()) {
233: if ('1' == $this->_profile->getInfoValue('order_item_info', 'is_virtual')) {
234: $this->getParentBlock()->unsetChild('sales.recurring.profile.view.shipping');
235: return;
236: }
237: $key = 'shipping_address_info';
238: } else {
239: $key = 'billing_address_info';
240: }
241: $this->setIsAddress(true);
242: $address = Mage::getModel('sales/order_address', $this->_profile->getData($key));
243: $this->_addInfo(array(
244: 'value' => preg_replace('/\\n{2,}/', "\n", $address->getFormated()),
245: ));
246: }
247:
248: 249: 250:
251: public function prepareRelatedOrdersFrontendGrid()
252: {
253: $this->_prepareRelatedOrders(array(
254: 'increment_id', 'created_at', 'customer_firstname', 'customer_lastname', 'base_grand_total', 'status'
255: ));
256: $this->_relatedOrders->addFieldToFilter('state', array(
257: 'in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()
258: ));
259:
260: $pager = $this->getLayout()->createBlock('page/html_pager')
261: ->setCollection($this->_relatedOrders)->setIsOutputRequired(false);
262: $this->setChild('pager', $pager);
263:
264: $this->setGridColumns(array(
265: new Varien_Object(array(
266: 'index' => 'increment_id',
267: 'title' => $this->__('Order #'),
268: 'is_nobr' => true,
269: 'width' => 1,
270: )),
271: new Varien_Object(array(
272: 'index' => 'created_at',
273: 'title' => $this->__('Date'),
274: 'is_nobr' => true,
275: 'width' => 1,
276: )),
277: new Varien_Object(array(
278: 'index' => 'customer_name',
279: 'title' => $this->__('Customer Name'),
280: )),
281: new Varien_Object(array(
282: 'index' => 'base_grand_total',
283: 'title' => $this->__('Order Total'),
284: 'is_nobr' => true,
285: 'width' => 1,
286: 'is_amount' => true,
287: )),
288: new Varien_Object(array(
289: 'index' => 'status',
290: 'title' => $this->__('Order Status'),
291: 'is_nobr' => true,
292: 'width' => 1,
293: )),
294: ));
295:
296: $orders = array();
297: foreach ($this->_relatedOrders as $order) {
298: $orders[] = new Varien_Object(array(
299: 'increment_id' => $order->getIncrementId(),
300: 'created_at' => $this->formatDate($order->getCreatedAt()),
301: 'customer_name' => $order->getCustomerName(),
302: 'base_grand_total' => Mage::helper('core')->formatCurrency($order->getBaseGrandTotal(), false),
303: 'status' => $order->getStatusLabel(),
304: 'increment_id_link_url' => $this->getUrl('sales/order/view/', array('order_id' => $order->getId())),
305: ));
306: }
307: if ($orders) {
308: $this->setGridElements($orders);
309: }
310: }
311:
312: 313: 314: 315: 316: 317:
318: public function renderRowValue(Varien_Object $row)
319: {
320: $value = $row->getValue();
321: if (is_array($value)) {
322: $value = implode("\n", $value);
323: }
324: if (!$row->getSkipHtmlEscaping()) {
325: $value = $this->escapeHtml($value);
326: }
327: return nl2br($value);
328: }
329:
330: 331: 332: 333: 334:
335: protected function _prepareRelatedOrders($fieldsToSelect = '*')
336: {
337: if (null === $this->_relatedOrders) {
338: $this->_relatedOrders = Mage::getResourceModel('sales/order_collection')
339: ->addFieldToSelect($fieldsToSelect)
340: ->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
341: ->addRecurringProfilesFilter($this->_profile->getId())
342: ->setOrder('entity_id', 'desc');
343: }
344: }
345:
346: 347: 348: 349: 350: 351:
352: protected function _addInfo(array $data, $key = null)
353: {
354: $object = new Varien_Object($data);
355: if ($key) {
356: $this->_info[$key] = $object;
357: } else {
358: $this->_info[] = $object;
359: }
360: }
361:
362: 363: 364:
365: protected function _prepareLayout()
366: {
367: $this->_profile = Mage::registry('current_recurring_profile')
368: ->setStore(Mage::app()->getStore())
369: ->setLocale(Mage::app()->getLocale())
370: ;
371: return parent::_prepareLayout();
372: }
373:
374: 375: 376: 377: 378:
379: protected function _toHtml()
380: {
381: if (!$this->_profile || $this->_shouldRenderInfo && !$this->_info) {
382: return '';
383: }
384:
385: if ($this->hasShouldPrepareInfoTabs()) {
386: foreach ($this->getChildGroup('info_tabs') as $block) {
387: $block->setViewUrl(
388: $this->getUrl("*/*/{$block->getViewAction()}", array('profile' => $this->_profile->getId()))
389: );
390: }
391: }
392:
393: return parent::_toHtml();
394: }
395: }
396: