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_Adminhtml_CustomerController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037 protected function _initCustomer($idFieldName = 'id')
00038 {
00039 $customerId = (int) $this->getRequest()->getParam($idFieldName);
00040 $customer = Mage::getModel('customer/customer');
00041
00042 if ($customerId) {
00043 $customer->load($customerId);
00044 }
00045
00046 Mage::register('current_customer', $customer);
00047 return $this;
00048 }
00049
00050
00051
00052
00053 public function indexAction()
00054 {
00055 if ($this->getRequest()->getQuery('ajax')) {
00056 $this->_forward('grid');
00057 return;
00058 }
00059 $this->loadLayout();
00060
00061
00062
00063
00064 $this->_setActiveMenu('customer/manage');
00065
00066
00067
00068
00069 $this->_addContent(
00070 $this->getLayout()->createBlock('adminhtml/customer', 'customer')
00071 );
00072
00073
00074
00075
00076 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Customers'), Mage::helper('adminhtml')->__('Customers'));
00077 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Manage Customers'), Mage::helper('adminhtml')->__('Manage Customers'));
00078
00079 $this->renderLayout();
00080 }
00081
00082 public function gridAction()
00083 {
00084 $this->loadLayout();
00085 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_grid')->toHtml());
00086 }
00087
00088
00089
00090
00091 public function editAction()
00092 {
00093 $this->_initCustomer();
00094 $this->loadLayout();
00095
00096 $customer = Mage::registry('current_customer');
00097
00098
00099 $data = Mage::getSingleton('adminhtml/session')->getCustomerData(true);
00100
00101 if (isset($data['account'])) {
00102 $customer->addData($data['account']);
00103 }
00104 if (isset($data['address']) && is_array($data['address'])) {
00105 foreach ($data['address'] as $addressId => $address) {
00106 $addressModel = Mage::getModel('customer/address')->setData($address)
00107 ->setId($addressId);
00108 $customer->addAddress($addressModel);
00109 }
00110 }
00111
00112
00113
00114
00115 $this->_setActiveMenu('customer/new');
00116
00117 $this->renderLayout();
00118 }
00119
00120
00121
00122
00123 public function newAction()
00124 {
00125 $this->_forward('edit');
00126 }
00127
00128
00129
00130
00131 public function deleteAction()
00132 {
00133 $this->_initCustomer();
00134 $customer = Mage::registry('current_customer');
00135 if ($customer->getId()) {
00136 try {
00137 $customer->load($customer->getId());
00138 $customer->delete();
00139 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Customer was deleted'));
00140 }
00141 catch (Exception $e){
00142 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00143 }
00144 }
00145 $this->_redirect('*/customer');
00146 }
00147
00148
00149
00150
00151 public function saveAction()
00152 {
00153 if ($data = $this->getRequest()->getPost()) {
00154 $redirectBack = $this->getRequest()->getParam('back', false);
00155 $this->_initCustomer('customer_id');
00156 $customer = Mage::registry('current_customer');
00157
00158
00159 if (isset($data['account'])) {
00160 $customer->addData($data['account']);
00161 }
00162
00163 if (isset($data['address'])) {
00164
00165 if (isset($data['address']['_template_'])) {
00166 unset($data['address']['_template_']);
00167 }
00168
00169 foreach ($data['address'] as $index => $addressData) {
00170 $address = Mage::getModel('customer/address');
00171 $address->setData($addressData);
00172
00173 if ($addressId = (int) $index) {
00174 $address->setId($addressId);
00175 }
00176
00177
00178
00179 $address->setPostIndex($index);
00180 $customer->addAddress($address);
00181 }
00182 }
00183
00184 if(isset($data['subscription'])) {
00185 $customer->setIsSubscribed(true);
00186 } else {
00187 $customer->setIsSubscribed(false);
00188 }
00189
00190 $isNewCustomer = !$customer->getId();
00191 try {
00192 if ($customer->getPassword() == 'auto') {
00193 $customer->setPassword($customer->generatePassword());
00194 }
00195
00196
00197 if ($isNewCustomer) {
00198 $customer->setForceConfirmed(true);
00199 }
00200
00201 Mage::dispatchEvent('adminhtml_customer_prepare_save',
00202 array('customer' => $customer, 'request' => $this->getRequest())
00203 );
00204
00205 $customer->save();
00206
00207
00208 if ($customer->getWebsiteId() && $customer->hasData('sendemail')) {
00209 if ($isNewCustomer) {
00210 $customer->sendNewAccountEmail();
00211 }
00212
00213 elseif ((!$customer->getConfirmation())) {
00214 $customer->sendNewAccountEmail('confirmed');
00215 }
00216 }
00217
00218
00219
00220 if ($newPassword = $customer->getNewPassword()) {
00221 if ($newPassword == 'auto') {
00222 $newPassword = $customer->generatePassword();
00223 }
00224 $customer->changePassword($newPassword);
00225 $customer->sendPasswordReminderEmail();
00226 }
00227
00228 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Customer was successfully saved'));
00229 Mage::dispatchEvent('adminhtml_customer_save_after', array('customer' => $customer));
00230
00231 if ($redirectBack) {
00232 $this->_redirect('*/*/edit', array(
00233 'id' => $customer->getId(),
00234 '_current'=>true
00235 ));
00236 return;
00237 }
00238 }
00239 catch (Exception $e){
00240 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00241 Mage::getSingleton('adminhtml/session')->setCustomerData($data);
00242 $this->getResponse()->setRedirect($this->getUrl('*/customer/edit', array('id'=>$customer->getId())));
00243 return;
00244 }
00245 }
00246 $this->getResponse()->setRedirect($this->getUrl('*/customer'));
00247 }
00248
00249
00250
00251
00252 public function exportCsvAction()
00253 {
00254 $fileName = 'customers.csv';
00255 $content = $this->getLayout()->createBlock('adminhtml/customer_grid')
00256 ->getCsv();
00257
00258 $this->_prepareDownloadResponse($fileName, $content);
00259 }
00260
00261
00262
00263
00264 public function exportXmlAction()
00265 {
00266 $fileName = 'customers.xml';
00267 $content = $this->getLayout()->createBlock('adminhtml/customer_grid')
00268 ->getXml();
00269
00270 $this->_prepareDownloadResponse($fileName, $content);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
00284 {
00285 $this->_prepareDownloadResponse($fileName, $content, $contentType);
00286 }
00287
00288
00289
00290
00291
00292 public function ordersAction() {
00293 $this->_initCustomer();
00294 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_edit_tab_orders')->toHtml());
00295 }
00296
00297
00298
00299
00300
00301 public function lastOrdersAction() {
00302 $this->_initCustomer();
00303 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_edit_tab_view_orders')->toHtml());
00304 }
00305
00306
00307
00308
00309
00310 public function newsletterAction()
00311 {
00312 $this->_initCustomer();
00313 $subscriber = Mage::getModel('newsletter/subscriber')
00314 ->loadByCustomer(Mage::registry('current_customer'));
00315
00316 Mage::register('subscriber', $subscriber);
00317 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_edit_tab_newsletter_grid')->toHtml());
00318 }
00319
00320 public function wishlistAction()
00321 {
00322 $this->_initCustomer();
00323 $customer = Mage::registry('current_customer');
00324 if ($customer->getId()) {
00325 if($itemId = (int) $this->getRequest()->getParam('delete')) {
00326 try {
00327 Mage::getModel('wishlist/item')->load($itemId)
00328 ->delete();
00329 }
00330 catch (Exception $e) {
00331
00332 }
00333 }
00334 }
00335 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_edit_tab_wishlist')->toHtml());
00336 }
00337
00338
00339
00340
00341
00342 public function viewWishlistAction()
00343 {
00344 $this->_initCustomer();
00345 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/customer_edit_tab_view_wishlist')->toHtml());
00346 }
00347
00348
00349
00350
00351
00352
00353 public function cartAction()
00354 {
00355 $this->_initCustomer();
00356 $websiteId = $this->getRequest()->getParam('website_id');
00357
00358
00359 if ($deleteItemId = $this->getRequest()->getPost('delete')) {
00360 $quote = Mage::getModel('sales/quote')
00361 ->setWebsite(Mage::app()->getWebsite($websiteId))
00362 ->loadByCustomer(Mage::registry('current_customer'));
00363 $item = $quote->getItemById($deleteItemId);
00364 $quote->removeItem($deleteItemId);
00365 $quote->save();
00366 }
00367
00368 $this->getResponse()->setBody(
00369 $this->getLayout()->createBlock('adminhtml/customer_edit_tab_cart', '', array('website_id'=>$websiteId))
00370 ->toHtml()
00371 );
00372 }
00373
00374
00375
00376
00377
00378 public function viewCartAction()
00379 {
00380 $this->_initCustomer();
00381
00382 $this->getResponse()->setBody(
00383 $this->getLayout()->createBlock('adminhtml/customer_edit_tab_view_cart')
00384 ->setWebsiteId($this->getRequest()->getParam('website_id'))
00385 ->toHtml()
00386 );
00387 }
00388
00389
00390
00391
00392
00393
00394 public function cartsAction()
00395 {
00396 $this->_initCustomer();
00397 $this->getResponse()->setBody(
00398 $this->getLayout()->createBlock('adminhtml/customer_edit_tab_carts')->toHtml()
00399 );
00400 }
00401
00402 public function productReviewsAction()
00403 {
00404 $this->_initCustomer();
00405 $this->getResponse()->setBody(
00406 $this->getLayout()->createBlock('adminhtml/review_grid', 'admin.customer.reviews')
00407 ->setCustomerId(Mage::registry('current_customer')->getId())
00408 ->setUseAjax(true)
00409 ->toHtml()
00410 );
00411 }
00412
00413 public function productTagsAction()
00414 {
00415 $this->_initCustomer();
00416 $this->getResponse()->setBody(
00417 $this->getLayout()->createBlock('adminhtml/customer_edit_tab_tag', 'admin.customer.tags')
00418 ->setCustomerId(Mage::registry('current_customer')->getId())
00419 ->setUseAjax(true)
00420 ->toHtml()
00421 );
00422 }
00423
00424 public function tagGridAction()
00425 {
00426 $this->_initCustomer();
00427 $this->getResponse()->setBody(
00428 $this->getLayout()->createBlock('adminhtml/customer_edit_tab_tag', 'admin.customer.tags')
00429 ->setCustomerId(Mage::registry('current_customer'))
00430 ->toHtml()
00431 );
00432 }
00433
00434 public function validateAction()
00435 {
00436 $response = new Varien_Object();
00437 $response->setError(0);
00438 $websiteId = Mage::app()->getStore()->getWebsiteId();
00439 $accountData = $this->getRequest()->getPost('account');
00440
00441
00442 $customer = Mage::getModel('customer/customer');
00443 if ($id = $this->getRequest()->getParam('id')) {
00444 $customer->load($id);
00445 $websiteId = $customer->getWebsiteId();
00446 }
00447 if (isset($accountData['website_id'])) {
00448 $websiteId = $accountData['website_id'];
00449 }
00450
00451 # Checking if we received email. If not - ERROR
00452 if( !($accountData['email']) ) {
00453 $response->setError(1);
00454 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__("Please fill in 'email' field."));
00455 $this->_initLayoutMessages('adminhtml/session');
00456 $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
00457 } else {
00458 # Trying to load customer with the same email and return error message
00459 # if customer with the same email address exisits
00460 $checkCustomer = Mage::getModel('customer/customer')
00461 ->setWebsiteId($websiteId);
00462 $checkCustomer->loadByEmail($accountData['email']);
00463 if( $checkCustomer->getId() && ($checkCustomer->getId() != $customer->getId()) ) {
00464 $response->setError(1);
00465 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Customer with the same email already exists.'));
00466 $this->_initLayoutMessages('adminhtml/session');
00467 $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
00468 }
00469 }
00470 $this->getResponse()->setBody($response->toJson());
00471 }
00472
00473 public function massSubscribeAction()
00474 {
00475 $customersIds = $this->getRequest()->getParam('customer');
00476 if(!is_array($customersIds)) {
00477 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s)'));
00478
00479 } else {
00480 try {
00481 foreach ($customersIds as $customerId) {
00482 $customer = Mage::getModel('customer/customer')->load($customerId);
00483 $customer->setIsSubscribed(true);
00484 $customer->save();
00485 }
00486 Mage::getSingleton('adminhtml/session')->addSuccess(
00487 Mage::helper('adminhtml')->__(
00488 'Total of %d record(s) were successfully updated', count($customersIds)
00489 )
00490 );
00491 } catch (Exception $e) {
00492 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00493 }
00494 }
00495 $this->_redirect('*/*/index');
00496 }
00497
00498 public function massUnsubscribeAction()
00499 {
00500 $customersIds = $this->getRequest()->getParam('customer');
00501 if(!is_array($customersIds)) {
00502 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s)'));
00503 } else {
00504 try {
00505 foreach ($customersIds as $customerId) {
00506 $customer = Mage::getModel('customer/customer')->load($customerId);
00507 $customer->setIsSubscribed(false);
00508 $customer->save();
00509 }
00510 Mage::getSingleton('adminhtml/session')->addSuccess(
00511 Mage::helper('adminhtml')->__(
00512 'Total of %d record(s) were successfully updated', count($customersIds)
00513 )
00514 );
00515 } catch (Exception $e) {
00516 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00517 }
00518 }
00519
00520 $this->_redirect('*/*/index');
00521 }
00522
00523 public function massDeleteAction()
00524 {
00525 $customersIds = $this->getRequest()->getParam('customer');
00526 if(!is_array($customersIds)) {
00527 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s)'));
00528 } else {
00529 try {
00530 $customer = Mage::getModel('customer/customer');
00531 foreach ($customersIds as $customerId) {
00532 $customer->reset()
00533 ->load($customerId)
00534 ->delete();
00535 }
00536 Mage::getSingleton('adminhtml/session')->addSuccess(
00537 Mage::helper('adminhtml')->__(
00538 'Total of %d record(s) were successfully deleted', count($customersIds)
00539 )
00540 );
00541 } catch (Exception $e) {
00542 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00543 }
00544 }
00545
00546 $this->_redirect('*/*/index');
00547 }
00548
00549 public function massAssignGroupAction()
00550 {
00551 $customersIds = $this->getRequest()->getParam('customer');
00552 if(!is_array($customersIds)) {
00553 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select customer(s)'));
00554 } else {
00555 try {
00556 foreach ($customersIds as $customerId) {
00557 $customer = Mage::getModel('customer/customer')->load($customerId);
00558 $customer->setGroupId($this->getRequest()->getParam('group'));
00559 $customer->save();
00560 }
00561 Mage::getSingleton('adminhtml/session')->addSuccess(
00562 Mage::helper('adminhtml')->__(
00563 'Total of %d record(s) were successfully updated', count($customersIds)
00564 )
00565 );
00566 } catch (Exception $e) {
00567 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00568 }
00569 }
00570
00571 $this->_redirect('*/*/index');
00572 }
00573
00574 protected function _isAllowed()
00575 {
00576 return Mage::getSingleton('admin/session')->isAllowed('customer/manage');
00577 }
00578 }