Permalink
Browse files

feature(ui): Allows modifying system messages/errors

This adds functions to get and set all the system messages (and errors),
allowing plugins to remove/replace messages or errors already registered.

This also deprecates the Swiss army function system_messages().
  • Loading branch information...
mrclay committed Sep 23, 2015
1 parent a17f78e commit eee183c549320eff63406cfa825679b7260b2653
@@ -0,0 +1,28 @@
+<?php
+namespace Elgg\SystemMessages;
+
+/**
+ * Represents the state of system messages and errors.
+ *
+ * This is returned by elgg_get_system_messages() and must be given to elgg_set_system_messages().
+ *
+ * @see elgg_get_system_messages
+ * @see elgg_set_system_messages
+ * @since 2.1
+ */
+class RegisterSet {
+
+ /**
+ * @var string[] Strings added via system_message()
+ *
+ * @note do not change this property name. It must match SystemMessagesService::SUCCESS
+ */
+ public $success = [];
+
+ /**
+ * @var string[] Strings added via register_error()
+ *
+ * @note do not change this property name. It must match SystemMessagesService::ERROR
+ */
+ public $error = [];
+}
@@ -1,6 +1,8 @@
<?php
namespace Elgg;
+use Elgg\SystemMessages\RegisterSet;
+
/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
*
@@ -14,6 +16,10 @@
*/
class SystemMessagesService {
+ const SUCCESS = 'success';
+ const ERROR = 'error';
+ const SESSION_KEY = 'msg';
+
/**
* @var \ElggSession
*/
@@ -32,23 +38,30 @@ public function __construct(\ElggSession $session) {
* Empty and return the given register or all registers. In each case, the return value is
* a filtered version of the full registers array.
*
- * @param string $name The register. Empty string for all.
+ * @param string $register_name The register. Empty string for all.
*
* @return array The array of registers dumped
*/
- function dumpRegister($name = '') {
- $registers = $this->loadRegisters($name);
-
- if ($name !== "") {
- $return = array();
- $return[$name] = empty($registers[$name]) ? [] : $registers[$name];
- unset($registers[$name]);
- } else {
- $return = $registers;
- $registers = array();
+ public function dumpRegister($register_name = '') {
+ $set = $this->loadRegisters();
+ $return = [];
+
+ foreach ($set as $prop => $values) {
+ if ($register_name === $prop || $register_name === '') {
+ if ($values || $register_name === $prop) {
+ $return[$prop] = $values;
+ }
+
+ $set->{$prop} = [];
+ }
}
- $this->saveRegisters($registers);
+ // support arbitrary registers for 2.0 BC
+ if ($register_name && !isset($return[$register_name])) {
+ $return[$register_name] = [];
+ }
+
+ $this->saveRegisters($set);
return $return;
}
@@ -59,17 +72,16 @@ function dumpRegister($name = '') {
*
* @return integer The number of messages
*/
- function count($register_name = "") {
- $registers = $this->loadRegisters($register_name);
+ public function count($register_name = "") {
+ $set = $this->loadRegisters();
+ $count = 0;
- if ($register_name !== '') {
- return empty($registers[$register_name]) ? 0 : count($registers[$register_name]);
+ foreach ($set as $prop => $values) {
+ if ($register_name === $prop || $register_name === '') {
+ $count += count($values);
+ }
}
- $count = 0;
- foreach ($registers as $register) {
- $count += count($register);
- }
return $count;
}
@@ -78,85 +90,78 @@ function count($register_name = "") {
*
* @see system_messages()
*
- * @param string|array $message Message or messages to add
+ * @param string|string[] $message Message or messages to add
*
- * @return bool
+ * @return void
*/
- function addSuccessMessage($message) {
- return $this->addMessageToRegister($message, "success");
+ public function addSuccessMessage($message) {
+ $set = $this->loadRegisters();
+ foreach ((array)$message as $str) {
+ $set->success[] = $str;
+ }
+ $this->saveRegisters($set);
}
/**
* Display an error on next page load.
*
* @see system_messages()
*
- * @param string|array $error Error or errors to add
+ * @param string|string[] $error Error or errors to add
*
- * @return bool
- */
- function addErrorMessage($error) {
- return $this->addMessageToRegister($error, "error");
- }
-
- /**
- * Add a message(s) to a named register to be displayed
- *
- * Messages will not be displayed immediately, but are stored in the queue
- * for later display, usually upon next page load.
- *
- * The method of displaying these messages differs depending upon plugins and
- * viewtypes. The core default viewtype retrieves messages in
- * {@link views/default/page/shells/default.php} and displays messages as
- * javascript popups.
- *
- * @internal Messages are stored as strings in the Elgg session as ['msg'][$register] array.
- *
- * @param string|array $message
- * @param string $register_name
- *
- * @return bool
- * @access private
+ * @return void
*/
- function addMessageToRegister($message, $register_name = '') {
- $registers = $this->loadRegisters($register_name);
-
- if (is_string($message)) {
- $message = array($message);
+ public function addErrorMessage($error) {
+ $set = $this->loadRegisters();
+ foreach ((array)$error as $str) {
+ $set->error[] = $str;
}
- if (!isset($registers[$register_name])) {
- $registers[$register_name] = [];
- }
- $registers[$register_name] = array_merge($registers[$register_name], $message);
-
- $this->saveRegisters($registers);
- return true;
+ $this->saveRegisters($set);
}
/**
* Load the registers from the session
*
- * @param string $accessed_register The register being accessed
- *
- * @return array
+ * @return RegisterSet
*/
- protected function loadRegisters($accessed_register = '') {
- $registers = $this->session->get('msg', array());
-
- if (!isset($registers[$accessed_register]) && $accessed_register !== '') {
- $registers[$accessed_register] = array();
+ public function loadRegisters() {
+ $registers = $this->session->get(self::SESSION_KEY, array());
+ $set = new RegisterSet();
+ foreach ($registers as $key => $register) {
+ $set->{$key} = $register;
}
-
- return $registers;
+ return $set;
}
/**
* Save the registers to the session
*
- * @param array $registers The message registers
+ * The method of displaying these messages differs depending upon plugins and
+ * viewtypes. The core default viewtype retrieves messages in
+ * {@link views/default/page/shells/default.php} and displays messages as
+ * javascript popups.
+ *
+ * Messages are stored as strings in the Elgg session as ['msg'][$register] array.
+ *
+ * @param RegisterSet $set The set of registers
* @return void
*/
- protected function saveRegisters(array $registers) {
- $this->session->set('msg', $registers);
+ public function saveRegisters(RegisterSet $set) {
+ $filter = function ($el) {
+ return is_string($el) && $el !== "";
+ };
+
+ $data = [];
+ foreach ($set as $prop => $values) {
+ if (!is_array($values)) {
+ continue;
+ }
+ $arr = array_filter($values, $filter);
+ if ($arr) {
+ $data[$prop] = array_values($arr);
+ }
+ }
+
+ $this->session->set(self::SESSION_KEY, $data);
}
}
@@ -12,3 +12,54 @@ function row_to_elggrelationship($row) {
elgg_deprecated_notice(__FUNCTION__ . " is deprecated.", 2.1);
return _elgg_services()->relationshipsTable->rowToElggRelationship($row);
}
+
+/**
+ * Queues a message to be displayed.
+ *
+ * Messages will not be displayed immediately, but are stored in
+ * for later display, usually upon next page load.
+ *
+ * The method of displaying these messages differs depending upon plugins and
+ * viewtypes. The core default viewtype retrieves messages in
+ * {@link views/default/page/shells/default.php} and displays messages as
+ * javascript popups.
+ *
+ * @note Internal: Messages are stored as strings in the Elgg session as ['msg'][$register] array.
+ *
+ * @warning This function is used to both add to and clear the message
+ * stack. If $messages is null, $register will be returned and cleared.
+ * If $messages is null and $register is empty, all messages will be
+ * returned and removed.
+ *
+ * @param mixed $message Optionally, a single message or array of messages to add, (default: null)
+ * @param string $register Types of message: "error", "success" (default: success)
+ * @param bool $count Count the number of messages (default: false)
+ *
+ * @return bool|array Either the array of messages, or a response regarding
+ * whether the message addition was successful.
+ *
+ * @deprecated
+ */
+function system_messages($message = null, $register = "success", $count = false) {
+ elgg_deprecated_notice(__FUNCTION__ . ' is deprecated', '2.1');
+
+ $svc = _elgg_services()->systemMessages;
+ if ($count) {
+ return $svc->count($register);
+ }
+ if ($message === null) {
+ return $svc->dumpRegister($register);
+ }
+ if (!$register) {
+ return false;
+ }
+ $set = $svc->loadRegisters();
+ if (!isset($set->{$register})) {
+ $set->{$register} = [];
+ }
+ foreach ((array)$message as $str) {
+ $set->{$register}[] = $str;
+ }
+ $svc->saveRegisters($set);
+ return true;
+}
View
@@ -2,7 +2,6 @@
use Elgg\Filesystem\Directory;
-
/**
* Bootstrapping and helper procedural code available for use in Elgg core and plugins.
*
@@ -413,41 +412,6 @@ function sanitise_filepath($path, $append_slash = true) {
return $path;
}
-/**
- * Queues a message to be displayed.
- *
- * Messages will not be displayed immediately, but are stored in
- * for later display, usually upon next page load.
- *
- * The method of displaying these messages differs depending upon plugins and
- * viewtypes. The core default viewtype retrieves messages in
- * {@link views/default/page/shells/default.php} and displays messages as
- * javascript popups.
- *
- * @note Internal: Messages are stored as strings in the Elgg session as ['msg'][$register] array.
- *
- * @warning This function is used to both add to and clear the message
- * stack. If $messages is null, $register will be returned and cleared.
- * If $messages is null and $register is empty, all messages will be
- * returned and removed.
- *
- * @param mixed $message Optionally, a single message or array of messages to add, (default: null)
- * @param string $register Types of message: "error", "success" (default: success)
- * @param bool $count Count the number of messages (default: false)
- *
- * @return bool|array Either the array of messages, or a response regarding
- * whether the message addition was successful.
- */
-function system_messages($message = null, $register = "success", $count = false) {
- if ($count) {
- return _elgg_services()->systemMessages->count($register);
- }
- if ($message === null) {
- return _elgg_services()->systemMessages->dumpRegister($register);
- }
- return _elgg_services()->systemMessages->addMessageToRegister($message, $register);
-}
-
/**
* Counts the number of messages, either globally or in a particular register
*
@@ -469,7 +433,8 @@ function count_messages($register = "") {
* @return bool
*/
function system_message($message) {
- return _elgg_services()->systemMessages->addSuccessMessage($message);
+ _elgg_services()->systemMessages->addSuccessMessage($message);
+ return true;
}
/**
@@ -482,7 +447,29 @@ function system_message($message) {
* @return bool
*/
function register_error($error) {
- return _elgg_services()->systemMessages->addErrorMessage($error);
+ _elgg_services()->systemMessages->addErrorMessage($error);
+ return true;
+}
+
+/**
+ * Get a copy of the current system messages.
+ *
+ * @return \Elgg\SystemMessages\RegisterSet
+ * @since 2.1
+ */
+function elgg_get_system_messages() {
+ return _elgg_services()->systemMessages->loadRegisters();
+}
+
+/**
+ * Set the system messages. This will overwrite the state of all messages and errors!
+ *
+ * @param \Elgg\SystemMessages\RegisterSet $set Set of messages
+ * @return void
+ * @since 2.1
+ */
+function elgg_set_system_messages(\Elgg\SystemMessages\RegisterSet $set) {
+ _elgg_services()->systemMessages->saveRegisters($set);
}
/**
Oops, something went wrong.

0 comments on commit eee183c

Please sign in to comment.