Refactoring of error/success messages

This commit is contained in:
Marcin Kurczewski 2013-11-16 18:40:26 +01:00
parent fb02feeed3
commit 76a60ed5d7
17 changed files with 202 additions and 190 deletions

View file

@ -116,7 +116,7 @@ $(function()
}
else
{
postDom.find('.alert').html(data['errorHtml']).slideDown();
postDom.find('.alert').html(data['messageHtml']).slideDown();
enableUpload();
}
}

View file

@ -26,7 +26,7 @@ class Bootstrap
? 'layout-json'
: 'layout-normal';
$this->context->transport = new StdClass;
$this->context->transport->success = null;
StatusHelper::init();
AuthController::doLogIn();
@ -41,29 +41,24 @@ class Bootstrap
{
$workCallback();
}
catch (SimpleException $e)
{
$this->context->transport->errorMessage = rtrim($e->getMessage(), '.') . '.';
$this->context->transport->errorHtml = TextHelper::parseMarkdown($this->context->transport->errorMessage, true);
$this->context->transport->exception = $e;
$this->context->transport->success = false;
if (!$this->context->handleExceptions)
$this->context->viewName = 'error-simple';
(new \Chibi\View())->renderFile($this->context->layoutName);
}
catch (\Chibi\MissingViewFileException $e)
{
$this->context->json = true;
$this->context->layoutName = 'layout-json';
(new \Chibi\View())->renderFile($this->context->layoutName);
}
catch (SimpleException $e)
{
StatusHelper::failure(rtrim($e->getMessage(), '.') . '.');
if (!$this->context->handleExceptions)
$this->context->viewName = 'message';
(new \Chibi\View())->renderFile($this->context->layoutName);
}
catch (Exception $e)
{
$this->context->transport->errorMessage = rtrim($e->getMessage(), '.') . '.';
$this->context->transport->errorHtml = TextHelper::parseMarkdown($this->context->transport->errorMessage, true);
StatusHelper::failure(rtrim($e->getMessage(), '.') . '.');
$this->context->transport->exception = $e;
$this->context->transport->queries = array_map(function($x) { return preg_replace('/\s+/', ' ', $x); }, queryLogger()->getLogs());
$this->context->transport->success = false;
$this->context->viewName = 'error-exception';
(new \Chibi\View())->renderFile($this->context->layoutName);
}

View file

@ -76,7 +76,7 @@ class AuthController
$token = implode('|', [base64_encode($suppliedName), base64_encode($suppliedPassword)]);
setcookie('auth', TextHelper::encrypt($token), time() + 365 * 24 * 3600, '/');
}
$this->context->transport->success = true;
StatusHelper::success();
self::redirectAfterLog();
}
}

View file

@ -63,7 +63,7 @@ class CommentController
if (InputHelper::get('sender') != 'preview')
R::store($comment);
$this->context->transport->textPreview = $comment->getText();
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -79,6 +79,6 @@ class CommentController
R::preload($comment, ['commenter' => 'user']);
PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->commenter));
R::trash($comment);
$this->context->transport->success = true;
StatusHelper::success();
}
}

View file

@ -138,7 +138,7 @@ class PostController
$post->sharedTag = $dbTags;
R::store($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -339,7 +339,7 @@ class PostController
}
R::store($dbPost);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -437,7 +437,7 @@ class PostController
Model_Tag::removeUnused();
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -455,7 +455,7 @@ class PostController
{
$post->hidden = true;
R::store($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -471,7 +471,7 @@ class PostController
{
$post->hidden = false;
R::store($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -495,7 +495,7 @@ class PostController
$post->sharedTag = [];
R::store($post);
R::trash($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -522,7 +522,7 @@ class PostController
$post->link('favoritee')->user = $this->context->user;
R::store($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -551,7 +551,7 @@ class PostController
unset ($post->ownFavoritee[$finalKey]);
R::store($post);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -580,7 +580,7 @@ class PostController
}
$p->score = $score;
R::store($p);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -596,7 +596,7 @@ class PostController
Model_Property::set(Model_Property::FeaturedPostId, $post->id);
Model_Property::set(Model_Property::FeaturedPostUserId, $this->context->user->id);
Model_Property::set(Model_Property::FeaturedPostDate, time());
$this->context->transport->success = true;
StatusHelper::success();
}

View file

@ -70,7 +70,7 @@ class TagController
}
R::trash($sourceTag);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$this->view->context->success = true;
StatusHelper::success();
}
}
@ -101,7 +101,7 @@ class TagController
R::store($sourceTag);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$this->context->transport->success = true;
StatusHelper::success();
}
}

View file

@ -127,7 +127,7 @@ class UserController
{
$user->banned = true;
R::store($user);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -143,7 +143,7 @@ class UserController
{
$user->banned = false;
R::store($user);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -159,7 +159,7 @@ class UserController
{
$user->staff_confirmed = true;
R::store($user);
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -209,7 +209,7 @@ class UserController
R::store($user);
R::trash($user);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
$this->context->transport->success = true;
StatusHelper::success();
}
}
@ -247,7 +247,7 @@ class UserController
if ($user->id == $this->context->user->id)
$this->context->user = $user;
AuthController::doReLog();
$this->context->transport->success = true;
StatusHelper::success('Browsing settings updated!');
}
}
@ -312,6 +312,7 @@ class UserController
}
else
{
$user->email_unconfirmed = null;
$user->email_confirmed = $suppliedEmail;
}
}
@ -334,7 +335,10 @@ class UserController
if ($confirmMail)
self::sendEmailChangeConfirmation($user);
$this->context->transport->success = true;
$message = 'Account settings updated!';
if ($confirmMail)
$message .= ' You wlil be sent an e-mail address confirmation message soon.';
StatusHelper::success($message);
}
}
catch (Exception $e)
@ -414,7 +418,7 @@ class UserController
if (!$this->context->user->anonymous)
R::store($this->context->user);
$this->context->transport->success = true;
StatusHelper::success();
}
@ -485,7 +489,17 @@ class UserController
if (!empty($dbUser->email_unconfirmed))
self::sendEmailChangeConfirmation($dbUser);
$this->context->transport->success = true;
$message = 'Congratulations, your account was created.';
if (!empty($this->context->mailSent))
{
$message .= ' Please wait for activation e-mail.';
if ($this->config->registration->staffActivation)
$message .= ' After this, your registration must be confirmed by staff.';
}
elseif ($this->config->registration->staffActivation)
$message .= ' Your registration must be now confirmed by staff.';
StatusHelper::success($message);
if (!$this->config->registration->needEmailForRegistering and !$this->config->registration->staffActivation)
{
@ -503,6 +517,7 @@ class UserController
public function activationAction($token)
{
$this->context->subTitle = 'account activation';
$this->context->viewName = 'message';
if (empty($token))
throw new SimpleException('Invalid activation token');
@ -515,7 +530,7 @@ class UserController
throw new SimpleException('This user was already activated');
if ($dbToken->expires !== null and time() > $dbToken->expires)
throw new SimpleException('Activation link expired.');
throw new SimpleException('Activation link expired');
$dbUser = $dbToken->user;
$dbUser->email_confirmed = $dbUser->email_unconfirmed;
@ -523,7 +538,11 @@ class UserController
$dbToken->used = true;
R::store($dbToken);
R::store($dbUser);
$this->context->transport->success = true;
$message = 'Activation completed successfully.';
if ($this->config->registration->staffActivation)
$message .= ' However, your account still must be confirmed by staff.';
StatusHelper::success($message);
if (!$this->config->registration->staffActivation)
{

View file

@ -0,0 +1,30 @@
<?php
class StatusHelper
{
private static function flag($success, $message = null)
{
$context = \Chibi\Registry::getContext();
if (!empty($message))
{
$context->transport->message = $message;
$context->transport->messageHtml = TextHelper::parseMarkdown($message, true);
}
$context->transport->success = $success;
}
public static function init()
{
$context = \Chibi\Registry::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);
}
}

View file

@ -24,9 +24,7 @@
</div>
</div>
<?php if (isset($this->context->transport->errorMessage)): ?>
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorMessage ?></p>
<?php endif ?>
<?php $this->renderFile('message') ?>
<input type="hidden" name="submit" value="1"/>

View file

@ -1 +0,0 @@
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorHtml ?><br><a href="javascript:history.go(-1)">Go back</a></p>

5
src/Views/message.phtml Normal file
View file

@ -0,0 +1,5 @@
<?php if (!empty($this->context->transport->message)): ?>
<p class="alert <?php echo $this->context->transport->success ? 'alert-success' : 'alert-error'; ?>">
<?php echo $this->context->transport->messageHtml ?>
</p>
<?php endif ?>

View file

@ -1,128 +1,122 @@
<?php if ($this->context->transport->success === true): ?>
<p>Post created!</p>
<?php else: ?>
<div id="sidebar">
<div class="unit">
<h1>file upload</h1>
<p>Use tags to describe uploaded images. Try to specify characters, their look and shows they are from.</p>
<p>Set proper visibility setting if the image isn&rsquo;t safe for work or you&rsquo;re not sure it&rsquo;s 100% <span class="safety-safe">safe</span>.</p>
<p>Only registered users can view <span class="safety-sketchy">sketchy</span> or <span class="safety-unsafe">NSFW</span> content.</p>
<p>Click submit when you&rsquo;re done.</p>
</div>
</div>
<div id="sidebar">
<div class="unit">
<h1>file upload</h1>
<p>Use tags to describe uploaded images. Try to specify characters, their look and shows they are from.</p>
<p>Set proper visibility setting if the image isn&rsquo;t safe for work or you&rsquo;re not sure it&rsquo;s 100% <span class="safety-safe">safe</span>.</p>
<p>Only registered users can view <span class="safety-sketchy">sketchy</span> or <span class="safety-unsafe">NSFW</span> content.</p>
<p>Click submit when you&rsquo;re done.</p>
<div id="inner-content">
<div id="upload-step1">
<div class="tabs">
<nav>
<ul>
<li class="selected file">
<a href="#">
Upload from file
</a>
</li>
<li class="url">
<a href="#">
Upload from URL
</a>
</li>
</ul>
</nav>
</div>
<div class="tab file">
<input type=file multiple style="display: none"/>
<div id="file-handler-wrapper">
<div id="file-handler">
Drop files here!<br>
Or just click on this box.
</div>
</div>
</div>
<div class="tab url">
<div id="url-handler-wrapper">
<div id="url-handler">
<div class="input-wrapper"><textarea placeholder="Paste some URLs here, one per line." name="urls"></textarea></div>
</div>
<button type="submit">Add</button>
</div>
</div>
<div class="clear"></div>
</div>
<div id="upload-step2" data-redirect-url="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>">
<hr>
<div class="posts">
</div>
<div class="submit-wrapper">
<button id="the-submit" type="submit">Submit</button>
</div>
</div>
<div id="inner-content">
<div id="upload-step1">
<div class="tabs">
<nav>
<ul>
<li class="selected file">
<a href="#">
Upload from file
</a>
</li>
<div id="post-template" class="post">
<p class="alert alert-error">Some kind of error</p>
<li class="url">
<a href="#">
Upload from URL
</a>
</li>
</ul>
</nav>
<img class="thumbnail" src="<?php echo \Chibi\UrlHelper::absoluteUrl('/media/img/pixel.gif') ?>" alt="Thumbnail"/>
<div class="form-wrapper">
<div class="ops">
<a class="move-up-trigger">
move up <span>&uarr;</span>
</a>
<a class="move-down-trigger">
move down <span>&darr;</span>
</a>
<a class="remove-trigger">
remove <span>&times;</span>
</a>
</div>
<div class="tab file">
<input type=file multiple style="display: none"/>
<div id="file-handler-wrapper">
<div id="file-handler">
Drop files here!<br>
Or just click on this box.
</div>
</div>
</div>
<div class="tab url">
<div id="url-handler-wrapper">
<div id="url-handler">
<div class="input-wrapper"><textarea placeholder="Paste some URLs here, one per line." name="urls"></textarea></div>
</div>
<button type="submit">Add</button>
</div>
</div>
<div class="clear"></div>
</div>
<div id="upload-step2" data-redirect-url="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>">
<hr>
<div class="posts">
</div>
<div class="submit-wrapper">
<button id="the-submit" type="submit">Submit</button>
</div>
</div>
<div id="post-template" class="post">
<p class="alert alert-error">Some kind of error</p>
<img class="thumbnail" src="<?php echo \Chibi\UrlHelper::absoluteUrl('/media/img/pixel.gif') ?>" alt="Thumbnail"/>
<div class="form-wrapper">
<div class="ops">
<a class="move-up-trigger">
move up <span>&uarr;</span>
</a>
<a class="move-down-trigger">
move down <span>&darr;</span>
</a>
<a class="remove-trigger">
remove <span>&times;</span>
</a>
<form action="<?php echo \Chibi\UrlHelper::route('post', 'upload') ?>" method="post" class="aligned">
<div class="file-name">
<label class="left">File:</label>
<strong>filename.jpg</strong>
</div>
<form action="<?php echo \Chibi\UrlHelper::route('post', 'upload') ?>" method="post" class="aligned">
<div class="file-name">
<label class="left">File:</label>
<strong>filename.jpg</strong>
</div>
<div class="safety">
<label class="left">Safety:</label>
<?php $checked = false ?>
<?php foreach (PostSafety::getAll() as $safety): ?>
<label>
<input type="radio" name="safety" value="<?php echo $safety ?>"<?php if (!$checked) echo ' checked="checked"' ?>/>
<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?>
<?php $checked = true ?>
</label>
<?php endforeach ?>
<input type="hidden" name="anonymous" value="0"/>
<div class="safety">
<label class="left">Safety:</label>
<?php $checked = false ?>
<?php foreach (PostSafety::getAll() as $safety): ?>
<label>
<input type="checkbox" name="anonymous" value="1"/>
Upload anonymously
<input type="radio" name="safety" value="<?php echo $safety ?>"<?php if (!$checked) echo ' checked="checked"' ?>/>
<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?>
<?php $checked = true ?>
</label>
<?php endforeach ?>
<input type="hidden" name="anonymous" value="0"/>
<label>
<input type="checkbox" name="anonymous" value="1"/>
Upload anonymously
</label>
</div>
</div>
<div class="tags">
<label class="left">Tags:</label>
<div class="input-wrapper"><input type="text" name="tags" placeholder="enter some tags&hellip;"/></div>
</div>
<div class="tags">
<label class="left">Tags:</label>
<div class="input-wrapper"><input type="text" name="tags" placeholder="enter some tags&hellip;"/></div>
</div>
<div class="source">
<label class="left">Source:</label>
<div class="input-wrapper"><input type="text" name="source" placeholder="where did you get this from? (optional)"/></div>
</div>
<div class="source">
<label class="left">Source:</label>
<div class="input-wrapper"><input type="text" name="source" placeholder="where did you get this from? (optional)"/></div>
</div>
<input type="hidden" name="submit" value="1"/>
</form>
</div>
<input type="hidden" name="submit" value="1"/>
</form>
</div>
</div>
<?php endif ?>
</div>

View file

@ -1,6 +0,0 @@
<?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Activation completed successfully.
<?php if ($this->config->registration->staffActivation): ?>
<br>However, your account still must be confirmed by staff.
<?php endif ?></p>
<?php endif ?>

View file

@ -8,11 +8,7 @@
<input type="hidden" name="submit" value="1"/>
<?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Account settings updated!</p>
<?php elseif (isset($this->context->transport->errorMessage)): ?>
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorMessage ?></p>
<?php endif ?>
<?php $this->renderFile('message') ?>
<div>
<label class="left">&nbsp;</label>

View file

@ -52,11 +52,7 @@
<input type="hidden" name="submit" value="1"/>
<?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Account settings updated! <?php if (!empty($this->context->mailSent)) echo 'You will be sent new e-mail address confirmation message soon.' ?></p>
<?php elseif (isset($this->context->transport->errorMessage)): ?>
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorMessage ?></p>
<?php endif ?>
<?php $this->renderFile('message') ?>
<div>
<label class="left">&nbsp;</label>

View file

@ -1,13 +1,5 @@
<?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Congratulations, your account was created.
<?php if (!empty($this->context->mailSent)): ?>
<br>Please wait for activation e-mail.
<?php if ($this->config->registration->staffActivation): ?>
<br>After this, your registration must be confirmed by staff.
<?php endif ?>
<?php elseif ($this->config->registration->staffActivation): ?>
<br>Your registration must be now confirmed by staff.
<?php endif ?></p>
<?php $this->renderFile('message') ?>
<?php else: ?>
<form action="<?php echo \Chibi\UrlHelper::route('auth', 'register') ?>" class="auth aligned" method="post">
<div>
@ -38,11 +30,9 @@
<p id="email-info">Your e-mail will be used to show your <a href="http://gravatar.com/">Gravatar</a>.<br/>Leave blank for random Gravatar.</p>
</div>
<?php if (isset($this->context->transport->errorMessage)): ?>
<div>
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorMessage ?></p>
</div>
<?php endif ?>
<div>
<?php $this->renderFile('message') ?>
</div>
<input type="hidden" name="submit" value="1"/>

View file

@ -23,11 +23,7 @@
<input type="hidden" name="submit" value="1"/>
<?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Browsing settings updated!</p>
<?php elseif (isset($this->context->transport->errorMessage)): ?>
<p class="alert alert-error">Error: <?php echo $this->context->transport->errorMessage ?></p>
<?php endif ?>
<?php $this->renderFile('message') ?>
<div>
<label class="left">&nbsp;</label>