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 extends Mage_Customer_Model_Api_Resource
00035 {
00036 protected $_mapAttributes = array(
00037 'customer_id' => 'entity_id'
00038 );
00039
00040
00041
00042
00043
00044
00045
00046 public function create($customerData)
00047 {
00048 try {
00049 $customer = Mage::getModel('customer/customer')
00050 ->setData($customerData)
00051 ->save();
00052 } catch (Mage_Core_Exception $e) {
00053 $this->_fault('data_invalid', $e->getMessage());
00054 }
00055 return $customer->getId();
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065 public function info($customerId, $attributes = null)
00066 {
00067 $customer = Mage::getModel('customer/customer')->load($customerId);
00068
00069 if (!$customer->getId()) {
00070 $this->_fault('not_exists');
00071 }
00072
00073 if (!is_null($attributes) && !is_array($attributes)) {
00074 $attributes = array($attributes);
00075 }
00076
00077 $result = array();
00078
00079 foreach ($this->_mapAttributes as $attributeAlias=>$attributeCode) {
00080 $result[$attributeAlias] = $customer->getData($attributeCode);
00081 }
00082
00083 foreach ($this->getAllowedAttributes($customer, $attributes) as $attributeCode=>$attribute) {
00084 $result[$attributeCode] = $customer->getData($attributeCode);
00085 }
00086
00087 return $result;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public function items($filters)
00097 {
00098 $collection = Mage::getModel('customer/customer')->getCollection()
00099 ->addAttributeToSelect('*');
00100
00101 if (is_array($filters)) {
00102 try {
00103 foreach ($filters as $field => $value) {
00104 if (isset($this->_mapAttributes[$field])) {
00105 $field = $this->_mapAttributes[$field];
00106 }
00107
00108 $collection->addFieldToFilter($field, $value);
00109 }
00110 } catch (Mage_Core_Exception $e) {
00111 $this->_fault('filters_invalid', $e->getMessage());
00112 }
00113 }
00114
00115 $result = array();
00116 foreach ($collection as $customer) {
00117 $data = $customer->toArray();
00118 $row = array();
00119
00120 foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
00121 $row[$attributeAlias] = (isset($data[$attributeCode]) ? $data[$attributeCode] : null);
00122 }
00123
00124 foreach ($this->getAllowedAttributes($customer) as $attributeCode => $attribute) {
00125 if (isset($data[$attributeCode])) {
00126 $row[$attributeCode] = $data[$attributeCode];
00127 }
00128 }
00129
00130 $result[] = $row;
00131 }
00132
00133 return $result;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143 public function update($customerId, $customerData)
00144 {
00145 $customer = Mage::getModel('customer/customer')->load($customerId);
00146
00147 if (!$customer->getId()) {
00148 $this->_fault('not_exists');
00149 }
00150
00151 foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
00152 if (isset($customerData[$attributeCode])) {
00153 $customer->setData($attributeCode, $customerData[$attributeCode]);
00154 }
00155 }
00156
00157 $customer->save();
00158 return true;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 public function delete($customerId)
00168 {
00169 $customer = Mage::getModel('customer/customer')->load($customerId);
00170
00171 if (!$customer->getId()) {
00172 $this->_fault('not_exists');
00173 }
00174
00175 try {
00176 $customer->delete();
00177 } catch (Mage_Core_Exception $e) {
00178 $this->_fault('not_deleted', $e->getMessage());
00179 }
00180
00181 return true;
00182 }
00183
00184 }