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: 35: 36: 37: 38: 39: 40: 41:
42: abstract class Mage_Api2_Model_Resource
43: {
44: 45: 46:
47: const ACTION_TYPE_ENTITY = 'entity';
48: const ACTION_TYPE_COLLECTION = 'collection';
49:
50:
51: 52: 53:
54: const OPERATION_CREATE = 'create';
55: const OPERATION_RETRIEVE = 'retrieve';
56: const OPERATION_UPDATE = 'update';
57: const OPERATION_DELETE = 'delete';
58:
59:
60: 61: 62:
63: const OPERATION_ATTRIBUTE_READ = 'read';
64: const OPERATION_ATTRIBUTE_WRITE = 'write';
65:
66:
67: 68: 69:
70: const RESOURCE_NOT_FOUND = 'Resource not found.';
71: const RESOURCE_METHOD_NOT_ALLOWED = 'Resource does not support method.';
72: const RESOURCE_METHOD_NOT_IMPLEMENTED = 'Resource method not implemented yet.';
73: const RESOURCE_INTERNAL_ERROR = 'Resource internal error.';
74: const RESOURCE_DATA_PRE_VALIDATION_ERROR = 'Resource data pre-validation error.';
75: const RESOURCE_DATA_INVALID = 'Resource data invalid.';
76: const RESOURCE_UNKNOWN_ERROR = 'Resource unknown error.';
77: const RESOURCE_REQUEST_DATA_INVALID = 'The request data is invalid.';
78:
79:
80: 81: 82:
83: const RESOURCE_COLLECTION_PAGING_ERROR = 'Resource collection paging error.';
84: const RESOURCE_COLLECTION_PAGING_LIMIT_ERROR = 'The paging limit exceeds the allowed number.';
85: const RESOURCE_COLLECTION_ORDERING_ERROR = 'Resource collection ordering error.';
86: const RESOURCE_COLLECTION_FILTERING_ERROR = 'Resource collection filtering error.';
87: const RESOURCE_COLLECTION_ATTRIBUTES_ERROR = 'Resource collection including additional attributes error.';
88:
89:
90: 91: 92:
93: const RESOURCE_UPDATED_SUCCESSFUL = 'Resource updated successful.';
94:
95:
96: 97: 98:
99: const PAGE_SIZE_DEFAULT = 10;
100: const PAGE_SIZE_MAX = 100;
101:
102:
103: 104: 105: 106: 107:
108: protected $_request;
109:
110: 111: 112: 113: 114:
115: protected $_resourceType;
116:
117: 118: 119: 120: 121:
122: protected $_apiType;
123:
124: 125: 126: 127: 128:
129: protected $_version = null;
130:
131: 132: 133: 134: 135:
136: protected $_response;
137:
138: 139: 140: 141: 142:
143: protected $_filter;
144:
145: 146: 147: 148: 149:
150: protected $_renderer;
151:
152: 153: 154: 155: 156:
157: protected $_apiUser;
158:
159: 160: 161: 162: 163:
164: protected $_userType;
165:
166: 167: 168: 169: 170:
171: protected $_actionType;
172:
173: 174: 175: 176: 177:
178: protected $_operation;
179:
180: 181: 182: 183: 184:
185: protected $_returnData = false;
186:
187: 188: 189:
190: protected $_multicall;
191:
192: 193: 194: 195: 196: 197: 198: 199: 200:
201: public function dispatch()
202: {
203: switch ($this->getActionType() . $this->getOperation()) {
204:
205: case self::ACTION_TYPE_ENTITY . self::OPERATION_CREATE:
206:
207: $this->_critical(self::RESOURCE_METHOD_NOT_IMPLEMENTED);
208: break;
209: case self::ACTION_TYPE_COLLECTION . self::OPERATION_CREATE:
210:
211: if (!$this->_checkMethodExist('_create') && !$this->_checkMethodExist('_multiCreate')) {
212: $this->_critical(self::RESOURCE_METHOD_NOT_IMPLEMENTED);
213: }
214:
215: $requestData = $this->getRequest()->getBodyParams();
216: if (empty($requestData)) {
217: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
218: }
219:
220: if ($this->getRequest()->isAssocArrayInRequestBody()) {
221: $this->_errorIfMethodNotExist('_create');
222: $filteredData = $this->getFilter()->in($requestData);
223: if (empty($filteredData)) {
224: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
225: }
226: $newItemLocation = $this->_create($filteredData);
227: $this->getResponse()->setHeader('Location', $newItemLocation);
228: } else {
229: $this->_errorIfMethodNotExist('_multiCreate');
230: $filteredData = $this->getFilter()->collectionIn($requestData);
231: $this->_multiCreate($filteredData);
232: $this->_render($this->getResponse()->getMessages());
233: $this->getResponse()->setHttpResponseCode(Mage_Api2_Model_Server::HTTP_MULTI_STATUS);
234: }
235: break;
236:
237: case self::ACTION_TYPE_ENTITY . self::OPERATION_RETRIEVE:
238: $this->_errorIfMethodNotExist('_retrieve');
239: $retrievedData = $this->_retrieve();
240: $filteredData = $this->getFilter()->out($retrievedData);
241: $this->_render($filteredData);
242: break;
243: case self::ACTION_TYPE_COLLECTION . self::OPERATION_RETRIEVE:
244: $this->_errorIfMethodNotExist('_retrieveCollection');
245: $retrievedData = $this->_retrieveCollection();
246: $filteredData = $this->getFilter()->collectionOut($retrievedData);
247: $this->_render($filteredData);
248: break;
249:
250: case self::ACTION_TYPE_ENTITY . self::OPERATION_UPDATE:
251: $this->_errorIfMethodNotExist('_update');
252: $requestData = $this->getRequest()->getBodyParams();
253: if (empty($requestData)) {
254: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
255: }
256: $filteredData = $this->getFilter()->in($requestData);
257: if (empty($filteredData)) {
258: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
259: }
260: $this->_update($filteredData);
261: break;
262: case self::ACTION_TYPE_COLLECTION . self::OPERATION_UPDATE:
263: $this->_errorIfMethodNotExist('_multiUpdate');
264: $requestData = $this->getRequest()->getBodyParams();
265: if (empty($requestData)) {
266: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
267: }
268: $filteredData = $this->getFilter()->collectionIn($requestData);
269: $this->_multiUpdate($filteredData);
270: $this->_render($this->getResponse()->getMessages());
271: $this->getResponse()->setHttpResponseCode(Mage_Api2_Model_Server::HTTP_MULTI_STATUS);
272: break;
273:
274: case self::ACTION_TYPE_ENTITY . self::OPERATION_DELETE:
275: $this->_errorIfMethodNotExist('_delete');
276: $this->_delete();
277: break;
278: case self::ACTION_TYPE_COLLECTION . self::OPERATION_DELETE:
279: $this->_errorIfMethodNotExist('_multiDelete');
280: $requestData = $this->getRequest()->getBodyParams();
281: if (empty($requestData)) {
282: $this->_critical(self::RESOURCE_REQUEST_DATA_INVALID);
283: }
284: $this->_multiDelete($requestData);
285: $this->getResponse()->setHttpResponseCode(Mage_Api2_Model_Server::HTTP_MULTI_STATUS);
286: break;
287: default:
288: $this->_critical(self::RESOURCE_METHOD_NOT_IMPLEMENTED);
289: break;
290: }
291: }
292:
293: 294: 295: 296: 297:
298: protected function _errorIfMethodNotExist($methodName)
299: {
300: if (!$this->_checkMethodExist($methodName)) {
301: $this->_critical(self::RESOURCE_METHOD_NOT_IMPLEMENTED);
302: }
303: }
304:
305: 306: 307: 308: 309: 310:
311: protected function _checkMethodExist($methodName)
312: {
313: return method_exists($this, $methodName);
314: }
315:
316: 317: 318: 319: 320: 321:
322: public function getRequest()
323: {
324: if (!$this->_request) {
325: throw new Exception('Request is not set.');
326: }
327: return $this->_request;
328: }
329:
330: 331: 332: 333: 334: 335:
336: public function setRequest(Mage_Api2_Model_Request $request)
337: {
338: $this->setResourceType($request->getResourceType());
339: $this->setApiType($request->getApiType());
340: $this->_request = $request;
341: return $this;
342: }
343:
344: 345: 346: 347: 348: 349:
350: public function getResourceType()
351: {
352: if (!$this->_resourceType) {
353: $this->setResourceType($this->getRequest()->getResourceType());
354: }
355: return $this->_resourceType;
356: }
357:
358: 359: 360: 361: 362: 363:
364: public function setResourceType($resourceType)
365: {
366: $this->_resourceType = $resourceType;
367: return $this;
368: }
369:
370: 371: 372: 373: 374: 375:
376: public function getApiType()
377: {
378: if (!$this->_apiType) {
379: $this->setApiType($this->getRequest()->getApiType());
380: }
381: return $this->_apiType;
382: }
383:
384: 385: 386: 387: 388: 389:
390: public function setApiType($apiType)
391: {
392: $this->_apiType = $apiType;
393: return $this;
394: }
395:
396: 397: 398: 399: 400:
401: public function getVersion()
402: {
403: if (null === $this->_version) {
404: if (preg_match('/^.+([1-9]\d*)$/', get_class($this), $matches) ) {
405: $this->setVersion($matches[1]);
406: } else {
407: throw new Exception('Can not determine version from class name');
408: }
409: }
410: return $this->_version;
411: }
412:
413: 414: 415: 416: 417:
418: public function setVersion($version)
419: {
420: $this->_version = (int)$version;
421: }
422:
423: 424: 425: 426: 427:
428: public function getResponse()
429: {
430: if (!$this->_response) {
431: throw new Exception('Response is not set.');
432: }
433: return $this->_response;
434: }
435:
436: 437: 438: 439: 440:
441: public function setResponse(Mage_Api2_Model_Response $response)
442: {
443: $this->_response = $response;
444: }
445:
446: 447: 448: 449: 450:
451: public function getFilter()
452: {
453: if (!$this->_filter) {
454:
455: $filter = Mage::getModel('api2/acl_filter', $this);
456: $this->setFilter($filter);
457: }
458: return $this->_filter;
459: }
460:
461: 462: 463: 464: 465:
466: public function setFilter(Mage_Api2_Model_Acl_Filter $filter)
467: {
468: $this->_filter = $filter;
469: }
470:
471: 472: 473: 474: 475:
476: public function getRenderer()
477: {
478: if (!$this->_renderer) {
479: $renderer = Mage_Api2_Model_Renderer::factory($this->getRequest()->getAcceptTypes());
480: $this->setRenderer($renderer);
481: }
482:
483: return $this->_renderer;
484: }
485:
486: 487: 488: 489: 490:
491: public function setRenderer(Mage_Api2_Model_Renderer_Interface $renderer)
492: {
493: $this->_renderer = $renderer;
494: }
495:
496: 497: 498: 499: 500: 501:
502: public function getUserType()
503: {
504: if (!$this->_userType) {
505: $this->setUserType($this->getApiUser()->getType());
506: }
507: return $this->_userType;
508: }
509:
510: 511: 512: 513: 514: 515:
516: public function setUserType($userType)
517: {
518: $this->_userType = $userType;
519: return $this;
520: }
521:
522: 523: 524: 525: 526: 527:
528: public function getApiUser()
529: {
530: if (!$this->_apiUser) {
531: throw new Exception('API user is not set.');
532: }
533: return $this->_apiUser;
534: }
535:
536: 537: 538: 539: 540: 541:
542: public function setApiUser(Mage_Api2_Model_Auth_User_Abstract $apiUser)
543: {
544: $this->_apiUser = $apiUser;
545: return $this;
546: }
547:
548: 549: 550: 551: 552: 553:
554: public function getActionType()
555: {
556: if (!$this->_actionType) {
557: $this->setActionType($this->getRequest()->getActionType());
558: }
559: return $this->_actionType;
560: }
561:
562: 563: 564: 565: 566: 567:
568: public function setActionType($actionType)
569: {
570: $this->_actionType = $actionType;
571: return $this;
572: }
573:
574: 575: 576: 577: 578: 579:
580: public function getOperation()
581: {
582: if (!$this->_operation) {
583: $this->setOperation($this->getRequest()->getOperation());
584: }
585: return $this->_operation;
586: }
587:
588: 589: 590: 591: 592: 593:
594: public function setOperation($operation)
595: {
596: $this->_operation = $operation;
597: return $this;
598: }
599:
600: 601: 602: 603: 604:
605: public function getConfig()
606: {
607: return Mage::getSingleton('api2/config');
608: }
609:
610: 611: 612: 613: 614:
615: public function getWorkingModel()
616: {
617: return Mage::getModel($this->getConfig()->getResourceWorkingModel($this->getResourceType()));
618: }
619:
620: 621: 622: 623: 624:
625: protected function _render($data)
626: {
627: $this->getResponse()->setMimeType($this->getRenderer()->getMimeType())
628: ->setBody($this->getRenderer()->render($data));
629: }
630:
631: 632: 633: 634: 635: 636: 637:
638: protected function _critical($message, $code = null)
639: {
640: if ($code === null) {
641: $errors = $this->_getCriticalErrors();
642: if (!isset($errors[$message])) {
643: throw new Exception(
644: sprintf('Invalid error "%s" or error code missed.', $message),
645: Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR
646: );
647: }
648: $code = $errors[$message];
649: }
650: throw new Mage_Api2_Exception($message, $code);
651: }
652:
653: 654: 655: 656: 657:
658: protected function _getCriticalErrors()
659: {
660: return array(
661: '' => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
662: self::RESOURCE_NOT_FOUND => Mage_Api2_Model_Server::HTTP_NOT_FOUND,
663: self::RESOURCE_METHOD_NOT_ALLOWED => Mage_Api2_Model_Server::HTTP_METHOD_NOT_ALLOWED,
664: self::RESOURCE_METHOD_NOT_IMPLEMENTED => Mage_Api2_Model_Server::HTTP_METHOD_NOT_ALLOWED,
665: self::RESOURCE_DATA_PRE_VALIDATION_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
666: self::RESOURCE_INTERNAL_ERROR => Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR,
667: self::RESOURCE_UNKNOWN_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
668: self::RESOURCE_REQUEST_DATA_INVALID => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
669: self::RESOURCE_COLLECTION_PAGING_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
670: self::RESOURCE_COLLECTION_PAGING_LIMIT_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
671: self::RESOURCE_COLLECTION_ORDERING_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
672: self::RESOURCE_COLLECTION_FILTERING_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
673: self::RESOURCE_COLLECTION_ATTRIBUTES_ERROR => Mage_Api2_Model_Server::HTTP_BAD_REQUEST,
674: );
675: }
676:
677: 678: 679: 680: 681: 682: 683:
684: protected function _error($message, $code)
685: {
686: $this->getResponse()->setException(new Mage_Api2_Exception($message, $code));
687: return $this;
688: }
689:
690: 691: 692: 693: 694: 695: 696: 697:
698: protected function _successMessage($message, $code, $params = array())
699: {
700: $this->getResponse()->addMessage($message, $code, $params, Mage_Api2_Model_Response::MESSAGE_TYPE_SUCCESS);
701: return $this;
702: }
703:
704: 705: 706: 707: 708: 709: 710: 711:
712: protected function _errorMessage($message, $code, $params = array())
713: {
714: $this->getResponse()->addMessage($message, $code, $params, Mage_Api2_Model_Response::MESSAGE_TYPE_ERROR);
715: return $this;
716: }
717:
718: 719: 720: 721: 722: 723:
724: final protected function _applyCollectionModifiers(Varien_Data_Collection_Db $collection)
725: {
726: $pageNumber = $this->getRequest()->getPageNumber();
727: if ($pageNumber != abs($pageNumber)) {
728: $this->_critical(self::RESOURCE_COLLECTION_PAGING_ERROR);
729: }
730:
731: $pageSize = $this->getRequest()->getPageSize();
732: if (null == $pageSize) {
733: $pageSize = self::PAGE_SIZE_DEFAULT;
734: } else {
735: if ($pageSize != abs($pageSize) || $pageSize > self::PAGE_SIZE_MAX) {
736: $this->_critical(self::RESOURCE_COLLECTION_PAGING_LIMIT_ERROR);
737: }
738: }
739:
740: $orderField = $this->getRequest()->getOrderField();
741:
742: if (null !== $orderField) {
743: $operation = Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ;
744: if (!is_string($orderField)
745: || !array_key_exists($orderField, $this->getAvailableAttributes($this->getUserType(), $operation))
746: ) {
747: $this->_critical(self::RESOURCE_COLLECTION_ORDERING_ERROR);
748: }
749: $collection->setOrder($orderField, $this->getRequest()->getOrderDirection());
750: }
751: $collection->setCurPage($pageNumber)->setPageSize($pageSize);
752:
753: return $this->_applyFilter($collection);
754: }
755:
756: 757: 758: 759: 760: 761:
762: protected function _applyFilter(Varien_Data_Collection_Db $collection)
763: {
764: $filter = $this->getRequest()->getFilter();
765:
766: if (!$filter) {
767: return $this;
768: }
769: if (!is_array($filter)) {
770: $this->_critical(self::RESOURCE_COLLECTION_FILTERING_ERROR);
771: }
772: if (method_exists($collection, 'addAttributeToFilter')) {
773: $methodName = 'addAttributeToFilter';
774: } elseif (method_exists($collection, 'addFieldToFilter')) {
775: $methodName = 'addFieldToFilter';
776: } else {
777: return $this;
778: }
779: $allowedAttributes = $this->getFilter()->getAllowedAttributes(self::OPERATION_ATTRIBUTE_READ);
780:
781: foreach ($filter as $filterEntry) {
782: if (!is_array($filterEntry)
783: || !array_key_exists('attribute', $filterEntry)
784: || !in_array($filterEntry['attribute'], $allowedAttributes)
785: ) {
786: $this->_critical(self::RESOURCE_COLLECTION_FILTERING_ERROR);
787: }
788: $attributeCode = $filterEntry['attribute'];
789:
790: unset($filterEntry['attribute']);
791:
792: try {
793: $collection->$methodName($attributeCode, $filterEntry);
794: } catch(Exception $e) {
795: $this->_critical(self::RESOURCE_COLLECTION_FILTERING_ERROR);
796: }
797: }
798: return $this;
799: }
800:
801: 802: 803: 804: 805: 806:
807: protected function _multicall($resourceInstanceId)
808: {
809: if (!$this->_multicall) {
810: $this->_multicall = Mage::getModel('api2/multicall');
811: }
812: $resourceName = $this->getResourceType();
813: return $this->_multicall->call($resourceInstanceId, $resourceName, $this->getRequest());
814: }
815:
816: 817: 818: 819: 820: 821: 822:
823: protected function _getSubModel($resourceId, array $requestParams)
824: {
825: $resourceModel = Mage_Api2_Model_Dispatcher::loadResourceModel(
826: $this->getConfig()->getResourceModel($resourceId),
827: $this->getApiType(),
828: $this->getUserType(),
829: $this->getVersion()
830: );
831:
832:
833: $request = Mage::getModel('api2/request');
834:
835: $request->setParams($requestParams);
836:
837: $resourceModel
838: ->setRequest($request)
839: ->setApiUser($this->getApiUser())
840: ->setApiType($this->getApiType())
841: ->setResourceType($resourceId)
842: ->setOperation($this->getOperation())
843: ->setReturnData(true);
844:
845: return $resourceModel;
846: }
847:
848: 849: 850: 851: 852: 853: 854:
855: protected function _isSubCallAllowed($resourceId)
856: {
857:
858: $globalAcl = Mage::getSingleton('api2/acl_global');
859:
860: try {
861: return $globalAcl->isAllowed($this->getApiUser(), $resourceId, $this->getOperation());
862: } catch (Mage_Api2_Exception $e) {
863: throw new Exception('Invalid arguments for isAllowed() call');
864: }
865: }
866:
867: 868: 869: 870: 871: 872:
873: public function setReturnData($flag)
874: {
875: $this->_returnData = $flag;
876: return $this;
877: }
878:
879: 880: 881: 882: 883: 884:
885: protected function _getLocation($resource)
886: {
887:
888: $apiTypeRoute = Mage::getModel('api2/route_apiType');
889:
890: $chain = $apiTypeRoute->chain(
891: new Zend_Controller_Router_Route($this->getConfig()->getRouteWithEntityTypeAction($this->getResourceType()))
892: );
893: $params = array(
894: 'api_type' => $this->getRequest()->getApiType(),
895: 'id' => $resource->getId()
896: );
897: $uri = $chain->assemble($params);
898:
899: return '/' . $uri;
900: }
901:
902: 903: 904: 905: 906:
907: protected function _getResourceAttributes()
908: {
909: return array();
910: }
911:
912: 913: 914: 915: 916: 917: 918:
919: public function getAvailableAttributes($userType, $operation)
920: {
921: $available = $this->getAvailableAttributesFromConfig();
922: $excludedAttrs = $this->getExcludedAttributes($userType, $operation);
923: $includedAttrs = $this->getIncludedAttributes($userType, $operation);
924: $entityOnlyAttrs = $this->getEntityOnlyAttributes($userType, $operation);
925: $resourceAttrs = $this->_getResourceAttributes();
926:
927:
928: if (0 === key($resourceAttrs)) {
929: $resourceAttrs = array_combine($resourceAttrs, $resourceAttrs);
930: }
931: foreach ($resourceAttrs as $attrCode => $attrLabel) {
932: if (!isset($available[$attrCode])) {
933: $available[$attrCode] = empty($attrLabel) ? $attrCode : $attrLabel;
934: }
935: }
936: foreach (array_keys($available) as $code) {
937: if (in_array($code, $excludedAttrs) || ($includedAttrs && !in_array($code, $includedAttrs))) {
938: unset($available[$code]);
939: }
940: if (in_array($code, $entityOnlyAttrs)) {
941: $available[$code] .= ' *';
942: }
943: }
944: return $available;
945: }
946:
947: 948: 949: 950: 951: 952: 953:
954: public function getExcludedAttributes($userType, $operation)
955: {
956: return $this->getConfig()->getResourceExcludedAttributes($this->getResourceType(), $userType, $operation);
957: }
958:
959: 960: 961: 962: 963:
964: public function getForcedAttributes()
965: {
966: return $this->getConfig()->getResourceForcedAttributes($this->getResourceType(), $this->getUserType());
967: }
968:
969: 970: 971: 972: 973: 974: 975:
976: public function getIncludedAttributes($userType, $operationType)
977: {
978: return $this->getConfig()->getResourceIncludedAttributes($this->getResourceType(), $userType, $operationType);
979: }
980:
981: 982: 983: 984: 985: 986: 987:
988: public function getEntityOnlyAttributes($userType, $operationType)
989: {
990: return $this->getConfig()->getResourceEntityOnlyAttributes($this->getResourceType(), $userType, $operationType);
991: }
992:
993: 994: 995: 996: 997:
998: public function getAvailableAttributesFromConfig()
999: {
1000: return $this->getConfig()->getResourceAttributes($this->getResourceType());
1001: }
1002:
1003: 1004: 1005: 1006: 1007:
1008: public function getDbAttributes()
1009: {
1010: $available = array();
1011: $workModel = $this->getConfig()->getResourceWorkingModel($this->getResourceType());
1012:
1013: if ($workModel) {
1014:
1015: $resource = Mage::getResourceModel($workModel);
1016:
1017: if (method_exists($resource, 'getMainTable')) {
1018: $available = array_keys($resource->getReadConnection()->describeTable($resource->getMainTable()));
1019: }
1020: }
1021: return $available;
1022: }
1023:
1024: 1025: 1026: 1027: 1028: 1029: 1030:
1031: public function getEavAttributes($onlyVisible = false, $excludeSystem = false)
1032: {
1033: $attributes = array();
1034: $model = $this->getConfig()->getResourceWorkingModel($this->getResourceType());
1035:
1036:
1037: $entityType = Mage::getModel('eav/entity_type')->load($model, 'entity_model');
1038:
1039:
1040: foreach ($entityType->getAttributeCollection() as $attribute) {
1041: if ($onlyVisible && !$attribute->getIsVisible()) {
1042: continue;
1043: }
1044: if ($excludeSystem && $attribute->getIsSystem()) {
1045: continue;
1046: }
1047: $attributes[$attribute->getAttributeCode()] = $attribute->getFrontendLabel();
1048: }
1049:
1050: return $attributes;
1051: }
1052:
1053: 1054: 1055: 1056: 1057:
1058: protected function _getStore()
1059: {
1060: $store = $this->getRequest()->getParam('store');
1061: try {
1062: if ($this->getUserType() != Mage_Api2_Model_Auth_User_Admin::USER_TYPE) {
1063:
1064: if (!$store) {
1065: $store = Mage::app()->getDefaultStoreView();
1066: } else {
1067: $store = Mage::app()->getStore($store);
1068: }
1069: } else {
1070:
1071: if (is_null($store)) {
1072: $store = Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
1073: }
1074: $store = Mage::app()->getStore($store);
1075: }
1076: } catch (Mage_Core_Model_Store_Exception $e) {
1077:
1078: $this->_critical('Requested store is invalid', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
1079: }
1080: return $store;
1081: }
1082: }
1083: