diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-06-13 15:37:02 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | baa6979194573855b260593094983c33ec338dc7 (patch) | |
tree | 9e67e798ac6ad402e77ad8d7ee6c6621184a0255 /application/front/controller | |
parent | 9c75f877935fa6adec951a4d8d32b328aaab314f (diff) | |
download | Shaarli-baa6979194573855b260593094983c33ec338dc7.tar.gz Shaarli-baa6979194573855b260593094983c33ec338dc7.tar.zst Shaarli-baa6979194573855b260593094983c33ec338dc7.zip |
Improve ManageTagController coverage and error handling
Diffstat (limited to 'application/front/controller')
-rw-r--r-- | application/front/controller/admin/ManageShaareController.php (renamed from application/front/controller/admin/PostBookmarkController.php) | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/application/front/controller/admin/PostBookmarkController.php b/application/front/controller/admin/ManageShaareController.php index f3ee5dea..620bbc40 100644 --- a/application/front/controller/admin/PostBookmarkController.php +++ b/application/front/controller/admin/ManageShaareController.php | |||
@@ -16,7 +16,7 @@ use Slim\Http\Response; | |||
16 | * | 16 | * |
17 | * Slim controller used to handle Shaarli create or edit bookmarks. | 17 | * Slim controller used to handle Shaarli create or edit bookmarks. |
18 | */ | 18 | */ |
19 | class PostBookmarkController extends ShaarliAdminController | 19 | class ManageShaareController extends ShaarliAdminController |
20 | { | 20 | { |
21 | /** | 21 | /** |
22 | * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL | 22 | * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL |
@@ -33,7 +33,7 @@ class PostBookmarkController extends ShaarliAdminController | |||
33 | 33 | ||
34 | /** | 34 | /** |
35 | * GET /admin/shaare - Displays the bookmark form for creation. | 35 | * GET /admin/shaare - Displays the bookmark form for creation. |
36 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. | 36 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. |
37 | */ | 37 | */ |
38 | public function displayCreateForm(Request $request, Response $response): Response | 38 | public function displayCreateForm(Request $request, Response $response): Response |
39 | { | 39 | { |
@@ -97,14 +97,17 @@ class PostBookmarkController extends ShaarliAdminController | |||
97 | */ | 97 | */ |
98 | public function displayEditForm(Request $request, Response $response, array $args): Response | 98 | public function displayEditForm(Request $request, Response $response, array $args): Response |
99 | { | 99 | { |
100 | $id = $args['id']; | 100 | $id = $args['id'] ?? ''; |
101 | try { | 101 | try { |
102 | if (false === ctype_digit($id)) { | 102 | if (false === ctype_digit($id)) { |
103 | throw new BookmarkNotFoundException(); | 103 | throw new BookmarkNotFoundException(); |
104 | } | 104 | } |
105 | $bookmark = $this->container->bookmarkService->get($id); // Read database | 105 | $bookmark = $this->container->bookmarkService->get((int) $id); // Read database |
106 | } catch (BookmarkNotFoundException $e) { | 106 | } catch (BookmarkNotFoundException $e) { |
107 | $this->saveErrorMessage(t('Bookmark not found')); | 107 | $this->saveErrorMessage(sprintf( |
108 | t('Bookmark with identifier %s could not be found.'), | ||
109 | $id | ||
110 | )); | ||
108 | 111 | ||
109 | return $this->redirect($response, '/'); | 112 | return $this->redirect($response, '/'); |
110 | } | 113 | } |
@@ -177,10 +180,10 @@ class PostBookmarkController extends ShaarliAdminController | |||
177 | { | 180 | { |
178 | $this->checkToken($request); | 181 | $this->checkToken($request); |
179 | 182 | ||
180 | $ids = escape(trim($request->getParam('id'))); | 183 | $ids = escape(trim($request->getParam('id') ?? '')); |
181 | if (strpos($ids, ' ') !== false) { | 184 | if (empty($ids) || strpos($ids, ' ') !== false) { |
182 | // multiple, space-separated ids provided | 185 | // multiple, space-separated ids provided |
183 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'strlen')); | 186 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit')); |
184 | } else { | 187 | } else { |
185 | $ids = [$ids]; | 188 | $ids = [$ids]; |
186 | } | 189 | } |
@@ -193,16 +196,28 @@ class PostBookmarkController extends ShaarliAdminController | |||
193 | } | 196 | } |
194 | 197 | ||
195 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | 198 | $formatter = $this->container->formatterFactory->getFormatter('raw'); |
199 | $count = 0; | ||
196 | foreach ($ids as $id) { | 200 | foreach ($ids as $id) { |
197 | $id = (int) $id; | 201 | try { |
198 | // TODO: check if it exists | 202 | $bookmark = $this->container->bookmarkService->get((int) $id); |
199 | $bookmark = $this->container->bookmarkService->get($id); | 203 | } catch (BookmarkNotFoundException $e) { |
204 | $this->saveErrorMessage(sprintf( | ||
205 | t('Bookmark with identifier %s could not be found.'), | ||
206 | $id | ||
207 | )); | ||
208 | |||
209 | continue; | ||
210 | } | ||
211 | |||
200 | $data = $formatter->format($bookmark); | 212 | $data = $formatter->format($bookmark); |
201 | $this->container->pluginManager->executeHooks('delete_link', $data); | 213 | $this->container->pluginManager->executeHooks('delete_link', $data); |
202 | $this->container->bookmarkService->remove($bookmark, false); | 214 | $this->container->bookmarkService->remove($bookmark, false); |
215 | ++ $count; | ||
203 | } | 216 | } |
204 | 217 | ||
205 | $this->container->bookmarkService->save(); | 218 | if ($count > 0) { |
219 | $this->container->bookmarkService->save(); | ||
220 | } | ||
206 | 221 | ||
207 | // If we are called from the bookmarklet, we must close the popup: | 222 | // If we are called from the bookmarklet, we must close the popup: |
208 | if ($request->getParam('source') === 'bookmarklet') { | 223 | if ($request->getParam('source') === 'bookmarklet') { |
@@ -213,6 +228,11 @@ class PostBookmarkController extends ShaarliAdminController | |||
213 | return $this->redirect($response, '/'); | 228 | return $this->redirect($response, '/'); |
214 | } | 229 | } |
215 | 230 | ||
231 | /** | ||
232 | * Helper function used to display the shaare form whether it's a new or existing bookmark. | ||
233 | * | ||
234 | * @param array $link data used in template, either from parameters or from the data store | ||
235 | */ | ||
216 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response | 236 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response |
217 | { | 237 | { |
218 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); | 238 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); |