Refactoring Message

This commit is contained in:
ohartl 2016-02-28 00:30:14 +01:00
parent cc998c9dfb
commit 7336611dd2
10 changed files with 83 additions and 52 deletions

View file

@ -15,12 +15,41 @@ class Message
const TYPE_SUCCESS = 'success';
/**
* @var Message
*/
protected static $instance;
/**
* Holds all messages
*
* @var array
*/
protected static $messages = array();
protected $messages = array();
private function __construct()
{
}
private function __clone()
{
}
/**
* @return Message
*/
public static function getInstance()
{
if(is_null(static::$instance)){
static::$instance = new static();
}
return static::$instance;
}
/**
@ -29,13 +58,13 @@ class Message
* @param string $type Supported types: success, fail, info
* @param string $text
*/
public static function add($type, $text)
public function add($type, $text)
{
if(!in_array($type, array(static::TYPE_FAIL, static::TYPE_ERROR, static::TYPE_WARNING, static::TYPE_SUCCESS))){
throw new InvalidArgumentException;
}
static::$messages[] = array(
$this->messages[] = array(
'type' => $type,
'message' => $text,
);
@ -47,9 +76,9 @@ class Message
*
* @param string $text
*/
public static function fail($text)
public function fail($text)
{
static::add(static::TYPE_FAIL, $text);
$this->add(static::TYPE_FAIL, $text);
}
@ -58,9 +87,9 @@ class Message
*
* @param string $text
*/
public static function error($text)
public function error($text)
{
static::add(static::TYPE_ERROR, $text);
$this->add(static::TYPE_ERROR, $text);
}
@ -69,9 +98,9 @@ class Message
*
* @param string $text
*/
public static function warning($text)
public function warning($text)
{
static::add(static::TYPE_WARNING, $text);
$this->add(static::TYPE_WARNING, $text);
}
@ -80,9 +109,9 @@ class Message
*
* @param string $text
*/
public static function success($text)
public function success($text)
{
static::add(static::TYPE_SUCCESS, $text);
$this->add(static::TYPE_SUCCESS, $text);
}
@ -93,17 +122,19 @@ class Message
*
* @return string
*/
public static function render($type = null)
public function render($type = null)
{
$out = '';
if(count(static::$messages) > 0){
if(count($this->messages) > 0){
$out .= '<div class="notifications">';
foreach(static::$messages as $message){
foreach($this->messages as $message){
if(is_null($type) || $type == $message['type']){
$out .= '<div class="notification notification-'.$message['type'].'">'.$message['message'].'</div>';
}
}
$out .= '</div>';
}

View file

@ -23,11 +23,11 @@ if(isset($_POST['domain'])){
Router::redirect("admin/listdomains/?created=1");
}
else{
Message::fail("Domain already exists in database.");
Message::getInstance()->fail("Domain already exists in database.");
}
}
else{
Message::fail("Empty domain couldn't be created.");
Message::getInstance()->fail("Empty domain couldn't be created.");
}
}
@ -35,7 +35,7 @@ if(isset($_POST['domain'])){
<h1>Create new domain</h1>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<div class="buttons">
<a class="button" href="<?php echo Router::url('admin/listdomains'); ?>">&#10092; Back to domain list</a>

View file

@ -65,7 +65,7 @@ if(isset($_POST['savemode'])){
if(count($emailErrors) > 0){
Message::fail(implode("<br>", $emailErrors));
Message::getInstance()->fail(implode("<br>", $emailErrors));
}
else{
if(count($emailErrors) === 0 && $savemode === "edit" && !is_null($redirect)){
@ -111,7 +111,7 @@ if(isset($_POST['savemode'])){
}
}
Message::fail(implode("<br>", $errorMessages));
Message::getInstance()->fail(implode("<br>", $errorMessages));
}
else{
// multi source handling
@ -161,7 +161,7 @@ if(isset($_POST['savemode'])){
}
}
else{
Message::fail("Redirect couldn't be edited. Fill out all fields.");
Message::getInstance()->fail("Redirect couldn't be edited. Fill out all fields.");
}
}
@ -179,7 +179,7 @@ if(isset($_POST['savemode'])){
$errorMessages[] = "Source address \"{$existingRedirect->getSource()}\" is already redirected to some destination.";
}
Message::fail(implode("<br>", $errorMessages));
Message::getInstance()->fail(implode("<br>", $errorMessages));
}
else{
$inputDestination = emailsToString($inputDestinations);
@ -209,7 +209,7 @@ if(isset($_POST['savemode'])){
}
}
else{
Message::fail("Redirect couldn't be created. Fill out all fields.");
Message::getInstance()->fail("Redirect couldn't be created. Fill out all fields.");
}
}
}
@ -236,7 +236,7 @@ $domains = Domain::getByLimitedDomains();
So make sure you don't accidentally override a mailbox with a redirect.
</div>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if(defined('VALIDATE_ALIASES_SOURCE_DOMAIN_ENABLED') && Auth::getUser()->isDomainLimited() && $domains->count() === 0): ?>
<div class="notification notification-fail">

View file

@ -53,7 +53,7 @@ if(!is_null($saveMode)){
$userToEdit->changePassword($inputPassword, $inputPasswordRepeated);
}
catch(Exception $passwordInvalidException){
Message::fail($passwordInvalidException->getMessage());
Message::getInstance()->fail($passwordInvalidException->getMessage());
$passwordError = true;
}
}
@ -119,21 +119,21 @@ if(!is_null($saveMode)){
Router::redirect("admin/listusers/?created=1");
}
catch(Exception $passwordInvalidException){
Message::fail($passwordInvalidException->getMessage());
Message::getInstance()->fail($passwordInvalidException->getMessage());
}
}
else{
Message::fail("User already exists in database.");
Message::getInstance()->fail("User already exists in database.");
}
}
else{
Message::fail("The selected domain doesn't exist.");
Message::getInstance()->fail("The selected domain doesn't exist.");
}
}
else{
var_dump($_POST);
// Fields missing
Message::fail("Not all fields were filled out.");
Message::getInstance()->fail("Not all fields were filled out.");
}
}
}
@ -171,7 +171,7 @@ if(isset($_GET['id'])){
<input type="hidden" name="id" value="<?php echo $user->getId(); ?>"/>
<?php endif; ?>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if($mode === "edit"): ?>
<div class="input-group">

View file

@ -5,16 +5,16 @@ if(Auth::getUser()->isDomainLimited()){
}
if(isset($_GET['deleted']) && $_GET['deleted'] == "1"){
Message::success("Domain deleted successfully.");
Message::getInstance()->success("Domain deleted successfully.");
}
else if(isset($_GET['created']) && $_GET['created'] == "1"){
Message::success("Domain created successfully.");
Message::getInstance()->success("Domain created successfully.");
}
else if(isset($_GET['adm_del']) && $_GET['adm_del'] == "1"){
Message::fail("Domain couldn't be deleted because admin account would be affected.");
Message::getInstance()->fail("Domain couldn't be deleted because admin account would be affected.");
}
else if(isset($_GET['missing-permission']) && $_GET['missing-permission'] == "1"){
Message::fail("You don't have the permission to delete that domain.");
Message::getInstance()->fail("You don't have the permission to delete that domain.");
}
$domains = Domain::findAll();
@ -29,7 +29,7 @@ $domains = Domain::findAll();
</div>
<?php endif; ?>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if($domains->count() > 0): ?>
<table class="table">

View file

@ -1,16 +1,16 @@
<?php
if(isset($_GET['deleted']) && $_GET['deleted'] == "1"){
Message::success("Redirect deleted successfully.");
Message::getInstance()->success("Redirect deleted successfully.");
}
else if(isset($_GET['created']) && $_GET['created'] == "1"){
Message::success("Redirect created successfully.");
Message::getInstance()->success("Redirect created successfully.");
}
else if(isset($_GET['edited']) && $_GET['edited'] == "1"){
Message::success("Redirect edited successfully.");
Message::getInstance()->success("Redirect edited successfully.");
}
else if(isset($_GET['missing-permission']) && $_GET['missing-permission'] == "1"){
Message::fail("You don't have the permission to edit/delete redirects of that domain.");
Message::getInstance()->fail("You don't have the permission to edit/delete redirects of that domain.");
}
$redirects = AbstractRedirect::getMultiByLimitedDomains();
@ -29,7 +29,7 @@ $redirects = AbstractRedirect::getMultiByLimitedDomains();
</div>
<?php endif; ?>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if($redirects->count() > 0): ?>
<table class="table">

View file

@ -1,19 +1,19 @@
<?php
if(isset($_GET['deleted']) && $_GET['deleted'] == "1"){
Message::success("User deleted successfully.");
Message::getInstance()->success("User deleted successfully.");
}
else if(isset($_GET['created']) && $_GET['created'] == "1"){
Message::success("User created successfully.");
Message::getInstance()->success("User created successfully.");
}
else if(isset($_GET['edited']) && $_GET['edited'] == "1"){
Message::success("User edited successfully.");
Message::getInstance()->success("User edited successfully.");
}
else if(isset($_GET['adm_del']) && $_GET['adm_del'] == "1"){
Message::fail("Admin user cannot be deleted.");
Message::getInstance()->fail("Admin user cannot be deleted.");
}
else if(isset($_GET['missing-permission']) && $_GET['missing-permission'] == "1"){
Message::fail("You don't have the permission to edit/delete users of that domain.");
Message::getInstance()->fail("You don't have the permission to edit/delete users of that domain.");
}
$users = User::getByLimitedDomains();
@ -32,7 +32,7 @@ $users = User::getByLimitedDomains();
</div>
<?php endif; ?>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if($users->count() > 0): ?>
<table class="table">

View file

@ -7,7 +7,7 @@ if(Auth::isLoggedIn()){
if(isset($_POST['email']) && isset($_POST['password'])){
if(empty($_POST['email']) || empty($_POST['password'])){
Message::fail('Please fill out both email and password fields.');
Message::getInstance()->fail('Please fill out both email and password fields.');
}
else {
// Start login
@ -18,7 +18,7 @@ if(isset($_POST['email']) && isset($_POST['password'])){
else{
//Log error message
writeLog("WebMUM login failed for IP ".$_SERVER['REMOTE_ADDR']);
Message::fail("Sorry, but we cannot log you in with this combination of email and password, there might be a typo.");
Message::getInstance()->fail("Sorry, but we cannot log you in with this combination of email and password, there might be a typo.");
}
}
}
@ -27,7 +27,7 @@ if(isset($_POST['email']) && isset($_POST['password'])){
<h1>Login</h1>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<form class="form" action="" method="post">
<div class="input-group">

View file

@ -4,10 +4,10 @@ if(isset($_POST['password']) && isset($_POST['password_repeat'])){
try {
Auth::getUser()->changePassword($_POST['password'], $_POST['password_repeat']);
Message::success("Password changed successfully!");
Message::getInstance()->success("Password changed successfully!");
}
catch(Exception $passwordInvalidException){
Message::fail($passwordInvalidException->getMessage());
Message::getInstance()->fail($passwordInvalidException->getMessage());
}
}
@ -19,7 +19,7 @@ if(isset($_POST['password']) && isset($_POST['password_repeat'])){
<a class="button" href="<?php echo Router::url('private'); ?>">&#10092; Back to personal dashboard</a>
</div>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<form class="form" action="" method="post" autocomplete="off">
<div class="input-group">

View file

@ -10,7 +10,7 @@ $redirects = Auth::getUser()->getAnonymizedRedirects();
<a class="button" href="<?php echo Router::url('private'); ?>">&#10092; Back to personal dashboard</a>
</div>
<?php echo Message::render(); ?>
<?php echo Message::getInstance()->render(); ?>
<?php if($redirects->count() > 0): ?>
<table class="table">