2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Message
12 * simple usage examples:
14 * // display simple error message 'Error'
15 * PMA_Message::error()->display();
17 * // get simple success message 'Success'
18 * $message = PMA_Message::success();
20 * // get special notice 'Some locale notice'
21 * $message = PMA_Message::notice('strSomeLocaleNotice');
24 * more advanced usage example:
26 * // create a localized success message
27 * $message = PMA_Message::success('strSomeLocaleMessage');
29 * // create another message, a hint, with a localized string which expects
30 * // two parameters: $strSomeFootnote = 'Read the %smanual%s'
31 * $hint = PMA_Message::notice('strSomeFootnote');
32 * // replace %d with the following params
33 * $hint->addParam('[a@./Documentation.html#cfg_Example@_blank]');
34 * $hint->addParam('[/a]');
35 * // add this hint as a footnote
36 * $hint = PMA_showHint($hint);
38 * // add the retrieved footnote reference to the original message
39 * $message->addMessage($hint);
41 * // create another message ...
42 * $more = PMA_Message::notice('strSomeMoreLocale');
43 * $more->addString('strSomeEvenMoreLocale', '<br />');
44 * $more->addParam('parameter for strSomeMoreLocale');
45 * $more->addParam('more parameter for strSomeMoreLocale');
47 * // and add it also to the original message
48 * $message->addMessage($more);
49 * // finally add another raw message
50 * $message->addMessage('some final words', ' - ');
52 * // display() will now print all messages in the same order as they are added
53 * $message->display();
54 * // strSomeLocaleMessage <sup>1</sup> strSomeMoreLocale<br />
55 * // strSomeEvenMoreLocale - some final words
61 const SUCCESS
= 1; // 0001
62 const NOTICE
= 2; // 0010
63 const ERROR
= 8; // 1000
65 const SANITIZE_NONE
= 0; // 0000 0000
66 const SANITIZE_STRING
= 16; // 0001 0000
67 const SANITIZE_PARAMS
= 32; // 0010 0000
68 const SANITIZE_BOOTH
= 48; // 0011 0000
75 static public $level = array (
76 PMA_Message
::SUCCESS
=> 'success',
77 PMA_Message
::NOTICE
=> 'notice',
78 PMA_Message
::ERROR
=> 'error',
87 protected $_number = PMA_Message
::NOTICE
;
90 * The locale string identifier
95 protected $_string = '';
98 * The formatted message
103 protected $_message = '';
106 * Whether the message was already displayed
111 protected $_is_displayed = false;
119 protected $_hash = null;
127 protected $_params = array();
130 * holds additional messages
135 protected $_added_messages = array();
140 * @param string $string
141 * @param integer $number
142 * @param array $params
143 * @param integer $sanitize
145 public function __construct($string = '', $number = PMA_Message
::NOTICE
,
146 $params = array(), $sanitize = PMA_Message
::SANITIZE_NONE
)
148 $this->setString($string, $sanitize & PMA_Message
::SANITIZE_STRING
);
149 $this->setNumber($number);
150 $this->setParams($params, $sanitize & PMA_Message
::SANITIZE_PARAMS
);
154 * magic method: return string representation for this object
158 public function __toString()
160 return $this->getMessage();
164 * get PMA_Message of type success
166 * shorthand for getting a simple success message
169 * @param string $string a localized string e.g. __('Your SQL query has been executed successfully')
170 * @return PMA_Message
172 static public function success($string = '')
174 if (empty($string)) {
175 $string = __('Your SQL query has been executed successfully');
178 return new PMA_Message($string, PMA_Message
::SUCCESS
);
182 * get PMA_Message of type error
184 * shorthand for getting a simple error message
187 * @param string $string a localized string e.g. __('Error')
188 * @return PMA_Message
190 static public function error($string = '')
192 if (empty($string)) {
193 $string = __('Error');
196 return new PMA_Message($string, PMA_Message
::ERROR
);
200 * get PMA_Message of type notice
202 * shorthand for getting a simple notice message
205 * @param string $string a localized string e.g. __('The additional features for working with linked tables have been deactivated. To find out why click %shere%s.')
206 * @return PMA_Message
208 static public function notice($string)
210 return new PMA_Message($string, PMA_Message
::NOTICE
);
214 * get PMA_Message with customized content
216 * shorthand for getting a customized message
219 * @param string $message
220 * @param integer $type
221 * @return PMA_Message
223 static public function raw($message, $type = PMA_Message
::NOTICE
)
225 $r = new PMA_Message('', $type);
226 $r->setMessage($message);
231 * get PMA_Message for number of affected rows
233 * shorthand for getting a customized message
236 * @param integer $rows Number of rows
237 * @return PMA_Message
239 static public function affected_rows($rows)
241 $message = PMA_Message
::success(_ngettext('%1$d row affected.', '%1$d rows affected.', $rows));
242 $message->addParam($rows);
247 * get PMA_Message for number of deleted rows
249 * shorthand for getting a customized message
252 * @param integer $rows Number of rows
253 * @return PMA_Message
255 static public function deleted_rows($rows)
257 $message = PMA_Message
::success(_ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows));
258 $message->addParam($rows);
263 * get PMA_Message for number of inserted rows
265 * shorthand for getting a customized message
268 * @param integer $rows Number of rows
269 * @return PMA_Message
271 static public function inserted_rows($rows)
273 $message = PMA_Message
::success(_ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows));
274 $message->addParam($rows);
279 * get PMA_Message of type error with custom content
281 * shorthand for getting a customized error message
284 * @param string $message
285 * @return PMA_Message
287 static public function rawError($message)
289 return PMA_Message
::raw($message, PMA_Message
::ERROR
);
293 * get PMA_Message of type notice with custom content
295 * shorthand for getting a customized notice message
298 * @param string $message
299 * @return PMA_Message
301 static public function rawNotice($message)
303 return PMA_Message
::raw($message, PMA_Message
::NOTICE
);
307 * get PMA_Message of type success with custom content
309 * shorthand for getting a customized success message
312 * @param string $message
313 * @return PMA_Message
315 static public function rawSuccess($message)
317 return PMA_Message
::raw($message, PMA_Message
::SUCCESS
);
321 * returns whether this message is a success message or not
322 * and optionaly makes this message a success message
324 * @param boolean $set
325 * @return boolean whether this is a success message or not
327 public function isSuccess($set = false)
330 $this->setNumber(PMA_Message
::SUCCESS
);
333 return $this->getNumber() === PMA_Message
::SUCCESS
;
337 * returns whether this message is a notice message or not
338 * and optionally makes this message a notice message
340 * @param boolean $set
341 * @return boolean whether this is a notice message or not
343 public function isNotice($set = false)
346 $this->setNumber(PMA_Message
::NOTICE
);
349 return $this->getNumber() === PMA_Message
::NOTICE
;
353 * returns whether this message is an error message or not
354 * and optionally makes this message an error message
356 * @param boolean $set
357 * @return boolean whether this is an error message or not
359 public function isError($set = false)
362 $this->setNumber(PMA_Message
::ERROR
);
365 return $this->getNumber() === PMA_Message
::ERROR
;
369 * set raw message (overrides string)
371 * @param string $message
372 * @param boolean $sanitize whether to sanitize $message or not
374 public function setMessage($message, $sanitize = false)
377 $message = PMA_Message
::sanitize($message);
379 $this->_message
= $message;
383 * set string (does not take effect if raw message is set)
385 * @param string $_string
386 * @param boolean $sanitize whether to sanitize $string or not
388 public function setString($_string, $sanitize = true)
391 $_string = PMA_Message
::sanitize($_string);
393 $this->_string
= $_string;
397 * set message type number
399 * @param integer $number
401 public function setNumber($number)
403 $this->_number
= $number;
407 * add parameter, usually in conjunction with strings
411 * $message->addParam('strLocale', false);
412 * $message->addParam('[em]some string[/em]');
413 * $message->addParam('<img src="img" />', false);
416 * @param mixed $param
417 * @param boolean $raw
419 public function addParam($param, $raw = true)
421 if ($param instanceof PMA_Message
) {
422 $this->_params
[] = $param;
424 $this->_params
[] = htmlspecialchars($param);
426 $this->_params
[] = PMA_Message
::notice($param);
431 * add another string to be concatenated on displaying
433 * @param string $string to be added
434 * @param string $separator to use between this and previous string/message
436 public function addString($string, $separator = ' ')
438 $this->_added_messages
[] = $separator;
439 $this->_added_messages
[] = PMA_Message
::notice($string);
443 * add a bunch of messages at once
445 * @param array $messages to be added
446 * @param string $separator to use between this and previous string/message
448 public function addMessages($messages, $separator = ' ')
450 foreach ($messages as $message) {
451 $this->addMessage($message, $separator);
456 * add another raw message to be concatenated on displaying
458 * @param mixed $message to be added
459 * @param string $separator to use between this and previous string/message
461 public function addMessage($message, $separator = ' ')
463 if (strlen($separator)) {
464 $this->_added_messages
[] = $separator;
467 if ($message instanceof PMA_Message
) {
468 $this->_added_messages
[] = $message;
470 $this->_added_messages
[] = PMA_Message
::rawNotice($message);
475 * set all params at once, usually used in conjunction with string
477 * @param array $params
478 * @param boolean $sanitize
480 public function setParams($params, $sanitize = false)
483 $params = PMA_Message
::sanitize($params);
485 $this->_params
= $params;
489 * return all parameters
493 public function getParams()
495 return $this->_params
;
499 * return all added messages
503 public function getAddedMessages()
505 return $this->_added_messages
;
512 * @param mixed $message the message(s)
513 * @return mixed the sanitized message(s)
516 static public function sanitize($message)
518 if (is_array($message)) {
519 foreach ($message as $key => $val) {
520 $message[$key] = PMA_Message
::sanitize($val);
526 return htmlspecialchars($message);
530 * decode $message, taking into account our special codes
534 * @param string $message the message
535 * @return string the decoded message
538 static public function decodeBB($message)
540 return PMA_sanitize($message, false, true);
544 * wrapper for sprintf()
546 * @return string formatted
548 static public function format()
550 $params = func_get_args();
551 if (isset($params[1]) && is_array($params[1])) {
552 array_unshift($params[1], $params[0]);
553 $params = $params[1];
556 return call_user_func_array('sprintf', $params);
560 * returns unique PMA_Message::$_hash, if not exists it will be created
562 * @return string PMA_Message::$_hash
564 public function getHash()
566 if (null === $this->_hash
) {
578 * returns compiled message
580 * @return string complete message
582 public function getMessage()
584 $message = $this->_message
;
586 if (0 === strlen($message)) {
587 $string = $this->getString();
588 if (isset($GLOBALS[$string])) {
589 $message = $GLOBALS[$string];
590 } elseif (0 === strlen($string)) {
597 if (count($this->getParams()) > 0) {
598 $message = PMA_Message
::format($message, $this->getParams());
601 $message = PMA_Message
::decodeBB($message);
603 foreach ($this->getAddedMessages() as $add_message) {
604 $message .= $add_message;
611 * returns PMA_Message::$_string
613 * @return string PMA_Message::$_string
615 public function getString()
617 return $this->_string
;
621 * returns PMA_Message::$_number
623 * @return integer PMA_Message::$_number
625 public function getNumber()
627 return $this->_number
;
631 * returns level of message
633 * @return string level of message
635 public function getLevel()
637 return PMA_Message
::$level[$this->getNumber()];
641 * Displays the message in HTML
644 public function display()
646 echo $this->getDisplay();
647 $this->isDisplayed(true);
651 * returns HTML code for displaying this message
654 * @return string whole message box
656 public function getDisplay()
658 return '<div class="' . $this->getLevel() . '">'
659 . $this->getMessage() . '</div>';
663 * sets and returns whether the message was displayed or not
665 * @param boolean $is_displayed
666 * @return boolean PMA_Message::$_is_displayed
668 public function isDisplayed($is_displayed = false)
671 $this->_is_displayed
= true;
674 return $this->_is_displayed
;