diff options
author | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
commit | cf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch) | |
tree | 86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/src/Alert | |
parent | 619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff) |
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/src/Alert')
-rwxr-xr-x | main/app/sprinkles/core/src/Alert/AlertStream.php | 144 | ||||
-rwxr-xr-x | main/app/sprinkles/core/src/Alert/CacheAlertStream.php | 84 | ||||
-rwxr-xr-x | main/app/sprinkles/core/src/Alert/SessionAlertStream.php | 70 |
3 files changed, 298 insertions, 0 deletions
diff --git a/main/app/sprinkles/core/src/Alert/AlertStream.php b/main/app/sprinkles/core/src/Alert/AlertStream.php new file mode 100755 index 0000000..3946cbf --- /dev/null +++ b/main/app/sprinkles/core/src/Alert/AlertStream.php @@ -0,0 +1,144 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Core\Alert; + +use UserFrosting\Fortress\ServerSideValidator; + +/** + * AlertStream Class + * + * Implements an alert stream for use between HTTP requests, with i18n support via the MessageTranslator class + * + * @author Alex Weissman (https://alexanderweissman.com) + * @see http://www.userfrosting.com/components/#messages + */ +abstract class AlertStream +{ + + /** + * @var string + */ + protected $messagesKey; + + /** + * @var UserFrosting\I18n\MessageTranslator|null + */ + protected $messageTranslator = null; + + /** + * Create a new message stream. + */ + public function __construct($messagesKey, $translator = null) + { + $this->messagesKey = $messagesKey; + + $this->setTranslator($translator); + } + + /** + * Set the translator to be used for all message streams. Must be done before `addMessageTranslated` can be used. + * + * @param UserFrosting\I18n\MessageTranslator $translator A MessageTranslator to be used to translate messages when added via `addMessageTranslated`. + */ + public function setTranslator($translator) + { + $this->messageTranslator = $translator; + return $this; + } + + /** + * Adds a raw text message to the cache message stream. + * + * @param string $type The type of message, indicating how it will be styled when outputted. Should be set to "success", "danger", "warning", or "info". + * @param string $message The message to be added to the message stream. + * @return MessageStream this MessageStream object. + */ + public function addMessage($type, $message) + { + $messages = $this->messages(); + $messages[] = array( + "type" => $type, + "message" => $message + ); + $this->saveMessages($messages); + return $this; + } + + /** + * Adds a text message to the cache message stream, translated into the currently selected language. + * + * @param string $type The type of message, indicating how it will be styled when outputted. Should be set to "success", "danger", "warning", or "info". + * @param string $messageId The message id for the message to be added to the message stream. + * @param array[string] $placeholders An optional hash of placeholder names => placeholder values to substitute into the translated message. + * @return MessageStream this MessageStream object. + */ + public function addMessageTranslated($type, $messageId, $placeholders = array()) + { + if (!$this->messageTranslator){ + throw new \RuntimeException("No translator has been set! Please call MessageStream::setTranslator first."); + } + + $message = $this->messageTranslator->translate($messageId, $placeholders); + return $this->addMessage($type, $message); + } + + /** + * Get the messages and then clear the message stream. + * This function does the same thing as `messages()`, except that it also clears all messages afterwards. + * This is useful, because typically we don't want to view the same messages more than once. + * + * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. + */ + public function getAndClearMessages() + { + $messages = $this->messages(); + $this->resetMessageStream(); + return $messages; + } + + /** + * Add error messages from a ServerSideValidator object to the message stream. + * + * @param ServerSideValidator $validator + */ + public function addValidationErrors(ServerSideValidator $validator) + { + foreach ($validator->errors() as $idx => $field) { + foreach($field as $eidx => $error) { + $this->addMessage("danger", $error); + } + } + } + + /** + * Return the translator for this message stream. + * + * @return MessageTranslator The translator for this message stream. + */ + public function translator() + { + return $this->messageTranslator; + } + + /** + * Get the messages from this message stream. + * + * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. + */ + abstract public function messages(); + + /** + * Clear all messages from this message stream. + */ + abstract public function resetMessageStream(); + + /** + * Save messages to the stream + */ + abstract protected function saveMessages($message); +} diff --git a/main/app/sprinkles/core/src/Alert/CacheAlertStream.php b/main/app/sprinkles/core/src/Alert/CacheAlertStream.php new file mode 100755 index 0000000..1fd5131 --- /dev/null +++ b/main/app/sprinkles/core/src/Alert/CacheAlertStream.php @@ -0,0 +1,84 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Core\Alert; + +use Illuminate\Cache\Repository as Cache; +use UserFrosting\I18n\MessageTranslator; +use UserFrosting\Support\Repository\Repository; + +/** + * CacheAlertStream Class + * Implements a message stream for use between HTTP requests, with i18n + * support via the MessageTranslator class using the cache system to store + * the alerts. Note that the tags are added each time instead of the + * constructor since the session_id can change when the user logs in or out + * + * @author Louis Charette + */ +class CacheAlertStream extends AlertStream +{ + /** + * @var Cache Object We use the cache object so that added messages will automatically appear in the cache. + */ + protected $cache; + + /** + * @var Repository Object We use the cache object so that added messages will automatically appear in the cache. + */ + protected $config; + + /** + * Create a new message stream. + * + * @param string $messagesKey Store the messages under this key + * @param MessageTranslator|null $translator + * @param Cache $cache + * @param Repository $config + */ + public function __construct($messagesKey, MessageTranslator $translator = null, Cache $cache, Repository $config) + { + $this->cache = $cache; + $this->config = $config; + parent::__construct($messagesKey, $translator); + } + + /** + * Get the messages from this message stream. + * + * @return array An array of messages, each of which is itself an array containing 'type' and 'message' fields. + */ + public function messages() + { + if ($this->cache->tags('_s'.session_id())->has($this->messagesKey)) { + return $this->cache->tags('_s'.session_id())->get($this->messagesKey) ?: []; + } else { + return []; + } + } + + /** + * Clear all messages from this message stream. + * + * @return void + */ + public function resetMessageStream() + { + $this->cache->tags('_s'.session_id())->forget($this->messagesKey); + } + + /** + * Save messages to the stream + * + * @param string $messages The message + * @return void + */ + protected function saveMessages($messages) + { + $this->cache->tags('_s'.session_id())->forever($this->messagesKey, $messages); + } +} diff --git a/main/app/sprinkles/core/src/Alert/SessionAlertStream.php b/main/app/sprinkles/core/src/Alert/SessionAlertStream.php new file mode 100755 index 0000000..8b4604b --- /dev/null +++ b/main/app/sprinkles/core/src/Alert/SessionAlertStream.php @@ -0,0 +1,70 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Core\Alert; + +use UserFrosting\I18n\MessageTranslator; +use UserFrosting\Session\Session; + +/** + * SessionAlertStream Class + * Implements a message stream for use between HTTP requests, with i18n support via the MessageTranslator class + * Using the session storage to store the alerts + * + * @author Alex Weissman (https://alexanderweissman.com) + */ +class SessionAlertStream extends AlertStream +{ + /** + * @var Session We use the session object so that added messages will automatically appear in the session. + */ + protected $session; + + /** + * Create a new message stream. + * + * @param string $messagesKey Store the messages under this key + * @param MessageTranslator|null $translator + * @param Session $session + */ + public function __construct($messagesKey, MessageTranslator $translator = null, Session $session) + { + $this->session = $session; + parent::__construct($messagesKey, $translator); + } + + /** + * Get the messages from this message stream. + * + * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. + */ + public function messages() + { + return $this->session[$this->messagesKey] ?: []; + } + + /** + * Clear all messages from this message stream. + * + * @return void + */ + public function resetMessageStream() + { + $this->session[$this->messagesKey] = []; + } + + /** + * Save messages to the stream + * + * @param string $messages The message + * @return void + */ + protected function saveMessages($messages) + { + $this->session[$this->messagesKey] = $messages; + } +} |