00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Customer_Model_Customer_Api_V2 extends Mage_Customer_Model_Customer_Api
00035 {
00036
00037
00038
00039
00040
00041
00042
00043 protected function _prepareData($data)
00044 {
00045 if (null !== ($_data = get_object_vars($data))) {
00046 return $_data;
00047 }
00048 return array();
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 public function create($customerData)
00058 {
00059 $customerData = $this->_prepareData($customerData);
00060 try {
00061 $customer = Mage::getModel('customer/customer')
00062 ->setData($customerData)
00063 ->save();
00064 } catch (Mage_Core_Exception $e) {
00065 $this->_fault('data_invalid', $e->getMessage());
00066 }
00067 return $customer->getId();
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public function items($filters)
00077 {
00078 $collection = Mage::getModel('customer/customer')->getCollection()
00079 ->addAttributeToSelect('*');
00080
00081 $preparedFilters = array();
00082 if (isset($filters->filter)) {
00083 foreach ($filters->filter as $_filter) {
00084 $preparedFilters[$_filter->key] = $_filter->value;
00085 }
00086 }
00087 if (isset($filters->complex_filter)) {
00088 foreach ($filters->complex_filter as $_filter) {
00089 $_value = $_filter->value;
00090 $preparedFilters[$_filter->key] = array(
00091 $_value->key => $_value->value
00092 );
00093 }
00094 }
00095
00096 if (!empty($preparedFilters)) {
00097 try {
00098 foreach ($preparedFilters as $field => $value) {
00099 if (isset($this->_mapAttributes[$field])) {
00100 $field = $this->_mapAttributes[$field];
00101 }
00102 $collection->addFieldToFilter($field, $value);
00103 }
00104 } catch (Mage_Core_Exception $e) {
00105 $this->_fault('filters_invalid', $e->getMessage());
00106 }
00107 }
00108
00109 $result = array();
00110 foreach ($collection as $customer) {
00111 $data = $customer->toArray();
00112 $row = array();
00113
00114 foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
00115 $row[$attributeAlias] = (isset($data[$attributeCode]) ? $data[$attributeCode] : null);
00116 }
00117
00118 foreach ($this->getAllowedAttributes($customer) as $attributeCode => $attribute) {
00119 if (isset($data[$attributeCode])) {
00120 $row[$attributeCode] = $data[$attributeCode];
00121 }
00122 }
00123
00124 $result[] = $row;
00125 }
00126
00127 return $result;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 public function update($customerId, $customerData)
00138 {
00139 $customer = Mage::getModel('customer/customer')->load($customerId);
00140
00141 if (!$customer->getId()) {
00142 $this->_fault('not_exists');
00143 }
00144
00145 foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
00146 if (isset($customerData->$attributeCode)) {
00147 $customer->setData($attributeCode, $customerData->$attributeCode);
00148 }
00149 }
00150
00151 $customer->save();
00152 return true;
00153 }
00154 }