2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoBasicFormatter extends EchoNotificationFormatter {
|
|
|
|
protected $messageKey = false;
|
|
|
|
protected $messageParams = false;
|
2012-07-31 00:29:49 +00:00
|
|
|
protected $requiredParameters = array(
|
|
|
|
'title-message',
|
|
|
|
'title-params',
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $title, $content, $email, $icon;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
public function __construct( $params ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
parent::__construct( $params );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
$this->title = array();
|
|
|
|
$this->title['message'] = $params['title-message'];
|
|
|
|
$this->title['params'] = $params['title-params'];
|
|
|
|
|
|
|
|
if ( isset( $params['content-message'] ) ) {
|
|
|
|
$this->content = array();
|
|
|
|
$this->content['message'] = $params['content-message'];
|
|
|
|
|
|
|
|
if ( isset( $params['content-params'] ) ) {
|
|
|
|
$this->content['params'] = $params['content-params'];
|
|
|
|
} else {
|
|
|
|
$this->content['params'] = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->email = array();
|
|
|
|
if ( isset( $params['email-subject-message'] ) ) {
|
|
|
|
$this->email['subject'] = array();
|
|
|
|
$this->email['subject']['message'] = $params['email-subject-message'];
|
|
|
|
|
|
|
|
if ( isset( $params['email-subject-params'] ) ) {
|
|
|
|
$this->email['subject']['params'] = $params['email-subject-params'];
|
|
|
|
} else {
|
|
|
|
$this->email['subject']['params'] = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->email = array(
|
|
|
|
'subject' => array(
|
|
|
|
'message' => 'echo-email-subject-default',
|
|
|
|
'params' => array(),
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'message' => 'echo-email-body-default',
|
|
|
|
'params' => array(
|
|
|
|
'text-notification',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $params['email-body-message'] ) ) {
|
|
|
|
$this->email['body'] = array();
|
|
|
|
$this->email['body']['message'] = $params['email-body-message'];
|
|
|
|
|
|
|
|
if ( isset( $params['email-body-params'] ) ) {
|
|
|
|
$this->email['body']['params'] = $params['email-subject-params'];
|
|
|
|
} else {
|
|
|
|
$this->email['body']['params'] = array( 'text-notification' );
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( isset( $params['icon'] ) ) {
|
|
|
|
$this->icon = $params['icon'];
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
|
|
|
* @param $type string
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function format( $event, $user, $type ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
if ( $this->outputFormat === 'email' ) {
|
|
|
|
return $this->formatEmail( $event, $user, $type );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( $this->outputFormat === 'text' ) {
|
|
|
|
return $this->formatNotificationTitle( $event, $user )->text();
|
|
|
|
}
|
2012-05-17 15:36:18 +00:00
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
// Assume html
|
|
|
|
$title = $this->formatNotificationTitle( $event, $user )->parse();
|
|
|
|
$output = Xml::tags( 'div', array( 'class' => 'mw-echo-title' ), $title ) . "\n";
|
|
|
|
if ( !is_null( $this->content ) ) {
|
|
|
|
$content = $this->formatContent( $event, $user );
|
|
|
|
$content .= ' ' . $this->formatTimestamp( $event->getTimestamp(), $user );
|
|
|
|
$output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n";
|
2012-08-02 18:18:25 +00:00
|
|
|
} else {
|
|
|
|
$content = $this->formatTimestamp( $event->getTimestamp(), $user );
|
|
|
|
$output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n";
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !is_null( $this->icon ) ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
$output = Xml::tags( 'div',
|
|
|
|
array( 'class' => "mw-echo-icon mw-echo-icon-{$this->icon}" ),
|
|
|
|
' '
|
|
|
|
) . $output;
|
2012-05-17 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatNotificationTitle( $event, $user ) {
|
|
|
|
return $this->formatFragment( $this->title, $event, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatContent( $event, $user ) {
|
|
|
|
if ( is_null( $this->content ) ) {
|
|
|
|
return '';
|
2012-05-17 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
return $this->formatFragment( $this->content, $event, $user )->parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatFragment( $details, $event, $user ) {
|
|
|
|
$message = wfMessage( $details['message'] )
|
2012-08-31 21:50:46 +00:00
|
|
|
->inLanguage( $user->getOption( 'language' ) );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
$this->processParams( $details['params'], $event, $message, $user );
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatEmail( $event, $user, $type ) {
|
2012-08-02 18:47:03 +00:00
|
|
|
$subject = $this->formatFragment( $this->email['subject'], $event, $user )->text();
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2012-08-02 18:47:03 +00:00
|
|
|
$body = $this->formatFragment( $this->email['body'], $event, $user )->text();
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2012-05-17 15:36:18 +00:00
|
|
|
return array( 'subject' => $subject, 'body' => $body );
|
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
protected function processParams( $params, $event, $message, $user ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $params as $param ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
$this->processParam( $event, $param, $message, $user );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 05:09:43 +00:00
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $param
|
|
|
|
* @param $message Message
|
|
|
|
* @param $user User
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2012-09-02 09:30:38 +00:00
|
|
|
protected function processParam( EchoEvent $event, $param, $message, $user ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
if ( $param === 'agent' ) {
|
|
|
|
// Actually converts to two parameters for gender support
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$event->getAgent() ) {
|
2012-07-26 17:23:18 +00:00
|
|
|
$message->params( '', wfMessage( 'echo-no-agent' )->text() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
|
|
|
$agent = $event->getAgent();
|
|
|
|
$message->params( $agent->getName() );
|
2012-08-30 16:04:39 +00:00
|
|
|
$message->rawParams( $this->formatUser( $agent ) );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-05-17 15:36:18 +00:00
|
|
|
} elseif ( $param === 'user' ) {
|
|
|
|
$message->params( $user->getName() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} elseif ( $param === 'title' ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$event->getTitle() ) {
|
2012-07-26 17:23:18 +00:00
|
|
|
$message->params( wfMessage( 'echo-no-title' )->text() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
2012-07-31 00:29:49 +00:00
|
|
|
$message->params( $this->formatTitle( $event->getTitle() ) );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
} elseif ( $param == 'text-notification' ) {
|
|
|
|
$oldOutputFormat = $this->outputFormat;
|
2012-08-30 16:04:39 +00:00
|
|
|
$this->setOutputFormat( 'text' );
|
2012-07-31 00:29:49 +00:00
|
|
|
// $type is ignored in this class
|
|
|
|
$textNotification = $this->format( $event, $user, '' );
|
|
|
|
$this->setOutputFormat( $oldOutputFormat );
|
|
|
|
|
|
|
|
$message->params( $textNotification );
|
2012-05-17 15:36:18 +00:00
|
|
|
} else {
|
|
|
|
throw new MWException( "Unrecognised parameter $param" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-26 17:23:18 +00:00
|
|
|
}
|