2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 namespace PMA\libraries
;
10 use PMA\libraries\Sanitize
;
15 * simple usage examples:
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'));
27 * more advanced usage example:
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);
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
59 static public $level = array (
60 Message
::SUCCESS
=> 'success',
61 Message
::NOTICE
=> 'notice',
62 Message
::ERROR
=> 'error',
71 protected $number = Message
::NOTICE
;
74 * The locale string identifier
79 protected $string = '';
82 * The formatted message
87 protected $message = '';
90 * Whether the message was already displayed
95 protected $isDisplayed = false;
98 * Whether to use BB code when displaying.
103 protected $useBBCode = true;
111 protected $hash = null;
119 protected $params = array();
122 * holds additional messages
127 protected $addedMessages = array();
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
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')
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')
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.')
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
225 static public function raw($message, $type = Message
::NOTICE
)
227 $r = new Message('', $type);
228 $r->setMessage($message);
229 $r->setBBCode(false);
234 * get Message for number of affected rows
236 * shorthand for getting a customized message
238 * @param integer $rows Number of rows
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);
253 * get Message for number of deleted rows
255 * shorthand for getting a customized message
257 * @param integer $rows Number of rows
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);
272 * get Message for number of inserted rows
274 * shorthand for getting a customized message
276 * @param integer $rows Number of rows
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);
291 * get Message of type error with custom content
293 * shorthand for getting a customized error message
295 * @param string $message A localized string
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
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
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)
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)
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)
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?
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
406 public function setMessage($message, $sanitize = false)
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
422 public function setString($string, $sanitize = true)
425 $string = Message
::sanitize($string);
427 $this->string = $string;
431 * set message type number
433 * @param integer $number message type number to set
437 public function setNumber($number)
439 $this->number
= $number;
443 * add string or Message parameter
447 * $message->addParam('[em]some string[/em]');
450 * @param mixed $param parameter to add
454 public function addParam($param)
456 if ($param instanceof Message
) {
457 $this->params
[] = $param;
459 $this->params
[] = htmlspecialchars($param);
464 * add parameter as raw HTML, usually in conjunction with strings
468 * $message->addParamHtml('<img src="img" />');
471 * @param string $param parameter to add
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
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
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
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
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
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
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
582 public function setParams($params, $sanitize = false)
585 $params = Message
::sanitize($params);
587 $this->params
= $params;
591 * return all parameters
595 public function getParams()
597 return $this->params
;
601 * return all added messages
605 public function getAddedMessages()
607 return $this->addedMessages
;
613 * @param mixed $message the message(s)
615 * @return mixed the sanitized message(s)
619 static public function sanitize($message)
621 if (is_array($message)) {
622 foreach ($message as $key => $val) {
623 $message[$key] = Message
::sanitize($val);
629 return htmlspecialchars($message);
633 * decode $message, taking into account our special codes
636 * @param string $message the message
638 * @return string the decoded message
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
) {
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) {
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;
718 * Returns only message string without image & other HTML.
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
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)
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';
811 $image = 's_notice.png';
813 $message = Message
::notice(Util
::getImage($image)) . " " . $message;