-
Notifications
You must be signed in to change notification settings - Fork 78
/
fMessaging.php
234 lines (204 loc) · 6.91 KB
/
fMessaging.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Provides session-based messaging for page-to-page communication
*
* @copyright Copyright (c) 2007-2010 Will Bond
* @author Will Bond [wb] <[email protected]>
* @author Jeff Turcotte [jt] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fMessaging
*
* @version 1.0.0b8
* @changes 1.0.0b8 [BREAK] Now messages always print as divs. Also added class option [jt, 2013-04-09]
* @changes 1.0.0b7 Fixed a small PHPDoc error [wb, 2010-03-15]
* @changes 1.0.0b6 Updated class to use new fSession API [wb, 2009-10-23]
* @changes 1.0.0b5 Made the `$recipient` parameter optional for all methods [wb, 2009-07-08]
* @changes 1.0.0b4 Added support for `'*'` and arrays of names to ::check() [wb, 2009-06-02]
* @changes 1.0.0b3 Updated class to use new fSession API [wb, 2009-05-08]
* @changes 1.0.0b2 Changed ::show() to accept more than one message name, or * for all messages [wb, 2009-01-12]
* @changes 1.0.0b The initial implementation [wb, 2008-03-05]
*/
class fMessaging
{
// The following constants allow for nice looking callbacks to static methods
const check = 'fMessaging::check';
const create = 'fMessaging::create';
const reset = 'fMessaging::reset';
const retrieval = 'fMessaging::retrieval';
const show = 'fMessaging::show';
/**
* The class to apply to all messages along with the specified class
*
* @var string
*/
static protected $class = '';
/**
* Checks to see if a message exists of the name specified for the recipient specified
*
* @param string $name The name or array of names of the message(s) to check for, or `'*'` to check for any
* @param string $recipient The intended recipient
* @return boolean If a message of the name and recipient specified exists
*/
static public function check($name, $recipient=NULL)
{
if ($recipient === NULL) {
$recipient = '{default}';
}
// Check all messages if * is specified
if (is_string($name) && $name == '*') {
fSession::open();
$prefix = __CLASS__ . '::' . $recipient . '::';
$keys = array_keys($_SESSION);
foreach ($keys as $key) {
if (strpos($key, $prefix) === 0) {
return TRUE;
}
}
return FALSE;
}
// Handle checking multiple messages
if (is_array($name)) {
foreach ($names as $name) {
if (self::check($name, $recipient)) {
return TRUE;
}
}
return FALSE;
}
return fSession::get(__CLASS__ . '::' . $recipient . '::' . $name, NULL) !== NULL;
}
/**
* Creates a message that is stored in the session and retrieved by another page
*
* @param string $name A name for the message
* @param string $recipient The intended recipient - this may be ommitted
* @param string $message The message to send
* @param string :$name
* @param string :$message
* @return void
*/
static public function create($name, $recipient, $message=NULL)
{
// This allows for the $recipient parameter to be optional
if ($message === NULL) {
$message = $recipient;
$recipient = '{default}';
}
fSession::set(__CLASS__ . '::' . $recipient . '::' . $name, $message);
}
/**
* Resets the data of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
fSession::clear(__CLASS__ . '::');
}
/**
* Retrieves and removes a message from the session
*
* @param string $name The name of the message to retrieve
* @param string $recipient The intended recipient
* @return string The message contents
*/
static public function retrieve($name, $recipient=NULL)
{
if ($recipient === NULL) {
$recipient = '{default}';
}
$key = __CLASS__ . '::' . $recipient . '::' . $name;
$message = fSession::get($key, NULL);
fSession::delete($key);
return $message;
}
/**
* Configures a class to be present in all messages when no custom css class is given
*
* @param string $class A class for all messages
* @return void
*/
static public function setClass($class)
{
self::$class = $class;
}
/**
* Retrieves a message, removes it from the session and prints it - will not print if no content
*
* The message will be printed in a `p` tag if it does not contain
* any block level HTML, otherwise it will be printed in a `div` tag.
*
* @param mixed $name The name or array of names of the message(s) to show, or `'*'` to show all
* @param string $recipient The intended recipient
* @param string $css_class Overrides using the `$name` as the CSS class when displaying the message - only used if a single `$name` is specified
* @return boolean If one or more messages was shown
*/
static public function show($name, $recipient=NULL, $css_class=NULL)
{
if ($recipient === NULL) {
$recipient = '{default}';
}
// Find all messages if * is specified
if (is_string($name) && $name == '*') {
fSession::open();
$prefix = __CLASS__ . '::' . $recipient . '::';
$keys = array_keys($_SESSION);
$name = array();
foreach ($keys as $key) {
if (strpos($key, $prefix) === 0) {
$name[] = substr($key, strlen($prefix));
}
}
}
// Handle showing multiple messages
if (is_array($name)) {
$shown = FALSE;
$names = $name;
foreach ($names as $name) {
$class = trim(self::$class . ' ' . $name);
$class = ($css_class === NULL) ? $class : $css_class;
$shown = fHTML::show(
self::retrieve($name, $recipient),
$class,
TRUE
) || $shown;
}
return $shown;
}
$class = self::$class . ' ' . $name;
$class = ($css_class === NULL) ? $class : $css_class;
// Handle a single message
return fHTML::show(self::retrieve($name, $recipient), $class, TRUE);
}
/**
* Forces use as a static class
*
* @return fMessaging
*/
private function __construct() { }
}
/**
* Copyright (c) 2007-2010 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/