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: class Mage_Customer_Model_Resource_Customer extends Mage_Eav_Model_Entity_Abstract
36: {
37: 38: 39:
40: public function __construct()
41: {
42: $this->setType('customer');
43: $this->setConnection('customer_read', 'customer_write');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _getDefaultAttributes()
52: {
53: return array(
54: 'entity_type_id',
55: 'attribute_set_id',
56: 'created_at',
57: 'updated_at',
58: 'increment_id',
59: 'store_id',
60: 'website_id'
61: );
62: }
63:
64: 65: 66: 67: 68: 69: 70:
71: protected function _beforeSave(Varien_Object $customer)
72: {
73: parent::_beforeSave($customer);
74:
75: if (!$customer->getEmail()) {
76: throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
77: }
78:
79: $adapter = $this->_getWriteAdapter();
80: $bind = array('email' => $customer->getEmail());
81:
82: $select = $adapter->select()
83: ->from($this->getEntityTable(), array($this->getEntityIdField()))
84: ->where('email = :email');
85: if ($customer->getSharingConfig()->isWebsiteScope()) {
86: $bind['website_id'] = (int)$customer->getWebsiteId();
87: $select->where('website_id = :website_id');
88: }
89: if ($customer->getId()) {
90: $bind['entity_id'] = (int)$customer->getId();
91: $select->where('entity_id != :entity_id');
92: }
93:
94: $result = $adapter->fetchOne($select, $bind);
95: if ($result) {
96: throw Mage::exception(
97: 'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
98: Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
99: );
100: }
101:
102:
103: if ($customer->getForceConfirmed()) {
104: $customer->setConfirmation(null);
105: } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
106: $customer->setConfirmation($customer->getRandomConfirmationKey());
107: }
108:
109: if (!$customer->getConfirmation()) {
110: $customer->setConfirmation(null);
111: }
112:
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121:
122: protected function _afterSave(Varien_Object $customer)
123: {
124: $this->_saveAddresses($customer);
125: return parent::_afterSave($customer);
126: }
127:
128: 129: 130: 131: 132: 133:
134: protected function _saveAddresses(Mage_Customer_Model_Customer $customer)
135: {
136: $defaultBillingId = $customer->getData('default_billing');
137: $defaultShippingId = $customer->getData('default_shipping');
138: foreach ($customer->getAddresses() as $address) {
139: if ($address->getData('_deleted')) {
140: if ($address->getId() == $defaultBillingId) {
141: $customer->setData('default_billing', null);
142: }
143: if ($address->getId() == $defaultShippingId) {
144: $customer->setData('default_shipping', null);
145: }
146: $address->delete();
147: } else {
148: $address->setParentId($customer->getId())
149: ->setStoreId($customer->getStoreId())
150: ->setIsCustomerSaveTransaction(true)
151: ->save();
152: if (($address->getIsPrimaryBilling() || $address->getIsDefaultBilling())
153: && $address->getId() != $defaultBillingId
154: ) {
155: $customer->setData('default_billing', $address->getId());
156: }
157: if (($address->getIsPrimaryShipping() || $address->getIsDefaultShipping())
158: && $address->getId() != $defaultShippingId
159: ) {
160: $customer->setData('default_shipping', $address->getId());
161: }
162: }
163: }
164: if ($customer->dataHasChangedFor('default_billing')) {
165: $this->saveAttribute($customer, 'default_billing');
166: }
167: if ($customer->dataHasChangedFor('default_shipping')) {
168: $this->saveAttribute($customer, 'default_shipping');
169: }
170:
171: return $this;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: protected function _getLoadRowSelect($object, $rowId)
182: {
183: $select = parent::_getLoadRowSelect($object, $rowId);
184: if ($object->getWebsiteId() && $object->getSharingConfig()->isWebsiteScope()) {
185: $select->where('website_id =?', (int)$object->getWebsiteId());
186: }
187:
188: return $select;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199: 200:
201: public function loadByEmail(Mage_Customer_Model_Customer $customer, $email, $testOnly = false)
202: {
203: $adapter = $this->_getReadAdapter();
204: $bind = array('customer_email' => $email);
205: $select = $adapter->select()
206: ->from($this->getEntityTable(), array($this->getEntityIdField()))
207: ->where('email = :customer_email');
208:
209: if ($customer->getSharingConfig()->isWebsiteScope()) {
210: if (!$customer->hasData('website_id')) {
211: Mage::throwException(
212: Mage::helper('customer')->__('Customer website ID must be specified when using the website scope')
213: );
214: }
215: $bind['website_id'] = (int)$customer->getWebsiteId();
216: $select->where('website_id = :website_id');
217: }
218:
219: $customerId = $adapter->fetchOne($select, $bind);
220: if ($customerId) {
221: $this->load($customer, $customerId);
222: } else {
223: $customer->setData(array());
224: }
225:
226: return $this;
227: }
228:
229: 230: 231: 232: 233: 234: 235:
236: public function changePassword(Mage_Customer_Model_Customer $customer, $newPassword)
237: {
238: $customer->setPassword($newPassword);
239: $this->saveAttribute($customer, 'password_hash');
240: return $this;
241: }
242:
243: 244: 245: 246: 247:
248: public function findEmailDuplicates()
249: {
250: $adapter = $this->_getReadAdapter();
251: $select = $adapter->select()
252: ->from($this->getTable('customer/entity'), array('email', 'cnt' => 'COUNT(*)'))
253: ->group('email')
254: ->order('cnt DESC')
255: ->limit(1);
256: $lookup = $adapter->fetchRow($select);
257: if (empty($lookup)) {
258: return false;
259: }
260: return $lookup['cnt'] > 1;
261: }
262:
263: 264: 265: 266: 267: 268:
269: public function checkCustomerId($customerId)
270: {
271: $adapter = $this->_getReadAdapter();
272: $bind = array('entity_id' => (int)$customerId);
273: $select = $adapter->select()
274: ->from($this->getTable('customer/entity'), 'entity_id')
275: ->where('entity_id = :entity_id')
276: ->limit(1);
277:
278: $result = $adapter->fetchOne($select, $bind);
279: if ($result) {
280: return true;
281: }
282: return false;
283: }
284:
285: 286: 287: 288: 289: 290:
291: public function getWebsiteId($customerId)
292: {
293: $adapter = $this->_getReadAdapter();
294: $bind = array('entity_id' => (int)$customerId);
295: $select = $adapter->select()
296: ->from($this->getTable('customer/entity'), 'website_id')
297: ->where('entity_id = :entity_id');
298:
299: return $adapter->fetchOne($select, $bind);
300: }
301:
302: 303: 304: 305: 306: 307:
308: public function setNewIncrementId(Varien_Object $object)
309: {
310: if (Mage::getStoreConfig(Mage_Customer_Model_Customer::XML_PATH_GENERATE_HUMAN_FRIENDLY_ID)) {
311: parent::setNewIncrementId($object);
312: }
313: return $this;
314: }
315:
316: 317: 318: 319: 320: 321: 322: 323: 324:
325: public function changeResetPasswordLinkToken(Mage_Customer_Model_Customer $customer, $newResetPasswordLinkToken) {
326: if (is_string($newResetPasswordLinkToken) && !empty($newResetPasswordLinkToken)) {
327: $customer->setRpToken($newResetPasswordLinkToken);
328: $currentDate = Varien_Date::now();
329: $customer->setRpTokenCreatedAt($currentDate);
330: $this->saveAttribute($customer, 'rp_token');
331: $this->saveAttribute($customer, 'rp_token_created_at');
332: }
333: return $this;
334: }
335: }
336: