| @@ 126-160 (lines=35) @@ | ||
| 123 | * @param Request $request |
|
| 124 | * @return array |
|
| 125 | */ |
|
| 126 | public function createAction(Request $request) |
|
| 127 | { |
|
| 128 | $admin = $this->getAdminFromRequest($request); |
|
| 129 | $admin->handleRequest($request, $this->getUser()); |
|
| 130 | // check permissions |
|
| 131 | $this->forward404IfNotAllowed($admin); |
|
| 132 | // create form |
|
| 133 | $form = $this->createForm($admin->getConfiguration()->getParameter('form'), $admin->create()); |
|
| 134 | $form->handleRequest($request); |
|
| 135 | ||
| 136 | if ($form->isValid()) { |
|
| 137 | // save entity |
|
| 138 | $success = $admin->save(); |
|
| 139 | ||
| 140 | if ($success) { |
|
| 141 | // if save is pressed, user stay on the edit view |
|
| 142 | if ($request->request->get('submit') == 'save') { |
|
| 143 | $editRoute = $admin->generateRouteName('edit'); |
|
| 144 | ||
| 145 | return $this->redirectToRoute($editRoute, [ |
|
| 146 | 'id' => $admin->getUniqueEntity()->getId(), |
|
| 147 | ]); |
|
| 148 | } else { |
|
| 149 | // otherwise user is redirected to list view |
|
| 150 | $listRoute = $admin->generateRouteName('list'); |
|
| 151 | ||
| 152 | return $this->redirectToRoute($listRoute); |
|
| 153 | } |
|
| 154 | } |
|
| 155 | } |
|
| 156 | return [ |
|
| 157 | 'admin' => $admin, |
|
| 158 | 'form' => $form->createView(), |
|
| 159 | ]; |
|
| 160 | } |
|
| 161 | ||
| 162 | /** |
|
| 163 | * Generic edit action. |
|
| @@ 171-202 (lines=32) @@ | ||
| 168 | * |
|
| 169 | * @return array|RedirectResponse |
|
| 170 | */ |
|
| 171 | public function editAction(Request $request) |
|
| 172 | { |
|
| 173 | $admin = $this->getAdminFromRequest($request); |
|
| 174 | $admin->handleRequest($request, $this->getUser()); |
|
| 175 | // check permissions |
|
| 176 | $this->forward404IfNotAllowed($admin); |
|
| 177 | // create form |
|
| 178 | $form = $this->createForm($admin->getConfiguration()->getParameter('form'), $admin->getUniqueEntity()); |
|
| 179 | $form->handleRequest($request); |
|
| 180 | $accessor = PropertyAccess::createPropertyAccessor(); |
|
| 181 | ||
| 182 | if ($form->isValid()) { |
|
| 183 | $admin->save(); |
|
| 184 | ||
| 185 | if ($request->request->get('submit') == 'save') { |
|
| 186 | $saveRoute = $admin->generateRouteName('edit'); |
|
| 187 | ||
| 188 | return $this->redirectToRoute($saveRoute, [ |
|
| 189 | 'id' => $accessor->getValue($admin->getUniqueEntity(), 'id'), |
|
| 190 | ]); |
|
| 191 | } else { |
|
| 192 | $listRoute = $admin->generateRouteName('list'); |
|
| 193 | // redirect to list |
|
| 194 | return $this->redirectToRoute($listRoute); |
|
| 195 | } |
|
| 196 | } |
|
| 197 | ||
| 198 | return [ |
|
| 199 | 'admin' => $admin, |
|
| 200 | 'form' => $form->createView(), |
|
| 201 | ]; |
|
| 202 | } |
|
| 203 | ||
| 204 | /** |
|
| 205 | * Generic delete action |
|