Refactoring, move messages to Message class

This commit is contained in:
ohartl 2016-02-27 11:49:58 +01:00
parent 5e72d0e715
commit 300dadbab6
12 changed files with 175 additions and 97 deletions

View file

@ -286,31 +286,34 @@ body {
}
#content .notification {
height: auto;
width: 100%;
margin: 15px 0;
text-align: center;
border: 1px solid;
border-radius: 3px;
padding: 15px 10px;
box-sizing: border-box;
#content .notifications {
}
#content .notification.notification-fail {
background-color: #fcacac;
border-color: red;
}
#content .notification.notification-warning {
background-color: #fcf897;
border-color: #ffe600;
#content .notification {
height: auto;
width: 100%;
margin: 15px 0;
text-align: center;
border: 1px solid;
border-radius: 3px;
padding: 15px 10px;
box-sizing: border-box;
}
#content .notification.notification-success {
background-color: rgba(182, 255, 183, 1);
border-color: green;
}
#content .notification.notification-fail {
background-color: #fcacac;
border-color: red;
}
#content .notification.notification-warning {
background-color: #fcf897;
border-color: #ffe600;
}
#content .notification.notification-success {
background-color: rgba(182, 255, 183, 1);
border-color: green;
}
#footer {

View file

@ -0,0 +1,113 @@
<?php
/**
* Created by PhpStorm.
* User: oliver
* Date: 26.02.2016
* Time: 22:47
*/
class Message
{
const TYPE_FAIL = 'fail';
const TYPE_ERROR = 'fail';
const TYPE_WARNING = 'warning';
const TYPE_SUCCESS = 'success';
/**
* Holds all messages
*
* @var array
*/
protected static $messages = array();
/**
* Add a new message
*
* @param string $type Supported types: success, fail, info
* @param string $text
*/
public static 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(
'type' => $type,
'message' => $text,
);
}
/**
* Add a new success message
*
* @param string $text
*/
public static function fail($text)
{
static::add(static::TYPE_FAIL, $text);
}
/**
* Add a new success message
*
* @param string $text
*/
public static function error($text)
{
static::add(static::TYPE_ERROR, $text);
}
/**
* Add a new success message
*
* @param string $text
*/
public static function warning($text)
{
static::add(static::TYPE_WARNING, $text);
}
/**
* Add a new success message
*
* @param string $text
*/
public static function success($text)
{
static::add(static::TYPE_SUCCESS, $text);
}
/**
* Render all messages
*
* @param null|string $type null = render all
*
* @return string
*/
public static function render($type = null)
{
$out = '';
if(count(static::$messages) > 0){
$out .= '<div class="notifications">';
foreach(static::$messages as $message){
if(is_null($type) || $type == $message['type']){
$out .= '<div class="notification notification-'.$message['type'].'">'.$message['message'].'</div>';
}
}
$out .= '</div>';
}
return $out;
}
}

View file

@ -10,44 +10,6 @@ function dbError($errorMessage, $sql = null)
}
/**
* Holds all messages
* @var array
*/
$MESSAGES = array();
/**
* Add a new message
* @param string $type Supported types: success, fail, info
* @param string $message
*/
function add_message($type, $message)
{
global $MESSAGES;
$newmessage = array();
$newmessage['type'] = $type;
$newmessage['message'] = $message;
$MESSAGES[] = $newmessage;
}
/**
* Print all messages
*/
function output_messages()
{
global $MESSAGES;
if(count($MESSAGES) > 0) {
echo '<div class="messages">';
foreach($MESSAGES as $message){
echo '<div class="notification notification-'.$message['type'].'">'.$message['message'].'</div>';
}
echo '</div>';
}
}
/**
* Add message to logfile
*

View file

@ -23,11 +23,11 @@ if(isset($_POST['domain'])){
Router::redirect("admin/listdomains/?created=1");
}
else{
add_message("fail", "Domain already exists in database.");
Message::fail("Domain already exists in database.");
}
}
else{
add_message("fail", "Empty domain couldn't be created.");
Message::fail("Empty domain couldn't be created.");
}
}
@ -35,7 +35,7 @@ if(isset($_POST['domain'])){
<h1>Create new domain</h1>
<?php output_messages(); ?>
<?php echo Message::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){
add_message("fail", implode("<br>", $emailErrors));
Message::fail(implode("<br>", $emailErrors));
}
else{
if(count($emailErrors) === 0 && $savemode === "edit" && !is_null($redirect)){
@ -111,7 +111,7 @@ if(isset($_POST['savemode'])){
}
}
add_message("fail", implode("<br>", $errorMessages));
Message::fail(implode("<br>", $errorMessages));
}
else{
// multi source handling
@ -161,7 +161,7 @@ if(isset($_POST['savemode'])){
}
}
else{
add_message("fail", "Redirect couldn't be edited. Fill out all fields.");
Message::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.";
}
add_message("fail", implode("<br>", $errorMessages));
Message::fail(implode("<br>", $errorMessages));
}
else{
$inputDestination = emailsToString($inputDestinations);
@ -209,7 +209,7 @@ if(isset($_POST['savemode'])){
}
}
else{
add_message("fail", "Redirect couldn't be created. Fill out all fields.");
Message::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 output_messages(); ?>
<?php echo Message::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){
add_message("fail", $passwordInvalidException->getMessage());
Message::fail($passwordInvalidException->getMessage());
$passwordError = true;
}
}
@ -119,21 +119,21 @@ if(!is_null($saveMode)){
Router::redirect("admin/listusers/?created=1");
}
catch(Exception $passwordInvalidException){
add_message("fail", $passwordInvalidException->getMessage());
Message::fail($passwordInvalidException->getMessage());
}
}
else{
add_message("fail", "User already exists in database.");
Message::fail("User already exists in database.");
}
}
else{
add_message("fail", "The selected domain doesn't exist.");
Message::fail("The selected domain doesn't exist.");
}
}
else{
var_dump($_POST);
// Fields missing
add_message("fail", "Not all fields were filled out.");
Message::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 output_messages(); ?>
<?php echo Message::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"){
add_message("success", "Domain deleted successfully.");
Message::success("Domain deleted successfully.");
}
else if(isset($_GET['created']) && $_GET['created'] == "1"){
add_message("success", "Domain created successfully.");
Message::success("Domain created successfully.");
}
else if(isset($_GET['adm_del']) && $_GET['adm_del'] == "1"){
add_message("fail", "Domain couldn't be deleted because admin account would be affected.");
Message::fail("Domain couldn't be deleted because admin account would be affected.");
}
else if(isset($_GET['missing-permission']) && $_GET['missing-permission'] == "1"){
add_message("fail", "You don't have the permission to delete that domain.");
Message::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 output_messages(); ?>
<?php echo Message::render(); ?>
<?php if($domains->count() > 0): ?>
<table class="table">

View file

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

View file

@ -1,19 +1,19 @@
<?php
if(isset($_GET['deleted']) && $_GET['deleted'] == "1"){
add_message("success", "User deleted successfully.");
Message::success("User deleted successfully.");
}
else if(isset($_GET['created']) && $_GET['created'] == "1"){
add_message("success", "User created successfully.");
Message::success("User created successfully.");
}
else if(isset($_GET['edited']) && $_GET['edited'] == "1"){
add_message("success", "User edited successfully.");
Message::success("User edited successfully.");
}
else if(isset($_GET['adm_del']) && $_GET['adm_del'] == "1"){
add_message("fail", "Admin user cannot be deleted.");
Message::fail("Admin user cannot be deleted.");
}
else if(isset($_GET['missing-permission']) && $_GET['missing-permission'] == "1"){
add_message("fail", "You don't have the permission to edit/delete users of that domain.");
Message::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 output_messages(); ?>
<?php echo Message::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'])){
add_message('fail', 'Please fill out both email and password fields.');
Message::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']);
add_message("fail", "Sorry, but we cannot log you in with this combination of email and password, there might be a typo.");
Message::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 output_messages(); ?>
<?php echo Message::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']);
add_message("success", "Password changed successfully!");
Message::success("Password changed successfully!");
}
catch(Exception $passwordInvalidException){
add_message("fail", $passwordInvalidException->getMessage());
Message::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 output_messages(); ?>
<?php echo Message::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 output_messages(); ?>
<?php echo Message::render(); ?>
<?php if($redirects->count() > 0): ?>
<table class="table">