/** * Saves a single customer. If no customer id passed, * the save function creates a new customer model and persist * it by the shopware model manager. * The sub models billing, shipping and debit will be filled * by the passed parameter arrays billing, shipping and debit. */ public function saveAction() { $id = $this->Request()->getParam('id', null); $paymentId = $this->Request()->getParam('paymentId', null); /** @var $namespace Enlight_Components_Snippet_Namespace */ $namespace = Shopware()->Snippets()->getNamespace('backend/customer'); //customer id passed? If this is the case the customer was edited if (!empty($id)) { //check if the user has the rights to update an existing customer if (!$this->_isAllowed('update', 'customer')) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $namespace->get('no_edit_rights', 'You do not have sufficient rights to edit a customer.'))); return; } $customer = $this->getRepository()->find($id); $paymentData = $this->getManager()->getRepository('Shopware\\Models\\Customer\\PaymentData')->findOneBy(array('customer' => $customer, 'paymentMean' => $paymentId)); } else { //check if the user has the rights to create a new customer if (!$this->_isAllowed('create', 'customer')) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $namespace->get('no_create_rights', 'You do not have sufficient rights to view create a customer.'))); return; } $customer = new Customer(); } try { $params = $this->Request()->getParams(); if (!$paymentData instanceof PaymentData && !empty($params['paymentData']) && array_filter($params['paymentData'][0])) { $paymentData = new PaymentData(); $customer->addPaymentData($paymentData); $paymentData->setPaymentMean($this->getManager()->getRepository('Shopware\\Models\\Payment\\Payment')->find($paymentId)); } $params = $this->prepareCustomerData($params, $customer, $paymentData); //set parameter to the customer model. $customer->fromArray($params); $password = $this->Request()->getParam('newPassword', null); //encode the password with md5 if (!empty($password)) { $customer->setPassword($password); } $this->getManager()->persist($customer); $this->getManager()->flush(); $this->View()->assign(array('success' => true, 'data' => $this->getCustomer($customer->getId()))); } catch (\Doctrine\ORM\ORMException $e) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $e->getMessage())); } }