/app/code/core/Mage/CatalogInventory/Helper/Minsaleqty.php

https://github.com/speedupmate/Magento-CE-Mirror · PHP · 208 lines · 117 code · 9 blank · 82 comment · 28 complexity · 41f67edf3e30d7308cb6e1b187287db4 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  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. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_CatalogInventory
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * MinSaleQty value manipulation helper
  28. */
  29. class Mage_CatalogInventory_Helper_Minsaleqty
  30. {
  31. /**
  32. * Retrieve fixed qty value
  33. *
  34. * @param mixed $qty
  35. * @return float|null
  36. */
  37. protected function _fixQty($qty)
  38. {
  39. return (!empty($qty) ? (float)$qty : null);
  40. }
  41. /**
  42. * Generate a storable representation of a value
  43. *
  44. * @param mixed $value
  45. * @return string
  46. */
  47. protected function _serializeValue($value)
  48. {
  49. if (is_numeric($value)) {
  50. $data = (float)$value;
  51. return (string)$data;
  52. } else if (is_array($value)) {
  53. $data = array();
  54. foreach ($value as $groupId => $qty) {
  55. if (!array_key_exists($groupId, $data)) {
  56. $data[$groupId] = $this->_fixQty($qty);
  57. }
  58. }
  59. if (count($data) == 1 && array_key_exists(Mage_Customer_Model_Group::CUST_GROUP_ALL, $data)) {
  60. return (string)$data[Mage_Customer_Model_Group::CUST_GROUP_ALL];
  61. }
  62. return serialize($data);
  63. } else {
  64. return '';
  65. }
  66. }
  67. /**
  68. * Create a value from a storable representation
  69. *
  70. * @param mixed $value
  71. * @return array
  72. */
  73. protected function _unserializeValue($value)
  74. {
  75. if (is_numeric($value)) {
  76. return array(
  77. Mage_Customer_Model_Group::CUST_GROUP_ALL => $this->_fixQty($value)
  78. );
  79. } else if (is_string($value) && !empty($value)) {
  80. try {
  81. return Mage::helper('core/unserializeArray')->unserialize($value);
  82. } catch (Exception $e) {
  83. return array();
  84. }
  85. } else {
  86. return array();
  87. }
  88. }
  89. /**
  90. * Check whether value is in form retrieved by _encodeArrayFieldValue()
  91. *
  92. * @param mixed
  93. * @return bool
  94. */
  95. protected function _isEncodedArrayFieldValue($value)
  96. {
  97. if (!is_array($value)) {
  98. return false;
  99. }
  100. unset($value['__empty']);
  101. foreach ($value as $_id => $row) {
  102. if (!is_array($row) || !array_key_exists('customer_group_id', $row) || !array_key_exists('min_sale_qty', $row)) {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. /**
  109. * Encode value to be used in Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
  110. *
  111. * @param array
  112. * @return array
  113. */
  114. protected function _encodeArrayFieldValue(array $value)
  115. {
  116. $result = array();
  117. foreach ($value as $groupId => $qty) {
  118. $_id = Mage::helper('core')->uniqHash('_');
  119. $result[$_id] = array(
  120. 'customer_group_id' => $groupId,
  121. 'min_sale_qty' => $this->_fixQty($qty),
  122. );
  123. }
  124. return $result;
  125. }
  126. /**
  127. * Decode value from used in Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
  128. *
  129. * @param array
  130. * @return array
  131. */
  132. protected function _decodeArrayFieldValue(array $value)
  133. {
  134. $result = array();
  135. unset($value['__empty']);
  136. foreach ($value as $_id => $row) {
  137. if (!is_array($row) || !array_key_exists('customer_group_id', $row) || !array_key_exists('min_sale_qty', $row)) {
  138. continue;
  139. }
  140. $groupId = $row['customer_group_id'];
  141. $qty = $this->_fixQty($row['min_sale_qty']);
  142. $result[$groupId] = $qty;
  143. }
  144. return $result;
  145. }
  146. /**
  147. * Retrieve min_sale_qty value from config
  148. *
  149. * @param int $customerGroupId
  150. * @param mixed $store
  151. * @return float|null
  152. */
  153. public function getConfigValue($customerGroupId, $store = null)
  154. {
  155. $value = Mage::getStoreConfig(Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MIN_SALE_QTY, $store);
  156. $value = $this->_unserializeValue($value);
  157. if ($this->_isEncodedArrayFieldValue($value)) {
  158. $value = $this->_decodeArrayFieldValue($value);
  159. }
  160. $result = null;
  161. foreach ($value as $groupId => $qty) {
  162. if ($groupId == $customerGroupId) {
  163. $result = $qty;
  164. break;
  165. } else if ($groupId == Mage_Customer_Model_Group::CUST_GROUP_ALL) {
  166. $result = $qty;
  167. }
  168. }
  169. return $this->_fixQty($result);
  170. }
  171. /**
  172. * Make value readable by Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
  173. *
  174. * @param mixed $value
  175. * @return array
  176. */
  177. public function makeArrayFieldValue($value)
  178. {
  179. $value = $this->_unserializeValue($value);
  180. if (!$this->_isEncodedArrayFieldValue($value)) {
  181. $value = $this->_encodeArrayFieldValue($value);
  182. }
  183. return $value;
  184. }
  185. /**
  186. * Make value ready for store
  187. *
  188. * @param mixed $value
  189. * @return string
  190. */
  191. public function makeStorableArrayFieldValue($value)
  192. {
  193. if ($this->_isEncodedArrayFieldValue($value)) {
  194. $value = $this->_decodeArrayFieldValue($value);
  195. }
  196. $value = $this->_serializeValue($value);
  197. return $value;
  198. }
  199. }