/**
  * @param Route $route
  * @return Route
  */
 public function saveRoute(Route $route)
 {
     $id = $route->getRouteId();
     $data = $route->getArrayCopy();
     if ($id > 0) {
         $this->update($data, array('routeId' => $id));
     } else {
         unset($data['routeId']);
         $this->insert($data);
     }
     if (!$route->getRouteId()) {
         $route->setRouteId($this->getLastInsertValue());
     }
     return $route;
 }
 /**
  * @return \Zend\Http\Response|ViewModel
  */
 public function detailAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     $action = $this->params()->fromQuery('action', '');
     $helper = new RouteHelper($this->getDbAdapter());
     $form = $helper->getForm();
     $route = $this->routeTable()->getRoute($id);
     $isEdit = true;
     if (!$route) {
         $isEdit = false;
         $route = new Route();
     }
     $permissions = $this->routePermissionTable()->grantRoles($id);
     if ($action == 'clone') {
         $isEdit = false;
         $id = 0;
         $route->setRouteId(0);
     }
     $form->bind($route);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post_data = $request->getPost()->toArray();
         $form->setData($post_data);
         $form->setInputFilter($helper->getInputFilter($id));
         if ($form->isValid()) {
             $db = $this->routeTable()->getAdapter();
             $conn = $db->getDriver()->getConnection();
             try {
                 $conn->beginTransaction();
                 $routeId = $this->routeTable()->saveRoute($route)->getRouteId();
                 $grant_roles = isset($post_data['grant_roles']) ? $post_data['grant_roles'] : array();
                 $this->routePermissionTable()->saveRoutePermission($routeId, $grant_roles);
                 $conn->commit();
                 $this->flashMessenger()->addSuccessMessage('Save successful');
             } catch (\Exception $ex) {
                 $conn->rollback();
                 $this->flashMessenger()->addErrorMessage($ex->getMessage());
             }
             return $this->redirect()->toRoute('route');
         }
     }
     return new ViewModel(array('form' => $form, 'id' => $id, 'isEdit' => $isEdit, 'roles' => $this->roleTreeData(), 'permissions' => $permissions));
 }