2 /* vim: set expandtab sw=4 ts=4 sts=4: */
10 use PhpMyAdmin\Sanitize
;
16 * simple usage examples:
18 * // display simple error message 'Error'
19 * Message::error()->display();
21 * // get simple success message 'Success'
22 * $message = Message::success();
24 * // get special notice
25 * $message = Message::notice(__('This is a localized notice'));
28 * more advanced usage example:
30 * // create another message, a hint, with a localized string which expects
31 * $hint = Message::notice('Read the %smanual%s');
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);
46 const SUCCESS
= 1; // 0001
47 const NOTICE
= 2; // 0010
48 const ERROR
= 8; // 1000
50 const SANITIZE_NONE
= 0; // 0000 0000
51 const SANITIZE_STRING
= 16; // 0001 0000
52 const SANITIZE_PARAMS
= 32; // 0010 0000
53 const SANITIZE_BOOTH
= 48; // 0011 0000
60 static public $level = array (
61 Message
::SUCCESS
=> 'success',
62 Message
::NOTICE
=> 'notice',
63 Message
::ERROR
=> 'error',
72 protected $number = Message
::NOTICE
;
75 * The locale string identifier
80 protected $string = '';
83 * The formatted message
88 protected $message = '';
91 * Whether the message was already displayed
96 protected $isDisplayed = false;
99 * Whether to use BB code when displaying.
104 protected $useBBCode = true;
112 protected $hash = null;
120 protected $params = array();
123 * holds additional messages
128 protected $addedMessages = array();
133 * @param string $string The message to be displayed
134 * @param integer $number A numeric representation of the type of message
135 * @param array $params An array of parameters to use in the message
136 * @param integer $sanitize A flag to indicate what to sanitize, see
137 * constant definitions above
139 public function __construct($string = '', $number = Message
::NOTICE
,
140 array $params = array(), $sanitize = Message
::SANITIZE_NONE
142 $this->setString($string, $sanitize & Message
::SANITIZE_STRING
);
143 $this->setNumber($number);
144 $this->setParams($params, $sanitize & Message
::SANITIZE_PARAMS
);
148 * magic method: return string representation for this object
152 public function __toString()
154 return $this->getMessage();
158 * get Message of type success
160 * shorthand for getting a simple success message
162 * @param string $string A localized string
163 * e.g. __('Your SQL query has been
164 * executed successfully')
169 static public function success($string = '')
171 if (empty($string)) {
172 $string = __('Your SQL query has been executed successfully.');
175 return new Message($string, Message
::SUCCESS
);
179 * get Message of type error
181 * shorthand for getting a simple error message
183 * @param string $string A localized string e.g. __('Error')
188 static public function error($string = '')
190 if (empty($string)) {
191 $string = __('Error');
194 return new Message($string, Message
::ERROR
);
198 * get Message of type notice
200 * shorthand for getting a simple notice message
202 * @param string $string A localized string
203 * e.g. __('The additional features for working with
204 * linked tables have been deactivated. To find out
205 * why click %shere%s.')
210 static public function notice($string)
212 return new Message($string, Message
::NOTICE
);
216 * get Message with customized content
218 * shorthand for getting a customized message
220 * @param string $message A localized string
221 * @param integer $type A numeric representation of the type of message
226 static public function raw($message, $type = Message
::NOTICE
)
228 $r = new Message('', $type);
229 $r->setMessage($message);
230 $r->setBBCode(false);
235 * get Message for number of affected rows
237 * shorthand for getting a customized message
239 * @param integer $rows Number of rows
244 static public function getMessageForAffectedRows($rows)
246 $message = Message
::success(
247 _ngettext('%1$d row affected.', '%1$d rows affected.', $rows)
249 $message->addParam($rows);
254 * get Message for number of deleted rows
256 * shorthand for getting a customized message
258 * @param integer $rows Number of rows
263 static public function getMessageForDeletedRows($rows)
265 $message = Message
::success(
266 _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows)
268 $message->addParam($rows);
273 * get Message for number of inserted rows
275 * shorthand for getting a customized message
277 * @param integer $rows Number of rows
282 static public function getMessageForInsertedRows($rows)
284 $message = Message
::success(
285 _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows)
287 $message->addParam($rows);
292 * get Message of type error with custom content
294 * shorthand for getting a customized error message
296 * @param string $message A localized string
301 static public function rawError($message)
303 return Message
::raw($message, Message
::ERROR
);
307 * get Message of type notice with custom content
309 * shorthand for getting a customized notice message
311 * @param string $message A localized string
316 static public function rawNotice($message)
318 return Message
::raw($message, Message
::NOTICE
);
322 * get Message of type success with custom content
324 * shorthand for getting a customized success message
326 * @param string $message A localized string
331 static public function rawSuccess($message)
333 return Message
::raw($message, Message
::SUCCESS
);
337 * returns whether this message is a success message or not
338 * and optionally makes this message a success message
340 * @param boolean $set Whether to make this message of SUCCESS type
342 * @return boolean whether this is a success message or not
344 public function isSuccess($set = false)
347 $this->setNumber(Message
::SUCCESS
);
350 return $this->getNumber() === Message
::SUCCESS
;
354 * returns whether this message is a notice message or not
355 * and optionally makes this message a notice message
357 * @param boolean $set Whether to make this message of NOTICE type
359 * @return boolean whether this is a notice message or not
361 public function isNotice($set = false)
364 $this->setNumber(Message
::NOTICE
);
367 return $this->getNumber() === Message
::NOTICE
;
371 * returns whether this message is an error message or not
372 * and optionally makes this message an error message
374 * @param boolean $set Whether to make this message of ERROR type
376 * @return boolean Whether this is an error message or not
378 public function isError($set = false)
381 $this->setNumber(Message
::ERROR
);
384 return $this->getNumber() === Message
::ERROR
;
388 * Set whether we should use BB Code when rendering.
390 * @param boolean $useBBCode Use BB Code?
394 public function setBBCode($useBBCode)
396 $this->useBBCode
= $useBBCode;
400 * set raw message (overrides string)
402 * @param string $message A localized string
403 * @param boolean $sanitize Whether to sanitize $message or not
407 public function setMessage($message, $sanitize = false)
410 $message = Message
::sanitize($message);
412 $this->message
= $message;
416 * set string (does not take effect if raw message is set)
418 * @param string $string string to set
419 * @param boolean $sanitize whether to sanitize $string or not
423 public function setString($string, $sanitize = true)
426 $string = Message
::sanitize($string);
428 $this->string = $string;
432 * set message type number
434 * @param integer $number message type number to set
438 public function setNumber($number)
440 $this->number
= $number;
444 * add string or Message parameter
448 * $message->addParam('[em]some string[/em]');
451 * @param mixed $param parameter to add
455 public function addParam($param)
457 if ($param instanceof Message ||
is_float($param) ||
is_int($param)) {
458 $this->params
[] = $param;
460 $this->params
[] = htmlspecialchars($param);
465 * add parameter as raw HTML, usually in conjunction with strings
469 * $message->addParamHtml('<img src="img" />');
472 * @param string $param parameter to add
476 public function addParamHtml($param)
478 $this->params
[] = Message
::notice($param);
482 * add a bunch of messages at once
484 * @param Message[] $messages to be added
485 * @param string $separator to use between this and previous string/message
489 public function addMessages($messages, $separator = ' ')
491 foreach ($messages as $message) {
492 $this->addMessage($message, $separator);
497 * add a bunch of messages at once
499 * @param string[] $messages to be added
500 * @param string $separator to use between this and previous string/message
504 public function addMessagesString($messages, $separator = ' ')
506 foreach ($messages as $message) {
507 $this->addText($message, $separator);
512 * Real implementation of adding message
514 * @param mixed $message to be added
515 * @param string $separator to use between this and previous string/message
519 private function _addMessage($message, $separator)
521 if (!empty($separator)) {
522 $this->addedMessages
[] = $separator;
524 $this->addedMessages
[] = $message;
528 * add another raw message to be concatenated on displaying
530 * @param Message $message to be added
531 * @param string $separator to use between this and previous string/message
535 public function addMessage($message, $separator = ' ')
537 if (!($message instanceof Message
)) {
538 trigger_error('Invalid parameter passed to addMessage');
540 $this->_addMessage($message, $separator);
544 * add another raw message to be concatenated on displaying
546 * @param string $message to be added
547 * @param string $separator to use between this and previous string/message
551 public function addText($message, $separator = ' ')
553 if (!is_string($message)) {
554 trigger_error('Invalid parameter passed to addMessage');
556 $this->_addMessage(Message
::notice(htmlspecialchars($message)), $separator);
560 * add another html message to be concatenated on displaying
562 * @param string $message to be added
563 * @param string $separator to use between this and previous string/message
567 public function addHtml($message, $separator = ' ')
569 if (!is_string($message)) {
570 trigger_error('Invalid parameter passed to addMessage');
572 $this->_addMessage(Message
::rawNotice($message), $separator);
576 * set all params at once, usually used in conjunction with string
578 * @param array|string $params parameters to set
579 * @param boolean $sanitize whether to sanitize params
583 public function setParams($params, $sanitize = false)
586 $params = Message
::sanitize($params);
588 $this->params
= $params;
592 * return all parameters
596 public function getParams()
598 return $this->params
;
602 * return all added messages
606 public function getAddedMessages()
608 return $this->addedMessages
;
614 * @param mixed $message the message(s)
616 * @return mixed the sanitized message(s)
620 static public function sanitize($message)
622 if (is_array($message)) {
623 foreach ($message as $key => $val) {
624 $message[$key] = Message
::sanitize($val);
630 return htmlspecialchars($message);
634 * decode $message, taking into account our special codes
637 * @param string $message the message
639 * @return string the decoded message
643 static public function decodeBB($message)
645 return Sanitize
::sanitize($message, false, true);
649 * wrapper for sprintf()
651 * @return string formatted
653 static public function format()
655 $params = func_get_args();
656 if (isset($params[1]) && is_array($params[1])) {
657 array_unshift($params[1], $params[0]);
658 $params = $params[1];
661 return call_user_func_array('sprintf', $params);
665 * returns unique Message::$hash, if not exists it will be created
667 * @return string Message::$hash
669 public function getHash()
671 if (null === $this->hash
) {
683 * returns compiled message
685 * @return string complete message
687 public function getMessage()
689 $message = $this->message
;
691 if (strlen($message) === 0) {
692 $string = $this->getString();
693 if (strlen($string) === 0) {
700 if ($this->isDisplayed()) {
701 $message = $this->getMessageWithIcon($message);
703 if (count($this->getParams()) > 0) {
704 $message = Message
::format($message, $this->getParams());
707 if ($this->useBBCode
) {
708 $message = Message
::decodeBB($message);
711 foreach ($this->getAddedMessages() as $add_message) {
712 $message .= $add_message;
719 * Returns only message string without image & other HTML.
723 public function getOnlyMessage()
725 return $this->message
;
730 * returns Message::$string
732 * @return string Message::$string
734 public function getString()
736 return $this->string;
740 * returns Message::$number
742 * @return integer Message::$number
744 public function getNumber()
746 return $this->number
;
750 * returns level of message
752 * @return string level of message
754 public function getLevel()
756 return Message
::$level[$this->getNumber()];
760 * Displays the message in HTML
764 public function display()
766 echo $this->getDisplay();
767 $this->isDisplayed(true);
771 * returns HTML code for displaying this message
773 * @return string whole message box
775 public function getDisplay()
777 $this->isDisplayed(true);
778 return '<div class="' . $this->getLevel() . '">'
779 . $this->getMessage() . '</div>';
783 * sets and returns whether the message was displayed or not
785 * @param boolean $isDisplayed whether to set displayed flag
787 * @return boolean Message::$isDisplayed
789 public function isDisplayed($isDisplayed = false)
792 $this->isDisplayed
= true;
795 return $this->isDisplayed
;
799 * Returns the message with corresponding image icon
801 * @param string $message the message(s)
803 * @return string message with icon
805 public function getMessageWithIcon($message)
807 if ('error' == $this->getLevel()) {
809 } elseif ('success' == $this->getLevel()) {
810 $image = 's_success';
814 $message = Message
::notice(Util
::getImage($image)) . " " . $message;