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