Quellcode durchsuchen

Refactoring, move messages to Message class

ohartl vor 9 Jahren
Ursprung
Commit
300dadbab6

+ 24 - 21
include/css/style.css

@@ -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 .notification.notification-fail {
-		background-color: #fcacac;
-		border-color: red;
+	#content .notifications {
 	}
 
-		#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 {

+ 113 - 0
include/php/classes/Message.php

@@ -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;
+	}
+
+}

+ 0 - 38
include/php/global.inc.php

@@ -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
  *

+ 3 - 3
include/php/pages/admin/createdomain.php

@@ -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>

+ 6 - 6
include/php/pages/admin/editredirect.php

@@ -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">

+ 6 - 6
include/php/pages/admin/edituser.php

@@ -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">

+ 5 - 5
include/php/pages/admin/listdomains.php

@@ -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">

+ 5 - 5
include/php/pages/admin/listredirects.php

@@ -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">

+ 6 - 6
include/php/pages/admin/listusers.php

@@ -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">

+ 3 - 3
include/php/pages/login.php

@@ -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">

+ 3 - 3
include/php/pages/private/changepass.php

@@ -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">

+ 1 - 1
include/php/pages/private/yourredirects.php

@@ -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">