Improved support for no Javascript

This commit is contained in:
Marcin Kurczewski 2014-05-16 19:31:04 +02:00
parent ee3f2ca9d3
commit 5f246d7a51
10 changed files with 123 additions and 44 deletions

@ -1 +1 @@
Subproject commit 3c70aa336c0571d83557d5fe66ff1b5cc0f3478f
Subproject commit 19bb5ee3901cf6402d7507c30b8fc183c3fc0bcc

View file

@ -49,7 +49,7 @@ $context->layoutName
$context->viewName = '';
$context->transport = new StdClass;
session_start();
SessionHelper::init();
if (!Auth::isLoggedIn())
Auth::tryAutoLogin();

View file

@ -13,7 +13,7 @@ $(function()
var enable = !aDom.parents('.post').hasClass('tagged');
var url = $(this).attr('href');
url = url.replace('_enable_', enable ? '1' : '0');
url = url.replace(/\/[01]\/?$/, '/' + (enable ? '1' : '0'));
postJSON({ url: url }).success(function(data)
{
aDom.removeClass('inactive');

View file

@ -36,6 +36,7 @@ class CommentController
JobArgs::ARG_NEW_TEXT => InputHelper::get('text')
]);
}
$this->redirectToLastVisitedUrl();
}
public function editView($id)
@ -65,6 +66,7 @@ class CommentController
JobArgs::ARG_NEW_TEXT => InputHelper::get('text')
]);
}
$this->redirectToLastVisitedUrl();
}
public function deleteAction($id)
@ -74,5 +76,14 @@ class CommentController
[
JobArgs::ARG_COMMENT_ID => $id,
]);
$this->redirectToLastVisitedUrl();
}
private function redirectToLastVisitedUrl()
{
$lastUrl = SessionHelper::getLastVisitedUrl();
if ($lastUrl)
\Chibi\Util\Url::forward($lastUrl);
exit;
}
}

View file

@ -78,6 +78,8 @@ class PostController
JobArgs::ARG_TAG_NAME => $tag,
JobArgs::ARG_NEW_STATE => $enable,
]);
$this->redirectToLastVisitedUrl();
}
public function uploadView()
@ -161,11 +163,13 @@ class PostController
}
Api::run(new EditPostJob(), $jobArgs);
$this->redirectToGenericView($id);
}
public function flagAction($id)
{
Api::run(new FlagPostJob(), [JobArgs::ARG_POST_ID => $id]);
$this->redirectToGenericView($id);
}
public function hideAction($id)
@ -173,6 +177,7 @@ class PostController
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
$this->redirectToGenericView($id);
}
public function unhideAction($id)
@ -180,12 +185,14 @@ class PostController
Api::run(new TogglePostVisibilityJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
$this->redirectToGenericView($id);
}
public function deleteAction($id)
{
Api::run(new DeletePostJob(), [
JobArgs::ARG_POST_ID => $id]);
$this->redirectToGenericView($id);
}
public function addFavoriteAction($id)
@ -193,6 +200,7 @@ class PostController
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => true]);
$this->redirectToGenericView($id);
}
public function removeFavoriteAction($id)
@ -200,6 +208,7 @@ class PostController
Api::run(new TogglePostFavoriteJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_STATE => false]);
$this->redirectToGenericView($id);
}
public function scoreAction($id, $score)
@ -207,12 +216,14 @@ class PostController
Api::run(new ScorePostJob(), [
JobArgs::ARG_POST_ID => $id,
JobArgs::ARG_NEW_POST_SCORE => $score]);
$this->redirectToGenericView($id);
}
public function featureAction($id)
{
Api::run(new FeaturePostJob(), [
JobArgs::ARG_POST_ID => $id]);
$this->redirectToGenericView($id);
}
public function genericView($id)
@ -281,7 +292,7 @@ class PostController
$context->layoutName = 'layout-file';
}
protected function splitPostIds($string)
private function splitPostIds($string)
{
$ids = preg_split('/\D/', trim($string));
$ids = array_filter($ids, function($x) { return $x != ''; });
@ -290,11 +301,27 @@ class PostController
return $ids;
}
protected function splitTags($string)
private function splitTags($string)
{
$tags = preg_split('/[,;\s]+/', trim($string));
$tags = array_filter($tags, function($x) { return $x != ''; });
$tags = array_unique($tags);
return $tags;
}
private function redirectToLastVisitedUrl()
{
$lastUrl = SessionHelper::getLastVisitedUrl();
if ($lastUrl)
\Chibi\Util\Url::forward($lastUrl);
exit;
}
private function redirectToGenericView($id)
{
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(
['PostController', 'genericView'],
['id' => $id]));
exit;
}
}

View file

@ -94,25 +94,6 @@ class UserController
Messenger::message('Browsing settings updated!');
}
public function toggleSafetyAction($safety)
{
$safety = new PostSafety($safety);
$safety->validate();
$user = Auth::getCurrentUser();
$user->getSettings()->enableSafety($safety, !$user->getSettings()->hasEnabledSafety($safety));
$desiredSafety = $user->getSettings()->get(UserSettings::SETTING_SAFETY);
$user = Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_ENTITY => Auth::getCurrentUser(),
JobArgs::ARG_NEW_SETTINGS => [UserSettings::SETTING_SAFETY => $desiredSafety],
]);
Auth::setCurrentUser($user);
}
public function editAction($identifier)
{
$this->genericView($identifier, 'edit');
@ -143,6 +124,26 @@ class UserController
Messenger::message($message);
}
public function toggleSafetyAction($safety)
{
$safety = new PostSafety($safety);
$safety->validate();
$user = Auth::getCurrentUser();
$user->getSettings()->enableSafety($safety, !$user->getSettings()->hasEnabledSafety($safety));
$desiredSafety = $user->getSettings()->get(UserSettings::SETTING_SAFETY);
$user = Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_ENTITY => Auth::getCurrentUser(),
JobArgs::ARG_NEW_SETTINGS => [UserSettings::SETTING_SAFETY => $desiredSafety],
]);
Auth::setCurrentUser($user);
$this->redirectToLastVisitedUrl();
}
public function deleteAction($identifier)
{
$this->genericView($identifier, 'delete');
@ -165,6 +166,7 @@ class UserController
Api::run(
new FlagUserJob(),
$this->appendUserIdentifierArgument([], $identifier));
$this->redirectToGenericView($identifier);
}
public function banAction($identifier)
@ -174,6 +176,7 @@ class UserController
$this->appendUserIdentifierArgument([
JobArgs::ARG_NEW_STATE => true
], $identifier));
$this->redirectToGenericView($identifier);
}
public function unbanAction($identifier)
@ -183,6 +186,7 @@ class UserController
$this->appendUserIdentifierArgument([
JobArgs::ARG_NEW_STATE => true
], $identifier));
$this->redirectToGenericView($identifier);
}
public function acceptRegistrationAction($identifier)
@ -190,6 +194,7 @@ class UserController
Api::run(
new AcceptUserRegistrationJob(),
$this->appendUserIdentifierArgument([], $identifier));
$this->redirectToGenericView($identifier);
}
public function registrationView()
@ -328,4 +333,20 @@ class UserController
$arguments[JobArgs::ARG_USER_NAME] = $userIdentifier;
return $arguments;
}
private function redirectToLastVisitedUrl()
{
$lastUrl = SessionHelper::getLastVisitedUrl();
if ($lastUrl)
\Chibi\Util\Url::forward($lastUrl);
exit;
}
private function redirectToGenericView($identifier)
{
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(
['UserController', 'genericView'],
['identifier' => $identifier]));
exit;
}
}

View file

@ -1,6 +1,16 @@
<?php
class SessionHelper
{
public static function init()
{
session_start();
register_shutdown_function(function()
{
if (\Chibi\Util\Headers::getRequestMethod() == 'GET')
$_SESSION['last-visited-url'] = Core::getContext()->query;
});
}
public static function get($key, $default = null)
{
if (!isset($_SESSION[$key]))
@ -12,4 +22,11 @@ class SessionHelper
{
$_SESSION[$key] = $value;
}
public static function getLastVisitedUrl()
{
if (!isset($_SESSION['last-visited-url']))
return '';
return \Chibi\Util\Url::makeAbsolute($_SESSION['last-visited-url']);
}
}

View file

@ -32,7 +32,7 @@ if ($masstag)
href="<?= \Chibi\Router::linkTo(['PostController', 'toggleTagAction'], [
'id' => $this->context->post->getId(),
'tag' => $this->context->additionalInfo,
'enable' => '_enable_']) ?>"
'enable' => in_array('tagged', $classNames) ? '0' : '1']) ?>"
data-text-tagged="Tagged"
data-text-untagged="Untagged">

View file

@ -229,7 +229,9 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if ($canEditAnything): ?>
<div class="hl-option">
<a class="edit-post icon" href="#">
<a class="edit-post icon" href="<?= \Chibi\Router::linkTo(
['PostController', 'editView'],
['id' => $this->context->transport->post->getId()]) ?>">
<i class="icon-edit"></i>
<span>Edit</span>
</a>

View file

@ -30,7 +30,7 @@ $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'], 'POST', '/post/{id}/delete', $postValidation);
\Chibi\Router::register(['PostController', 'deleteAction'], null, '/post/{id}/delete', $postValidation);
\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}', $postValidation);
\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{query}', $postValidation);
@ -48,14 +48,14 @@ $postValidation =
\Chibi\Router::register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation);
\Chibi\Router::register(['PostController', 'thumbView'], 'GET', '/post/{name}/thumb', $postValidation);
\Chibi\Router::register(['PostController', 'toggleTagAction'], 'POST', '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
\Chibi\Router::register(['PostController', 'flagAction'], 'POST', '/post/{id}/flag', $postValidation);
\Chibi\Router::register(['PostController', 'hideAction'], 'POST', '/post/{id}/hide', $postValidation);
\Chibi\Router::register(['PostController', 'unhideAction'], 'POST', '/post/{id}/unhide', $postValidation);
\Chibi\Router::register(['PostController', 'removeFavoriteAction'], 'POST', '/post/{id}/rem-fav', $postValidation);
\Chibi\Router::register(['PostController', 'addFavoriteAction'], 'POST', '/post/{id}/add-fav', $postValidation);
\Chibi\Router::register(['PostController', 'scoreAction'], 'POST', '/post/{id}/score/{score}', $postValidation);
\Chibi\Router::register(['PostController', 'featureAction'], 'POST', '/post/{id}/feature', $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);
$commentValidation =
[
@ -66,7 +66,7 @@ $commentValidation =
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments', $commentValidation);
\Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', $commentValidation);
\Chibi\Router::register(['CommentController', 'addAction'], 'POST', '/comment/add', $commentValidation);
\Chibi\Router::register(['CommentController', 'deleteAction'], 'POST', '/comment/{id}/delete', $commentValidation);
\Chibi\Router::register(['CommentController', 'deleteAction'], null, '/comment/{id}/delete', $commentValidation);
\Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation);
\Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation);
@ -102,6 +102,8 @@ $userValidation =
\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}', $userValidation);
\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}/{page}', $userValidation);
\Chibi\Router::register(['UserController', 'editAction'], 'POST', '/user/{identifier}/edit', $userValidation);
\Chibi\Router::register(['UserController', 'registrationView'], 'GET', '/register', $userValidation);
\Chibi\Router::register(['UserController', 'registrationAction'], 'POST', '/register', $userValidation);
@ -112,11 +114,10 @@ $userValidation =
\Chibi\Router::register(['UserController', 'passwordResetAction'], 'POST', '/password-reset', $userValidation);
\Chibi\Router::register(['UserController', 'passwordResetAction'], 'GET', '/password-reset/{tokenText}', $userValidation);
\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{identifier}/flag', $userValidation);
\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{identifier}/ban', $userValidation);
\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{identifier}/unban', $userValidation);
\Chibi\Router::register(['UserController', 'acceptRegistrationAction'], 'POST', '/user/{identifier}/accept-registration', $userValidation);
\Chibi\Router::register(['UserController', 'deleteAction'], 'POST', '/user/{identifier}/delete', $userValidation);
\Chibi\Router::register(['UserController', 'settingsAction'], 'POST', '/user/{identifier}/settings', $userValidation);
\Chibi\Router::register(['UserController', 'editAction'], 'POST', '/user/{identifier}/edit', $userValidation);
\Chibi\Router::register(['UserController', 'toggleSafetyAction'], 'POST', '/user/toggle-safety/{safety}', $userValidation);
\Chibi\Router::register(['UserController', 'flagAction'], null, '/user/{identifier}/flag', $userValidation);
\Chibi\Router::register(['UserController', 'banAction'], null, '/user/{identifier}/ban', $userValidation);
\Chibi\Router::register(['UserController', 'unbanAction'], null, '/user/{identifier}/unban', $userValidation);
\Chibi\Router::register(['UserController', 'acceptRegistrationAction'], null, '/user/{identifier}/accept-registration', $userValidation);
\Chibi\Router::register(['UserController', 'deleteAction'], null, '/user/{identifier}/delete', $userValidation);
\Chibi\Router::register(['UserController', 'settingsAction'], null, '/user/{identifier}/settings', $userValidation);
\Chibi\Router::register(['UserController', 'toggleSafetyAction'], null, '/user/toggle-safety/{safety}', $userValidation);