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: abstract class Mage_Catalog_Model_Api2_Product_Website_Rest extends Mage_Catalog_Model_Api2_Product_Website
35: {
36: 37: 38:
39: protected function _retrieve()
40: {
41: $this->_critical(self::RESOURCE_METHOD_NOT_ALLOWED);
42: }
43:
44: 45: 46: 47: 48:
49: protected function _retrieveCollection()
50: {
51: $return = array();
52: foreach ($this->_loadProductById($this->getRequest()->getParam('product_id'))->getWebsiteIds() as $websiteId) {
53: $return[] = array('website_id' => $websiteId);
54: }
55: return $return;
56: }
57:
58: 59: 60: 61: 62: 63:
64: protected function _create(array $data)
65: {
66:
67: $product = $this->_loadProductById($this->getRequest()->getParam('product_id'));
68:
69:
70: $validator = Mage::getModel('catalog/api2_product_website_validator_admin_website');
71: if (!$validator->isValidDataForWebsiteAssignmentToProduct($product, $data)) {
72: foreach ($validator->getErrors() as $error) {
73: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
74: }
75: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
76: }
77:
78: $websiteIds = $product->getWebsiteIds();
79:
80: $website = Mage::getModel('core/website')->load($data['website_id']);
81: $websiteIds[] = $website->getId();
82: $product->setWebsiteIds($websiteIds);
83:
84: try{
85: $product->save();
86:
87: 88: 89:
90: if (isset($data['copy_to_stores'])) {
91: foreach ($data['copy_to_stores'] as $storeData) {
92: Mage::getModel('catalog/product')
93: ->setStoreId($storeData['store_from'])
94: ->load($product->getId())
95: ->setStoreId($storeData['store_to'])
96: ->save();
97: }
98: }
99:
100: } catch (Mage_Core_Exception $e) {
101: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
102: } catch (Exception $e) {
103: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
104: }
105:
106: return $this->_getLocation($website, $product);
107: }
108:
109: 110: 111: 112: 113: 114:
115: protected function _multiCreate(array $data)
116: {
117:
118: $product = $this->_loadProductById($this->getRequest()->getParam('product_id'));
119: $websiteIds = $product->getWebsiteIds();
120: foreach ($data as $singleData) {
121: try {
122: if (!is_array($singleData)) {
123: $this->_errorMessage(self::RESOURCE_DATA_INVALID, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
124: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
125: }
126:
127: $validator = Mage::getModel('catalog/api2_product_website_validator_admin_website');
128: if (!$validator->isValidDataForWebsiteAssignmentToProduct($product, $singleData)) {
129: foreach ($validator->getErrors() as $error) {
130: $this->_errorMessage($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST, array(
131: 'website_id' => isset($singleData['website_id']) ? $singleData['website_id'] : null,
132: 'product_id' => $product->getId(),
133: ));
134: }
135: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
136: }
137:
138:
139: $website = Mage::getModel('core/website')->load($singleData['website_id']);
140: $websiteIds[] = $website->getId();
141: $product->setWebsiteIds($websiteIds);
142:
143: $product->save();
144:
145: 146: 147:
148: if (isset($singleData['copy_to_stores'])) {
149: foreach ($singleData['copy_to_stores'] as $storeData) {
150: Mage::getModel('catalog/product')
151: ->setStoreId($storeData['store_from'])
152: ->load($product->getId())
153: ->setStoreId($storeData['store_to'])
154: ->save();
155: }
156: }
157:
158: $this->_successMessage(
159: Mage_Api2_Model_Resource::RESOURCE_UPDATED_SUCCESSFUL,
160: Mage_Api2_Model_Server::HTTP_OK,
161: array(
162: 'website_id' => $website->getId(),
163: 'product_id' => $product->getId(),
164: )
165: );
166: } catch (Mage_Api2_Exception $e) {
167:
168: if ($e->getMessage() != self::RESOURCE_DATA_PRE_VALIDATION_ERROR) {
169: $this->_errorMessage(
170: $e->getMessage(),
171: $e->getCode(),
172: array(
173: 'website_id' => isset($singleData['website_id']) ? $singleData['website_id'] : null,
174: 'product_id' => $product->getId(),
175: )
176: );
177: }
178: } catch (Exception $e) {
179: $this->_errorMessage(
180: Mage_Api2_Model_Resource::RESOURCE_INTERNAL_ERROR,
181: Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR,
182: array(
183: 'website_id' => isset($singleData['website_id']) ? $singleData['website_id'] : null,
184: 'product_id' => $product->getId(),
185: )
186: );
187: }
188: }
189: }
190:
191: 192: 193: 194: 195:
196: protected function _update(array $data)
197: {
198: $this->_critical(self::RESOURCE_METHOD_NOT_ALLOWED);
199: }
200:
201: 202: 203:
204: protected function _delete()
205: {
206:
207: $product = $this->_loadProductById($this->getRequest()->getParam('product_id'));
208:
209:
210: $website = $this->_loadWebsiteById($this->getRequest()->getParam('website_id'));
211:
212:
213: $validator = Mage::getModel('catalog/api2_product_website_validator_admin_website');
214: if (!$validator->isWebsiteAssignedToProduct($website, $product)) {
215: foreach ($validator->getErrors() as $error) {
216: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
217: }
218: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
219: }
220:
221: $websiteIds = $product->getWebsiteIds();
222:
223: unset($websiteIds[array_search($website->getId(), $websiteIds)]);
224: $product->setWebsiteIds($websiteIds);
225:
226: try {
227: $product->save();
228: } catch (Mage_Core_Exception $e) {
229: $this->_critical($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
230: } catch (Exception $e) {
231: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
232: }
233: }
234:
235: 236: 237: 238: 239: 240:
241: protected function _getLocation($website)
242: {
243:
244: $apiTypeRoute = Mage::getModel('api2/route_apiType');
245:
246: $chain = $apiTypeRoute->chain(
247: new Zend_Controller_Router_Route($this->getConfig()->getRouteWithEntityTypeAction($this->getResourceType()))
248: );
249: $params = array(
250: 'api_type' => $this->getRequest()->getApiType(),
251: 'product_id' => $this->getRequest()->getParam('product_id'),
252: 'website_id' => $website->getId()
253: );
254: $uri = $chain->assemble($params);
255:
256: return '/' . $uri;
257: }
258: }
259: