Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Message.php
blob8b239e6b14422882e9392b87124ac88310f2230a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class Message
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\Sanitize;
12 /**
13 * a single message
15 * simple usage examples:
16 * <code>
17 * // display simple error message 'Error'
18 * Message::error()->display();
20 * // get simple success message 'Success'
21 * $message = Message::success();
23 * // get special notice
24 * $message = Message::notice(__('This is a localized notice'));
25 * </code>
27 * more advanced usage example:
28 * <code>
29 * // create another message, a hint, with a localized string which expects
30 * $hint = Message::notice('Read the %smanual%s');
31 * // replace placeholders with the following params
32 * $hint->addParam('[doc@cfg_Example]');
33 * $hint->addParam('[/doc]');
34 * // add this hint as a tooltip
35 * $hint = showHint($hint);
37 * // add the retrieved tooltip reference to the original message
38 * $message->addMessage($hint);
39 * </code>
41 * @package PhpMyAdmin
43 class Message
45 const SUCCESS = 1; // 0001
46 const NOTICE = 2; // 0010
47 const ERROR = 8; // 1000
49 const SANITIZE_NONE = 0; // 0000 0000
50 const SANITIZE_STRING = 16; // 0001 0000
51 const SANITIZE_PARAMS = 32; // 0010 0000
52 const SANITIZE_BOOTH = 48; // 0011 0000
54 /**
55 * message levels
57 * @var array
59 static public $level = array (
60 Message::SUCCESS => 'success',
61 Message::NOTICE => 'notice',
62 Message::ERROR => 'error',
65 /**
66 * The message number
68 * @access protected
69 * @var integer
71 protected $number = Message::NOTICE;
73 /**
74 * The locale string identifier
76 * @access protected
77 * @var string
79 protected $string = '';
81 /**
82 * The formatted message
84 * @access protected
85 * @var string
87 protected $message = '';
89 /**
90 * Whether the message was already displayed
92 * @access protected
93 * @var boolean
95 protected $isDisplayed = false;
97 /**
98 * Whether to use BB code when displaying.
100 * @access protected
101 * @var boolean
103 protected $useBBCode = true;
106 * Unique id
108 * @access protected
109 * @var string
111 protected $hash = null;
114 * holds parameters
116 * @access protected
117 * @var array
119 protected $params = array();
122 * holds additional messages
124 * @access protected
125 * @var array
127 protected $addedMessages = array();
130 * Constructor
132 * @param string $string The message to be displayed
133 * @param integer $number A numeric representation of the type of message
134 * @param array $params An array of parameters to use in the message
135 * @param integer $sanitize A flag to indicate what to sanitize, see
136 * constant definitions above
138 public function __construct($string = '', $number = Message::NOTICE,
139 $params = array(), $sanitize = Message::SANITIZE_NONE
141 $this->setString($string, $sanitize & Message::SANITIZE_STRING);
142 $this->setNumber($number);
143 $this->setParams($params, $sanitize & Message::SANITIZE_PARAMS);
147 * magic method: return string representation for this object
149 * @return string
151 public function __toString()
153 return $this->getMessage();
157 * get Message of type success
159 * shorthand for getting a simple success message
161 * @param string $string A localized string
162 * e.g. __('Your SQL query has been
163 * executed successfully')
165 * @return Message
166 * @static
168 static public function success($string = '')
170 if (empty($string)) {
171 $string = __('Your SQL query has been executed successfully.');
174 return new Message($string, Message::SUCCESS);
178 * get Message of type error
180 * shorthand for getting a simple error message
182 * @param string $string A localized string e.g. __('Error')
184 * @return Message
185 * @static
187 static public function error($string = '')
189 if (empty($string)) {
190 $string = __('Error');
193 return new Message($string, Message::ERROR);
197 * get Message of type notice
199 * shorthand for getting a simple notice message
201 * @param string $string A localized string
202 * e.g. __('The additional features for working with
203 * linked tables have been deactivated. To find out
204 * why click %shere%s.')
206 * @return Message
207 * @static
209 static public function notice($string)
211 return new Message($string, Message::NOTICE);
215 * get Message with customized content
217 * shorthand for getting a customized message
219 * @param string $message A localized string
220 * @param integer $type A numeric representation of the type of message
222 * @return Message
223 * @static
225 static public function raw($message, $type = Message::NOTICE)
227 $r = new Message('', $type);
228 $r->setMessage($message);
229 $r->setBBCode(false);
230 return $r;
234 * get Message for number of affected rows
236 * shorthand for getting a customized message
238 * @param integer $rows Number of rows
240 * @return Message
241 * @static
243 static public function getMessageForAffectedRows($rows)
245 $message = Message::success(
246 _ngettext('%1$d row affected.', '%1$d rows affected.', $rows)
248 $message->addParam($rows);
249 return $message;
253 * get Message for number of deleted rows
255 * shorthand for getting a customized message
257 * @param integer $rows Number of rows
259 * @return Message
260 * @static
262 static public function getMessageForDeletedRows($rows)
264 $message = Message::success(
265 _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows)
267 $message->addParam($rows);
268 return $message;
272 * get Message for number of inserted rows
274 * shorthand for getting a customized message
276 * @param integer $rows Number of rows
278 * @return Message
279 * @static
281 static public function getMessageForInsertedRows($rows)
283 $message = Message::success(
284 _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows)
286 $message->addParam($rows);
287 return $message;
291 * get Message of type error with custom content
293 * shorthand for getting a customized error message
295 * @param string $message A localized string
297 * @return Message
298 * @static
300 static public function rawError($message)
302 return Message::raw($message, Message::ERROR);
306 * get Message of type notice with custom content
308 * shorthand for getting a customized notice message
310 * @param string $message A localized string
312 * @return Message
313 * @static
315 static public function rawNotice($message)
317 return Message::raw($message, Message::NOTICE);
321 * get Message of type success with custom content
323 * shorthand for getting a customized success message
325 * @param string $message A localized string
327 * @return Message
328 * @static
330 static public function rawSuccess($message)
332 return Message::raw($message, Message::SUCCESS);
336 * returns whether this message is a success message or not
337 * and optionally makes this message a success message
339 * @param boolean $set Whether to make this message of SUCCESS type
341 * @return boolean whether this is a success message or not
343 public function isSuccess($set = false)
345 if ($set) {
346 $this->setNumber(Message::SUCCESS);
349 return $this->getNumber() === Message::SUCCESS;
353 * returns whether this message is a notice message or not
354 * and optionally makes this message a notice message
356 * @param boolean $set Whether to make this message of NOTICE type
358 * @return boolean whether this is a notice message or not
360 public function isNotice($set = false)
362 if ($set) {
363 $this->setNumber(Message::NOTICE);
366 return $this->getNumber() === Message::NOTICE;
370 * returns whether this message is an error message or not
371 * and optionally makes this message an error message
373 * @param boolean $set Whether to make this message of ERROR type
375 * @return boolean Whether this is an error message or not
377 public function isError($set = false)
379 if ($set) {
380 $this->setNumber(Message::ERROR);
383 return $this->getNumber() === Message::ERROR;
387 * Set whether we should use BB Code when rendering.
389 * @param boolean $useBBCode Use BB Code?
391 * @return void
393 public function setBBCode($useBBCode)
395 $this->useBBCode = $useBBCode;
399 * set raw message (overrides string)
401 * @param string $message A localized string
402 * @param boolean $sanitize Whether to sanitize $message or not
404 * @return void
406 public function setMessage($message, $sanitize = false)
408 if ($sanitize) {
409 $message = Message::sanitize($message);
411 $this->message = $message;
415 * set string (does not take effect if raw message is set)
417 * @param string $string string to set
418 * @param boolean $sanitize whether to sanitize $string or not
420 * @return void
422 public function setString($string, $sanitize = true)
424 if ($sanitize) {
425 $string = Message::sanitize($string);
427 $this->string = $string;
431 * set message type number
433 * @param integer $number message type number to set
435 * @return void
437 public function setNumber($number)
439 $this->number = $number;
443 * add string or Message parameter
445 * usage
446 * <code>
447 * $message->addParam('[em]some string[/em]');
448 * </code>
450 * @param mixed $param parameter to add
452 * @return void
454 public function addParam($param)
456 if ($param instanceof Message) {
457 $this->params[] = $param;
458 } else {
459 $this->params[] = htmlspecialchars($param);
464 * add parameter as raw HTML, usually in conjunction with strings
466 * usage
467 * <code>
468 * $message->addParamHtml('<img src="img" />');
469 * </code>
471 * @param string $param parameter to add
473 * @return void
475 public function addParamHtml($param)
477 $this->params[] = Message::notice($param);
481 * add a bunch of messages at once
483 * @param Message[] $messages to be added
484 * @param string $separator to use between this and previous string/message
486 * @return void
488 public function addMessages($messages, $separator = ' ')
490 foreach ($messages as $message) {
491 $this->addMessage($message, $separator);
496 * add a bunch of messages at once
498 * @param string[] $messages to be added
499 * @param string $separator to use between this and previous string/message
501 * @return void
503 public function addMessagesString($messages, $separator = ' ')
505 foreach ($messages as $message) {
506 $this->addText($message, $separator);
511 * Real implementation of adding message
513 * @param mixed $message to be added
514 * @param string $separator to use between this and previous string/message
516 * @return void
518 private function _addMessage($message, $separator)
520 if (!empty($separator)) {
521 $this->addedMessages[] = $separator;
523 $this->addedMessages[] = $message;
527 * add another raw message to be concatenated on displaying
529 * @param Message $message to be added
530 * @param string $separator to use between this and previous string/message
532 * @return void
534 public function addMessage($message, $separator = ' ')
536 if (!($message instanceof Message)) {
537 trigger_error('Invalid parameter passed to addMessage');
539 $this->_addMessage($message, $separator);
543 * add another raw message to be concatenated on displaying
545 * @param string $message to be added
546 * @param string $separator to use between this and previous string/message
548 * @return void
550 public function addText($message, $separator = ' ')
552 if (!is_string($message)) {
553 trigger_error('Invalid parameter passed to addMessage');
555 $this->_addMessage(Message::notice(htmlspecialchars($message)), $separator);
559 * add another html message to be concatenated on displaying
561 * @param string $message to be added
562 * @param string $separator to use between this and previous string/message
564 * @return void
566 public function addHtml($message, $separator = ' ')
568 if (!is_string($message)) {
569 trigger_error('Invalid parameter passed to addMessage');
571 $this->_addMessage(Message::rawNotice($message), $separator);
575 * set all params at once, usually used in conjunction with string
577 * @param array|string $params parameters to set
578 * @param boolean $sanitize whether to sanitize params
580 * @return void
582 public function setParams($params, $sanitize = false)
584 if ($sanitize) {
585 $params = Message::sanitize($params);
587 $this->params = $params;
591 * return all parameters
593 * @return array
595 public function getParams()
597 return $this->params;
601 * return all added messages
603 * @return array
605 public function getAddedMessages()
607 return $this->addedMessages;
611 * Sanitizes $message
613 * @param mixed $message the message(s)
615 * @return mixed the sanitized message(s)
616 * @access public
617 * @static
619 static public function sanitize($message)
621 if (is_array($message)) {
622 foreach ($message as $key => $val) {
623 $message[$key] = Message::sanitize($val);
626 return $message;
629 return htmlspecialchars($message);
633 * decode $message, taking into account our special codes
634 * for formatting
636 * @param string $message the message
638 * @return string the decoded message
639 * @access public
640 * @static
642 static public function decodeBB($message)
644 return Sanitize::sanitize($message, false, true);
648 * wrapper for sprintf()
650 * @return string formatted
652 static public function format()
654 $params = func_get_args();
655 if (isset($params[1]) && is_array($params[1])) {
656 array_unshift($params[1], $params[0]);
657 $params = $params[1];
660 return call_user_func_array('sprintf', $params);
664 * returns unique Message::$hash, if not exists it will be created
666 * @return string Message::$hash
668 public function getHash()
670 if (null === $this->hash) {
671 $this->hash = md5(
672 $this->getNumber() .
673 $this->string .
674 $this->message
678 return $this->hash;
682 * returns compiled message
684 * @return string complete message
686 public function getMessage()
688 $message = $this->message;
690 if (strlen($message) === 0) {
691 $string = $this->getString();
692 if (strlen($string) === 0) {
693 $message = '';
694 } else {
695 $message = $string;
699 if ($this->isDisplayed()) {
700 $message = $this->getMessageWithIcon($message);
702 if (count($this->getParams()) > 0) {
703 $message = Message::format($message, $this->getParams());
706 if ($this->useBBCode) {
707 $message = Message::decodeBB($message);
710 foreach ($this->getAddedMessages() as $add_message) {
711 $message .= $add_message;
714 return $message;
718 * Returns only message string without image & other HTML.
720 * @return string
722 public function getOnlyMessage()
724 return $this->message;
729 * returns Message::$string
731 * @return string Message::$string
733 public function getString()
735 return $this->string;
739 * returns Message::$number
741 * @return integer Message::$number
743 public function getNumber()
745 return $this->number;
749 * returns level of message
751 * @return string level of message
753 public function getLevel()
755 return Message::$level[$this->getNumber()];
759 * Displays the message in HTML
761 * @return void
763 public function display()
765 echo $this->getDisplay();
766 $this->isDisplayed(true);
770 * returns HTML code for displaying this message
772 * @return string whole message box
774 public function getDisplay()
776 $this->isDisplayed(true);
777 return '<div class="' . $this->getLevel() . '">'
778 . $this->getMessage() . '</div>';
782 * sets and returns whether the message was displayed or not
784 * @param boolean $isDisplayed whether to set displayed flag
786 * @return boolean Message::$isDisplayed
788 public function isDisplayed($isDisplayed = false)
790 if ($isDisplayed) {
791 $this->isDisplayed = true;
794 return $this->isDisplayed;
798 * Returns the message with corresponding image icon
800 * @param string $message the message(s)
802 * @return string message with icon
804 public function getMessageWithIcon($message)
806 if ('error' == $this->getLevel()) {
807 $image = 's_error.png';
808 } elseif ('success' == $this->getLevel()) {
809 $image = 's_success.png';
810 } else {
811 $image = 's_notice.png';
813 $message = Message::notice(Util::getImage($image)) . " " . $message;
814 return $message;