From ef4ba5a3480e7b6b0a59757ad95f227129265bc8 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 20 May 2014 21:46:49 +0200 Subject: [PATCH] Added ability to access posts using their hashes --- src/Controllers/PostController.php | 134 ++++++++++++++++------------- src/CustomMarkdown.php | 2 +- src/Views/post/post-edit.phtml | 2 +- src/Views/post/post-small.phtml | 4 +- src/Views/post/post-view.phtml | 24 +++--- src/Views/static/static-main.phtml | 5 +- src/routes.php | 24 +++--- 7 files changed, 105 insertions(+), 90 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 6e5750db..387b7216 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -90,19 +90,20 @@ class PostController extends AbstractController $this->listView('order:random', $page, 'random'); } - public function toggleTagAction($id, $tag, $enable) + public function toggleTagAction($identifier, $tag, $enable) { Access::assert(new Privilege( Privilege::MassTag, - Access::getIdentity(PostModel::getById($id)->getUploader()))); + Access::getIdentity(PostModel::getByIdOrName($identifier)->getUploader()))); - Api::run( - new TogglePostTagJob(), - [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_TAG_NAME => $tag, - JobArgs::ARG_NEW_STATE => $enable, - ]); + $jobArgs = + [ + JobArgs::ARG_TAG_NAME => $tag, + JobArgs::ARG_NEW_STATE => $enable, + ]; + $jobArgs = $this->appendPostIdentifierArgument($args, $identifier); + + Api::run(new TogglePostTagJob(), $jobArgs); if ($this->isAjax()) $this->renderAjax(); @@ -147,18 +148,19 @@ class PostController extends AbstractController $this->redirectToPostList(); } - public function editView($id) + public function editView($identifier) { - $post = Api::run(new GetPostJob(), [ - JobArgs::ARG_POST_ID => $id]); + $jobArgs = []; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + $post = Api::run(new GetPostJob(), $jobArgs); $context = Core::getContext()->transport->post = $post; $this->renderView('post-edit'); } - public function editAction($id) + public function editAction($identifier) { - $post = PostModel::getByIdOrName($id); + $post = PostModel::getByIdOrName($identifier); $editToken = InputHelper::get('edit-token'); if ($editToken != $post->getEditToken()) @@ -166,12 +168,12 @@ class PostController extends AbstractController $jobArgs = [ - JobArgs::ARG_POST_ID => $id, JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'), JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')), JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'), JobArgs::ARG_NEW_RELATED_POST_IDS => $this->splitPostIds(InputHelper::get('relations')), ]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); if (!empty(InputHelper::get('url'))) { @@ -202,97 +204,100 @@ class PostController extends AbstractController if ($this->isAjax()) $this->renderAjax(); else - $this->redirectToGenericView($id); + $this->redirectToGenericView($identifier); } - public function flagAction($id) + public function flagAction($identifier) { - Api::run(new FlagPostJob(), [JobArgs::ARG_POST_ID => $id]); + $jobArgs = []; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new FlagPostJob(), $jobArgs); if ($this->isAjax()) $this->renderAjax(); else - $this->redirectToGenericView($id); + $this->redirectToGenericView($identifier); } - public function hideAction($id) + public function hideAction($identifier) { - Api::run(new TogglePostVisibilityJob(), [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_NEW_STATE => false]); - $this->redirectToGenericView($id); + $jobArgs = [JobArgs::ARG_NEW_STATE => false]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new TogglePostVisibilityJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function unhideAction($id) + public function unhideAction($identifier) { - Api::run(new TogglePostVisibilityJob(), [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_NEW_STATE => true]); - $this->redirectToGenericView($id); + $jobArgs = [JobArgs::ARG_NEW_STATE => true]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new TogglePostVisibilityJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function deleteAction($id) + public function deleteAction($identifier) { - Api::run(new DeletePostJob(), [ - JobArgs::ARG_POST_ID => $id]); + $jobArgs = []; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new DeletePostJob(), $jobArgs); $this->redirectToPostList(); } - public function addFavoriteAction($id) + public function addFavoriteAction($identifier) { - Api::run(new TogglePostFavoriteJob(), [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_NEW_STATE => true]); - $this->redirectToGenericView($id); + $jobArgs = [JobArgs::ARG_NEW_STATE => true]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new TogglePostFavoriteJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function removeFavoriteAction($id) + public function removeFavoriteAction($identifier) { - Api::run(new TogglePostFavoriteJob(), [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_NEW_STATE => false]); - $this->redirectToGenericView($id); + $jobArgs = [JobArgs::ARG_NEW_STATE => false]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new TogglePostFavoriteJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function scoreAction($id, $score) + public function scoreAction($identifier, $score) { - Api::run(new ScorePostJob(), [ - JobArgs::ARG_POST_ID => $id, - JobArgs::ARG_NEW_POST_SCORE => $score]); - $this->redirectToGenericView($id); + $jobArgs = [JobArgs::ARG_NEW_POST_SCORE => $score]; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new ScorePostJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function featureAction($id) + public function featureAction($identifier) { - Api::run(new FeaturePostJob(), [ - JobArgs::ARG_POST_ID => $id]); - $this->redirectToGenericView($id); + $jobArgs = []; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + Api::run(new FeaturePostJob(), $jobArgs); + $this->redirectToGenericView($identifier); } - public function genericView($id) + public function genericView($identifier) { $context = Core::getContext(); - $post = Api::run(new GetPostJob(), [ - JobArgs::ARG_POST_ID => $id]); + $jobArgs = []; + $jobArgs = $this->appendPostIdentifierArgument($jobArgs, $identifier); + $post = Api::run(new GetPostJob(), $jobArgs); try { $context->transport->lastSearchQuery = InputHelper::get('last-search-query'); list ($prevPostId, $nextPostId) = PostSearchService::getPostIdsAround( - $context->transport->lastSearchQuery, $id); + $context->transport->lastSearchQuery, $identifier); } #search for some reason was invalid, e.g. tag was deleted in the meantime catch (Exception $e) { $context->transport->lastSearchQuery = ''; list ($prevPostId, $nextPostId) = - PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $id); + PostSearchService::getPostIdsAround($context->transport->lastSearchQuery, $identifier); } - //todo: - //move these to PostEntity when implementing ApiController $isUserFavorite = Auth::getCurrentUser()->hasFavorited($post); $userScore = Auth::getCurrentUser()->getScore($post); $flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', [])); @@ -336,6 +341,15 @@ class PostController extends AbstractController } + private function appendPostIdentifierArgument(array $arguments, $postIdentifier) + { + if (is_numeric($postIdentifier)) + $arguments[JobArgs::ARG_POST_ID] = $postIdentifier; + else + $arguments[JobArgs::ARG_POST_NAME] = $postIdentifier; + return $arguments; + } + private function splitPostIds($string) { $ids = preg_split('/\D/', trim($string)); @@ -358,10 +372,10 @@ class PostController extends AbstractController $this->redirect(\Chibi\Router::linkTo(['PostController', 'listView'])); } - private function redirectToGenericView($id) + private function redirectToGenericView($identifier) { $this->redirect(\Chibi\Router::linkTo( ['PostController', 'genericView'], - ['id' => $id])); + ['identifier' => $identifier])); } } diff --git a/src/CustomMarkdown.php b/src/CustomMarkdown.php index 3cc4d7e3..b92be4b7 100644 --- a/src/CustomMarkdown.php +++ b/src/CustomMarkdown.php @@ -127,7 +127,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doPosts($text) { - $link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['id' => '_post_']); + $link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['identifier' => '_post_']); return preg_replace_callback('/(?:(?hashPart('' . $x[0] . ''); diff --git a/src/Views/post/post-edit.phtml b/src/Views/post/post-edit.phtml index 8dcc2dec..3bb48031 100644 --- a/src/Views/post/post-edit.phtml +++ b/src/Views/post/post-edit.phtml @@ -1,7 +1,7 @@
$this->context->transport->post->getId()]) ?>" enctype="multipart/form-data" class="edit-post"> diff --git a/src/Views/post/post-small.phtml b/src/Views/post/post-small.phtml index 40f5f55f..9f735ed2 100644 --- a/src/Views/post/post-small.phtml +++ b/src/Views/post/post-small.phtml @@ -30,7 +30,7 @@ if ($masstag) getSettings()->hasEnabledPostTagTitles()): ?> title="context->post->getTags()) ?>" - href=" $this->context->post->getId()]) ?>"> + href=" $this->context->post->getId()]) ?>"> 0; context->transport->nextPostId): ?> + ['identifier' => $this->context->transport->nextPostId]) ?>"> @@ -45,7 +45,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; context->transport->prevPostId): ?> + ['identifier' => $this->context->transport->prevPostId]) ?>"> @@ -155,7 +155,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; { return \Chibi\Router::linkTo( ['PostController', 'scoreAction'], - ['id' => $this->context->transport->post->getId(), 'score' => $score]); + ['identifier' => $this->context->transport->post->getId(), 'score' => $score]); } ?> 0; + ['identifier' => $this->context->transport->post->getId()]) ?>"> Add to favorites @@ -219,7 +219,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; + ['identifier' => $this->context->transport->post->getId()]) ?>"> Remove from favorites @@ -231,7 +231,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
+ ['identifier' => $this->context->transport->post->getId()]) ?>"> Edit @@ -264,7 +264,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
  • + ['identifier' => $relatedPost->getId()]) ?>">
  • @@ -286,7 +286,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Feature on main page', 'simple-action' => \Chibi\Router::linkTo( ['PostController', 'featureAction'], - ['id' => $this->context->transport->post->getId()]), + ['identifier' => $this->context->transport->post->getId()]), 'data-confirm-text' => 'Are you sure you want to feature this post on the main page?', 'data-redirect-url' => \Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']), ]; @@ -313,7 +313,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Flag for moderator attention', 'simple-action' => \Chibi\Router::linkTo( ['PostController', 'flagAction'], - ['id' => $this->context->transport->post->getId()]), + ['identifier' => $this->context->transport->post->getId()]), 'data-confirm-text' => 'Are you sure you want to flag this post?', ]; } @@ -331,7 +331,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Unhide', 'simple-action' => \Chibi\Router::linkTo( ['PostController', 'unhideAction'], - ['id' => $this->context->transport->post->getId()]), + ['identifier' => $this->context->transport->post->getId()]), ]; } else @@ -342,7 +342,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Hide', 'simple-action' => \Chibi\Router::linkTo( ['PostController', 'hideAction'], - ['id' => $this->context->transport->post->getId()]), + ['identifier' => $this->context->transport->post->getId()]), ]; } } @@ -357,7 +357,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; 'text' => 'Delete', 'simple-action' => \Chibi\Router::linkTo( ['PostController', 'deleteAction'], - ['id' => $this->context->transport->post->getId()]), + ['identifier' => $this->context->transport->post->getId()]), 'data-confirm-text' => 'Are you sure you want to delete this post?', 'data-redirect-url' => \Chibi\Router::linkTo(['PostController', 'listView']), ]; diff --git a/src/Views/static/static-main.phtml b/src/Views/static/static-main.phtml index 48cbf3d4..d52deab8 100644 --- a/src/Views/static/static-main.phtml +++ b/src/Views/static/static-main.phtml @@ -18,8 +18,9 @@ $this->assets->addStylesheet('static-main.css');
    context->transport->post = $this->context->featuredPost; - $this->context->imageLink = \Chibi\Router::linkTo(['PostController', 'genericView'], [ - 'id' => $this->context->featuredPost->getId()]); + $this->context->imageLink = \Chibi\Router::linkTo( + ['PostController', 'genericView'], + ['identifier' => $this->context->featuredPost->getId()]); ?> renderExternal('post-file-render') ?> diff --git a/src/routes.php b/src/routes.php index 3ae89c14..14d21955 100644 --- a/src/routes.php +++ b/src/routes.php @@ -32,9 +32,9 @@ $postValidation = \Chibi\Router::register(['PostController', 'uploadView'], 'GET', '/posts/upload', $postValidation); \Chibi\Router::register(['PostController', 'uploadAction'], 'POST', '/posts/upload', $postValidation); -\Chibi\Router::register(['PostController', 'editView'], 'GET', '/post/{id}/edit', $postValidation); -\Chibi\Router::register(['PostController', 'editAction'], 'POST', '/post/{id}/edit', $postValidation); -\Chibi\Router::register(['PostController', 'deleteAction'], null, '/post/{id}/delete', $postValidation); +\Chibi\Router::register(['PostController', 'editView'], 'GET', '/post/{identifier}/edit', $postValidation); +\Chibi\Router::register(['PostController', 'editAction'], 'POST', '/post/{identifier}/edit', $postValidation); +\Chibi\Router::register(['PostController', 'deleteAction'], null, '/post/{identifier}/delete', $postValidation); \Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}', $postValidation); \Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{page}', $postValidation); @@ -49,18 +49,18 @@ $postValidation = \Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted', $postValidation); \Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted/{page}', $postValidation); -\Chibi\Router::register(['PostController', 'genericView'], 'GET', '/post/{id}', $postValidation); +\Chibi\Router::register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation); \Chibi\Router::register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation); \Chibi\Router::register(['PostController', 'thumbnailView'], 'GET', '/post/{name}/thumb', $postValidation); -\Chibi\Router::register(['PostController', 'toggleTagAction'], null, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation); -\Chibi\Router::register(['PostController', 'flagAction'], null, '/post/{id}/flag', $postValidation); -\Chibi\Router::register(['PostController', 'hideAction'], null, '/post/{id}/hide', $postValidation); -\Chibi\Router::register(['PostController', 'unhideAction'], null, '/post/{id}/unhide', $postValidation); -\Chibi\Router::register(['PostController', 'removeFavoriteAction'], null, '/post/{id}/rem-fav', $postValidation); -\Chibi\Router::register(['PostController', 'addFavoriteAction'], null, '/post/{id}/add-fav', $postValidation); -\Chibi\Router::register(['PostController', 'scoreAction'], null, '/post/{id}/score/{score}', $postValidation); -\Chibi\Router::register(['PostController', 'featureAction'], null, '/post/{id}/feature', $postValidation); +\Chibi\Router::register(['PostController', 'toggleTagAction'], null, '/post/{identifier}/toggle-tag/{tag}/{enable}', $postValidation); +\Chibi\Router::register(['PostController', 'flagAction'], null, '/post/{identifier}/flag', $postValidation); +\Chibi\Router::register(['PostController', 'hideAction'], null, '/post/{identifier}/hide', $postValidation); +\Chibi\Router::register(['PostController', 'unhideAction'], null, '/post/{identifier}/unhide', $postValidation); +\Chibi\Router::register(['PostController', 'removeFavoriteAction'], null, '/post/{identifier}/rem-fav', $postValidation); +\Chibi\Router::register(['PostController', 'addFavoriteAction'], null, '/post/{identifier}/add-fav', $postValidation); +\Chibi\Router::register(['PostController', 'scoreAction'], null, '/post/{identifier}/score/{score}', $postValidation); +\Chibi\Router::register(['PostController', 'featureAction'], null, '/post/{identifier}/feature', $postValidation); $commentValidation = [