public function addAction() { if (method_exists($this, 'getAddForm')) { $form = $this->getAddForm(); } else { $form = $this->getForm(); } $classe = $this->getEntityClass(); $entity = new $classe(); $this->getEventManager()->trigger('getForm', $this, ['form' => $form, 'entityClass' => $this->getEntityClass(), 'id' => 0, 'entity' => $entity]); $form->bind($entity); $redirectUrl = $this->url()->fromRoute($this->getActionRoute(), [], true); $prg = $this->fileprg($form, $redirectUrl, true); if ($prg instanceof Response) { return $prg; } elseif ($prg === false) { if ($form->has('guid')) { $guid = $form->get('guid')->getValue(); if (!$guid) { $guid = IdGenerator::generate(); $form->get('guid')->setValue($guid); } } $this->getEventManager()->trigger('getForm', $this, ['form' => $form, 'entityClass' => $this->getEntityClass(), 'entity' => $entity]); return ['entityForm' => $form, 'entity' => $entity, 'history' => $this->setHistory()]; } $this->createServiceEvent()->setEntityClass($this->getEntityClass())->setDescription("Account Created"); $this->getEventManager()->trigger('add', $this, ['form' => $form, 'entityClass' => $this->getEntityClass(), 'entity' => $entity]); $savedEntity = $this->getEntityService()->save($form, $entity); if ($savedEntity && $savedEntity instanceof Account) { $name = $savedEntity->getName(); $this->getServiceEvent()->setEntityId($savedEntity->getId())->setMessage("Account : {$name} was created."); $this->logEvent("AddAction.post"); } else { return ['entityForm' => $form, 'entity' => $entity, 'history' => $this->getHistory()]; } $this->flashMessenger()->addSuccessMessage($this->getServiceLocator()->get('translator')->translate($this->successAddMessage)); if ($this->needAddOther($form)) { $action = 'add'; } else { $action = 'list'; } return $this->getHistoricalRedirect('list'); }
/** * Handle Import & Save (Stage 5) * * @param array $post * @param Form $form */ protected function handleImport($post, $form) { $outcome = true; $request = $this->getRequest(); $fields = $form->getLeadAttributes(); $this->results['fields'] = $fields; $form->setData($post); if ($form->isValid()) { if (isset($post['leads'])) { $accountName = empty($post['Company']) ? false : $post['Company']; $account_id = false; if ($accountName) { $account = $accountName ? $this->findAccount($accountName) : false; if (!$account) { $account = new Account(); $guid = IdGenerator::generate(); $account->setName($accountName)->setDescription($accountName); $account->setGuid($guid); try { $em = $this->getEntityManager(); $em->persist($account); $em->flush(); $account_id = $account->getId(); } catch (\Exception $e) { $account = false; $outcome = false; $this->flashMessenger()->addErrorMessage($e->getMessage()); } } else { $account_id = $account->getId(); } } else { $account = false; } $all_leads_json = $post['leads']; $all_leads = @\Zend\Json\Json::decode($all_leads_json, \Zend\Json\Json::TYPE_ARRAY); $leads = isset($all_leads['valid']) ? $all_leads['valid'] : false; $company = $post['Company']; if ($leads && is_array($leads)) { $leads = array_values($leads); try { $this->createServiceEvent()->setEntityClass($this->getEntityClass())->setDescription("Lead Import"); $em = $this->getEntityManager(); for ($i = 1; $i <= count($leads); ++$i) { $extract = isset($leads[$i - 1]) ? $leads[$i - 1] : false; if ($extract) { $lead = null; $this->createServiceEvent()->setEntityClass($this->getEntityClass())->setDescription("Lead Import"); $entity = new Lead(); $lead = $this->hydrate($entity, $extract); if ($lead) { try { $em = $this->getEntityManager(); $em->persist($lead); $em->flush(); $lead_id = $lead->getId(); if ($account_id && $lead instanceof Lead) { $account = $this->findAccount($account_id, 'id'); $account->addLead($lead); } $message = 'Lead #' . $lead_id . ' was imported.'; $this->getServiceEvent()->setEntityId($lead_id)->setMessage($message); $this->logEvent("ImportAction.post"); $em->flush(); $em->clear(); } catch (\Exception $e) { $this->logError($e); if ($outcome) { $this->flashMessenger()->addErrorMessage($e->getMessage()); } $outcome = false; $em->clear(); } } elseif ($outcome) { $outcome = false; } } } $em->flush(); $em->clear(); } catch (\Exception $e) { $this->logError($e); if ($outcome) { $this->flashMessenger()->addErrorMessage($e->getMessage()); } $outcome = false; } } if (!$outcome) { $message = "One or more records could not be properly imported."; $this->flashMessenger()->addErrorMessage($message); } else { $message = count($leads) . " record(s) were imported."; $this->flashMessenger()->addSuccessMessage($message); $this->stage = 5; } } else { $outcome = false; } } else { $outcome = false; } return $outcome; }