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_Entity_Customer extends Mage_Eav_Model_Entity_Abstract
00035 {
00036
00037
00038
00039
00040 public function __construct()
00041 {
00042 $resource = Mage::getSingleton('core/resource');
00043 $this->setType('customer');
00044 $this->setConnection(
00045 $resource->getConnection('customer_read'),
00046 $resource->getConnection('customer_write')
00047 );
00048 }
00049
00050
00051
00052
00053
00054
00055 protected function _getDefaultAttributes()
00056 {
00057 return array(
00058 'entity_type_id',
00059 'attribute_set_id',
00060 'created_at',
00061 'updated_at',
00062 'increment_id',
00063 'store_id',
00064 'website_id'
00065 );
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075 protected function _beforeSave(Varien_Object $customer)
00076 {
00077 parent::_beforeSave($customer);
00078 $select = $this->_getReadAdapter()->select()
00079 ->from($this->getEntityTable(), array($this->getEntityIdField()))
00080 ->where('email=?', $customer->getEmail());
00081 if ($customer->getSharingConfig()->isWebsiteScope()) {
00082 $select->where('website_id=?', (int) $customer->getWebsiteId());
00083 }
00084 if ($customer->getId()) {
00085 $select->where('entity_id !=?', $customer->getId());
00086 }
00087
00088 if ($this->_getWriteAdapter()->fetchOne($select)) {
00089 Mage::throwException(Mage::helper('customer')->__('Customer email already exists'));
00090 }
00091
00092
00093 if ($customer->getForceConfirmed()) {
00094 $customer->setConfirmation(null);
00095 }
00096 elseif ((!$customer->getId()) && ($customer->isConfirmationRequired())) {
00097 $customer->setConfirmation($customer->getRandomConfirmationKey());
00098 }
00099
00100 if (!$customer->getConfirmation()) {
00101 $customer->setConfirmation(null);
00102 }
00103
00104 return $this;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 protected function _afterSave(Varien_Object $customer)
00114 {
00115 $this->_saveAddresses($customer);
00116 return parent::_afterSave($customer);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 protected function _saveAddresses(Mage_Customer_Model_Customer $customer)
00126 {
00127 foreach ($customer->getAddresses() as $address) {
00128 if ($address->getData('_deleted')) {
00129 $address->delete();
00130 }
00131 else {
00132 $address->setParentId($customer->getId())
00133 ->setStoreId($customer->getStoreId())
00134 ->save();
00135 }
00136 }
00137 return $this;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 protected function _getLoadRowSelect($object, $rowId)
00148 {
00149 $select = parent::_getLoadRowSelect($object, $rowId);
00150 if ($object->getWebsiteId() && $object->getSharingConfig()->isWebsiteScope()) {
00151 $select->where('website_id=?', (int) $object->getWebsiteId());
00152 }
00153 return $select;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 public function loadByEmail(Mage_Customer_Model_Customer $customer, $email, $testOnly = false)
00166 {
00167 $select = $this->_getReadAdapter()->select()
00168 ->from($this->getEntityTable(), array($this->getEntityIdField()))
00169
00170 ->where('email=:customer_email');
00171 if ($customer->getSharingConfig()->isWebsiteScope()) {
00172 if (!$customer->hasData('website_id')) {
00173 Mage::throwException(Mage::helper('customer')->__('Customer website id must be specified, when using website scope.'));
00174 }
00175 $select->where('website_id=?', (int)$customer->getWebsiteId());
00176 }
00177
00178 if ($id = $this->_getReadAdapter()->fetchOne($select, array('customer_email' => $email))) {
00179 $this->load($customer, $id);
00180 }
00181 else {
00182 $customer->setData(array());
00183 }
00184 return $this;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 public function changePassword(Mage_Customer_Model_Customer $customer, $newPassword)
00195 {
00196 $customer->setPassword($newPassword);
00197 $this->saveAttribute($customer, 'password_hash');
00198 return $this;
00199 }
00200
00201
00202
00203
00204
00205
00206 public function findEmailDuplicates()
00207 {
00208 $lookup = $this->_getReadAdapter()->fetchRow("SELECT email, COUNT(*) AS `qty`
00209 FROM `{$this->getTable('customer/entity')}`
00210 GROUP BY 1 ORDER BY 2 DESC LIMIT 1
00211 ");
00212 if (empty($lookup)) {
00213 return false;
00214 }
00215 return $lookup['qty'] > 1;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 public function checkCustomerId($customerId)
00225 {
00226 $select = $this->_getReadAdapter()->select()
00227 ->from($this->getTable('customer/entity'), 'entity_id')
00228 ->where('entity_id=?', $customerId)
00229 ->limit(1);
00230 if ($this->_getReadAdapter()->fetchOne($select)) {
00231 return true;
00232 }
00233 return false;
00234 }
00235 }