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
21 * $message = PMA_Message::notice(__('This is a localized notice'));
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: $strSomeTooltip = 'Read the %smanual%s'
31 * $hint = PMA_Message::notice('strSomeTooltip');
32 * // replace placeholders with the following params
33 * $hint->addParam('[doc@cfg_Example]');
34 * $hint->addParam('[/doc]');
35 * // add this hint as a tooltip
36 * $hint = showHint($hint);
38 * // add the retrieved tooltip 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
62 const SUCCESS
= 1; // 0001
63 const NOTICE
= 2; // 0010
64 const ERROR
= 8; // 1000
66 const SANITIZE_NONE
= 0; // 0000 0000
67 const SANITIZE_STRING
= 16; // 0001 0000
68 const SANITIZE_PARAMS
= 32; // 0010 0000
69 const SANITIZE_BOOTH
= 48; // 0011 0000
76 static public $level = array (
77 PMA_Message
::SUCCESS
=> 'success',
78 PMA_Message
::NOTICE
=> 'notice',
79 PMA_Message
::ERROR
=> 'error',
88 protected $number = PMA_Message
::NOTICE
;
91 * The locale string identifier
96 protected $string = '';
99 * The formatted message
104 protected $message = '';
107 * Whether the message was already displayed
112 protected $isDisplayed = false;
120 protected $hash = null;
128 protected $params = array();
131 * holds additional messages
136 protected $addedMessages = array();
141 * @param string $string The message to be displayed
142 * @param integer $number A numeric representation of the type of message
143 * @param array $params An array of parameters to use in the message
144 * @param integer $sanitize A flag to indicate what to sanitize, see
145 * constant definitions above
147 public function __construct($string = '', $number = PMA_Message
::NOTICE
,
148 $params = array(), $sanitize = PMA_Message
::SANITIZE_NONE
150 $this->setString($string, $sanitize & PMA_Message
::SANITIZE_STRING
);
151 $this->setNumber($number);
152 $this->setParams($params, $sanitize & PMA_Message
::SANITIZE_PARAMS
);
156 * magic method: return string representation for this object
160 public function __toString()
162 return $this->getMessage();
166 * get PMA_Message of type success
168 * shorthand for getting a simple success message
170 * @param string $string A localized string
171 * e.g. __('Your SQL query has been
172 * executed successfully')
174 * @return PMA_Message
177 static public function success($string = '')
179 if (empty($string)) {
180 $string = __('Your SQL query has been executed successfully.');
183 return new PMA_Message($string, PMA_Message
::SUCCESS
);
187 * get PMA_Message of type error
189 * shorthand for getting a simple error message
191 * @param string $string A localized string e.g. __('Error')
193 * @return PMA_Message
196 static public function error($string = '')
198 if (empty($string)) {
199 $string = __('Error');
202 return new PMA_Message($string, PMA_Message
::ERROR
);
206 * get PMA_Message of type notice
208 * shorthand for getting a simple notice message
210 * @param string $string A localized string
211 * e.g. __('The additional features for working with
212 * linked tables have been deactivated. To find out
213 * why click %shere%s.')
215 * @return PMA_Message
218 static public function notice($string)
220 return new PMA_Message($string, PMA_Message
::NOTICE
);
224 * get PMA_Message with customized content
226 * shorthand for getting a customized message
228 * @param string $message A localized string
229 * @param integer $type A numeric representation of the type of message
231 * @return PMA_Message
234 static public function raw($message, $type = PMA_Message
::NOTICE
)
236 $r = new PMA_Message('', $type);
237 $r->setMessage($message);
242 * get PMA_Message for number of affected rows
244 * shorthand for getting a customized message
246 * @param integer $rows Number of rows
248 * @return PMA_Message
251 static public function getMessageForAffectedRows($rows)
253 $message = PMA_Message
::success(
254 _ngettext('%1$d row affected.', '%1$d rows affected.', $rows)
256 $message->addParam($rows);
261 * get PMA_Message for number of deleted rows
263 * shorthand for getting a customized message
265 * @param integer $rows Number of rows
267 * @return PMA_Message
270 static public function getMessageForDeletedRows($rows)
272 $message = PMA_Message
::success(
273 _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows)
275 $message->addParam($rows);
280 * get PMA_Message for number of inserted rows
282 * shorthand for getting a customized message
284 * @param integer $rows Number of rows
286 * @return PMA_Message
289 static public function getMessageForInsertedRows($rows)
291 $message = PMA_Message
::success(
292 _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows)
294 $message->addParam($rows);
299 * get PMA_Message of type error with custom content
301 * shorthand for getting a customized error message
303 * @param string $message A localized string
305 * @return PMA_Message
308 static public function rawError($message)
310 return PMA_Message
::raw($message, PMA_Message
::ERROR
);
314 * get PMA_Message of type notice with custom content
316 * shorthand for getting a customized notice message
318 * @param string $message A localized string
320 * @return PMA_Message
323 static public function rawNotice($message)
325 return PMA_Message
::raw($message, PMA_Message
::NOTICE
);
329 * get PMA_Message of type success with custom content
331 * shorthand for getting a customized success message
333 * @param string $message A localized string
335 * @return PMA_Message
338 static public function rawSuccess($message)
340 return PMA_Message
::raw($message, PMA_Message
::SUCCESS
);
344 * returns whether this message is a success message or not
345 * and optionally makes this message a success message
347 * @param boolean $set Whether to make this message of SUCCESS type
349 * @return boolean whether this is a success message or not
351 public function isSuccess($set = false)
354 $this->setNumber(PMA_Message
::SUCCESS
);
357 return $this->getNumber() === PMA_Message
::SUCCESS
;
361 * returns whether this message is a notice message or not
362 * and optionally makes this message a notice message
364 * @param boolean $set Whether to make this message of NOTICE type
366 * @return boolean whether this is a notice message or not
368 public function isNotice($set = false)
371 $this->setNumber(PMA_Message
::NOTICE
);
374 return $this->getNumber() === PMA_Message
::NOTICE
;
378 * returns whether this message is an error message or not
379 * and optionally makes this message an error message
381 * @param boolean $set Whether to make this message of ERROR type
383 * @return boolean Whether this is an error message or not
385 public function isError($set = false)
388 $this->setNumber(PMA_Message
::ERROR
);
391 return $this->getNumber() === PMA_Message
::ERROR
;
395 * set raw message (overrides string)
397 * @param string $message A localized string
398 * @param boolean $sanitize Whether to sanitize $message or not
402 public function setMessage($message, $sanitize = false)
405 $message = PMA_Message
::sanitize($message);
407 $this->message
= $message;
411 * set string (does not take effect if raw message is set)
413 * @param string $string string to set
414 * @param boolean $sanitize whether to sanitize $string or not
418 public function setString($string, $sanitize = true)
421 $string = PMA_Message
::sanitize($string);
423 $this->string = $string;
427 * set message type number
429 * @param integer $number message type number to set
433 public function setNumber($number)
435 $this->number
= $number;
439 * add parameter, usually in conjunction with strings
443 * $message->addParam('strLocale', false);
444 * $message->addParam('[em]some string[/em]');
445 * $message->addParam('<img src="img" />', false);
448 * @param mixed $param parameter to add
449 * @param boolean $raw whether parameter should be passed as is
450 * without html escaping
454 public function addParam($param, $raw = true)
456 if ($param instanceof PMA_Message
) {
457 $this->params
[] = $param;
459 $this->params
[] = htmlspecialchars($param);
461 $this->params
[] = PMA_Message
::notice($param);
466 * add another string to be concatenated on displaying
468 * @param string $string to be added
469 * @param string $separator to use between this and previous string/message
473 public function addString($string, $separator = ' ')
475 $this->addedMessages
[] = $separator;
476 $this->addedMessages
[] = PMA_Message
::notice($string);
480 * add a bunch of messages at once
482 * @param array $messages to be added
483 * @param string $separator to use between this and previous string/message
487 public function addMessages($messages, $separator = ' ')
489 foreach ($messages as $message) {
490 $this->addMessage($message, $separator);
495 * add another raw message to be concatenated on displaying
497 * @param mixed $message to be added
498 * @param string $separator to use between this and previous string/message
502 public function addMessage($message, $separator = ' ')
504 if (/*overload*/mb_strlen($separator)) {
505 $this->addedMessages
[] = $separator;
508 if ($message instanceof PMA_Message
) {
509 $this->addedMessages
[] = $message;
511 $this->addedMessages
[] = PMA_Message
::rawNotice($message);
516 * set all params at once, usually used in conjunction with string
518 * @param array|string $params parameters to set
519 * @param boolean $sanitize whether to sanitize params
523 public function setParams($params, $sanitize = false)
526 $params = PMA_Message
::sanitize($params);
528 $this->params
= $params;
532 * return all parameters
536 public function getParams()
538 return $this->params
;
542 * return all added messages
546 public function getAddedMessages()
548 return $this->addedMessages
;
554 * @param mixed $message the message(s)
556 * @return mixed the sanitized message(s)
560 static public function sanitize($message)
562 if (is_array($message)) {
563 foreach ($message as $key => $val) {
564 $message[$key] = PMA_Message
::sanitize($val);
570 return htmlspecialchars($message);
574 * decode $message, taking into account our special codes
577 * @param string $message the message
579 * @return string the decoded message
583 static public function decodeBB($message)
585 return PMA_sanitize($message, false, true);
589 * wrapper for sprintf()
591 * @return string formatted
593 static public function format()
595 $params = func_get_args();
596 if (isset($params[1]) && is_array($params[1])) {
597 array_unshift($params[1], $params[0]);
598 $params = $params[1];
601 return call_user_func_array('sprintf', $params);
605 * returns unique PMA_Message::$hash, if not exists it will be created
607 * @return string PMA_Message::$hash
609 public function getHash()
611 if (null === $this->hash
) {
623 * returns compiled message
625 * @return string complete message
627 public function getMessage()
629 $message = $this->message
;
631 if (0 === /*overload*/mb_strlen($message)) {
632 $string = $this->getString();
633 if (isset($GLOBALS[$string])) {
634 $message = $GLOBALS[$string];
635 } elseif (0 === /*overload*/mb_strlen($string)) {
642 if ($this->isDisplayed()) {
643 $message = $this->getMessageWithIcon($message);
645 if (count($this->getParams()) > 0) {
646 $message = PMA_Message
::format($message, $this->getParams());
649 $message = PMA_Message
::decodeBB($message);
651 foreach ($this->getAddedMessages() as $add_message) {
652 $message .= $add_message;
659 * Returns only message string without image & other HTML.
663 public function getOnlyMessage()
665 return $this->message
;
670 * returns PMA_Message::$string
672 * @return string PMA_Message::$string
674 public function getString()
676 return $this->string;
680 * returns PMA_Message::$number
682 * @return integer PMA_Message::$number
684 public function getNumber()
686 return $this->number
;
690 * returns level of message
692 * @return string level of message
694 public function getLevel()
696 return PMA_Message
::$level[$this->getNumber()];
700 * Displays the message in HTML
704 public function display()
706 echo $this->getDisplay();
707 $this->isDisplayed(true);
711 * returns HTML code for displaying this message
713 * @return string whole message box
715 public function getDisplay()
717 $this->isDisplayed(true);
718 return '<div class="' . $this->getLevel() . '">'
719 . $this->getMessage() . '</div>';
723 * sets and returns whether the message was displayed or not
725 * @param boolean $isDisplayed whether to set displayed flag
727 * @return boolean PMA_Message::$isDisplayed
729 public function isDisplayed($isDisplayed = false)
732 $this->isDisplayed
= true;
735 return $this->isDisplayed
;
739 * Returns the message with corresponding image icon
741 * @param string $message the message(s)
743 * @return string message with icon
745 public function getMessageWithIcon($message)
747 if ('error' == $this->getLevel()) {
748 $image = 's_error.png';
749 } elseif ('success' == $this->getLevel()) {
750 $image = 's_success.png';
752 $image = 's_notice.png';
754 $message = PMA_Message
::notice(PMA_Util
::getImage($image)) . " " . $message;