Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 146
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CollaborationHubContentEditor
0.00% covered (danger)
0.00%
0 / 146
0.00% covered (danger)
0.00%
0 / 6
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
20
 getOptions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 showContentForm
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
12
 setCollabkitTheme
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 importContentFormData
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Specialized editing interface for CollaborationHubContent pages.
5 *
6 * @todo Unicode unsafe browsers?
7 * @file
8 */
9
10use MediaWiki\EditPage\EditPage;
11use MediaWiki\Html\Html;
12use MediaWiki\HTMLForm\HTMLForm;
13use MediaWiki\Output\OutputPage;
14use MediaWiki\Request\WebRequest;
15
16class CollaborationHubContentEditor extends EditPage {
17
18    /** @var string */
19    protected $colour;
20
21    public function __construct( Article $page ) {
22        parent::__construct( $page );
23        // Make human readable the default format for editing, but still
24        // save as json. Can be overriden by url ?format=application/json param.
25        $this->contentFormat = CollaborationHubContentHandler::FORMAT_WIKI;
26
27        // Nice JavaScript buttons
28        $out = $this->getContext()->getOutput();
29        $out->addModules( [
30            'mediawiki.htmlform',
31            'ext.CollaborationKit.hubtheme'
32        ] );
33        $out->addModuleStyles( [
34            'ext.CollaborationKit.edit.styles',
35        ] );
36        $out->addJsConfigVars(
37            'wgCollaborationKitColourList',
38            CollaborationHubContent::getThemeColours()
39        );
40    }
41
42    /**
43     * Prepares form fields.
44     *
45     * @param array $parts
46     * @return string[] html
47     */
48    protected function getFormFields( $parts ) {
49        $fields = [ [], [] ];
50
51        $fields[0] = [
52            'displayname' => [
53                'type' => 'text',
54                'cssclass' => 'mw-ck-display-input',
55                'label-message' => 'collaborationkit-hubedit-displayname',
56                'help-message' => 'collaborationkit-hubedit-displayname-help',
57                'name' => 'wpCollabHubDisplayName',
58                'id' => 'wpCollabHubDisplayName',
59                'default' => $parts[0]
60            ],
61            'icon' => [
62                'type' => 'text',
63                'cssclass' => 'mw-ck-hub-image-input',
64                'label-message' => 'collaborationkit-hubedit-image',
65                'help-message' => 'collaborationkit-hubedit-image-help',
66                'name' => 'wpCollabHubImage',
67                'default' => $parts[3],
68                'id' => 'wpCollabHubImage'
69            ],
70        ];
71
72        // Colour messages:
73        // collaborationkit-red, collaborationkit-lightgrey, collaborationkit-skyblue,
74        // collaborationkit-bluegrey, collaborationkit-aquamarine, collaborationkit-violet,
75        // collaborationkit-salmon, collaborationkit-yellow, collaborationkit-gold,
76        // collaborationkit-brightgreen
77        $colours = [];
78        foreach ( CollaborationHubContent::getThemeColours() as $colour ) {
79            $colours['collaborationkit-' . $colour] = $colour;
80        }
81        if ( $parts[4] == '' ) {
82            $selectedColour = 'lightgrey';
83        } else {
84            $selectedColour = $parts[4];
85        }
86        $fields[0]['colour'] = [
87            'type' => 'select',
88            'cssclass' => 'mw-ck-colour-input',
89            'name' => 'wpCollabHubColour',
90            'id' => 'wpCollabHubColour',
91            'label-message' => 'collaborationkit-hubedit-colour',
92            'options' => $this->getOptions( $colours ),
93            'default' => $selectedColour
94        ];
95
96        $this->colour = $selectedColour;
97
98        $fields[1]['introduction'] = [
99            'type' => 'textarea',
100            'cssclass' => 'mw-ck-introduction-input',
101            'label-message' => 'collaborationkit-hubedit-introduction',
102            'placeholder-message' => 'collaborationkit-hubedit-introduction-placeholder',
103            'name' => 'wpCollabHubIntroduction',
104            'default' => $parts[1],
105            'rows' => 8,
106            'id' => 'wpCollabHubIntroduction'
107        ];
108
109        if ( $parts[5] == '' ) {
110            $includedContent = '';
111        } else {
112            $includedContent = $parts[5];
113        }
114        $fields[1]['content'] = [
115            'type' => 'textarea',
116            'cssclass' => 'mw-ck-content-input',
117            'label-message' => 'collaborationkit-hubedit-content',
118            'help-message' => 'collaborationkit-hubedit-content-help',
119            'name' => 'wpCollabHubContent',
120            'default' => $includedContent,
121            'rows' => 18,
122            'id' => 'wpCollabHubContent'
123        ];
124
125        $fields[1]['footer'] = [
126            'type' => 'textarea',
127            'cssclass' => 'mw-ck-footer-input',
128            'label-message' => 'collaborationkit-hubedit-footer',
129            'help-message' => 'collaborationkit-hubedit-footer-help',
130            'name' => 'wpCollabHubFooter',
131            'default' => $parts[2],
132            'rows' => 6,
133            'id' => 'wpCollabHubFooter'
134        ];
135
136        $dummyForm1 = HTMLForm::factory( 'ooui', $fields[0], $this->getContext() );
137        $dummyForm2 = HTMLForm::factory( 'ooui', $fields[1], $this->getContext() );
138
139        return [ $dummyForm1->prepareForm()->getBody(),
140            $dummyForm2->prepareForm()->getBody() ];
141    }
142
143    /**
144     * Build and return the associative array for the content source field.
145     *
146     * @param array $mapping
147     * @return array
148     */
149    protected function getOptions( $mapping ) {
150        $options = [];
151        foreach ( $mapping as $msgKey => $option ) {
152            $options[wfMessage( $msgKey )->escaped()] = $option;
153        }
154        return $options;
155    }
156
157    /**
158     * Renders and adds the editing form to the parser output.
159     */
160    protected function showContentForm() {
161        if ( $this->contentFormat !== CollaborationHubContentHandler::FORMAT_WIKI ) {
162            parent::showContentForm();
163            return;
164        }
165
166        $parts = explode(
167            CollaborationKitSerialization::SERIALIZATION_SPLIT,
168            $this->textbox1,
169            6
170        );
171        if ( count( $parts ) !== 6 ) {
172            parent::showContentForm();
173            return;
174        }
175
176        $out = $this->getContext()->getOutput();
177
178        $partFields = $this->getFormFields( $parts );
179        // See setCollabkitTheme for how the setProperty works.
180        $out->setProperty( 'collabkit-theme', $this->colour );
181        $out->addHTML( Html::rawElement(
182            'div',
183            [ 'class' => 'mw-collabkit-modifiededitform' ],
184                Html::rawElement(
185                    'div',
186                    [ 'class' => 'ck-createcollaborationhub-setup' ],
187                    $partFields[0]
188                ) .
189                Html::rawElement(
190                    'div',
191                    [ 'class' => 'ck-createcollaborationhub-block' ],
192                    $partFields[1]
193                )
194            )
195        );
196    }
197
198    /**
199     * Hook handler for OutputPageBodyAttributes.
200     *
201     * Used to set the color theme for Hub edit pages.
202     *
203     * @param OutputPage $out
204     * @param Skin $skin
205     * @param array &$bodyAttribs Attributes for the <body> element
206     */
207    public static function setCollabkitTheme( OutputPage $out, $skin,
208        &$bodyAttribs
209    ) {
210        $theme = $out->getProperty( 'collabkit-theme' );
211        if ( $theme ) {
212            $themeClass = 'mw-ck-theme-' . $theme;
213            if ( !isset( $bodyAttribs['class'] ) ) {
214                $bodyAttribs['class'] = $themeClass;
215            } else {
216                $bodyAttribs['class'] .= ' ' . $themeClass;
217            }
218        }
219    }
220
221    /**
222     * Converts input from the editing form into the text/x-collabkit
223     * serialization used for processing the edit.
224     *
225     * @param WebRequest &$request
226     * @return string|null
227     */
228    protected function importContentFormData( &$request ) {
229        $format = $request->getVal( 'format', CollaborationListContentHandler::FORMAT_WIKI );
230        if ( $format !== CollaborationListContentHandler::FORMAT_WIKI ) {
231            return parent::importContentFormData( $request );
232        }
233        $displayname = trim( $request->getText( 'wpCollabHubDisplayName', '' ) );
234        if ( $displayname === '' ) {
235            // Only 1 textbox?
236            return parent::importContentFormData( $request );
237        }
238
239        $introduction = trim( $request->getText( 'wpCollabHubIntroduction', '' ) );
240        $footer = trim( $request->getText( 'wpCollabHubFooter', '' ) );
241        $image = trim( $request->getText( 'wpCollabHubImage', '' ) );
242        $colour = trim( $request->getText( 'wpCollabHubColour', '' ) );
243        $content = trim( $request->getText( 'wpCollabHubContent', '' ) );
244
245        return CollaborationKitSerialization::getSerialization( [
246            $displayname,
247            $introduction,
248            $footer,
249            $image,
250            $colour,
251            $content
252        ] );
253    }
254}