PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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