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: class Mage_XmlConnect_Block_Catalog_Product_Options_Giftcard extends Mage_XmlConnect_Block_Catalog_Product_Options
35: {
36: 37: 38: 39: 40:
41: public function getSenderName()
42: {
43: $senderName = $this->getDefaultValue('giftcard_sender_name');
44: if (!strlen($senderName)) {
45: $firstName = (string) Mage::getSingleton('customer/session')->getCustomer()->getFirstname();
46: $lastName = (string) Mage::getSingleton('customer/session')->getCustomer()->getLastname();
47:
48: if ($firstName && $lastName) {
49: $senderName = $firstName . ' ' . $lastName;
50: } else {
51: $senderName = '';
52: }
53: }
54: return $senderName;
55: }
56:
57: 58: 59: 60: 61:
62: public function getSenderEmail()
63: {
64: $senderEmail = $this->getDefaultValue('giftcard_sender_email');
65:
66: if (!strlen($senderEmail)) {
67: $senderEmail = (string) Mage::getSingleton('customer/session')->getCustomer()->getEmail();
68: }
69: return $senderEmail;
70: }
71:
72: 73: 74: 75: 76: 77:
78: protected function getDefaultValue($value)
79: {
80: if ($this->getProduct()) {
81: return (string) $this->getProduct()->getPreconfiguredValues()->getData($value);
82: } else {
83: return '';
84: }
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function isMessageAvailable(Mage_Catalog_Model_Product $product)
94: {
95: if ($product->getUseConfigAllowMessage()) {
96: return Mage::getStoreConfigFlag(Enterprise_GiftCard_Model_Giftcard::XML_PATH_ALLOW_MESSAGE);
97: } else {
98: return (int) $product->getAllowMessage();
99: }
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function isEmailAvailable(Mage_Catalog_Model_Product $product)
109: {
110: if ($product->getTypeInstance()->isTypePhysical()) {
111: return false;
112: }
113: return true;
114: }
115:
116: 117: 118: 119: 120: 121:
122: public function isAmountAvailable(Mage_Catalog_Model_Product $product)
123: {
124: if (!$product->getGiftcardAmounts()) {
125: return false;
126: }
127: return true;
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: public function getProductOptionsXml(Mage_Catalog_Model_Product $product, $isObject = false)
138: {
139:
140: $this->setProduct($product);
141:
142:
143: $xmlModel = $this->getProductCustomOptionsXmlObject($product);
144:
145:
146: $optionsXmlObj = $xmlModel->options;
147:
148: if (!$product->isSalable()) {
149: return $isObject ? $xmlModel : $xmlModel->asNiceXml();
150: }
151:
152:
153: $priceModel = $product->getPriceModel();
154:
155:
156: $coreHelper = Mage::helper('core');
157:
158: $configValue = $this->getDefaultValue('giftcard_amount');
159:
160: 161: 162:
163:
164:
165: $fixedAmountsNode = $optionsXmlObj->addChild('fixed_amounts');
166: if ($this->isAmountAvailable($product)) {
167: $amounts = $priceModel->getSortedAmounts($product);
168: if (count($amounts)) {
169: foreach ($amounts as $price) {
170: $amountNode = $fixedAmountsNode->addChild('amount');
171: if ($configValue == $price) {
172: $amountNode->addAttribute('selected', 1);
173: }
174: $amountNode->addAttribute('formatted_price', $xmlModel->xmlAttribute(
175: $coreHelper->currency($price, true, false)
176: ));
177: $amountNode->addAttribute('price', $price);
178: }
179: }
180: }
181:
182: 183: 184:
185:
186: $openAmountNode = $optionsXmlObj->addChild('open_amount');
187: if ($product->getAllowOpenAmount()) {
188: $openAmountNode->addAttribute('enabled', 1);
189:
190: if ($configValue == 'custom') {
191: $openAmountNode->addAttribute('selected_amount', $this->getDefaultValue('custom_giftcard_amount'));
192: }
193: if ($priceModel->getMinAmount($product)) {
194: $minPrice = $product->getOpenAmountMin();
195: $minAmount = $coreHelper->currency($minPrice, true, false);
196: } else {
197: $minAmount = $minPrice = 0;
198: }
199: $openAmountNode->addAttribute('formatted_min_amount', $xmlModel->xmlAttribute($minAmount));
200: $openAmountNode->addAttribute('min_amount', $minPrice);
201:
202: if ($priceModel->getMaxAmount($product)) {
203: $maxPrice = $product->getOpenAmountMax();
204: $maxAmount = $coreHelper->currency($maxPrice, true, false);
205: } else {
206: $maxAmount = $maxPrice = 0;
207: }
208: $openAmountNode->addAttribute('formatted_max_amount', $xmlModel->xmlAttribute($maxAmount));
209: $openAmountNode->addAttribute('max_amount', $maxPrice);
210: } else {
211: $openAmountNode->addAttribute('enabled', 0);
212: }
213:
214: 215: 216:
217: $form = $optionsXmlObj->addCustomChild('form', null, array(
218: 'name' => 'giftcard-send-form',
219: 'method' => 'post'
220: ));
221:
222: $senderFieldset = $form->addCustomChild('fieldset', null, array(
223: 'legend' => $this->__('Sender Information')
224: ));
225:
226: $senderFieldset->addField('giftcard_sender_name', 'text', array(
227: 'label' => Mage::helper('enterprise_giftcard')->__('Sender Name'),
228: 'required' => 'true',
229: 'value' => $this->getSenderName()
230: ));
231:
232: $recipientFieldset = $form->addCustomChild('fieldset', null, array(
233: 'legend' => $this->__('Recipient Information')
234: ));
235:
236: $recipientFieldset->addField('giftcard_recipient_name', 'text', array(
237: 'label' => Mage::helper('enterprise_giftcard')->__('Recipient Name'),
238: 'required' => 'true',
239: 'value' => $this->getDefaultValue('giftcard_recipient_name')
240: ));
241:
242: if ($this->isEmailAvailable($product)) {
243: $senderFieldset->addField('giftcard_sender_email', 'email', array(
244: 'label' => Mage::helper('enterprise_giftcard')->__('Sender Email'),
245: 'required' => 'true',
246: 'value' => $this->getSenderEmail()
247: ));
248:
249: $recipientFieldset->addField('giftcard_recipient_email', 'email', array(
250: 'label' => Mage::helper('enterprise_giftcard')->__('Recipient Email'),
251: 'required' => 'true',
252: 'value' => $this->getDefaultValue('giftcard_recipient_email')
253: ));
254: }
255:
256: if ($this->isMessageAvailable($product)) {
257: $messageMaxLength = (int) Mage::getStoreConfig(
258: Enterprise_GiftCard_Model_Giftcard::XML_PATH_MESSAGE_MAX_LENGTH
259: );
260: $recipientFieldset->addField('giftcard_message', 'textarea', array(
261: 'label' => Mage::helper('enterprise_giftcard')->__('Message'),
262: 'required' => 'false',
263: 'max_length'=> $messageMaxLength,
264: 'value' => $this->getDefaultValue('giftcard_message')
265: ));
266: }
267: return $isObject ? $xmlModel : $xmlModel->asNiceXml();
268: }
269: }
270: