PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/Unirgy/Giftcert/Model/Product/TypeCE150.php

https://bitbucket.org/deniskulikouski/belvg.deniska
PHP | 269 lines | 177 code | 26 blank | 66 comment | 43 complexity | e35a8ec206573343dd53de9bca2458c0 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Unirgy_Giftcert extension
  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. *
  12. * @category Unirgy
  13. * @package Unirgy_Giftcert
  14. * @copyright Copyright (c) 2008 Unirgy LLC
  15. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. */
  17. /**
  18. * @category Unirgy
  19. * @package Unirgy_Giftcert
  20. * @author Boris (Moshe) Gurevich <moshe@unirgy.com>
  21. */
  22. class Unirgy_GiftCert_Model_Product_TypeCE150 extends Mage_Catalog_Model_Product_Type_Abstract
  23. {
  24. const DELIVERY_TYPE = 'delivery_type';
  25. const DELIVERY_TYPE_PHYSICAL = 'physical';
  26. const DELIVERY_TYPE_VIRTUAL = 'virtual';
  27. /**
  28. * Generate product options for cart item
  29. *
  30. * @param Varien_Object $buyRequest
  31. * @param Mage_Catalog_Model_Product $product
  32. * @param string $processMode
  33. *
  34. * @return array|string
  35. */
  36. protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
  37. {
  38. if (is_null($product)) {
  39. $product = $this->getProduct();
  40. }
  41. if ($multiple = $buyRequest->getData('multiple-recipients')) {
  42. $result = array();
  43. $multiple = explode(',', $multiple);
  44. foreach ($multiple as $i) {
  45. $temp = $this->_addCertificate($buyRequest, $product, $i, $processMode);
  46. if (is_string($temp)) {
  47. return $temp;
  48. }
  49. $result = array_merge($result, $temp);
  50. }
  51. return $result;
  52. } else {
  53. $result = parent::_prepareProduct($buyRequest, $product, $processMode);
  54. }
  55. if (is_string($result)) {
  56. return $result;
  57. }
  58. $store = Mage::app()->getStore();
  59. // $amountConfig = $hlp->getAmountConfig($product);
  60. if ($store->isAdmin()) {
  61. $amount = $product->getPrice();
  62. // Attempt to add to cart from product list
  63. // Need to update info_buyRequest[amount] somehow
  64. //if ($amountConfig['type']=='fixed') {
  65. // $amount = $amountConfig['amount'];
  66. } else {
  67. $amount = Mage::app()->getLocale()->getNumber($buyRequest->getAmount());
  68. if (!$amount) {
  69. return Mage::helper('ugiftcert')->__('Please enter gift certificate information');
  70. } elseif (!Mage::helper('ugiftcert')->validateAmount($product, $amount)) {
  71. return Mage::helper('ugiftcert')->__('Supplied amount is invalid.');
  72. }
  73. }
  74. // maintain same price for not base currency
  75. $amount /= $store->getCurrentCurrencyRate();
  76. if (!$buyRequest->getAmount()) {
  77. $buyRequest->setAmount($amount);
  78. }
  79. $product->addCustomOption('amount', $amount);
  80. $fields = $this->_getFields();
  81. $options = array();
  82. foreach ($fields as $p=> $k) {
  83. if ($v = $buyRequest->getData($p)) {
  84. switch ($p) {
  85. case 'recipient_email':
  86. $valid = filter_var($v, FILTER_VALIDATE_EMAIL);
  87. if (!$valid) {
  88. return Mage::helper('ugiftcert')->__('Supplied recipient email is invalid.');
  89. }
  90. break;
  91. case 'recipient_address':
  92. case 'recipient_name':
  93. if (!trim($v)) {
  94. return Mage::helper('ugiftcert')->__('Supplied recipient data is invalid.');
  95. }
  96. break;
  97. case 'sender_email':
  98. if(trim($v)){
  99. $valid = filter_var($v, FILTER_VALIDATE_EMAIL);
  100. if (!$valid) {
  101. return Mage::helper('ugiftcert')->__('Supplied sender email is invalid.');
  102. }
  103. }
  104. break;
  105. case 'sender_use_address':
  106. case 'recipient_use_address':
  107. $addr_options = Unirgy_Giftcert_Model_Source_Address::getOptionsStatic();
  108. if(!isset($addr_options[$v])){
  109. return Mage::helper('ugiftcert')->__('Supplied address type is invalid.');
  110. }
  111. }
  112. $options[$k] = $v;
  113. }
  114. }
  115. if(!isset($options[self::DELIVERY_TYPE])){
  116. return Mage::helper('ugiftcert')->__('Missing delivery type.');
  117. } else if(!in_array($options[self::DELIVERY_TYPE], array(self::DELIVERY_TYPE_PHYSICAL, self::DELIVERY_TYPE_VIRTUAL))){
  118. return Mage::helper('ugiftcert')->__('Unknown delivery type.');
  119. }
  120. if (!isset($options['recipient_use_address']) || $options['recipient_use_address'] == Unirgy_Giftcert_Model_Source_Address::CUSTOM) {
  121. if ($options[self::DELIVERY_TYPE] == self::DELIVERY_TYPE_PHYSICAL && !isset($options['recipient_address'])) {
  122. return Mage::helper('ugiftcert')->__('Recipient address is required.');
  123. } else if ($options[self::DELIVERY_TYPE] == self::DELIVERY_TYPE_VIRTUAL && !isset($options['recipient_email'])) {
  124. return Mage::helper('ugiftcert')->__('Recipient email is required.');
  125. }
  126. }
  127. foreach ($options as $k => $v) {
  128. $product->addCustomOption($k, $v);
  129. }
  130. return $result;
  131. }
  132. protected function _getFields()
  133. {
  134. $hlp = Mage::helper('ugiftcert');
  135. $fields = array();
  136. foreach ($hlp->getGiftcertOptionVars() as $k=> $l) {
  137. $fields[$k] = $k;
  138. }
  139. $fields['message'] = 'recipient_message'; // legacy templates (before 0.7.5)
  140. $fields['sender_use_address'] = 'sender_use_address';
  141. $fields['recipient_use_address'] = 'recipient_use_address';
  142. return $fields;
  143. }
  144. /**
  145. * Check whether quote item is virtual
  146. *
  147. * @param null|Mage_Catalog_Model_Product $product
  148. *
  149. * @return boolean
  150. */
  151. public function isVirtual($product = null)
  152. {
  153. if (is_null($product)) {
  154. $product = $this->getProduct();
  155. }
  156. if (Mage::getStoreConfig('ugiftcert/address/always_virtual', $product->getStoreId())) {
  157. return true;
  158. }
  159. $item = $this->_getProductItem($product);
  160. if (!$item) {
  161. return false;
  162. }
  163. $options = array();
  164. foreach ($item->getOptions() as $option) {
  165. $options[$option->getCode()] = $option->getValue();
  166. }
  167. if(isset($options[self::DELIVERY_TYPE])){
  168. return $options[self::DELIVERY_TYPE] == self::DELIVERY_TYPE_VIRTUAL;
  169. }
  170. if ((!empty($options['recipient_email']) && empty($options['recipient_address']))
  171. || (empty($options['recipient_name']) && empty($options['toself_printed']))
  172. ) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. /**
  179. * Get product's quote item,
  180. * if product has been added to cart, then it has some configuration and
  181. * it can be determined if it is virtual or not. If we get quote from session
  182. * we risk getting into a loop, that is why we only check for quote id, if we
  183. * have one, load a quote object with it, but do not collect totals, since this
  184. * starts endless loop.
  185. * If no quote id, no loop will be started, so no extra logic is needed.
  186. *
  187. * @param Mage_Catalog_Model_Product $product
  188. *
  189. * @return boolean|Mage_Sales_Model_Quote_Item
  190. */
  191. protected function _getProductItem($product)
  192. {
  193. $item = false;
  194. $quoteId = Mage::getModel('checkout/session')->getQuoteId();
  195. if ($quoteId) {
  196. /* @var $quote Mage_Sales_Model_Quote*/
  197. $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
  198. $quote->getResource()->loadActive($quote, $quoteId); //Ben added this line
  199. // there is a quote started already, do not load
  200. $items = Mage::getModel('sales/quote_item')->getCollection()->setQuote($quote);
  201. foreach ($items as $i) {
  202. /* @var $i Mage_Sales_Model_Quote_Item */
  203. if ($i->representProduct($product)) {
  204. $item = $i;
  205. break;
  206. }
  207. }
  208. } else {
  209. $item = Mage::getModel('checkout/session')->getQuote()->getItemByProduct($product);
  210. }
  211. return $item;
  212. }
  213. public function getEditableAttributes($product = null)
  214. {
  215. $editableAttributes = parent::getEditableAttributes($product);
  216. if (isset($editableAttributes['price'])) {
  217. unset($editableAttributes['price']);
  218. }
  219. return $editableAttributes;
  220. }
  221. /**
  222. * @param Varien_Object $buyRequest
  223. * @param Mage_Catalog_Model_Product $product
  224. * @param int $i - counter
  225. * @param string $processMode
  226. *
  227. * @return array|string
  228. */
  229. private function _addCertificate(Varien_Object $buyRequest, $product, $i, $processMode)
  230. {
  231. $fields = $this->_getFields();
  232. $clone = clone $product;
  233. $cloneBuyRequest = clone $buyRequest;
  234. $cloneBuyRequest->unsetData('multiple-recipients');
  235. $cloneBuyRequest->setData('qty', 1);
  236. foreach ($fields as $k => $v) {
  237. $key = $k . '-' . $i;
  238. $val = $buyRequest->getDataUsingMethod($key);
  239. $cloneBuyRequest->setData($k, $val);
  240. }
  241. return $this->_prepareProduct($cloneBuyRequest, $clone, $processMode);
  242. }
  243. }