AJAX doesn't rely on StatusHelper

Since the purpose that StatusHelper was mainly created for no longer
holds, it was simplified to Messenger. It is now is used to transport
simple messages to views and still transports info whether the message
is about success or failure.
This commit is contained in:
Marcin Kurczewski 2014-05-01 22:29:36 +02:00
parent 925fccbd17
commit feec48ed83
13 changed files with 126 additions and 178 deletions

View file

@ -154,12 +154,18 @@ $context->layoutName = $context->json
: 'layout-normal';
$context->viewName = '';
$context->transport = new StdClass;
StatusHelper::init();
session_start();
if (!Auth::isLoggedIn())
Auth::tryAutoLogin();
register_shutdown_function(function()
{
$error = error_get_last();
if ($error !== null)
\Chibi\Util\Headers::setCode(400);
});
try
{
try
@ -183,7 +189,9 @@ catch (SimpleException $e)
{
if ($e instanceof SimpleNotFoundException)
\Chibi\Util\Headers::setCode(404);
StatusHelper::failure($e->getMessage());
else
\Chibi\Util\Headers::setCode(400);
Messenger::message($e->getMessage(), false);
if (!$context->handleExceptions)
$context->viewName = 'message';
renderView();
@ -191,7 +199,7 @@ catch (SimpleException $e)
catch (Exception $e)
{
\Chibi\Util\Headers::setCode(400);
StatusHelper::failure($e->getMessage());
Messenger::message($e->getMessage());
$context->transport->exception = $e;
$context->transport->queries = \Chibi\Database::getLogs();
$context->viewName = 'error-exception';

View file

@ -39,8 +39,6 @@ $(function()
type: 'POST',
success: function(data)
{
if (data['success'])
{
if (preview)
{
@ -75,17 +73,12 @@ $(function()
}
formDom.find(':input').attr('readonly', false);
formDom.removeClass('inactive');
}
else
{
alert(data['message']);
formDom.find(':input').attr('readonly', false);
formDom.removeClass('inactive');
}
},
error: function()
error: function(xhr)
{
alert('Fatal error');
alert(xhr.responseJSON
? xhr.responseJSON.message
: 'Fatal error');
formDom.find(':input').attr('readonly', false);
formDom.removeClass('inactive');
}

View file

@ -63,15 +63,15 @@ $(function()
aDom.addClass('inactive');
var url = $(this).attr('href') + '?json';
$.get(url).always(function(data)
$.get(url).success(function(data)
{
if (data['success'])
window.location.reload();
else
}).error(function(xhr)
{
alert(data['message'] ? data['message'] : 'Fatal error');
alert(xhr.responseJSON
? xhr.responseJSON.message
: 'Fatal error');
aDom.removeClass('inactive');
}
});
});
});
@ -112,9 +112,7 @@ $(function()
aDom.addClass('inactive');
var url = $(this).attr('href') + '?json';
$.get(url, {submit: 1}).always(function(data)
{
if (data['success'])
$.post(url, {submit: 1}).success(function(data)
{
if (aDom.hasAttr('data-redirect-url'))
window.location.href = aDom.attr('data-redirect-url');
@ -122,12 +120,12 @@ $(function()
aDom.data('callback')();
else
window.location.reload();
}
else
}).error(function(xhr)
{
alert(data['message'] ? data['message'] : 'Fatal error');
alert(xhr.responseJSON
? xhr.responseJSON.message
: 'Fatal error');
aDom.removeClass('inactive');
}
});
});

View file

@ -14,9 +14,7 @@ $(function()
var enable = !aDom.parents('.post').hasClass('tagged');
var url = $(this).attr('href') + '?json';
url = url.replace('_enable_', enable ? '1' : '0');
$.get(url, {submit: 1}).always(function(data)
{
if (data['success'])
$.get(url, {submit: 1}).success(function(data)
{
aDom.removeClass('inactive');
aDom.parents('.post').removeClass('tagged');
@ -25,12 +23,12 @@ $(function()
aDom.text(enable
? aDom.attr('data-text-tagged')
: aDom.attr('data-text-untagged'));
}
else
}).error(function(xhr)
{
alert(data['message'] ? data['message'] : 'Fatal error');
alert(xhr.responseJSON
? xhr.responseJSON.message
: 'Fatal error');
aDom.removeClass('inactive');
}
});
});
});

View file

@ -106,24 +106,21 @@ $(function()
dataType: 'json',
type: 'POST',
success: function(data)
{
if (data['success'])
{
postDom.slideUp(function()
{
postDom.remove();
sendNextPost();
});
}
else
{
postDom.find('.alert').html(data['messageHtml']).slideDown();
enableUpload();
}
},
error: function(data)
error: function(xhr)
{
postDom.find('.alert').html('Fatal error').slideDown();
postDom
.find('.alert')
.html(xhr.responseJSON
? xhr.responseJSON.messageHtml
: 'Fatal error')
.slideDown();
enableUpload();
}
};

View file

@ -109,8 +109,6 @@ $(function()
type: 'POST',
success: function(data)
{
if (data['success'])
{
disableExitConfirmation();
@ -121,17 +119,14 @@ $(function()
$('body').trigger('dom-update');
});
formDom.parents('.unit').hide();
}
else
{
alert(data['message']);
}
formDom.find(':input').attr('readonly', false);
formDom.removeClass('inactive');
},
error: function()
error: function(xhr)
{
alert('Fatal error');
alert(xhr.responseJSON
? xhr.responseJSON.message
: 'Fatal error');
formDom.find(':input').attr('readonly', false);
formDom.removeClass('inactive');
}

View file

@ -20,8 +20,6 @@ class AuthController
$suppliedPassword = InputHelper::get('password');
$remember = boolval(InputHelper::get('remember'));
$dbUser = Auth::login($suppliedName, $suppliedPassword, $remember);
StatusHelper::success();
self::redirectAfterLog();
}

View file

@ -61,7 +61,6 @@ class CommentController
LogHelper::log('{user} commented on {post}', ['post' => TextHelper::reprPost($post->id)]);
}
$context->transport->textPreview = $comment->getText();
StatusHelper::success();
}
public function editAction($id)
@ -89,7 +88,6 @@ class CommentController
'post' => TextHelper::reprPost($comment->getPost())]);
}
$context->transport->textPreview = $comment->getText();
StatusHelper::success();
}
public function deleteAction($id)
@ -104,6 +102,5 @@ class CommentController
LogHelper::log('{user} removed comment from {post}', [
'post' => TextHelper::reprPost($comment->getPost())]);
StatusHelper::success();
}
}

View file

@ -101,7 +101,6 @@ class PostController
$post->setTags($tags);
PostModel::save($post);
StatusHelper::success();
}
public function favoritesAction($page = 1)
@ -167,8 +166,6 @@ class PostController
LogHelper::flush();
PostModel::save($post);
});
StatusHelper::success();
}
public function editAction($id)
@ -190,8 +187,6 @@ class PostController
PostModel::save($post);
TagModel::removeUnused();
StatusHelper::success();
}
public function flagAction($id)
@ -211,7 +206,6 @@ class PostController
SessionHelper::set('flagged', $flagged);
LogHelper::log('{user} flagged {post} for moderator attention', ['post' => TextHelper::reprPost($post)]);
StatusHelper::success();
}
public function hideAction($id)
@ -226,7 +220,6 @@ class PostController
PostModel::save($post);
LogHelper::log('{user} hidden {post}', ['post' => TextHelper::reprPost($post)]);
StatusHelper::success();
}
public function unhideAction($id)
@ -241,7 +234,6 @@ class PostController
PostModel::save($post);
LogHelper::log('{user} unhidden {post}', ['post' => TextHelper::reprPost($post)]);
StatusHelper::success();
}
public function deleteAction($id)
@ -255,7 +247,6 @@ class PostController
PostModel::remove($post);
LogHelper::log('{user} deleted {post}', ['post' => TextHelper::reprPost($id)]);
StatusHelper::success();
}
public function addFavoriteAction($id)
@ -270,7 +261,6 @@ class PostController
UserModel::updateUserScore(Auth::getCurrentUser(), $post, 1);
UserModel::addToUserFavorites(Auth::getCurrentUser(), $post);
StatusHelper::success();
}
public function removeFavoriteAction($id)
@ -284,7 +274,6 @@ class PostController
return;
UserModel::removeFromUserFavorites(Auth::getCurrentUser(), $post);
StatusHelper::success();
}
public function scoreAction($id, $score)
@ -298,7 +287,6 @@ class PostController
return;
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
StatusHelper::success();
}
public function featureAction($id)
@ -309,7 +297,6 @@ class PostController
PropertyModel::set(PropertyModel::FeaturedPostId, $post->id);
PropertyModel::set(PropertyModel::FeaturedPostDate, time());
PropertyModel::set(PropertyModel::FeaturedPostUserName, Auth::getCurrentUser()->name);
StatusHelper::success();
LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]);
}

View file

@ -103,7 +103,7 @@ class TagController
'source' => TextHelper::reprTag($suppliedSourceTag),
'target' => TextHelper::reprTag($suppliedTargetTag)]);
StatusHelper::success('Tags merged successfully.');
Messenger::message('Tags merged successfully.');
}
public function renameAction()
@ -130,7 +130,7 @@ class TagController
'source' => TextHelper::reprTag($suppliedSourceTag),
'target' => TextHelper::reprTag($suppliedTargetTag)]);
StatusHelper::success('Tag renamed successfully.');
Messenger::message('Tag renamed successfully.');
}
public function massTagRedirectAction()

View file

@ -46,8 +46,6 @@ class UserController
LogHelper::log('{user} flagged {subject} for moderator attention', [
'subject' => TextHelper::reprUser($user)]);
StatusHelper::success();
}
public function banAction($name)
@ -64,7 +62,6 @@ class UserController
UserModel::save($user);
LogHelper::log('{user} banned {subject}', ['subject' => TextHelper::reprUser($user)]);
StatusHelper::success();
}
public function unbanAction($name)
@ -81,7 +78,6 @@ class UserController
UserModel::save($user);
LogHelper::log('{user} unbanned {subject}', ['subject' => TextHelper::reprUser($user)]);
StatusHelper::success();
}
public function acceptRegistrationAction($name)
@ -96,7 +92,6 @@ class UserController
$user->staffConfirmed = true;
UserModel::save($user);
LogHelper::log('{user} confirmed {subject}\'s account', ['subject' => TextHelper::reprUser($user)]);
StatusHelper::success();
}
public function deleteAction($name)
@ -133,7 +128,6 @@ class UserController
\Chibi\Util\Url::forward(\Chibi\Router::linkTo(['IndexController', 'indexAction']));
LogHelper::log('{user} removed {subject}\'s account', ['subject' => TextHelper::reprUser($name)]);
StatusHelper::success();
}
public function settingsAction($name)
@ -167,7 +161,7 @@ class UserController
UserModel::save($user);
if ($user->id == Auth::getCurrentUser()->id)
Auth::setCurrentUser($user);
StatusHelper::success('Browsing settings updated!');
Messenger::message('Browsing settings updated!');
}
public function editAction($name)
@ -278,7 +272,7 @@ class UserController
$message = 'Account settings updated!';
if ($confirmMail)
$message .= ' You will be sent an e-mail address confirmation message soon.';
StatusHelper::success($message);
Messenger::message($message);
}
catch (Exception $e)
{
@ -343,8 +337,6 @@ class UserController
if ($user->accessRank != AccessRank::Anonymous)
UserModel::save($user);
Auth::setCurrentUser($user);
StatusHelper::success();
}
public function registrationAction()
@ -420,7 +412,7 @@ class UserController
$message .= ' Your registration must be now confirmed by staff.';
LogHelper::log('{subject} just signed up', ['subject' => TextHelper::reprUser($dbUser)]);
StatusHelper::success($message);
Messenger::message($message);
if (!getConfig()->registration->needEmailForRegistering and !getConfig()->registration->staffActivation)
{
@ -448,7 +440,7 @@ class UserController
$message = 'Activation completed successfully.';
if (getConfig()->registration->staffActivation)
$message .= ' However, your account still must be confirmed by staff.';
StatusHelper::success($message);
Messenger::message($message);
if (!getConfig()->registration->staffActivation)
{
@ -479,7 +471,7 @@ class UserController
LogHelper::log('{subject} just reset password', ['subject' => TextHelper::reprUser($dbUser)]);
$message = 'Password reset successful. Your new password is **' . $randomPassword . '**.';
StatusHelper::success($message);
Messenger::message($message);
Auth::setCurrentUser($dbUser);
}
@ -499,7 +491,7 @@ class UserController
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
self::sendPasswordResetConfirmation($user);
StatusHelper::success('E-mail sent. Follow instructions to reset password.');
Messenger::message('E-mail sent. Follow instructions to reset password.');
}
public function activationProxyAction()
@ -521,7 +513,7 @@ class UserController
throw new SimpleException('This user has no e-mail specified; activation cannot proceed');
}
self::sendEmailChangeConfirmation($user);
StatusHelper::success('Activation e-mail resent.');
Messenger::message('Activation e-mail resent.');
}
private function loadUserView($user)

View file

@ -1,33 +0,0 @@
<?php
class StatusHelper
{
private static function flag($success, $message = null)
{
$context = getContext();
if (!empty($message))
{
if (!preg_match('/[.?!]$/', $message))
$message .= '.';
$context->transport->message = $message;
$context->transport->messageHtml = TextHelper::parseMarkdown($message, true);
}
$context->transport->success = $success;
}
public static function init()
{
$context = getContext();
$context->transport->success = null;
}
public static function success($message = null)
{
self::flag(true, $message);
}
public static function failure($message = null)
{
self::flag(false, $message);
}
}

18
src/Messenger.php Normal file
View file

@ -0,0 +1,18 @@
<?php
class Messenger
{
public static function message($message, $success = true)
{
if (empty($message))
return;
$context = getContext();
if (!preg_match('/[.?!]$/', $message))
$message .= '.';
$context->transport->success = $success;
$context->transport->message = $message;
$context->transport->messageHtml = TextHelper::parseMarkdown($message, true);
}
}