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: class Mage_XmlConnect_CustomerController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: public function loginAction()
42: {
43: $session = $this->_getSession();
44: $request = $this->getRequest();
45: if ($session->isLoggedIn()) {
46: $this->_message($this->__('You are already logged in.'), self::MESSAGE_STATUS_ERROR);
47: return;
48: }
49:
50: if ($request->isPost()) {
51: $user = $request->getParam('username');
52: $pass = $request->getParam('password');
53: try {
54: if ($session->login($user, $pass)) {
55: if ($session->getCustomer()->getIsJustConfirmed()) {
56: $session->getCustomer()->sendNewAccountEmail('confirmed', '', Mage::app()->getStore()->getId());
57: }
58: $this->_message($this->__('Authentication complete.'), self::MESSAGE_STATUS_SUCCESS);
59: } else {
60: $this->_message($this->__('Invalid login or password.'), self::MESSAGE_STATUS_ERROR);
61: }
62: } catch (Mage_Core_Exception $e) {
63: switch ($e->getCode()) {
64: case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
65:
66: $message = $e->getMessage();
67: break;
68: case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
69: $message = $e->getMessage();
70: break;
71: default:
72: $message = $e->getMessage();
73: }
74: $this->_message($message, self::MESSAGE_STATUS_ERROR);
75: } catch (Exception $e) {
76: $this->_message($this->__('Customer authentication problem.'), self::MESSAGE_STATUS_ERROR);
77: }
78: } else {
79: $this->_message($this->__('Login and password are required.'), self::MESSAGE_STATUS_ERROR);
80: }
81: }
82:
83: 84: 85: 86: 87:
88: public function logoutAction()
89: {
90: try {
91: $this->_getSession()->logout();
92: $this->_message($this->__('Logout complete.'), self::MESSAGE_STATUS_SUCCESS);
93: } catch (Mage_Core_Exception $e) {
94: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
95: } catch (Exception $e) {
96: Mage::logException($e);
97: $this->_message($this->__('Customer logout problem.'), self::MESSAGE_STATUS_ERROR);
98: }
99: }
100:
101: 102: 103: 104: 105:
106: public function formAction()
107: {
108: try {
109: $customer = null;
110: $editFlag = (int)$this->getRequest()->getParam('edit');
111: if ($editFlag == 1) {
112: if (!$this->_getSession()->isLoggedIn()) {
113: $this->_message(
114: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
115: );
116: return;
117: }
118: $customer = $this->_getSession()->getCustomer();
119: }
120:
121: $this->loadLayout(false)->getLayout()->getBlock('xmlconnect.customer.form')->setCustomer($customer);
122: $this->renderLayout();
123: } catch (Mage_Core_Exception $e) {
124: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
125: } catch (Exception $e) {
126: Mage::logException($e);
127: $this->_message($this->__('Can\'t load customer form.'), self::MESSAGE_STATUS_ERROR);
128: }
129: }
130:
131: 132: 133: 134: 135:
136: public function editAction()
137: {
138: if (!$this->_getSession()->isLoggedIn()) {
139: $this->_message(
140: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
141: );
142: return;
143: }
144: if ($this->getRequest()->isPost()) {
145: $customer = $this->_getSession()->getCustomer();
146:
147:
148: $customerForm = Mage::getModel('customer/form');
149: $customerForm->setFormCode('customer_account_edit')->setEntity($customer);
150:
151: $customerData = $customerForm->extractData($this->getRequest());
152:
153: $errors = array();
154: $customerErrors = $customerForm->validateData($customerData);
155: if ($customerErrors !== true) {
156: $errors = array_merge($customerErrors, $errors);
157: } else {
158: $customerForm->compactData($customerData);
159: $customerErrors = $customer->validate();
160: if (is_array($customerErrors)) {
161: $errors = array_merge($customerErrors, $errors);
162: }
163: }
164:
165: if ($this->getRequest()->getParam('change_password')) {
166: $currPass = $this->getRequest()->getPost('current_password');
167: $newPass = $this->getRequest()->getPost('password');
168: $confPass = $this->getRequest()->getPost('confirmation');
169:
170: if (empty($currPass) || empty($newPass) || empty($confPass)) {
171: $errors[] = $this->__('Password fields cannot be empty.');
172: }
173:
174: if ($newPass != $confPass) {
175: $errors[] = $this->__('Please make sure your passwords match.');
176: }
177:
178: $oldPass = $this->_getSession()->getCustomer()->getPasswordHash();
179: if (strpos($oldPass, ':')) {
180: list(, $salt) = explode(':', $oldPass);
181: } else {
182: $salt = false;
183: }
184:
185: if ($customer->hashPassword($currPass, $salt) == $oldPass) {
186: $customer->setPassword($newPass);
187: } else {
188: $errors[] = $this->__('Invalid current password.');
189: }
190: }
191:
192: if (!empty($errors)) {
193:
194: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
195: $message->addChild('status', self::MESSAGE_STATUS_ERROR);
196: $message->addChild('text', implode(' ', $errors));
197: $this->getResponse()->setBody($message->asNiceXml());
198: return;
199: }
200:
201: try {
202: $customer->save();
203: $this->_getSession()->setCustomer($customer);
204: $this->_message($this->__('Account information has been saved.'), self::MESSAGE_STATUS_SUCCESS);
205: return;
206: } catch (Mage_Core_Exception $e) {
207: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
208: } catch (Exception $e) {
209: if ($e instanceof Mage_Eav_Model_Entity_Attribute_Exception) {
210: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
211: } else {
212: $this->_message($this->__('Can\'t save the customer.'), self::MESSAGE_STATUS_ERROR);
213: }
214: }
215: } else {
216: $this->_message($this->__('POST data is not valid.'), self::MESSAGE_STATUS_ERROR);
217: }
218: }
219:
220: 221: 222: 223: 224:
225: public function saveAction()
226: {
227: $session = $this->_getSession();
228: $request = $this->getRequest();
229: if ($session->isLoggedIn()) {
230: $this->_message($this->__('You are already logged in.'), self::MESSAGE_STATUS_ERROR);
231: return;
232: }
233:
234: $session->setEscapeMessages(true);
235: if ($request->isPost()) {
236: $errors = array();
237:
238:
239: $customer = Mage::registry('current_customer');
240: if (is_null($customer)) {
241: $customer = Mage::getModel('customer/customer');
242: }
243:
244:
245: $customerForm = Mage::getModel('customer/form');
246: $customerForm->setFormCode('customer_account_create')->setEntity($customer);
247:
248: $customerData = $customerForm->extractData($this->getRequest());
249:
250: if ($this->getRequest()->getParam('is_subscribed', false)) {
251: $customer->setIsSubscribed(1);
252: }
253:
254: 255: 256:
257: $customer->getGroupId();
258:
259: try {
260: $customerErrors = $customerForm->validateData($customerData);
261: if ($customerErrors !== true) {
262: $errors = array_merge($customerErrors, $errors);
263: } else {
264: $customerForm->compactData($customerData);
265: $customer->setPassword($this->getRequest()->getPost('password'));
266: $customer->setConfirmation($this->getRequest()->getPost('confirmation'));
267: $customerErrors = $customer->validate();
268: if (is_array($customerErrors)) {
269: $errors = array_merge($customerErrors, $errors);
270: }
271: }
272:
273: $validationResult = count($errors) == 0;
274: if (true === $validationResult) {
275: $customer->save();
276:
277: if ($customer->isConfirmationRequired()) {
278: $customer->sendNewAccountEmail(
279: 'confirmation',
280: $session->getBeforeAuthUrl(),
281: Mage::app()->getStore()->getId()
282: );
283: $message = $this->__('Account confirmation is required. Please check your email for the confirmation link.');
284:
285: $messageXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
286: $messageXmlObj->addChild('status', self::MESSAGE_STATUS_SUCCESS);
287: $messageXmlObj->addChild('text', $message);
288: $messageXmlObj->addChild('confirmation', 1);
289: $this->getResponse()->setBody($messageXmlObj->asNiceXml());
290: return;
291: } else {
292: $session->setCustomerAsLoggedIn($customer);
293: $customer->sendNewAccountEmail('registered', '', Mage::app()->getStore()->getId());
294: $this->_message($this->__('Thank you for registering!'), self::MESSAGE_STATUS_SUCCESS);
295: return;
296: }
297: } else {
298: if (is_array($errors)) {
299: $message = implode("\n", $errors);
300: } else {
301: $message = $this->__('Invalid customer data.');
302: }
303: $this->_message($message, self::MESSAGE_STATUS_ERROR);
304: return;
305: }
306: } catch (Mage_Core_Exception $e) {
307: if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
308: $message = $this->__('An account with this email address already exists.');
309: $session->setEscapeMessages(false);
310: } else {
311: $message = $e->getMessage();
312: }
313: $this->_message($message, self::MESSAGE_STATUS_ERROR);
314: } catch (Exception $e) {
315: $this->_message($this->__('Can\'t save the customer.'), self::MESSAGE_STATUS_ERROR);
316: }
317: }
318: }
319:
320: 321: 322: 323: 324:
325: public function forgotPasswordAction()
326: {
327: $email = $this->getRequest()->getPost('email');
328: if ($email) {
329: if (!Zend_Validate::is($email, 'EmailAddress')) {
330: $this->_message($this->__('Invalid email address.'), self::MESSAGE_STATUS_ERROR);
331: return;
332: }
333: $customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
334: ->loadByEmail($email);
335:
336: if ($customer->getId()) {
337: try {
338: $newPassword = $customer->generatePassword();
339: $customer->changePassword($newPassword, false);
340: $customer->sendPasswordReminderEmail();
341: $this->_message(
342: $this->__('A new password has been sent.'), self::MESSAGE_STATUS_SUCCESS
343: );
344: return;
345: } catch (Mage_Core_Exception $e) {
346: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
347: } catch (Exception $e) {
348: $this->_message(
349: $this->__('Problem changing or sending password.'), self::MESSAGE_STATUS_ERROR
350: );
351: }
352: } else {
353: $this->_message(
354: $this->__('This email address was not found in our records.'), self::MESSAGE_STATUS_ERROR
355: );
356: }
357: } else {
358: $this->_message($this->__('Customer email not specified.'), self::MESSAGE_STATUS_ERROR);
359: }
360: }
361:
362: 363: 364: 365: 366:
367: public function addressAction()
368: {
369: if (!$this->_getSession()->isLoggedIn()) {
370: $this->_message(
371: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
372: );
373: return;
374: }
375:
376: if (count($this->_getSession()->getCustomer()->getAddresses())) {
377: try {
378: $this->loadLayout(false);
379: $this->renderLayout();
380: } catch (Mage_Core_Exception $e) {
381: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
382: } catch (Exception $e) {
383: $this->_message($this->__('Unable to load addresses.'), self::MESSAGE_STATUS_ERROR);
384: Mage::logException($e);
385: }
386: } else {
387:
388: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
389: $message->addChild('status', self::MESSAGE_STATUS_ERROR);
390: $message->addChild('is_empty_address_book', 1);
391: $this->getResponse()->setBody($message->asNiceXml());
392: }
393: }
394:
395: 396: 397: 398: 399:
400: public function addressFormAction()
401: {
402: try {
403: if (!$this->_getSession()->isLoggedIn()) {
404: $this->_message(
405: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
406: );
407: return;
408: }
409:
410: $address = Mage::getModel('customer/address');
411:
412: 413: 414:
415: $addressId = (int)$this->getRequest()->getParam('id');
416: if ($addressId) {
417: $address->load($addressId);
418: if ($address->getCustomerId() != $this->_getSession()->getCustomerId()) {
419: $this->_message($this->__('Specified address does not exist.'), self::MESSAGE_STATUS_ERROR);
420: return;
421: }
422: }
423:
424: $this->loadLayout(false)->getLayout()->getBlock('xmlconnect.customer.address.form')->setAddress($address);
425:
426: $this->renderLayout();
427: } catch (Mage_Core_Exception $e) {
428: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
429: Mage::logException($e);
430: } catch (Exception $e) {
431: Mage::logException($e);
432: $this->_message($this->__('Can\'t load customer form.'), self::MESSAGE_STATUS_ERROR);
433: }
434: }
435:
436: 437: 438: 439: 440:
441: public function deleteAddressAction()
442: {
443: if (!$this->_getSession()->isLoggedIn()) {
444: $this->_message(
445: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
446: );
447: return;
448: }
449:
450: $addressId = $this->getRequest()->getParam('id', false);
451:
452: if ($addressId) {
453: $address = Mage::getModel('customer/address')->load($addressId);
454:
455:
456: if ($address->getCustomerId() != $this->_getSession()->getCustomerId()) {
457: $this->_message($this->__('Address does not belong to this customer.'), self::MESSAGE_STATUS_ERROR);
458: return;
459: }
460:
461: try {
462: $address->delete();
463: $this->_message($this->__('Address has been deleted.'), self::MESSAGE_STATUS_SUCCESS);
464: } catch (Exception $e) {
465: Mage::logException($e);
466: $this->_message($this->__('An error occurred while deleting the address.'), self::MESSAGE_STATUS_ERROR);
467: }
468: }
469: }
470:
471: 472: 473: 474: 475:
476: public function saveAddressAction()
477: {
478: if (!$this->_getSession()->isLoggedIn()) {
479: $this->_message(
480: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
481: );
482: return;
483: }
484:
485:
486: if ($this->getRequest()->isPost()) {
487: $customer = $this->_getSession()->getCustomer();
488:
489: $address = Mage::getModel('customer/address');
490: $addressId = $this->getRequest()->getParam('id');
491: if ($addressId) {
492: $existsAddress = $customer->getAddressById($addressId);
493: if ($existsAddress->getId() && $existsAddress->getCustomerId() == $customer->getId()) {
494: $address->setId($existsAddress->getId());
495: }
496: }
497:
498: $errors = array();
499:
500:
501: $addressForm = Mage::getModel('customer/form');
502: $addressForm->setFormCode('customer_address_edit')
503: ->setEntity($address);
504: $addressData = $addressForm->extractData($this->getRequest());
505: $addressErrors = $addressForm->validateData($addressData);
506: if ($addressErrors !== true) {
507: $errors = $addressErrors;
508: }
509:
510: try {
511: $addressForm->compactData($addressData);
512: $address->setCustomerId($customer->getId())
513: ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
514: ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
515:
516: $addressErrors = $address->validate();
517: if ($addressErrors !== true) {
518: $errors = array_merge($errors, $addressErrors);
519: }
520:
521: $addressValidation = count($errors) == 0;
522:
523: if (true === $addressValidation) {
524: $address->save();
525:
526: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
527: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
528: $message->addChild('text', $this->__('Address has been saved.'));
529: $message->addChild('address_id', $address->getId());
530: $this->getResponse()->setBody($message->asNiceXml());
531: return;
532: } else {
533: if (is_array($errors)) {
534: $this->_message(implode('. ', $errors), self::MESSAGE_STATUS_ERROR);
535: } else {
536: $this->_message($this->__('Can\'t save address.'), self::MESSAGE_STATUS_ERROR);
537: }
538: }
539: } catch (Mage_Core_Exception $e) {
540: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
541: } catch (Exception $e) {
542: Mage::logException($e);
543: $this->_message($this->__('Can\'t save address.'), self::MESSAGE_STATUS_ERROR);
544: }
545: } else {
546: $this->_message($this->__('Address data not specified.'), self::MESSAGE_STATUS_ERROR);
547: }
548: }
549:
550: 551: 552: 553: 554:
555: public function orderListAction()
556: {
557: if (!$this->_getSession()->isLoggedIn()) {
558: $this->_message(
559: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
560: );
561: return;
562: }
563:
564: try {
565: $this->loadLayout(false);
566: $this->renderLayout();
567: } catch (Mage_Core_Exception $e) {
568: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
569: } catch (Exception $e) {
570: $this->_message($this->__('Unable to load order list.'), self::MESSAGE_STATUS_ERROR);
571: Mage::logException($e);
572: }
573: }
574:
575: 576: 577: 578: 579:
580: public function orderDetailsAction()
581: {
582: try {
583: if (!$this->_getSession()->isLoggedIn()) {
584: $this->_message(
585: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
586: );
587: return;
588: }
589:
590: $orderId = (int) $this->getRequest()->getParam('order_id');
591: if (!$orderId) {
592: $this->_message($this->__('Order id is not specified.'), self::MESSAGE_STATUS_ERROR);
593: return;
594: }
595:
596: $order = Mage::getModel('sales/order')->load($orderId);
597:
598: if ($this->_canViewOrder($order)) {
599: Mage::register('current_order', $order);
600: } else {
601: $this->_message($this->__('Order is not available.'), self::MESSAGE_STATUS_ERROR);
602: return;
603: }
604: $this->loadLayout(false);
605: $this->renderLayout();
606: return;
607: } catch (Mage_Core_Exception $e) {
608: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
609: } catch (Exception $e) {
610: $this->_message($this->__('Unable to render an order.'), self::MESSAGE_STATUS_ERROR);
611: Mage::logException($e);
612: }
613: }
614:
615: 616: 617: 618: 619: 620:
621: protected function _canViewOrder($order)
622: {
623: $customerId = Mage::getSingleton('customer/session')->getCustomerId();
624: $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
625: if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
626: && in_array($order->getState(), $availableStates, true)
627: ) {
628: return true;
629: }
630: return false;
631: }
632:
633: 634: 635: 636: 637:
638: public function isLogginedAction()
639: {
640:
641: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
642: $message->addChild('is_loggined', (int)$this->_getSession()->isLoggedIn());
643: $this->getResponse()->setBody($message->asNiceXml());
644: }
645:
646: 647: 648: 649: 650: 651:
652: protected function _filterPostData($data)
653: {
654: $data = $this->_filterDates($data, array('dob'));
655: return $data;
656: }
657:
658: 659: 660: 661: 662:
663: protected function _getSession()
664: {
665: return Mage::getSingleton('customer/session');
666: }
667:
668: 669: 670: 671: 672:
673: public function storeCreditAction()
674: {
675: try {
676: 677: 678:
679: if (!is_object(Mage::getConfig()->getNode('modules/Enterprise_CustomerBalance'))) {
680: $this->_message(
681: $this->__('Customer balance available in enterprise version of Magento only.'),
682: self::MESSAGE_STATUS_ERROR
683: );
684: return;
685: }
686: $this->loadLayout(false);
687: $this->renderLayout();
688: return;
689: } catch (Mage_Core_Exception $e) {
690: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
691: } catch (Exception $e) {
692: $this->_message($this->__('Unable to render the store credits.'), self::MESSAGE_STATUS_ERROR);
693: Mage::logException($e);
694: }
695: }
696:
697: 698: 699: 700: 701:
702: public function giftcardCheckAction()
703: {
704: try {
705: 706: 707:
708: if (!is_object(Mage::getConfig()->getNode('modules/Enterprise_GiftCardAccount'))) {
709: $this->_message(
710: $this->__('Gift card account available in enterprise version of Magento only.'),
711: self::MESSAGE_STATUS_ERROR
712: );
713: return;
714: }
715:
716: $card = Mage::getModel('enterprise_giftcardaccount/giftcardaccount')
717: ->loadByCode($this->getRequest()->getParam('giftcard_code', ''));
718: Mage::register('current_giftcardaccount', $card);
719:
720: $card->isValid(true, true, true, false);
721:
722: $this->loadLayout(false);
723: $this->renderLayout();
724: return;
725: } catch (Mage_Core_Exception $e) {
726: $card->unsetData();
727: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
728: } catch (Exception $e) {
729: $this->_message($this->__('Unable to render a gift card account.'), self::MESSAGE_STATUS_ERROR);
730: Mage::logException($e);
731: }
732: }
733:
734: 735: 736: 737: 738:
739: public function giftcardRedeemAction()
740: {
741: try {
742: 743: 744:
745: if (!is_object(Mage::getConfig()->getNode('modules/Enterprise_GiftCardAccount'))) {
746: $this->_message(
747: $this->__('Gift card account available in enterprise version of Magento only.'),
748: self::MESSAGE_STATUS_ERROR
749: );
750: return;
751: }
752:
753: $code = $this->getRequest()->getParam('giftcard_code', '');
754: if ($code) {
755: if (!Mage::helper('enterprise_customerbalance')->isEnabled()) {
756: Mage::throwException($this->__('Redemption functionality is disabled.'));
757: }
758: Mage::getModel('enterprise_giftcardaccount/giftcardaccount')->loadByCode($code)
759: ->setIsRedeemed(true)->redeem();
760:
761: $this->_message(
762: $this->__('Gift Card "%s" was redeemed.', Mage::helper('core')->escapeHtml($code)),
763: self::MESSAGE_STATUS_SUCCESS
764: );
765: }
766: return;
767: } catch (Mage_Core_Exception $e) {
768: if (isset($card) && is_object($card)) {
769: $card->unsetData();
770: }
771: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
772: } catch (Exception $e) {
773: $this->_message($this->__('Cannot redeem Gift Card.'), self::MESSAGE_STATUS_ERROR);
774: Mage::logException($e);
775: }
776: }
777: }
778: