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_Adminhtml_CustomerController extends Mage_Adminhtml_Controller_Action
35: {
36:
37: protected function _initCustomer($idFieldName = 'id')
38: {
39: $this->_title($this->__('Customers'))->_title($this->__('Manage Customers'));
40:
41: $customerId = (int) $this->getRequest()->getParam($idFieldName);
42: $customer = Mage::getModel('customer/customer');
43:
44: if ($customerId) {
45: $customer->load($customerId);
46: }
47:
48: Mage::register('current_customer', $customer);
49: return $this;
50: }
51:
52: 53: 54:
55: public function indexAction()
56: {
57: $this->_title($this->__('Customers'))->_title($this->__('Manage Customers'));
58:
59: if ($this->getRequest()->getQuery('ajax')) {
60: $this->_forward('grid');
61: return;
62: }
63: $this->loadLayout();
64:
65: 66: 67:
68: $this->_setActiveMenu('customer/manage');
69:
70: 71: 72:
73: $this->_addContent(
74: $this->getLayout()->createBlock('adminhtml/customer', 'customer')
75: );
76:
77: 78: 79:
80: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Customers'), Mage::helper('adminhtml')->__('Customers'));
81: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Manage Customers'), Mage::helper('adminhtml')->__('Manage Customers'));
82:
83: $this->renderLayout();
84: }
85:
86: public function gridAction()
87: {
88: $this->loadLayout();
89: $this->renderLayout();
90: }
91:
92: 93: 94:
95: public function editAction()
96: {
97: $this->_initCustomer();
98: $this->loadLayout();
99:
100:
101: $customer = Mage::registry('current_customer');
102:
103:
104: $data = Mage::getSingleton('adminhtml/session')->getCustomerData(true);
105:
106:
107: if ($data) {
108: $request = clone $this->getRequest();
109: $request->setParams($data);
110:
111: if (isset($data['account'])) {
112:
113: $customerForm = Mage::getModel('customer/form');
114: $customerForm->setEntity($customer)
115: ->setFormCode('adminhtml_customer')
116: ->setIsAjaxRequest(true);
117: $formData = $customerForm->extractData($request, 'account');
118: $customerForm->restoreData($formData);
119: }
120:
121: if (isset($data['address']) && is_array($data['address'])) {
122:
123: $addressForm = Mage::getModel('customer/form');
124: $addressForm->setFormCode('adminhtml_customer_address');
125:
126: foreach (array_keys($data['address']) as $addressId) {
127: if ($addressId == '_template_') {
128: continue;
129: }
130:
131: $address = $customer->getAddressItemById($addressId);
132: if (!$address) {
133: $address = Mage::getModel('customer/address');
134: $customer->addAddress($address);
135: }
136:
137: $formData = $addressForm->setEntity($address)
138: ->extractData($request);
139: $addressForm->restoreData($formData);
140: }
141: }
142: }
143:
144: $this->_title($customer->getId() ? $customer->getName() : $this->__('New Customer'));
145:
146: 147: 148:
149: $this->_setActiveMenu('customer/new');
150:
151: $this->renderLayout();
152: }
153:
154: 155: 156:
157: public function newAction()
158: {
159: $this->_forward('edit');
160: }
161:
162: 163: 164:
165: public function deleteAction()
166: {
167: $this->_initCustomer();
168: $customer = Mage::registry('current_customer');
169: if ($customer->getId()) {
170: try {
171: $customer->load($customer->getId());
172: $customer->delete();
173: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The customer has been deleted.'));
174: }
175: catch (Exception $e){
176: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
177: }
178: }
179: $this->_redirect('*/customer');
180: }
181:
182: 183: 184:
185: public function saveAction()
186: {
187: $data = $this->getRequest()->getPost();
188: if ($data) {
189: $redirectBack = $this->getRequest()->getParam('back', false);
190: $this->_initCustomer('customer_id');
191:
192:
193: $customer = Mage::registry('current_customer');
194:
195:
196: $customerForm = Mage::getModel('customer/form');
197: $customerForm->setEntity($customer)
198: ->setFormCode('adminhtml_customer')
199: ->ignoreInvisible(false)
200: ;
201:
202: $formData = $customerForm->extractData($this->getRequest(), 'account');
203:
204:
205: if (isset($formData['disable_auto_group_change'])) {
206: $formData['disable_auto_group_change'] = empty($formData['disable_auto_group_change']) ? '0' : '1';
207: }
208:
209: $errors = $customerForm->validateData($formData);
210: if ($errors !== true) {
211: foreach ($errors as $error) {
212: $this->_getSession()->addError($error);
213: }
214: $this->_getSession()->setCustomerData($data);
215: $this->getResponse()->setRedirect($this->getUrl('*/customer/edit', array('id' => $customer->getId())));
216: return;
217: }
218:
219: $customerForm->compactData($formData);
220:
221:
222: if (isset($data['address']['_template_'])) {
223: unset($data['address']['_template_']);
224: }
225:
226: $modifiedAddresses = array();
227: if (!empty($data['address'])) {
228:
229: $addressForm = Mage::getModel('customer/form');
230: $addressForm->setFormCode('adminhtml_customer_address')->ignoreInvisible(false);
231:
232: foreach (array_keys($data['address']) as $index) {
233: $address = $customer->getAddressItemById($index);
234: if (!$address) {
235: $address = Mage::getModel('customer/address');
236: }
237:
238: $requestScope = sprintf('address/%s', $index);
239: $formData = $addressForm->setEntity($address)
240: ->extractData($this->getRequest(), $requestScope);
241:
242:
243: $isDefaultBilling = isset($data['account']['default_billing'])
244: && $data['account']['default_billing'] == $index;
245: $address->setIsDefaultBilling($isDefaultBilling);
246: $isDefaultShipping = isset($data['account']['default_shipping'])
247: && $data['account']['default_shipping'] == $index;
248: $address->setIsDefaultShipping($isDefaultShipping);
249:
250: $errors = $addressForm->validateData($formData);
251: if ($errors !== true) {
252: foreach ($errors as $error) {
253: $this->_getSession()->addError($error);
254: }
255: $this->_getSession()->setCustomerData($data);
256: $this->getResponse()->setRedirect($this->getUrl('*/customer/edit', array(
257: 'id' => $customer->getId())
258: ));
259: return;
260: }
261:
262: $addressForm->compactData($formData);
263:
264:
265: $address->setPostIndex($index);
266:
267: if ($address->getId()) {
268: $modifiedAddresses[] = $address->getId();
269: } else {
270: $customer->addAddress($address);
271: }
272: }
273: }
274:
275:
276: if (isset($data['account']['default_billing'])) {
277: $customer->setData('default_billing', $data['account']['default_billing']);
278: }
279: if (isset($data['account']['default_shipping'])) {
280: $customer->setData('default_shipping', $data['account']['default_shipping']);
281: }
282: if (isset($data['account']['confirmation'])) {
283: $customer->setData('confirmation', $data['account']['confirmation']);
284: }
285:
286:
287: foreach ($customer->getAddressesCollection() as $customerAddress) {
288: if ($customerAddress->getId() && !in_array($customerAddress->getId(), $modifiedAddresses)) {
289: $customerAddress->setData('_deleted', true);
290: }
291: }
292:
293: if (Mage::getSingleton('admin/session')->isAllowed('customer/newsletter')) {
294: $customer->setIsSubscribed(isset($data['subscription']));
295: }
296:
297: if (isset($data['account']['sendemail_store_id'])) {
298: $customer->setSendemailStoreId($data['account']['sendemail_store_id']);
299: }
300:
301: $isNewCustomer = $customer->isObjectNew();
302: try {
303: $sendPassToEmail = false;
304:
305: if ($isNewCustomer) {
306: $customer->setPassword($data['account']['password']);
307: $customer->setForceConfirmed(true);
308: if ($customer->getPassword() == 'auto') {
309: $sendPassToEmail = true;
310: $customer->setPassword($customer->generatePassword());
311: }
312: }
313:
314: Mage::dispatchEvent('adminhtml_customer_prepare_save', array(
315: 'customer' => $customer,
316: 'request' => $this->getRequest()
317: ));
318:
319: $customer->save();
320:
321:
322: if ($customer->getWebsiteId() && (isset($data['account']['sendemail']) || $sendPassToEmail)) {
323: $storeId = $customer->getSendemailStoreId();
324: if ($isNewCustomer) {
325: $customer->sendNewAccountEmail('registered', '', $storeId);
326: } elseif ((!$customer->getConfirmation())) {
327:
328: $customer->sendNewAccountEmail('confirmed', '', $storeId);
329: }
330: }
331:
332: if (!empty($data['account']['new_password'])) {
333: $newPassword = $data['account']['new_password'];
334: if ($newPassword == 'auto') {
335: $newPassword = $customer->generatePassword();
336: }
337: $customer->changePassword($newPassword);
338: $customer->sendPasswordReminderEmail();
339: }
340:
341: Mage::getSingleton('adminhtml/session')->addSuccess(
342: Mage::helper('adminhtml')->__('The customer has been saved.')
343: );
344: Mage::dispatchEvent('adminhtml_customer_save_after', array(
345: 'customer' => $customer,
346: 'request' => $this->getRequest()
347: ));
348:
349: if ($redirectBack) {
350: $this->_redirect('*/*/edit', array(
351: 'id' => $customer->getId(),
352: '_current' => true
353: ));
354: return;
355: }
356: } catch (Mage_Core_Exception $e) {
357: $this->_getSession()->addError($e->getMessage());
358: $this->_getSession()->setCustomerData($data);
359: $this->getResponse()->setRedirect($this->getUrl('*/customer/edit', array('id' => $customer->getId())));
360: } catch (Exception $e) {
361: $this->_getSession()->addException($e,
362: Mage::helper('adminhtml')->__('An error occurred while saving the customer.'));
363: $this->_getSession()->setCustomerData($data);
364: $this->getResponse()->setRedirect($this->getUrl('*/customer/edit', array('id'=>$customer->getId())));
365: return;
366: }
367: }
368: $this->getResponse()->setRedirect($this->getUrl('*/customer'));
369: }
370:
371: 372: 373:
374: public function exportCsvAction()
375: {
376: $fileName = 'customers.csv';
377: $content = $this->getLayout()->createBlock('adminhtml/customer_grid')
378: ->getCsvFile();
379:
380: $this->_prepareDownloadResponse($fileName, $content);
381: }
382:
383: 384: 385:
386: public function exportXmlAction()
387: {
388: $fileName = 'customers.xml';
389: $content = $this->getLayout()->createBlock('adminhtml/customer_grid')
390: ->getExcelFile();
391:
392: $this->_prepareDownloadResponse($fileName, $content);
393: }
394:
395: 396: 397: 398: 399: 400: 401: 402: 403: 404:
405: protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
406: {
407: $this->_prepareDownloadResponse($fileName, $content, $contentType);
408: }
409:
410: 411: 412: 413:
414: public function ordersAction() {
415: $this->_initCustomer();
416: $this->loadLayout();
417: $this->renderLayout();
418: }
419:
420: 421: 422: 423:
424: public function lastOrdersAction() {
425: $this->_initCustomer();
426: $this->loadLayout();
427: $this->renderLayout();
428: }
429:
430: 431: 432: 433:
434: public function newsletterAction()
435: {
436: $this->_initCustomer();
437: $subscriber = Mage::getModel('newsletter/subscriber')
438: ->loadByCustomer(Mage::registry('current_customer'));
439:
440: Mage::register('subscriber', $subscriber);
441: $this->loadLayout();
442: $this->renderLayout();
443: }
444:
445: public function wishlistAction()
446: {
447: $this->_initCustomer();
448: $customer = Mage::registry('current_customer');
449: if ($customer->getId()) {
450: if($itemId = (int) $this->getRequest()->getParam('delete')) {
451: try {
452: Mage::getModel('wishlist/item')->load($itemId)
453: ->delete();
454: }
455: catch (Exception $e) {
456: Mage::logException($e);
457: }
458: }
459: }
460:
461: $this->getLayout()->getUpdate()
462: ->addHandle(strtolower($this->getFullActionName()));
463: $this->loadLayoutUpdates()->generateLayoutXml()->generateLayoutBlocks();
464:
465: $this->renderLayout();
466: }
467:
468: 469: 470: 471:
472: public function viewWishlistAction()
473: {
474: $this->_initCustomer();
475: $this->loadLayout();
476: $this->renderLayout();
477: }
478:
479: 480: 481: 482: 483:
484: public function cartAction()
485: {
486: $this->_initCustomer();
487: $websiteId = $this->getRequest()->getParam('website_id');
488:
489:
490: $deleteItemId = $this->getRequest()->getPost('delete');
491: if ($deleteItemId) {
492: $quote = Mage::getModel('sales/quote')
493: ->setWebsite(Mage::app()->getWebsite($websiteId))
494: ->loadByCustomer(Mage::registry('current_customer'));
495: $item = $quote->getItemById($deleteItemId);
496: if ($item && $item->getId()) {
497: $quote->removeItem($deleteItemId);
498: $quote->collectTotals()->save();
499: }
500: }
501:
502: $this->loadLayout();
503: $this->getLayout()->getBlock('admin.customer.view.edit.cart')->setWebsiteId($websiteId);
504: $this->renderLayout();
505: }
506:
507: 508: 509: 510:
511: public function viewCartAction()
512: {
513: $this->_initCustomer();
514: $layout = $this->loadLayout()
515: ->getLayout()
516: ->getBlock('admin.customer.view.cart')
517: ->setWebsiteId();
518: $this->renderLayout();
519: }
520:
521: 522: 523: 524:
525: public function cartsAction()
526: {
527: $this->_initCustomer();
528: $this->loadLayout();
529: $this->renderLayout();
530: }
531:
532: 533: 534: 535:
536: public function productReviewsAction()
537: {
538: $this->_initCustomer();
539: $this->loadLayout()
540: ->getLayout()
541: ->getBlock('admin.customer.reviews')
542: ->setCustomerId(Mage::registry('current_customer')->getId())
543: ->setUseAjax(true);
544: $this->renderLayout();
545: }
546:
547: 548: 549: 550:
551: public function productTagsAction()
552: {
553: $this->_initCustomer();
554: $this->loadLayout()
555: ->getLayout()
556: ->getBlock('admin.customer.tags')
557: ->setCustomerId(Mage::registry('current_customer')->getId())
558: ->setUseAjax(true);
559: $this->renderLayout();
560: }
561:
562: public function tagGridAction()
563: {
564: $this->_initCustomer();
565: $this->loadLayout();
566: $this->getLayout()->getBlock('admin.customer.tags')->setCustomerId(
567: Mage::registry('current_customer')
568: );
569: $this->renderLayout();
570: }
571:
572: public function validateAction()
573: {
574: $response = new Varien_Object();
575: $response->setError(0);
576: $websiteId = Mage::app()->getStore()->getWebsiteId();
577: $accountData = $this->getRequest()->getPost('account');
578:
579: $customer = Mage::getModel('customer/customer');
580: $customerId = $this->getRequest()->getParam('id');
581: if ($customerId) {
582: $customer->load($customerId);
583: $websiteId = $customer->getWebsiteId();
584: } else if (isset($accountData['website_id'])) {
585: $websiteId = $accountData['website_id'];
586: }
587:
588:
589: $customerForm = Mage::getModel('customer/form');
590: $customerForm->setEntity($customer)
591: ->setFormCode('adminhtml_customer')
592: ->setIsAjaxRequest(true)
593: ->ignoreInvisible(false)
594: ;
595:
596: $data = $customerForm->extractData($this->getRequest(), 'account');
597: $errors = $customerForm->validateData($data);
598: if ($errors !== true) {
599: foreach ($errors as $error) {
600: $this->_getSession()->addError($error);
601: }
602: $response->setError(1);
603: }
604:
605:
606: if (!$response->getError()) {
607:
608:
609: $checkCustomer = Mage::getModel('customer/customer')
610: ->setWebsiteId($websiteId);
611: $checkCustomer->loadByEmail($accountData['email']);
612: if ($checkCustomer->getId() && ($checkCustomer->getId() != $customer->getId())) {
613: $response->setError(1);
614: $this->_getSession()->addError(
615: Mage::helper('adminhtml')->__('Customer with the same email already exists.')
616: );
617: }
618: }
619:
620: $addressesData = $this->getRequest()->getParam('address');
621: if (is_array($addressesData)) {
622:
623: $addressForm = Mage::getModel('customer/form');
624: $addressForm->setFormCode('adminhtml_customer_address')->ignoreInvisible(false);
625: foreach (array_keys($addressesData) as $index) {
626: if ($index == '_template_') {
627: continue;
628: }
629: $address = $customer->getAddressItemById($index);
630: if (!$address) {
631: $address = Mage::getModel('customer/address');
632: }
633:
634: $requestScope = sprintf('address/%s', $index);
635: $formData = $addressForm->setEntity($address)
636: ->extractData($this->getRequest(), $requestScope);
637:
638: $errors = $addressForm->validateData($formData);
639: if ($errors !== true) {
640: foreach ($errors as $error) {
641: $this->_getSession()->addError($error);
642: }
643: $response->setError(1);
644: }
645: }
646: }
647:
648: if ($response->getError()) {
649: $this->_initLayoutMessages('adminhtml/session');
650: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
651: }
652:
653: $this->getResponse()->setBody($response->toJson());
654: }
655:
656: public function massSubscribeAction()
657: {
658: $customersIds = $this->getRequest()->getParam('customer');
659: if(!is_array($customersIds)) {
660: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s).'));
661:
662: } else {
663: try {
664: foreach ($customersIds as $customerId) {
665: $customer = Mage::getModel('customer/customer')->load($customerId);
666: $customer->setIsSubscribed(true);
667: $customer->save();
668: }
669: Mage::getSingleton('adminhtml/session')->addSuccess(
670: Mage::helper('adminhtml')->__('Total of %d record(s) were updated.', count($customersIds))
671: );
672: } catch (Exception $e) {
673: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
674: }
675: }
676: $this->_redirect('*/*/index');
677: }
678:
679: public function massUnsubscribeAction()
680: {
681: $customersIds = $this->getRequest()->getParam('customer');
682: if(!is_array($customersIds)) {
683: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s).'));
684: } else {
685: try {
686: foreach ($customersIds as $customerId) {
687: $customer = Mage::getModel('customer/customer')->load($customerId);
688: $customer->setIsSubscribed(false);
689: $customer->save();
690: }
691: Mage::getSingleton('adminhtml/session')->addSuccess(
692: Mage::helper('adminhtml')->__('Total of %d record(s) were updated.', count($customersIds))
693: );
694: } catch (Exception $e) {
695: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
696: }
697: }
698:
699: $this->_redirect('*/*/index');
700: }
701:
702: public function massDeleteAction()
703: {
704: $customersIds = $this->getRequest()->getParam('customer');
705: if(!is_array($customersIds)) {
706: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s).'));
707: } else {
708: try {
709: $customer = Mage::getModel('customer/customer');
710: foreach ($customersIds as $customerId) {
711: $customer->reset()
712: ->load($customerId)
713: ->delete();
714: }
715: Mage::getSingleton('adminhtml/session')->addSuccess(
716: Mage::helper('adminhtml')->__('Total of %d record(s) were deleted.', count($customersIds))
717: );
718: } catch (Exception $e) {
719: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
720: }
721: }
722:
723: $this->_redirect('*/*/index');
724: }
725:
726: public function massAssignGroupAction()
727: {
728: $customersIds = $this->getRequest()->getParam('customer');
729: if(!is_array($customersIds)) {
730: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s).'));
731: } else {
732: try {
733: foreach ($customersIds as $customerId) {
734: $customer = Mage::getModel('customer/customer')->load($customerId);
735: $customer->setGroupId($this->getRequest()->getParam('group'));
736: $customer->save();
737: }
738: Mage::getSingleton('adminhtml/session')->addSuccess(
739: Mage::helper('adminhtml')->__('Total of %d record(s) were updated.', count($customersIds))
740: );
741: } catch (Exception $e) {
742: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
743: }
744: }
745:
746: $this->_redirect('*/*/index');
747: }
748:
749: public function viewfileAction()
750: {
751: $file = null;
752: $plain = false;
753: if ($this->getRequest()->getParam('file')) {
754:
755: $file = Mage::helper('core')->urlDecode($this->getRequest()->getParam('file'));
756: } else if ($this->getRequest()->getParam('image')) {
757:
758: $file = Mage::helper('core')->urlDecode($this->getRequest()->getParam('image'));
759: $plain = true;
760: } else {
761: return $this->norouteAction();
762: }
763:
764: $path = Mage::getBaseDir('media') . DS . 'customer';
765:
766: $ioFile = new Varien_Io_File();
767: $ioFile->open(array('path' => $path));
768: $fileName = $ioFile->getCleanPath($path . $file);
769: $path = $ioFile->getCleanPath($path);
770:
771: if ((!$ioFile->fileExists($fileName) || strpos($fileName, $path) !== 0)
772: && !Mage::helper('core/file_storage')->processStorageFile(str_replace('/', DS, $fileName))
773: ) {
774: return $this->norouteAction();
775: }
776:
777: if ($plain) {
778: $extension = pathinfo($fileName, PATHINFO_EXTENSION);
779: switch (strtolower($extension)) {
780: case 'gif':
781: $contentType = 'image/gif';
782: break;
783: case 'jpg':
784: $contentType = 'image/jpeg';
785: break;
786: case 'png':
787: $contentType = 'image/png';
788: break;
789: default:
790: $contentType = 'application/octet-stream';
791: break;
792: }
793:
794: $ioFile->streamOpen($fileName, 'r');
795: $contentLength = $ioFile->streamStat('size');
796: $contentModify = $ioFile->streamStat('mtime');
797:
798: $this->getResponse()
799: ->setHttpResponseCode(200)
800: ->setHeader('Pragma', 'public', true)
801: ->setHeader('Content-type', $contentType, true)
802: ->setHeader('Content-Length', $contentLength)
803: ->setHeader('Last-Modified', date('r', $contentModify))
804: ->clearBody();
805: $this->getResponse()->sendHeaders();
806:
807: while (false !== ($buffer = $ioFile->streamRead())) {
808: echo $buffer;
809: }
810: } else {
811: $name = pathinfo($fileName, PATHINFO_BASENAME);
812: $this->_prepareDownloadResponse($name, array(
813: 'type' => 'filename',
814: 'value' => $fileName
815: ));
816: }
817:
818: exit();
819: }
820:
821: protected function _isAllowed()
822: {
823: return Mage::getSingleton('admin/session')->isAllowed('customer/manage');
824: }
825:
826: 827: 828: 829: 830: 831:
832: protected function _filterPostData($data)
833: {
834: $data['account'] = $this->_filterDates($data['account'], array('dob'));
835: return $data;
836: }
837: }
838: