Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / classes / Message.php
blob9c9604a6d67323b80d17718d1f99ceb4b90105df
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class Message
6 * @package PhpMyAdmin
7 */
8 namespace PhpMyAdmin;
10 use PhpMyAdmin\Sanitize;
11 use PhpMyAdmin\Util;
13 /**
14 * a single message
16 * simple usage examples:
17 * <code>
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'));
26 * </code>
28 * more advanced usage example:
29 * <code>
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);
40 * </code>
42 * @package PhpMyAdmin
44 class Message
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
55 /**
56 * message levels
58 * @var array
60 static public $level = array (
61 Message::SUCCESS => 'success',
62 Message::NOTICE => 'notice',
63 Message::ERROR => 'error',
66 /**
67 * The message number
69 * @access protected
70 * @var integer
72 protected $number = Message::NOTICE;
74 /**
75 * The locale string identifier
77 * @access protected
78 * @var string
80 protected $string = '';
82 /**
83 * The formatted message
85 * @access protected
86 * @var string
88 protected $message = '';
90 /**
91 * Whether the message was already displayed
93 * @access protected
94 * @var boolean
96 protected $isDisplayed = false;
98 /**
99 * Whether to use BB code when displaying.
101 * @access protected
102 * @var boolean
104 protected $useBBCode = true;
107 * Unique id
109 * @access protected
110 * @var string
112 protected $hash = null;
115 * holds parameters
117 * @access protected
118 * @var array
120 protected $params = array();
123 * holds additional messages
125 * @access protected
126 * @var array
128 protected $addedMessages = array();
131 * Constructor
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
150 * @return string
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')
166 * @return Message
167 * @static
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')
185 * @return Message
186 * @static
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.')
207 * @return Message
208 * @static
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
223 * @return Message
224 * @static
226 static public function raw($message, $type = Message::NOTICE)
228 $r = new Message('', $type);
229 $r->setMessage($message);
230 $r->setBBCode(false);
231 return $r;
235 * get Message for number of affected rows
237 * shorthand for getting a customized message
239 * @param integer $rows Number of rows
241 * @return Message
242 * @static
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);
250 return $message;
254 * get Message for number of deleted rows
256 * shorthand for getting a customized message
258 * @param integer $rows Number of rows
260 * @return Message
261 * @static
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);
269 return $message;
273 * get Message for number of inserted rows
275 * shorthand for getting a customized message
277 * @param integer $rows Number of rows
279 * @return Message
280 * @static
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);
288 return $message;
292 * get Message of type error with custom content
294 * shorthand for getting a customized error message
296 * @param string $message A localized string
298 * @return Message
299 * @static
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
313 * @return Message
314 * @static
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
328 * @return Message
329 * @static
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)
346 if ($set) {
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)
363 if ($set) {
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)
380 if ($set) {
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?
392 * @return void
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
405 * @return void
407 public function setMessage($message, $sanitize = false)
409 if ($sanitize) {
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
421 * @return void
423 public function setString($string, $sanitize = true)
425 if ($sanitize) {
426 $string = Message::sanitize($string);
428 $this->string = $string;
432 * set message type number
434 * @param integer $number message type number to set
436 * @return void
438 public function setNumber($number)
440 $this->number = $number;
444 * add string or Message parameter
446 * usage
447 * <code>
448 * $message->addParam('[em]some string[/em]');
449 * </code>
451 * @param mixed $param parameter to add
453 * @return void
455 public function addParam($param)
457 if ($param instanceof Message || is_float($param) || is_int($param)) {
458 $this->params[] = $param;
459 } else {
460 $this->params[] = htmlspecialchars($param);
465 * add parameter as raw HTML, usually in conjunction with strings
467 * usage
468 * <code>
469 * $message->addParamHtml('<img src="img" />');
470 * </code>
472 * @param string $param parameter to add
474 * @return void
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
487 * @return void
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
502 * @return void
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
517 * @return void
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
533 * @return void
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
549 * @return void
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
565 * @return void
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
581 * @return void
583 public function setParams($params, $sanitize = false)
585 if ($sanitize) {
586 $params = Message::sanitize($params);
588 $this->params = $params;
592 * return all parameters
594 * @return array
596 public function getParams()
598 return $this->params;
602 * return all added messages
604 * @return array
606 public function getAddedMessages()
608 return $this->addedMessages;
612 * Sanitizes $message
614 * @param mixed $message the message(s)
616 * @return mixed the sanitized message(s)
617 * @access public
618 * @static
620 static public function sanitize($message)
622 if (is_array($message)) {
623 foreach ($message as $key => $val) {
624 $message[$key] = Message::sanitize($val);
627 return $message;
630 return htmlspecialchars($message);
634 * decode $message, taking into account our special codes
635 * for formatting
637 * @param string $message the message
639 * @return string the decoded message
640 * @access public
641 * @static
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) {
672 $this->hash = md5(
673 $this->getNumber() .
674 $this->string .
675 $this->message
679 return $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) {
694 $message = '';
695 } else {
696 $message = $string;
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;
715 return $message;
719 * Returns only message string without image & other HTML.
721 * @return string
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
762 * @return void
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)
791 if ($isDisplayed) {
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()) {
808 $image = 's_error';
809 } elseif ('success' == $this->getLevel()) {
810 $image = 's_success';
811 } else {
812 $image = 's_notice';
814 $message = Message::notice(Util::getImage($image)) . " " . $message;
815 return $message;