Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / phpmyadmin / libraries / Message.class.php
blob1f93ad2de4daa8bf493838b6d672305db668b19f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Message
6 * @package PhpMyAdmin
7 */
9 /**
10 * a single message
12 * simple usage examples:
13 * <code>
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');
22 * </code>
24 * more advanced usage example:
25 * <code>
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 %d 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
56 * </code>
58 * @package PhpMyAdmin
60 class PMA_Message
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
71 /**
72 * message levels
74 * @var array
76 static public $level = array (
77 PMA_Message::SUCCESS => 'success',
78 PMA_Message::NOTICE => 'notice',
79 PMA_Message::ERROR => 'error',
82 /**
83 * The message number
85 * @access protected
86 * @var integer
88 protected $number = PMA_Message::NOTICE;
90 /**
91 * The locale string identifier
93 * @access protected
94 * @var string
96 protected $string = '';
98 /**
99 * The formatted message
101 * @access protected
102 * @var string
104 protected $message = '';
107 * Whether the message was already displayed
109 * @access protected
110 * @var boolean
112 protected $isDisplayed = false;
115 * Unique id
117 * @access protected
118 * @var string
120 protected $hash = null;
123 * holds parameters
125 * @access protected
126 * @var array
128 protected $params = array();
131 * holds additional messages
133 * @access protected
134 * @var array
136 protected $addedMessages = array();
139 * Constructor
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
158 * @return string
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
175 * @static
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
194 * @static
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
216 * @static
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
232 * @static
234 static public function raw($message, $type = PMA_Message::NOTICE)
236 $r = new PMA_Message('', $type);
237 $r->setMessage($message);
238 return $r;
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
249 * @static
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);
257 return $message;
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
268 * @static
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);
276 return $message;
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
287 * @static
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);
295 return $message;
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
306 * @static
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
321 * @static
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
336 * @static
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 optionaly 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)
353 if ($set) {
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)
370 if ($set) {
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)
387 if ($set) {
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
400 * @return void
402 public function setMessage($message, $sanitize = false)
404 if ($sanitize) {
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
416 * @return void
418 public function setString($string, $sanitize = true)
420 if ($sanitize) {
421 $string = PMA_Message::sanitize($string);
423 $this->string = $string;
427 * set message type number
429 * @param integer $number message type number to set
431 * @return void
433 public function setNumber($number)
435 $this->number = $number;
439 * add parameter, usually in conjunction with strings
441 * usage
442 * <code>
443 * $message->addParam('strLocale', false);
444 * $message->addParam('[em]some string[/em]');
445 * $message->addParam('<img src="img" />', false);
446 * </code>
448 * @param mixed $param parameter to add
449 * @param boolean $raw whether parameter should be passed as is
450 * without html escaping
452 * @return void
454 public function addParam($param, $raw = true)
456 if ($param instanceof PMA_Message) {
457 $this->params[] = $param;
458 } elseif ($raw) {
459 $this->params[] = htmlspecialchars($param);
460 } else {
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
471 * @return void
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
485 * @return void
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
500 * @return void
502 public function addMessage($message, $separator = ' ')
504 if (strlen($separator)) {
505 $this->addedMessages[] = $separator;
508 if ($message instanceof PMA_Message) {
509 $this->addedMessages[] = $message;
510 } else {
511 $this->addedMessages[] = PMA_Message::rawNotice($message);
516 * set all params at once, usually used in conjunction with string
518 * @param array $params parameters to set
519 * @param boolean $sanitize whether to sanitize params
521 * @return void
523 public function setParams($params, $sanitize = false)
525 if ($sanitize) {
526 $params = PMA_Message::sanitize($params);
528 $this->params = $params;
532 * return all parameters
534 * @return array
536 public function getParams()
538 return $this->params;
542 * return all added messages
544 * @return array
546 public function getAddedMessages()
548 return $this->addedMessages;
552 * Sanitizes $message
554 * @param mixed $message the message(s)
556 * @return mixed the sanitized message(s)
557 * @access public
558 * @static
560 static public function sanitize($message)
562 if (is_array($message)) {
563 foreach ($message as $key => $val) {
564 $message[$key] = PMA_Message::sanitize($val);
567 return $message;
570 return htmlspecialchars($message);
574 * decode $message, taking into account our special codes
575 * for formatting
577 * @param string $message the message
579 * @return string the decoded message
580 * @access public
581 * @static
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) {
612 $this->hash = md5(
613 $this->getNumber() .
614 $this->string .
615 $this->message
619 return $this->hash;
623 * returns compiled message
625 * @return string complete message
627 public function getMessage()
629 $message = $this->message;
631 if (0 === strlen($message)) {
632 $string = $this->getString();
633 if (isset($GLOBALS[$string])) {
634 $message = $GLOBALS[$string];
635 } elseif (0 === strlen($string)) {
636 $message = '';
637 } else {
638 $message = $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;
655 return $message;
659 * returns PMA_Message::$string
661 * @return string PMA_Message::$string
663 public function getString()
665 return $this->string;
669 * returns PMA_Message::$number
671 * @return integer PMA_Message::$number
673 public function getNumber()
675 return $this->number;
679 * returns level of message
681 * @return string level of message
683 public function getLevel()
685 return PMA_Message::$level[$this->getNumber()];
689 * Displays the message in HTML
691 * @return void
693 public function display()
695 echo $this->getDisplay();
696 $this->isDisplayed(true);
700 * returns HTML code for displaying this message
702 * @return string whole message box
704 public function getDisplay()
706 $this->isDisplayed(true);
707 return '<div class="' . $this->getLevel() . '">'
708 . $this->getMessage() . '</div>';
712 * sets and returns whether the message was displayed or not
714 * @param boolean $isDisplayed whether to set displayed flag
716 * @return boolean PMA_Message::$isDisplayed
718 public function isDisplayed($isDisplayed = false)
720 if ($isDisplayed) {
721 $this->isDisplayed = true;
724 return $this->isDisplayed;
728 * Returns the message with corresponding image icon
730 * @param string $message the message(s)
732 * @return string message with icon
734 public function getMessageWithIcon($message)
736 $image = '';
737 if ('error' == $this->getLevel()) {
738 $image = 's_error.png';
739 } elseif ('success' == $this->getLevel()) {
740 $image = 's_success.png';
741 } else {
742 $image = 's_notice.png';
744 $message = PMA_Message::notice(PMA_Util::getImage($image)) . " " . $message;
745 return $message;