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_Checkout_Model_Api_Resource extends Mage_Api_Model_Resource_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_attributesMap = array(
42: 'global' => array(),
43: );
44:
45: 46: 47: 48: 49:
50: protected $_ignoredAttributeCodes = array(
51: 'global' => array('entity_id', 'attribute_set_id', 'entity_type_id')
52: );
53:
54: 55: 56: 57:
58: protected $_storeIdSessionField = 'store_id';
59:
60:
61: 62: 63: 64: 65: 66:
67: protected function _isQuoteExist($quoteId)
68: {
69: if (empty($quoteId)) {
70: return false;
71: }
72:
73: try {
74: $quote = $this->_getQuote($quoteId);
75: } catch (Mage_Api_Exception $e) {
76: return false;
77: }
78:
79: if (!is_null($quote->getId())) {
80: $this->_fault('quote_already_exist');
81: }
82:
83: return false;
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: protected function _getStoreId($store = null)
94: {
95: if (is_null($store)) {
96: $store = ($this->_getSession()->hasData($this->_storeIdSessionField)
97: ? $this->_getSession()->getData($this->_storeIdSessionField) : 0);
98: }
99:
100: try {
101: $storeId = Mage::app()->getStore($store)->getId();
102:
103: } catch (Mage_Core_Model_Store_Exception $e) {
104: $this->_fault('store_not_exists');
105: }
106:
107: return $storeId;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: protected function _getQuote($quoteId, $store = null)
118: {
119:
120: $quote = Mage::getModel("sales/quote");
121:
122: if (!(is_string($store) || is_integer($store))) {
123: $quote->loadByIdWithoutStore($quoteId);
124: } else {
125: $storeId = $this->_getStoreId($store);
126:
127: $quote->setStoreId($storeId)
128: ->load($quoteId);
129: }
130: if (is_null($quote->getId())) {
131: $this->_fault('quote_not_exists');
132: }
133:
134: return $quote;
135: }
136:
137: 138: 139: 140: 141: 142:
143: protected function _getStoreIdFromQuote($quoteId)
144: {
145:
146: $quote = Mage::getModel('sales/quote')
147: ->loadByIdWithoutStore($quoteId);
148:
149: return $quote->getStoreId();
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160:
161: protected function _updateAttributes($data, $object, $type, array $attributes = null)
162: {
163: foreach ($data as $attribute=>$value) {
164: if ($this->_isAllowedAttribute($attribute, $type, $attributes)) {
165: $object->setData($attribute, $value);
166: }
167: }
168:
169: return $this;
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179:
180: protected function _getAttributes($object, $type, array $attributes = null)
181: {
182: $result = array();
183:
184: if (!is_object($object)) {
185: return $result;
186: }
187:
188: foreach ($object->getData() as $attribute=>$value) {
189: if (is_object($value)) {
190: continue;
191: }
192:
193: if ($this->_isAllowedAttribute($attribute, $type, $attributes)) {
194: $result[$attribute] = $value;
195: }
196: }
197:
198: foreach ($this->_attributesMap['global'] as $alias=>$attributeCode) {
199: $result[$alias] = $object->getData($attributeCode);
200: }
201:
202: if (isset($this->_attributesMap[$type])) {
203: foreach ($this->_attributesMap[$type] as $alias=>$attributeCode) {
204: $result[$alias] = $object->getData($attributeCode);
205: }
206: }
207:
208: return $result;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218:
219: protected function _isAllowedAttribute($attributeCode, $type, array $attributes = null)
220: {
221: if (!empty($attributes)
222: && !(in_array($attributeCode, $attributes))) {
223: return false;
224: }
225:
226: if (in_array($attributeCode, $this->_ignoredAttributeCodes['global'])) {
227: return false;
228: }
229:
230: if (isset($this->_ignoredAttributeCodes[$type])
231: && in_array($attributeCode, $this->_ignoredAttributeCodes[$type])) {
232: return false;
233: }
234:
235: return true;
236: }
237:
238: }
239: