/** * @return \Zend\Http\Response|ViewModel * @throws \Exception */ public function indexAction() { $id = (int) $this->params()->fromRoute('id', 0); $action = $this->params()->fromQuery('action', ''); if ($action == 'delete' && $id > 0) { $this->accountTypeTable()->deleteAccountType($id); $this->flashMessenger()->addInfoMessage('Delete successful!'); return $this->redirect()->toRoute("account_type"); } $parentAccountType = new AccountType(); $edit = false; if ($id > 0) { $accountType = $this->accountTypeTable()->getAccountType($id); if ($accountType->getParentTypeId()) { $parentAccountType = $this->accountTypeTable()->getAccountType($accountType->getParentTypeId()); } $edit = true; } else { $accountType = new AccountType(); } $accountTypes = $this->accountTypeTable()->getChildren(); $helper = new AccountTypeHelper(); $form = $helper->getForm($this->baseTypeCombo(), $this->defaultStatusCombo()); if ($action == 'clone') { $edit = false; $id = 0; $accountType->setAccountTypeId(0); } $form->bind($accountType); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $this->accountTypeTable()->saveAccountType($accountType); $this->flashMessenger()->addSuccessMessage('Save successful!'); return $this->redirect()->toRoute("account_type"); } } return new ViewModel(array('id' => $id, 'accountType' => $accountTypes, 'accountTypes' => $accountTypes, 'form' => $form, 'isEdit' => $edit, 'parent' => $parentAccountType)); }
/** * @param AccountType $accountType * @return AccountType */ public function saveAccountType(AccountType $accountType) { $id = $accountType->getAccountTypeId(); $data = $accountType->getArrayCopy(); if ($id > 0) { $this->update($data, array('accountTypeId' => $id)); } else { unset($data['accountTypeId']); $this->insert($data); } if (!$accountType->getAccountTypeId()) { $accountType->setAccountTypeId($this->getLastInsertValue()); } return $accountType; }