From ab23338fe9f1b3ed21455867f1c032d7b146ceb8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 2 Mar 2015 16:51:04 +0100 Subject: Initial Commit --- inc/message.inc.php | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 inc/message.inc.php (limited to 'inc/message.inc.php') diff --git a/inc/message.inc.php b/inc/message.inc.php new file mode 100644 index 0000000..7decc12 --- /dev/null +++ b/inc/message.inc.php @@ -0,0 +1,119 @@ + $type, + 'id' => $id, + 'params' => array_slice($params, 1) + ); + if (self::$flushed) self::renderList(); + } + + /** + * Render all currently queued messages, flushing the queue. + * After calling this, any further calls to add* will be rendered in + * place in the current page output. + */ + public static function renderList() + { + // Non-Ajax + foreach (self::$list as $item) { + $message = $item['id']; // Dictionary::getMessage($item['id']); + foreach ($item['params'] as $index => $text) { + $message = str_replace('{{' . $index . '}}', '' . htmlspecialchars($text) . '', $message); + } + Render::addTemplate('messagebox-' . $item['type'], array('message' => $message)); + self::$alreadyDisplayed[] = $item; + } + self::$list = array(); + self::$flushed = true; + } + + /** + * Get all queued messages, flushing the queue. + * Useful in api/ajax mode. + */ + public static function asString() + { + $return = ''; + foreach (self::$list as $item) { + $message = $item['id']; // Dictionary::getMessage($item['id']); + foreach ($item['params'] as $index => $text) { + $message = str_replace('{{' . $index . '}}', $text, $message); + } + $return .= '[' . $item['type'] . ']: ' . $message . "\n"; + self::$alreadyDisplayed[] = $item; + } + self::$list = array(); + return $return; + } + + /** + * Deserialize any messages from the current HTTP request and + * place them in the message queue. + */ + public static function fromRequest() + { + $messages = is_array($_REQUEST['message']) ? $_REQUEST['message'] : array($_REQUEST['message']); + foreach ($messages as $message) { + $data = explode('|', $message); + if (count($data) < 2 || !preg_match('/^(error|warning|info|success)$/', $data[0])) continue; + self::add($data[0], $data[1], array_slice($data, 1)); + } + } + + /** + * Turn the current message queue into a serialized version, + * suitable for appending to a GET or POST request + */ + public static function toRequest() + { + $parts = array(); + foreach (array_merge(self::$list, self::$alreadyDisplayed) as $item) { + $str = 'message[]=' . urlencode($item['type'] . '|' .$item['id']); + if (!empty($item['params'])) { + $str .= '|' . implode('|', $item['params']); + } + $parts[] = $str; + } + return implode('&', $parts); + } + +} + -- cgit v1.2.3-55-g7522