public function execute($request) { $id = trim($request->getParameter('hdnId')); $name = trim($request->getParameter('txtName')); $description = trim($request->getParameter('txtDescription')); $unitId = trim($request->getParameter('txtUnit_Id')); $parentId = trim($request->getParameter('hdnParent', null)); $subunit = new Subunit(); $subunit->setId($id); $subunit->setName($name); $subunit->setDescription($description); $subunit->setUnitId($unitId); $object = new stdClass(); try { if (empty($parentId)) { $result = $this->getCompanyStructureService()->saveSubunit($subunit); } else { $parentSubunit = $this->getCompanyStructureService()->getSubunitById($parentId); $result = $this->getCompanyStructureService()->addSubunit($parentSubunit, $subunit); } if ($result) { $object->affectedId = $subunit->getId(); $object->messageType = 'success'; $object->message = __(TopLevelMessages::SAVE_SUCCESS); } else { $object->messageType = 'warning'; $object->message = __('Failed to Save'); } } catch (Exception $e) { $object->messageType = 'warning'; $object->message = __('Name Already Exists'); } @ob_clean(); return $this->renderText(json_encode($object)); }
public function addSubunit(Subunit $parentSubunit, Subunit $subunit) { try { $subunit->setId(0); $subunit->getNode()->insertAsLastChildOf($parentSubunit); $parentSubunit->setRgt($parentSubunit->getRgt() + 2); $parentSubunit->save(); return true; } catch (Exception $e) { throw new DaoException($e->getMessage()); } }