public function addAction() { $viewModel = new ViewModel(); $form = $this->getServiceLocator()->get('Admin\\Form\\CustomerForm'); $request = $this->getRequest(); if ($request->isPost()) { $customer = new Customer(); $form->setInputFilter($customer->getInputFilter()); $form->setData($request->getPost()); $data = $request->getPost()->toArray(); $emails = array(); if (isset($data['emails'])) { foreach (array_filter($data['emails']) as $email) { if (preg_match("/([\\w\\-]+\\@[\\w\\-]+\\.[\\w\\-]+)/", $email)) { $emails[] = $email; } } $data['emails'] = json_encode($emails); } if (isset($data['addresses'])) { $data['addresses'] = json_encode(array_filter($data['addresses'])); } if (isset($data['phones'])) { $data['phones'] = json_encode(array_filter($data['phones'])); } $document = $data['identification']; $documentTypeId = $data['identification_type']; $classifications = $data['classification']; $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); $documentCompositeKeyValidator = new DocumentCompositeKeyValidator(array('adapter' => $dbAdapter, 'documentTypeId' => $documentTypeId)); $documentCompositeKeyValidatorResult = !$documentCompositeKeyValidator->isValid($document); $form->setData($data); if ($form->isValid() && $documentCompositeKeyValidatorResult) { $customer->exchangeArray($form->getData()); $customerId = $this->getCustomerTable()->save($customer); $this->getCustomerClassificationTable()->save($customerId, $classifications); return $this->redirect()->toRoute('admin/customer'); } else { $viewModel->setVariable('emails', json_decode($data['emails'])); $viewModel->setVariable('addresses', json_decode($data['addresses'])); $viewModel->setVariable('phones', json_decode($data['phones'])); $form->get("identification")->setMessages(array('error' => $documentCompositeKeyValidator->getMessage())); } } $viewModel->setVariables(array('form' => $form, 'config' => $this->config)); return $viewModel; }
public function save(Customer $customer) { $data = array('identification' => $customer->getIdentification(), 'identification_type' => $customer->getIdentificationType(), 'first_name' => $customer->getFirstName(), 'last_name' => $customer->getLastName(), 'emails' => $customer->getEmails(), 'addresses' => $customer->getAddresses(), 'phones' => $customer->getPhones(), 'zipcode' => $customer->getZipcode(), 'company' => $customer->getCompany(), 'manager' => $customer->getManager(), 'webpage' => $customer->getWebpage(), 'birthday' => date($customer->getBirthday(), time()), 'alias' => $customer->getAlias(), 'description' => $customer->getDescription(), 'city' => $customer->getCity()); $id = (int) $customer->getId(); $params = array(); $params['table'] = $this->tableGateway->getTableName(); $params['operation'] = 1; $params['data'] = json_encode($data); if ($id == 0) { $this->tableGateway->insert($data); $id = $this->tableGateway->getLastInsertValue(); if ($id) { $params['id'] = $id; $this->featureSet->getEventManager()->trigger("log.save", $this, $params); return $id; } else { return false; } } else { if ($this->get($id)) { $params['id'] = $id; $params['operation'] = 2; $this->featureSet->getEventManager()->trigger("log.save", $this, $params); $this->tableGateway->update($data, array('id' => $id)); return $id; } else { return false; } } }