Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialNotificationsFormatter
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 formatModel
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
30
 getIconURL
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications\Formatters;
4
5use MediaWiki\Extension\Notifications\Special\SpecialNotificationsMarkRead;
6use MediaWiki\Html\Html;
7use MediaWiki\Output\OutputPage;
8use MediaWiki\Utils\MWTimestamp;
9use OOUI\IconWidget;
10
11/**
12 * A formatter for Special:Notifications
13 *
14 * This formatter uses OOUI libraries. Any calls to this formatter must
15 * also call OutputPage::enableOOUI() before calling this formatter.
16 */
17class SpecialNotificationsFormatter extends EchoEventFormatter {
18    protected function formatModel( EchoEventPresentationModel $model ) {
19        $markReadSpecialPage = new SpecialNotificationsMarkRead();
20        $id = $model->getEventId();
21
22        $icon = Html::element(
23            'img',
24            [
25                'class' => 'mw-echo-icon',
26                'src' => $this->getIconURL( $model ),
27            ]
28        );
29
30        OutputPage::setupOOUI();
31
32        $markAsReadIcon = new IconWidget( [
33            'icon' => 'close',
34            'title' => wfMessage( 'echo-notification-markasread' )->text(),
35        ] );
36
37        $markAsReadForm = $markReadSpecialPage->getMinimalForm(
38            $id,
39            $this->msg( 'echo-notification-markasread' )->text(),
40            false,
41            $markAsReadIcon->toString()
42        );
43
44        $markAsReadButton = Html::rawElement(
45            'div',
46            [ 'class' => 'mw-echo-markAsReadButton' ],
47            // First submission attempt
48            $markAsReadForm->prepareForm()->getHTML( false )
49        );
50
51        $html = Html::rawElement(
52            'div',
53            [ 'class' => 'mw-echo-title' ],
54            $model->getHeaderMessage()->parse()
55        ) . "\n";
56
57        $body = $model->getBodyMessage();
58        if ( $body ) {
59            $html .= Html::element(
60                'div',
61                [ 'class' => 'mw-echo-payload' ],
62                $body->text()
63            ) . "\n";
64        }
65
66        $ts = $this->language->getHumanTimestamp(
67            new MWTimestamp( $model->getTimestamp() ),
68            null,
69            $this->user
70        );
71
72        $footerItems = [ Html::element( 'span', [ 'class' => 'mw-echo-notification-footer-element' ], $ts ) ];
73
74        // Add links to the footer, primary goes first, then secondary ones
75        $links = [];
76        $primaryLink = $model->getPrimaryLinkWithMarkAsRead();
77        if ( $primaryLink !== false ) {
78            $links[] = $primaryLink;
79        }
80        $links = array_merge( $links, array_filter( $model->getSecondaryLinks() ) );
81        foreach ( $links as $link ) {
82            $footerAttributes = [
83                'href' => $link['url'],
84                'class' => 'mw-echo-notification-footer-element',
85            ];
86
87            if ( isset( $link['tooltip'] ) ) {
88                $footerAttributes['title'] = $link['tooltip'];
89            }
90
91            $footerItems[] = Html::element(
92                'a',
93                $footerAttributes,
94                $link['label']
95            );
96        }
97
98        $pipe = wfMessage( 'pipe-separator' )->inLanguage( $this->language )->text();
99        $html .= Html::rawElement(
100            'div',
101            [ 'class' => 'mw-echo-notification-footer' ],
102            implode(
103                Html::element( 'span', [ 'class' => 'mw-echo-notification-footer-element' ], $pipe ),
104                $footerItems
105            )
106        ) . "\n";
107
108        return Html::rawElement( 'div', [ 'class' => 'mw-echo-state' ],
109            $markAsReadButton .
110            $icon .
111            Html::rawElement( 'div', [ 'class' => 'mw-echo-content' ], $html )
112        );
113    }
114
115    private function getIconURL( EchoEventPresentationModel $model ) {
116        return EchoIcon::getUrl(
117            $model->getIconType(),
118            $this->language->getDir()
119        );
120    }
121}